Question: How do I set up an HTTP proxy for Docker and the Amazon ECS container agent in Amazon Linux 2?

I want to set up an HTTP proxy for Docker and the Amazon Elastic Container Service (Amazon ECS) container agent in Amazon Linux 2.

Short descriptionTo set up an HTTP proxy for Docker and the Amazon ECS container agent in Amazon Linux 2, complete the steps in the following sections:Store the IP address and proxy server portSet up an HTTP proxy for the Docker daemonSet up an HTTP proxy for the Amazon ECS container agentSet up an HTTP proxy for ecs-initNote: You can also set your environment variables during launch using Amazon Elastic Compute Cloud (Amazon EC2) user data with a user data script. For a user data script that works on all versions of Linux, see HTTP proxy configuration.If you're using Amazon Linux, see How do I set up an HTTP proxy for Docker and the Amazon ECS container agent in Amazon Linux?ResolutionStore the IP address and proxy server port1.    Connect to the Amazon EC2 instance using SSH.2.    As a root user, store the IP address and port of the proxy server for later use in environment variables. For example:export PROXY_SERVER_IP=x.x.x.xexport PROXY_PORT=1234Set up an HTTP proxy for the Docker daemon1.    Run the following commands with sudo permissions:mkdir /etc/systemd/system/docker.service.dcat <<EOF > /etc/systemd/system/docker.service.d/http-proxy.conf[Service]Environment="HTTP_PROXY=http://$PROXY_SERVER_IP:$PROXY_PORT"Environment="HTTPS_PROXY=https://$PROXY_SERVER_IP:$PROXY_PORT"Environment="NO_PROXY=169.254.169.254,169.254.170.2"EOFNote: HTTP_PROXY is the socket address (IPaddress:Port) of the HTTP proxy that's used to connect the Amazon ECS container agent to the internet.If the HTTP_PROXY variable is set, you must set the NO_PROXY variable to 169.254.169.254,169.254.170.2. This setting filters Amazon EC2 instance metadata, AWS Identity and Access Management (IAM) roles for tasks, and Docker daemon traffic from the proxy.2.    To reload units because of docker.service changing on disk, run the following command:systemctl daemon-reload3.    To restart Docker, run the following command:systemctl restart docker.serviceNote: The preceding command stops all running containers, including the ecs-agent on the container instance.4.    To verify the HTTP proxy settings for Docker, run the following command:docker info | grep -i proxyNote: The command output shows the HTTP proxy and HTTPS proxy.Set up an HTTP proxy for the Amazon ECS container agent1.    In your /etc/ecs/ecs.config file, include the proxy configuration using the HTTP_PROXY and NO_PROXY agent configuration parameters. For example:cat <<EOF >> /etc/ecs/ecs.configECS_CLUSTER=your-cluster-nameHTTP_PROXY=http://$PROXY_SERVER_IP:$PROXY_PORTNO_PROXY=169.254.169.254,169.254.170.2,/var/run/docker.sockEOF2.    To apply the HTTP configurations from step 1 to the ecs-agent, run the following command:systemctl restart ecsNote: If you're using an Amazon ECS-optimized Amazon Machine Image (AMI), then the Amazon ECS container agent is run through ecs-init by default.3.    To verify the HTTP proxy settings for Docker and the Amazon ECS container agent, run the following command:docker inspect ecs-agent | grep -i proxyNote: The proxy settings appear in the command output.Set up an HTTP proxy for ecs-init1.    Run the following commands with sudo permissions:mkdir /etc/systemd/system/ecs.service.dcat <<EOF > /etc/systemd/system/ecs.service.d/http-proxy.conf[Service]Environment="HTTP_PROXY=$PROXY_SERVER_IP:$PROXY_PORT/"Environment="NO_PROXY=169.254.169.254,169.254.170.2,/var/run/docker.sock"EOF2.    To reload units because of docker.service changing on disk, run the following command:systemctl daemon-reload3.    To apply the HTTP configurations from step 1 to the ecs-agent, run the following command:systemctl restart ecsNote: The configuration for both Docker and the Amazon ECS container agent affects only the current running instance. To update all the instances in a cluster, create a launch configuration, and then use an Auto Scaling group to launch new instances.

Related informationUpdating the Amazon ECS container agentBootstrapping container instances with Amazon EC2 user data