1

I have a docker image based on amazonlinux:2.0.20220912.1 with Amazon Corretto Java SDK 11 installed.

I have also added a docker installation to this image by using this command in the Dockerfile

RUN amazon-linux-extras install docker

I use this image to run a gradle build of a Spring Boot project in a Jenkins pipeline. To make things even more complex, the Jenkins pipeline is using the Jenkins docker function called inside() which mounts the Jenkins workspace in the container and then run the groovy closure inside the container.

def dockerImage = docker.image("my docker image")
dockerImage.inside("--entrypoint='' -e AWS_DEFAULT_REGION=${region}") {
    sh './gradlew test'
}

The Spring Boot project have a junit test that use testcontainers so docker must be available inside the container. But when the test attempts to start a test container I get this error

java.lang.IllegalStateException: Could not find a valid Docker environment.

A lot of googling suggest I need to start a docker daemon inside the container. The problem is, I can't find a way to start it. Some google hits say I should use systemctl start docker, but that fails

Failed to get D-Bus connection: Operation not permitted

Other hits suggest using the command service docker start but there is no command service in the image.

So what is the magic incantation to start a docker daemon inside an amazonlinux:2.0.20220912.1 container?

ola
  • 129
  • 1
  • 10
  • Generally you don't: a container only runs one process, and it's especially complex to run a Docker daemon in a container. A common approach is to use a bind mount to reuse the host's Docker socket. A less-common, more complex approach is to use "Docker in Docker" to run a second Docker daemon in a second container, though this is generally discouraged. – David Maze Nov 03 '22 at 14:15
  • 1
    The easiest approach is probably to run the container you need outside of the amazonlinux container. Just like they describe in the [documentation](https://www.jenkins.io/doc/book/pipeline/docker/#running-sidecar-containers). – Garuno Nov 03 '22 at 14:41
  • @DavidMaze I would like to disagree with the first point. You can absolutely run multiple processes in the same container. It is not recommended to do it, but docker even has an example in the [official documentation](https://docs.docker.com/config/containers/multi-service_container/) on how to do this. – Garuno Nov 03 '22 at 14:49
  • @DavidMaze I have tried to bind-mount the hosts docker socket but it did not help. I used this call to start my docker container: dockerImage.inside("-v /var/run/docker.sock:/var/run/docker.sock --entrypoint=''") but the socket seem to be dead. The testcontainer can't start inside my container. I get this log messages inside my container: "DOCKER_HOST unix:///var/run/docker.sock is not listening", "docker-machine executable was not found on PATH ([/local/amazon-corretto-11.0.16.9.1/bin, /usr/local/sbin, /usr/local/bin, /usr/sbin, /usr/bin, /sbin, /bin])" – ola Nov 04 '22 at 10:38
  • Is it a question how to run Docker in a container, or how to have a Docker process running during the Docker build process? – Kevin Wittek Nov 07 '22 at 14:40
  • @KevinWittek the question is about how I can have docker available inside the container that haas been started by the Jenkins pipeline. It is not enough to just install docker when the image is built. But it seems like this is not possible so I will abandon the attempt. – ola Nov 08 '22 at 15:44
  • This is possible, you either need to use Docker-in-Docker approach or the Docker-Socket-Mounting approach. I'd suggest discussing with your CI team how to best get access of Docker within the container, since this is not trivial and setup dependent. – Kevin Wittek Nov 11 '22 at 08:12

0 Answers0