使用Dockerfile安装Docker

时间:2017-06-28 13:22:58

标签: scala docker sbt docker-in-docker

我有一个看起来像这样的Dockerfile:

# 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实例?

2 个答案:

答案 0 :(得分:2)

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

FROM docker:dind-stable
# Install your stuff

并考虑到这一点:

  

虽然一般不推荐在Docker中运行Docker,但有一些合法的用例,比如开发Docker本身。

答案 1 :(得分:1)

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

这是Dockefile的一个例子:

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 .

运行装有/var/run/docker.sock的docker容器,然后您可以在容器中使用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主机。请注意,存在潜在的安全风险。