Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to link binaries between docker containers

Tags:

docker

Is it possible to use docker to expose the binary from one container to another container?

For example, I have 2 containers:

  • centos6
  • sles11

I need both of these containers to have similar versions git installed. Unfortunately the sles container does not have the version of git that I need.

I want to spin up a git container like so:

$ cat Dockerfile
FROM ubuntu:14.04
MAINTAINER spuder

RUN apt-get update
RUN apt-get install -yq git
CMD /usr/bin/git
# ENTRYPOINT ['/usr/bin/git'] 

Then link the centos6 and sles11 containers to the git container so that they both have access to a git binary, without going through the trouble of installing it.

I'm running into the following problems:

  • You can't link a container to another non running container
  • I'm not sure if this is how docker containers are supposed to be used.

Looking at the docker documentation, it appears that linked containers have shared environment variables and ports, but not necessarily access to each others entrypoints.

How could I link the git container so that the cent and sles containers can access this command? Is this possible?

like image 622
spuder Avatar asked Aug 12 '14 22:08

spuder


1 Answers

You could create a dedicated git container and expose the data it downloads as a volume, then share that volume with the other two containers (centos6 and sles11). Volumes are available even when a container is not running.

If you want the other two containers to be able to run git from the dedicated git container, then you'll need to install (or copy) that git binary onto the shared volume.

Note that volumes are not part of an image, so they don't get preserved or exported when you docker save or docker export. They must be backed-up separately.

Example

Dockerfile:

FROM ubuntu
RUN apt-get update; apt-get install -y git
VOLUME /gitdata
WORKDIR /gitdata
CMD git clone https://github.com/metalivedev/isawesome.git

Then run:

$ docker build -t gitimage .
# Create the data container, which automatically clones and exits
$ docker run -v /gitdata --name gitcontainer gitimage
Cloning into 'isawesome'...
# This is just a generic container, but what I do in the shell
# you could do in your centos6 container, for example
$ docker run -it --rm --volumes-from gitcontainer ubuntu /bin/bash
root@e01e351e3ba8:/# cd gitdata/
root@e01e351e3ba8:/gitdata# ls 
isawesome
root@e01e351e3ba8:/gitdata# cd isawesome/
root@e01e351e3ba8:/gitdata/isawesome# ls
Dockerfile  README.md  container.conf  dotcloud.yml  nginx.conf
like image 174
Andy Avatar answered Oct 02 '22 14:10

Andy