使用 Docker 文件安装 Docker

Install Docker using a Dockerfile

我有一个 Docker 文件,如下所示:

# 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

我想做的是,我想将 Docker 安装到此映像中,并能够在其中形成 运行 docker 命令。如何启动已安装的 docker 实例?

我建议您使用官方 "dind" 图片(docker in docker):https://hub.docker.com/_/docker/ 。但是,您将需要重写 Dockerfile。

FROM docker:dind-stable
# Install your stuff

并且,考虑到这一点:

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

您可以仅安装 docker 客户端二进制文件并与容器和主机共享 dockerd

这是一个例子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

构建图像:

$ docker build -t docker-client .

运行一个docker容器,挂载/var/run/docker.sock,然后可以在容器中使用docker命令:

$ 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

注意挂载 docker.sock 意味着允许容器访问 docker 主机。请注意存在潜在的安全风险。