30

I am using a docker container to build and deploy my software to a collection of ec2's. In the deployment script I build my software and then package it in a docker image. The image is pushed to my private registry, pulled by my production ec2's and then run. So essentially I will need to run docker within a docker container.

The problem is that I can't actually start docker on my container. If I try

service docker start

I get

bash: service: command not found

And if I try

docker -d

I get

2014/10/07 15:54:35 docker daemon: 0.11.1-dev 02d20af/0.11.1; execdriver: native; graphdriver:
[e2feb6f9] +job serveapi(unix:///var/run/docker.sock)
[e2feb6f9] +job initserver()
[e2feb6f9.initserver()] Creating server
2014/10/07 15:54:35 Listening for HTTP on unix (/var/run/docker.sock)
[error] attach_loopback.go:42 There are no more loopback device available.
loopback mounting failed
[e2feb6f9] -job initserver() = ERR (1)
2014/10/07 15:54:35 loopback mounting failed

The service command doesn't exist on the docker container so I can't start docker. I'm not sure what I should be doing now to start docker so I'm a bit stuck here, any help is appreciated.

A bit more information

Host machine is running fedora 20 (will eventually be running amazon linux on an ec2)

Docker container is running centos 7.0

Host is running Docker version 1.2.0, build fa7b24f/1.2.0

Container is running docker-0.11.1-22.el7.centos.x86_64

Colin Murphy
  • 1,105
  • 3
  • 12
  • 22
  • Is there a reason for running centos/docker 0.11 in the container? If it's dedicated to running Docker you could use CoreOS or something similar. – naneau Oct 07 '14 at 15:38
  • The centos container isn't completely dedicated to running docker, almost all of its tasks are related to building and testing the software. Everything we do is known to run successfully on centos so we were hoping to be able to keep using centos in a container. – Colin Murphy Oct 07 '14 at 18:52
  • 1
    I think the "Docker way" would still be to spin up a separate container. Containers are really suited towards running a single application. They aren't meant to replace tasks better done by a virtual machine. If you have a complex stack that has many tasks, it may help to spread some of those tasks to separate containers. An example of such a task would be running other Docker containers. – naneau Oct 07 '14 at 20:39
  • faced same issue with same things, i am also CentOS lover. did you find any solution or a better approach to do that?? – Adeel Ahmad Nov 18 '14 at 16:50
  • Ran into same issue with CoreOS 591. Did anyone find a solution? – sukrit007 Feb 15 '15 at 04:53

3 Answers3

45

How about not running 'docker inside docker' and run docker on your host, but from within your docker container? Just mount your docker.sock and docker binary:

docker run -v /var/run/docker.sock:/run/docker.sock -v $(which docker):/bin/docker [your image]

https://github.com/sameersbn/docker-gitlab uses this approach to spin up docker containers, take a look at this image.

You can also take a look at: https://registry.hub.docker.com/u/mattgruter/doubledocker/

UPDATE on july 2016

The most current approach is to use docker:dind image, as described here: https://hub.docker.com/_/docker/

Short summary:

$ docker run --privileged --name some-docker -d docker:dind

and then: $ docker run --rm --link some-docker:docker docker info

cthulhu
  • 3,749
  • 1
  • 21
  • 25
  • 1
    -v $(which docker):/bin/docker trick worked for me when I was running ubuntu based images on ubuntu hosts, but didn't work on gentoo host - so I prefer install docker in container and only to map socket file – ISanych Oct 21 '14 at 17:21
  • 1
    What's the point of mounting docker socket inside a container when _everything_ is happening on your host and not in the container. – Tomas Tomecek Nov 07 '14 at 18:08
  • Well, mostly because it's not 'everything' happening on the host. See the docker-gitlab image as an example. – cthulhu Nov 08 '14 at 09:54
  • Using the method to bind sock and executable gives me the following error on OSX: `docker: Error response from daemon: mkdir /usr/local/bin/docker: file exists.` – Phillipp Sep 26 '16 at 18:54
  • The dind author suggests the approach of bind-mounting the docker socket (but *not* the docker binary) due to complications with the docker-in-docker approach. See https://jpetazzo.github.io/2015/09/03/do-not-use-docker-in-docker-for-ci/ – Mike McCabe Jan 30 '20 at 07:15
3

While in almost all cases I would suggest following @cthulhu's answer and not running "docker in docker", in the cases when you must (e.g. a test suite which tests against multiple docker version), use the following to create additional loopback devices:

#!/bin/bash

for i in {0..6}
do
    mknod -m0660 /dev/loop$i b 7 $i
done

(Taken from the thread for Docker Issue #7058)

lsowen
  • 3,728
  • 1
  • 21
  • 23
2

You can simply run docker inside the docker container using dind. Try this image from Jerome, as follows:

docker run --privileged -t -i jpetazzo/dind

Check this page for more details:
https://github.com/jpetazzo/dind

Sabin
  • 11,662
  • 3
  • 25
  • 39