docker配置代理拉取镜像
About
Setting Up Docker with Proxy on Ubuntu Server
In this guide, we will walk through the process of configuring Docker to work behind an HTTP proxy on an Ubuntu server. This setup is essential for environments where direct internet access is restricted.
Step 1: Verify Docker Installation
First, let's confirm that Docker is installed and check its version:
root@ubuntuserver:/home/zw# docker --version
Docker version 27.2.0, build 3ab4256
If Docker is not installed, you can follow the official Docker installation guide for Ubuntu.
Step 2: Create Configuration Directory
Next, we need to create a directory for Docker's systemd service overrides:
root@ubuntuserver:/home/zw# sudo mkdir -p /etc/systemd/system/docker.service.d
Step 3: Create Proxy Configuration File
Now, we will create a configuration file for the HTTP proxy. Attempting to directly edit the file will result in an error, as it does not yet exist:
root@ubuntuserver:/home/zw# /etc/systemd/system/docker.service.d/http-proxy.conf
bash: /etc/systemd/system/docker.service.d/http-proxy.conf: No such file or directory
Instead, we will create and edit the file using vim:
root@ubuntuserver:/home/zw# sudo vim /etc/systemd/system/docker.service.d/http-proxy.conf
In this file, add the following lines to configure the proxy settings:
[Service]
Environment="HTTP_PROXY=http://192.168.1.241:27890/"
Environment="HTTPS_PROXY=http://192.168.1.241:27890/"
Step 4: Reload Systemd and Restart Docker
After saving the configuration, reload the systemd manager configuration to recognize the new settings:
root@ubuntuserver:/home/zw# sudo systemctl daemon-reload
Then restart the Docker service to apply the changes:
root@ubuntuserver:/home/zw# sudo systemctl restart docker
Step 5: Verify Proxy Settings
To ensure that Docker is using the proxy correctly, you can check the environment variables for the Docker service:
root@ubuntuserver:/home/zw# sudo systemctl show --property=Environment docker
Environment=HTTP_PROXY=http://192.168.1.241:27890 HTTPS_PROXY=http://192.168.1.241:27890
Step 6: Pull a Docker Image
Finally, test the configuration by pulling a Docker image. In this example, we will pull the Debian image:
root@ubuntuserver:/home/zw# docker pull debian
Using default tag: latest
latest: Pulling from library/debian
903681d87777: Pull complete
Digest: sha256:aadf411dc9ed5199bc7dab48b3e6ce18f8bbee4f170127f5ff1b75cd8035eb36
Status: Downloaded newer image for debian:latest
docker.io/library/debian:latest
Conclusion
By following these steps, you have successfully configured Docker to work behind an HTTP proxy on your Ubuntu server. This allows you to pull images and perform other Docker operations in environments with restricted internet access.