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?
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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With