Is it possible to replicate Kubernetes container network environment with docker compose?

8/28/2017

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.

-- Chris Stryczynski
containers
docker
docker-compose
kubernetes

3 Answers

8/28/2017

I seem to have gotten it to work by adding network_mode: host to each of the container configs within my docker-compose config.

-- Chris Stryczynski
Source: StackOverflow

8/28/2017

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

  • host
  • service:(servicename in same compose file)
  • container:(name or id of a external container already running)

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

-- Tarun Lalwani
Source: StackOverflow

8/28/2017

Using Docker Links mechanism you can wire together containers and then declared ports will be available through localhost.

-- webdizz
Source: StackOverflow