Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can docker-compose share an ip between services with discrete ports?

We currently have docker containers with complex builds using supervisord so that we can group services together. For example, nginx and ssh.

I'm attempting to rebuild these with more service-driven isolation linked by shared volumes. However, without mapping the IP to the host, I can't seem to find a way to allow IP addresses to be shared even though the ports may be discrete.

What I'm trying to do is something like this:

version: '2'
services:
  web:
    image: nginx
    volumes:
    - /data/web:/var/www
    networks:
      public:
        ipv4_address: 10.0.0.1
    ports:
    - "10.0.0.1:80:80"
  ssh:
    image: alpine-sshd
    volumes:
    - /data/web:/var/www
    networks:
      public:
        ipv4_address: 10.0.0.1
    ports:
    - "10.0.0.1:22:22"
networks:
  public:
    external: true

...where public is a predefined docker macvlan network.

When I try this, I get the error:

ERROR: for ssh  Cannot start service ssh: Address already in use

I'm aware that another solution to this is to introduce a third service to work as a proxy. However, I thought this would be a simple enough case not to need it.

Is it possible to configure docker-compose/docker-networking to route by the port to allow the same IP address to be used for different containers?

like image 406
tu-Reinstate Monica-dor duh Avatar asked Oct 07 '18 11:10

tu-Reinstate Monica-dor duh


People also ask

Can you assign an IP to a Docker container?

When you connect an existing container to a different network using docker network connect , you can use the --ip or --ip6 flags on that command to specify the container's IP address on the additional network. In the same way, a container's hostname defaults to be the container's ID in Docker.

Can a container have multiple IP addresses?

IP addresses are a separate system from network interfaces, but essentially, you can have multiple IP addresses configured on a single interface, allowing you to bind services to network sockets for each IP:PORT combination.

Does Docker use IP forwarding?

Docker relies on the host being capable of performing certain functions to make Docker networking work. Namely, your Linux host must be configured to allow IP forwarding.


1 Answers

Is it possible to configure docker-compose/docker-networking to route by the port to allow the same IP address to be used for different containers?

Yes we can(familiar? -_-!). There is an option of network mode presented by Docker, called service:service-name.

When we execute docker run, we could add --network=service:service-name flag. It means that current container uses the same network namespace of service:service-name. More information reference here.

Try the following compose file below. I've tested it, which works well.

version: '2'
services:
  web:
    image: nginx
    networks:
      public:
        ipv4_address: 10.0.0.2
    ports:
      - "8880:80"
      - "2220:22"

  ssh:
    image: panubo/sshd
    network_mode: "service:web"
    depends_on:
      - web
networks:
  public:
    external: true
like image 80
Light.G Avatar answered Sep 28 '22 00:09

Light.G