Docker Community Forums

Share and learn in the Docker community.

Stable container IP after restart


(Olexander Kutsenko) #1

Hello!
How i can lock IP address for container ID?
When i restart container - it have new IP address, but i need to lock it as stable.

for example i need:
for container e5b87dfc49ec IP 172.17.0.9
for container eff41dbac5e7 IP 172.17.0.10
for container 0cfac0a78d11 IP 172.17.0.8

But when i stop container and start it again i get new IP adress.


(Sven Dowideit) #2

I’m almost sure you can’t (yet). The container IP’s are intended to be private, with container linking and its auto-updating of the container’s /etc/hosts file intended to cover most name based network relationships.

There are some cases where this isn’t enough though - perhaps if you can tell us more, we can help you solve your scenario.


(Olexander Kutsenko) #3

I need stable IP for nginx configuration in web server.
Where each container is a web-site.
Thats why after container restart or docker server restart i need to config my nginx config file and every time add new IP to domain.


(Olexander Kutsenko) #4

I fix this with ports mapping:

sudo docker run -i -t -d -p 8888:8888 my:newimage /bin/bash

and after type it in nginx config file

    server {
        listen 80;
        server_name my.server.name;

    location / {
            proxy_set_header Host $host;
            proxy_set_header X-Real_IP $remote_addr;
            proxy_cache off;
            proxy_pass http://localhost:8888;
               }
     }

(Volker Großmann) #5

@jwilder has an interesting approach to this.
A containerized nginx that listens to container (re)starts and regenerates the nginx configuration accordingly. This allows to start and stop containers as you like.

See https://registry.hub.docker.com/u/jwilder/nginx-proxy/


(Bruno Batista) #6

Works for me :smile: Thanks!