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?

arjunbnair
  • 330
  • 7
  • 18
  • 2
    Canonical advice is generally to [not use Docker-in-Docker for CI](https://jpetazzo.github.io/2015/09/03/do-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](https://stackoverflow.com/questions/18873474/can-i-specify-host-directory-to-mount-from-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 03 '21 at 03: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 03 '21 at 20:01
  • How can we automate `docker run` in DinD? – arjunbnair Jul 03 '21 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 03 '21 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 04 '21 at 19:14
  • I want to create a docker image, which when simply run would use DinD to run TeamCity inside. Its just experimental. So, typically saying, when the main container (Container that use docker:19-dind) starts up, it should spin up another container inside it using Teamcity's docker image and run Teamcity. – arjunbnair Jul 04 '21 at 21:13
  • Okay. Thank you. – arjunbnair Jul 05 '21 at 20:34

0 Answers0