Configure the Docker daemon to use a proxy
1. Create or edit Docker configuration file directory
2. Create or edit the proxy configuration file
3. Reload the system daemon and restart Docker
4. Verify proxy settings
5. Use docker pull to pull the image and verify the proxy settings
Step 1: Create or edit Docker configuration file directory
Docker's configuration file directory is located in /etc/systemd/system/. If this directory does not exist, you need to create it first. Execute the following command: mkdir -p /etc/systemd/system/
Step 2: Create or edit a file
Next, we need to create or edit the file:
vim /etc/systemd/system//, after opening the file in a text editor (such as nano), add the following:
[Service] Environment="HTTP_PROXY=http://10.100.100.1:20172" Environment="HTTPS_PROXY=http://10.100.100.1:20172" Environment="NO_PROXY=localhost,127.0.0.1"
The meaning of this configuration file content is as follows:
- HTTP_PROXY: Set the address and port of HTTP proxy.
- HTTPS_PROXY: Set the address and port of HTTPS proxy.
- NO_PROXY: Defines addresses that do not need to go to proxy, separated by commas.
Save the file and exit the editor.
Step 3: Reload the system daemon and restart Docker
After saving and closing the file, the systemd daemon needs to be reloaded and Docker restarted for the configuration to take effect. Execute the following command:
systemctl daemon-reload systemctl restart docker
Step 4: Verify proxy settings
To confirm whether the proxy settings are in effect correctly, you can use the following command to view the environment variables of the Docker service:
sudo systemctl show --property=Environment docker
You should see that the output contains the HTTP_PROXY, HTTPS_PROXY and NO_PROXY variables that were just set. For example:
Environment=HTTP_PROXY=http://10.100.100.1:20172 Environment=HTTPS_PROXY=http://10.100.100.1:20172 Environment=NO_PROXY=localhost,127.0.0.1
Step 5: Use docker pull to pull the image
Once the proxy settings are complete and take effect, you can try pulling the image using the Docker proxy. For example, execute the following command:
docker pull pytorch/pytorch:1.13.1-cuda11.6-cudnn8-devel
This command will pull the required Docker image through the configured proxy server.
Summarize
Through the above steps, the Docker daemon can be successfully configured to use HTTP and HTTPS proxy. This is especially useful when restricted network environments or when you need to access a Docker image repository through a proxy server. Verifying the pull of the mirror with docker pull ensures that the proxy configuration is in effect correctly.
This is the end of this article about the docker daemon configuration agent. For more related docker daemon configuration, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!