Skip to content
Gijs Molenaar edited this page Feb 12, 2015 · 10 revisions

Introduction

We are building complex computational pipelines, and we often want to start containers from containers. This document describes how to get data in and out of these containers.

Elements

  • Host - A system with docker installed, hosting containers
  • Container C - A Container running on Host spawned a user
  • Container Z - A Container running on Host, spawned by Container C

Inter container communication can be file based or network (TCP/UDP) based.

To connect on file level folders need to be mounted or docker volumes need to be attached between containers.

To communicate on network level ports need to be exposed and containers need to be linked.

Starting containers

A container can spawn new containers. The container needs to install the docker client and the user needs to bind these Host files and folders to the container:

  • /var/run/docker.pid
  • /var/run/docker.sock
  • /var/lib/docker

Example:

docker run -ti \
		-v /var/run/docker.pid:/var/run/docker.pid \
		-v /var/run/docker.sock:/var/run/docker.sock \
		-v /var/lib/docker:/var/lib/docker \
		radioastro/docker \
		bash

Example docker file:

FROM ubuntu:14.04
RUN apt-get update && apt-get install -yq apt-transport-https
RUN apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys 36A1D7869245C8950F966E92D8576A8BA88D21E9
ADD docker.list /etc/apt/sources.list.d/docker.list
RUN apt-get update
RUN apt-get install -yq lxc-docker

note: the docker version on the host and in container C need to match.

Communication

File based

A -> B means A accesses a file on B

Host -> Container C

Using the --volume/-v flag a user can bind a volume to the Container C. Files written by the container can then be accessed by the Host.

You can also copy a file from the container (running or not):

docker cp <container ID>:/etc/passwd .

Container C -> Host

Host files can be exposed to the container using --volume/-v

Container C - Container Z

Container C can access files in Container Z the same way as the Host can access Container C (using --volume or cp)

Container Z - Container C

Container Z can access files inside a volume of Container C using the --volumes-from flag. The HOSTNAME variable inside a container is set to the container ID, so you can attach the volume like this:

docker run -ti --volumes-from=$HOSTNAME ubuntu bash

It is necessary that the files you want to access are inside a volume. This volume should be defined during startup of Container C.

Container Z -> Host

Container Z can just access any file on the host equal to how Container C can access files on the host using the --volume flag.

Host -> Container Z

Container C can access files in Container Z the same way as the Host can access Container C (using --volume or cp)

Network based

A -> B means A accesses a TCP or UDP socket on B

Host -> Container C

expose a service on the docker0 interface

http://stackoverflow.com/questions/17770902/forward-host-port-to-docker-container

Container C -> Host

export ports on the container using -p or -P

Container C - Container Z

You need to figure out what the IP address is of the new container, since the hosts file is not updated:

$ CID=$(docker run -d -p 80:80 radioastro/iipsrv-astro)
$ docker inspect --format '{{ .NetworkSettings.IPAddress }}' $CID

Container Z - Container C

docker run -ti  --link=$HOSTNAME:container_C ubuntu bash

Container Z -> Host

expose a service on the docker0 interface

http://stackoverflow.com/questions/17770902/forward-host-port-to-docker-container

Host -> Container Z

export ports on the container using -p or -P