Docker Community Forums

Share and learn in the Docker community.

Dynamic IP address of container

(Testeriamtester) #1

Hello. How i have to configure my Dockerfile or how i have to run my container that i can run few container at the moment and each of it will have diferent IP. I’m WEB developer and i work with a lot of projects at the moment. So i have to switch between them comfortably. And i don’t want to stop my one container to run another. I’m not familiar with networks and i newbee with Docker. Can you help me?

(David Maze) #2

Here’s two pieces of very generic advice:

  1. Totally ignore containers’ IP addresses. Within a container, Docker provides a DNS service that will resolve the names of other containers. Outside a container, the docker run -p option publishes a port in the container to a port on the host. Docker networks tend to be artificial and hard to get to, and the IP address a container is assigned is unreachable in several circumstances (from other hosts; on Docker for Mac).

  2. Plan to delete and recreate containers often. Containers are lightweight and inexpensive. You shouldn’t care much if you have to stop and restart a container. As much as possible, have your custom code store data “somewhere else”, like a database container. Use the standard database containers and keep their data in a Docker volume. There are a couple of actions (updating code, changing Docker runtime options) that require a restart. Don’t depend on a container running forever to not lose data (it won’t). Don’t use docker commit (docker build is much more reproducible).

I think the best answer to your original question is that nothing stops you from running multiple copies of a container on different host ports

docker run -d --name web1 -p 8080:80 -v $PWD/configs/web1:/opt/web/config myapp
docker run -d --name web2 -p 8081:80 -v $PWD/configs/web2:/opt/web/config myapp
docker run -d --name web3 -p 8082:80 -v $PWD/configs/web3:/opt/web/config myapp

and then the applications will be accessible from http://$HOST_IP:8080/ etc.

(Testeriamtester) #3

Thank you for help but it seems to me that first generic advice i don’t understand totally (maby because English is not my native language (translator don’t help)) and second generic advice is consist of a lot of the same actions like recreating container. Also i work with MySQL data base and i need to listen 3306 port and i’m not familiar with network and i afraid something will be broken because of (“different host ports”) like -p 3307:3306. So my main goal is to work with different IP address. Can you help me with that, please?