8

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?

1 Answer 1

10

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
7
  • 1
    @tudor Emm... Maybe u should add the command creating docker network, which makes this question more specific. And am I have the right understanding about your requirement?
    – Light.G
    Oct 9, 2018 at 23:10
  • 1
    All your link says is "plus the special form service:[service name]" and makes no expanation for it. There is an issue lodged about the lack of docs here: github.com/docker/docker.github.io/issues/5325 and seems to suggest that "service:" and "container:" are the same. They site docs.docker.com/engine/reference/run/#network-container which explains that "With the network set to container a container will share the network stack of another container" so that makes sense. However there's a warning that this will not work in swarm mode. :-( Oct 9, 2018 at 23:22
  • @tudor I moved it as last paragraph and re-edited the content which shows the situation I met.
    – Light.G
    Oct 10, 2018 at 1:37
  • @tudor Not try that yet. I mean it’s default IP of docker network gateway when I create docker network with specifying it.
    – Light.G
    Oct 10, 2018 at 3:03
  • @tudor Oh yes, I specified with —-subnet.
    – Light.G
    Oct 10, 2018 at 3:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.