0

I am trying to run TeamCity CI Server within Docker DinD(Docker in Docker) by using a dockerfile. I am using the official docker:19-dind image as the base image.

The main purpose is to create a DinD container and run TeamCity's official container within that DinD container. First of all, is that really possible using DinD?

The dockerfile is as follows:

.dockerignore

# Official Docker in Docker 19 version as base image.
FROM docker:19-dind AS base
# Create work directory
WORKDIR /teamcity-ci-server
# Command to check version
RUN docker --version 


# Final image inherited from base image
FROM base as final
# Adding directory
WORKDIR /teamcity-ci-server
# Run commands to setup TeamCity CI Server
RUN docker pull jetbrains/teamcity-server \
&& docker images \
&& docker run -d --privileged --name teamcity-ci-server -p 5002:8111 jetbrains/teamcity-server 

# Add volume mount for DinD
VOLUME /var/run/docker.sock:/var/run/docker.sock

# Exposing port 
EXPOSE 5001

However, after running docker build -f .dockerignore -t teamcity-ci-server:v1 ., I am getting the following error:

Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?

enter image description here

I believe that this error is displaying because docker is not running. Think I cannot run systemctl start docker since this is not a linux image and systemctl does not work here.

Does anyone know how to fix this issue that's happening within Docker DinD images?

7
  • 2
    Canonical advice is generally to not use Docker-in-Docker for CI but rather to reuse the host's Docker socket. It looks like that VOLUME directive is trying to, but you can't specify a bind mount host directory in a Dockerfile; you need to use a docker run -v option for it. No matter what you do, you can't run docker commands in the Dockerfile itself.
    – David Maze
    Jul 3, 2021 at 3:57
  • I tried to create a simple Docker DinD container. Removed the VOLUME from the dockerfile as you suggested, since we are using a DinD image. Then I got inside the container using docker exec and then manually ran docker run -d --privileged --name teamcity-ci-server2 -p 5002:8111 jetbrains/teamcity-server. It actually worked. I was able to access Teamcity running within DinD from my machine's browser. However, as you mentioned that we cannot run docker commands from the dockerfile, are there any alternative?
    – arjunbnair
    Jul 3, 2021 at 20:01
  • How can we automate docker run in DinD?
    – arjunbnair
    Jul 3, 2021 at 20:04
  • What are you trying to accomplish? Imagine the output of docker build is a tar file and nothing else; what would be in that tar file?
    – David Maze
    Jul 3, 2021 at 22:18
  • 1
    A virtual machine might be a better match for that setup. It can run with an embedded Docker daemon, and then use a normal startup script to launch containers inside that, without requiring special access to the host.
    – David Maze
    Jul 4, 2021 at 19:14

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.