Every now and then I come across the requirement to build Docker images inside a Docker container. More often than not, this happens when I need to build Docker images as part of a Continuous Integration pipeline running Jenkins - where the Jenkins master (or agent) is running inside a Docker container.
Docker doesn't recommend running the Docker daemon inside a container (except for very few use cases like developing Docker itself), and the solutions to make this happen are generally hacky and/or unreliable.
Fear not though, there is an easy workaround: mount the host machine's Docker socket in the container. This will allow your container to use the host machine's Docker daemon to run containers and build images.
Your container still needs compatible Docker client binaries in it, but I have found this to be acceptable for all my use cases.
The TL;DR version using my prebuilt image is:
docker run \ -p 8080:8080 \ -v /var/run/docker.sock:/var/run/docker.sock \ --name jenkins \ getintodevops/jenkins-withdocker:lts
The guide below is for Jenkins, but you can apply the same logic to any other build server.
Building Docker containers with Jenkins inside a container
First, we'll run Jenkins as a container using the official Jenkins image:
docker run -p 8080:8080 \ -v /var/run/docker.sock:/var/run/docker.sock \ --name jenkins \ jenkins/jenkins:lts
Note that the key here is mounting /var/run/docker.sock from the host machine to the same location inside the container.
Then, we'll need to install the Docker binaries inside the container. Spawn an interactive shell inside the running Jenkins container:
docker exec -it -u root jenkins bash
Because the official Jenkins image is based on Debian 9, we can use apt to install the Docker binaries as instructed in the Docker installation guide. This is a single snippet to install some prerequisites, configure the official Docker apt repositories and install the latest Docker CE binaries:
apt-get update && \ apt-get -y install apt-transport-https \ ca-certificates \ curl \ gnupg2 \ software-properties-common && \ curl -fsSL https://download.docker.com/linux/$(. /etc/os-release; echo "$ID")/gpg > /tmp/dkey; apt-key add /tmp/dkey && \ add-apt-repository \ "deb [arch=amd64] https://download.docker.com/linux/$(. /etc/os-release; echo "$ID") \ $(lsb_release -cs) \ stable" && \ apt-get update && \ apt-get -y install docker-ce
Caveat: The Docker daemon running on your host machine must be compatible with the version of client binaries you are installing. To verify the version, run docker version on your host machine.
Ta-da! Your Jenkins container should now have a functioning Docker installation. Verify by running:
docker ps
The output should be the same as when running the command on the host machine (you should at least expect to see the Jenkins container running!).
Next, you'll need to finish the installation of Jenkins as usual. Open the Jenkins installer by navigating to http://localhost:8080.
You will need the initial admin password, which can be obtained by running:
docker exec jenkins cat /var/jenkins_home/secrets/initialAdminPassword
Next select "Install recommended plugins", and wait for Jenkins to install everything. The Docker Pipeline plugin is installed by default, which means you are ready to go!
I have pre-built a Jenkins 2.73.1 image with Docker 17.09.0-ce binaries. It's available on Docker Hub as getintodevops/jenkins-withdocker.
Next, read How to build your first Docker image with Jenkins!
tip, docker
相关推荐
这里准备了一份docker-ce-v18的离线包供下载。 也可以自己手动下载最新包,下载方法如下: mkdir /apps/docker-ce-v18 yum install --downloadonly --downloaddir=/apps/docker-ce-v18 \ yum-utils device-...
从Github上下载下来的最新1.25.4的docker-compose-Linux-x86_64........
一键离线安装docker、docker-compose
docker-compose-window-2.14.2.exe免费下载
docker-ce-17.03.2.ce-1.el7.centos.x86_64 Centos7 离线安装包和依赖包、 audit-libs-python-2.7.6-3.el7.x86_64.rpm checkpolicy-2.5-4.el7.x86_64.rpm docker-ce-17.03.2.ce-1.el7.centos.x86_64.rpm docker-ce-...
$mv docker-compose-Linux-x86_64 docker-compose $chmod +x docker-compose $docker-compose version --------------------------------------------------------------- docker-compose version 1.24.0, build 0...
安装docker,sudo dpkg -i docker-ce_17.03.2~ce-0~ubuntu-xenial_amd64.deb所使用的文件
1、免积分,不需要VIP,直接下载即可(如果后期被系统修改要VIP,可随意私信我要); 2、由于网上关于 aarch64 的资源特别少,尤其是 docker 这一块的,其他网友写的...3、本资源适用于docker-compose-linux-aarch64
Docker-in-Action.pdf In 2011, I started working at Amazon.com. In that first week my life was changed as I learned how to use their internal build, dependency modeling, and deployment tool- ing. This ...
docker run --rm \ -e SETTINGS_FLAVOR=qiniustorage \ -e QINIU_BUCKET=YOUR_BUCKET \ -e QINIU_ACCESSKEY=YOUR_ACCESSKEY \ -e QINIU_SECRETKEY=YOUR_SECRETKEY \ -e QINIU_DOMAIN=YOUR_BUCKET_DOMAIN \ -p...
多体系结构:在amd64,arm(RaspberryPI ...)等上运行变更日志 :page_with_curl: 参见最新版本最新标签:跑步最低要求: docker run -d --name radicale \ -p 5232:5232 \ tomsquest/docker-radicale 基本说明: ...
tvial/docker-mailserver邮箱服务器。可以自己在小网中搭建一个邮件服务器自己玩。启动方法 docker run --name forsaken-mail -itd -p 2255:25 -p 3000:3000 tvial/docker-mailserver:latest
docker-compose-viz, Docker 组合图形可视化 docker-compose-viz :如何使用 Docker考虑到当前工作目录是你的docker-compose.yml 文件所在的位置:docker run --rm -it --name dcv
liunx的docker-ce18.09.0-3稳定版本,网上的资源比较少。 You could try using --skip-broken to work around the problem You could try running: rpm -Va --nofiles --nodigest
docker离线安装包 Docker provides a way to run applications securely isolated in a container, packaged with all its dependencies and libraries.
mv docker-compose /usr/local/bin/docker-compose 验证安装 systemctl restart docker docker version (看到版本信息证明安装成功) docker-compose version (看到版本信息证明安装成功)
docker-machine版本0.14.0 chmod +x docker-machine-Linux-x86_64 sudo cp docker-machine-Linux-x86_64 /usr/local/bin/docker-machine docker-machine -v
Docker-CI Docker-CI是一个小程序,可让您使用Github Container Registry或DockerHub轻松实现持续集成。 它使用标签设置不同的选项以为每个容器启用Docker-ci。 Docker-CI监视容器的创建,这意味着您不必在更新...
安装docker-compose的文件,放在/usr/local/bin/docker-compose下,没有目录则新创建一个
Docker is powerful and simple to use, and it makes life easier for developers and administrators alike providing shorter build times, fewer production bugs, and effortless application roll-out. ...