5

I have a Dockerfile that looks like this:

# Pull base image
FROM  openjdk:8

ENV SCALA_VERSION 2.12.2
ENV SBT_VERSION 0.13.15

# Scala expects this file
RUN touch /usr/lib/jvm/java-8-openjdk-amd64/release

# Install Scala
## Piping curl directly in tar
RUN \
  curl -fsL http://downloads.typesafe.com/scala/$SCALA_VERSION/scala-$SCALA_VERSION.tgz | tar xfz - -C /root/ && \
  echo >> /root/.bashrc && \
  echo 'export PATH=~/scala-$SCALA_VERSION/bin:$PATH' >> /root/.bashrc

# Install sbt
RUN \
  curl -L -o sbt-$SBT_VERSION.deb http://dl.bintray.com/sbt/debian/sbt-$SBT_VERSION.deb && \
  dpkg -i sbt-$SBT_VERSION.deb && \
  rm sbt-$SBT_VERSION.deb && \
  apt-get update && \
  apt-get install sbt && \
  sbt sbtVersion

# Install Docker
RUN \
  # if we have older versions, let's get rid of them first
  apt-get install docker

# Define working directory
WORKDIR /root

What I want to do is, I would like to install Docker into this image and be able to run docker commands form within. How do I start the installed docker instance?

joesan
  • 13,963
  • 27
  • 95
  • 232

2 Answers2

6

You can install the docker client binary only and share dockerd with the container and host.

Here is an example Dockerfile:

FROM  openjdk:8

# Install your dependencies
# ...

# Install curl
RUN apt-get update && apt-get install -y \
  curl \
  && rm -rf /var/lib/apt/lists/*

# Install docker client    
ENV DOCKER_CHANNEL stable
ENV DOCKER_VERSION 17.03.1-ce
ENV DOCKER_API_VERSION 1.27
RUN curl -fsSL "https://download.docker.com/linux/static/${DOCKER_CHANNEL}/x86_64/docker-${DOCKER_VERSION}.tgz" \
  | tar -xzC /usr/local/bin --strip=1 docker/docker

Build the image:

$ docker build -t docker-client .

Run a docker container with mounting /var/run/docker.sock, and then you can use docker command in the container:

$ docker run --rm -it -v /var/run/docker.sock:/var/run/docker.sock docker-client /bin/bash
root@c696b78206a8:/# docker version
Client:
 Version:      17.03.1-ce
 API version:  1.27
 Go version:   go1.7.5
 Git commit:   c6d412e
 Built:        Mon Mar 27 16:58:30 2017
 OS/Arch:      linux/amd64

Server:
 Version:      17.05.0-ce
 API version:  1.29 (minimum version 1.12)
 Go version:   go1.7.5
 Git commit:   89658be
 Built:        Thu May  4 21:43:09 2017
 OS/Arch:      linux/amd64
 Experimental: false

Note that mounting the docker.sock means that the container is permitted access to the docker host. Please be aware that there are potential security risks.

Joshua Wade
  • 4,755
  • 2
  • 24
  • 44
minamijoyo
  • 3,305
  • 1
  • 18
  • 20
  • Thanks for the reply! The container getting access to the docker host is not a problem because this will be used only to build and run tests. Once this is done, we will not use docker in docker! – joesan Jul 03 '17 at 10:56
3

I would recommend you that use the official "dind" image (docker in docker): https://hub.docker.com/_/docker/ . However you will need to rewrite your Dockerfile.

FROM docker:dind-stable
# Install your stuff

And, take into account this:

Although running Docker inside Docker is generally not recommended, there are some legitimate use cases, such as development of Docker itself.

Robert
  • 33,429
  • 8
  • 90
  • 94