Docker Community Forums

Share and learn in the Docker community.

Static ipv4 for container in pre-existing (external) network?


#1

Hi all,

I am running an application which is having a license attached to IP address. Unfortunately every time I restart docker my container has different IP. There is any way, I can assign static ipv4 in the existing network?

I’ve created network via command
docker network create nginx-proxy

Now in docker-compose.yml I have:

version: '3'

services:
  nginx:
    image: nginx
    container_name: nginx
    ports:
      - "80:80"
      - "443:443"

networks:
  default:
    external:
      name: nginx-proxy

I’ve found many configurations but for networks created within same docker-compose config, nothing for the external network.

Thanks for any help.


(Rubendario) #2

Try this, if it does not work because version, remove “ip_range”. i hope it helps you.

version: '2'
services:
  nginx:
    image: nginx
    container_name: nginx-container
    networks:
      static-network:
        ipv4_address: 172.20.128.2
networks:
  static-network:
    ipam:
      config:
        - subnet: 172.20.0.0/16
          #docker-compose v3+ do not use ip_range
          ip_range: 172.28.5.0/24

from host you can test using:

docker-compose up -d
curl 172.20.128.2

#3

@rubendario thanks for the help. I finally found a way to do it in version 3.

version: '3'

services:
  nginx:
    image: nginx
    container_name: nginx
    networks:
      nginx-proxy:
        ipv4_address: "172.20.0.123"
    ports:
      - "80:80"
      - "443:443"

networks:
  default:
    external:
      name: nginx-proxy
  nginx-proxy:
    external: true

Apart from defining the default network, you need to explicitly define that its external network.