Kubernetes has a concept of pods where containers can share ports between them. For example within the same pod, a container can access another container (listening on port 80) via localhost:80
.
However on docker-compose, localhost
refers to the container itself.
Is there anyway to implement the kubernetes network config on docker?
Essentially I have a kubernetes config that I would like to reuse in a docker-compose config, without having to modify the images.
I seem to have gotten it to work by adding network_mode: host
to each of the container configs within my docker-compose config.
Yes you can. You run a service and then you can use network_mode: service:<nameofservice>
version: '3'
services:
mainnetwork:
image: alpine
command: tail -f /dev/null
mysql:
image: mysql
network_mode: service:mainnetwork
environment:
- "MYSQL_ROOT_PASSWORD=root"
mysqltest:
image: mysql
command: bash -c "sleep 10 && mysql -uroot -proot -h 127.0.0.1 -e 'CREATE DATABASE tarun;'"
network_mode: service:mainnetwork
Edit-1
So the network_mode
can have below possible values
In this case i have used service:mainnetwork
, so the mainnetwork needs to be up.
Also this has been tested on Docker 17.06 ce
. So I assume you are using a newer version
Using Docker Links mechanism you can wire together containers and then declared ports will be available through localhost.