There are times when we need to call “localhost” on a host from within a Docker container. For example, when developing locally using Docker I need to call another service that is running on my host machine and not within the context of Docker, so I can’t use Docker bridge networking.
Recently I need to call a localhost service from a container running the AWS CLI. If I tried to call http://localhost:1234 from within the AWS CLI Docker container I would be making a request to the localhost that the container is thinking about NOT the localhost on my machine (AKA the host).
To be able to communicate from within a container to a host service we can use the --net="host"
flag as part of the docker run
command. More on Docker Host Networking
The result would look like this:
docker run --rm -it --net="host" -v ~/.aws:/root/.aws amazon/aws-cli <command>