1

My network configuration looks something like this on a docker host:

➜  server git:(master) ✗ ip addr
3: ens9: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP qlen 1000
    link/ether 00:02:c9:bb:3b:f4 brd ff:ff:ff:ff:ff:ff
    inet <IP Address>.18/29 brd <Default Gateway>.23 scope global ens9
       valid_lft forever preferred_lft forever
    inet <IP Address>.19/29 brd <Default Gateway>.23 scope global secondary ens9:0
       valid_lft forever preferred_lft forever

Currently, I have existing non-Docker services running on port 443 on the .18 IP address, and I need to keep it that way. However, when I open a port using a Dockerfile, it fails to start because port 443 is already in use by the host.

Is there a way to globally switch the IP address that Docker uses when opening a port up? I do not want to have to set this manually on each container's configuration file, which of course already works.

1 Answer 1

1

You don't need to expose ports to access certain containers on the service declaration. In fact, you can declare a network inside a single docker-compose.yml with a different subnet like this:

version: '2'

networks:
  custom-network:
    ipam:
      driver: default
      config:
        - subnet: 73.0.0.0/16
          ip_range: 73.0.0.0/24
          gateway: 73.0.0.254

services:
  simple-nginx:
    image: nginx
    networks:
      custom-network:
        ipv4_address: 73.0.0.10

And access the nginx service using the static IP 73.0.0.10 for the custom-network.

For more information, you can always check the networking docs for docker-compose: https://docs.docker.com/compose/networking/

Or directly the docker docs: https://docs.docker.com/engine/userguide/networking/

2
  • Do I have to do this in the docker-compose.yml file? This would make the containers incredibly non-portable. Is there a global way to do this on the Docker host?
    – David
    Jan 23, 2017 at 17:04
  • The docker-compose is just a simple way to declare the services. You can always declare the network docker network create --subnet 78.0.0.0/16 --ip-range 78.0.0.0/24 --gateway 78.0.0.254 custom-network and then join the container in the network: docker network connect custom-network your-container Jan 23, 2017 at 17:22

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .