0
votes

When creating a Jenkins Docker container, it is very useful to able to connect to the Docker daemon. In that way, I can start docker commands inside the Jenkins container.

For example, after starting the Jenkins Docker container, I would like to 'docker exec -it container-id bash' and start 'docker ps'.

On Linux you can use bind-mounts on /var/run/docker.sock. On Windows this seems not possible. The solution is by using 'named pipes'. So, in my docker-compose.yml file I tried to create a named pipe.

version: '2'
services:
  jenkins:
    image: jenkins-docker
    build:
      context: ./
      dockerfile: Dockerfile_docker
    ports:
      - "8080:8080"
      - "50000:50000"
    networks:
      - jenkins
    volumes:
      - jenkins_home:/var/jenkins_home
      - \\.\pipe\docker_engine:\\.\pipe\docker_engine
      # - /var/run/docker.sock:/var/run/docker.sock
      # - /path/to/postgresql/data:/var/run/postgresql/data
      # - etc.

Starting docker-compose with this file, I get the following error:

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

How can I setup the docker-compose file so that I can use the docker.sock (or Docker) inside the started container?

On Linux you can use something like volumes: /var/run/docker.sock:/var/run/docker.sock. This does not work in a Windows environment. When you add this folder (/var) to Oracle VM Virtualbox, it won't get any IP forever. And on many posts

2
Windows10 not use mobylinux vm? You setup your own virtual machine as host in virtualbox?atline
It looks that installing an linux /ubuntu VM is the best solution. I will start experimenting with it.tm1701

2 Answers

1
votes

You can expose the daemon on tcp://localhost:2375 without TLS in the settings. This way you can configure Jenkins to use the Docker API instead of the socket. I encourage you to read this article by Nick Janetakis about "Understanding how the Docker Daemon and the Docker CLI work together".

And then there are several Docker plugins for Jenkins that allows this connection:

enter image description here

Also, you can find additional information in the Docker plugin documentation on wiki.jenkins.io:

def dockerCloudParameters = [
  connectTimeout:   3,
  containerCapStr:  '4',
  credentialsId:    '',
  dockerHostname:   '',
  name:             'docker.local',
  readTimeout:      60,
  serverUrl:        'unix:///var/run/docker.sock', // <-- Replace here by the tcp address
  version:          ''
]

EDIT 1:

I don't know if it is useful, but the Docker Daemon on Windows is located to C:\ProgramData\docker according to the Docker Daemon configuration doc.

EDIT 2:

You need to say explicitly the container to use the host network because you want to expose both Jenkins and Docker API. Following this documentation, you only have to add --network=host (or network_mode: 'host' in docker-compose) to your container/service. For further information, you can read this article to understand what is the purpose of this network mode.

1
votes

First try was to start a Docker environment using "Docker Quickstart terminal". This is a good solution when running Docker commands within that environment.

When installing a complete CI/CD Jenkins environment via Docker means that WITHIN the Jenkins Docker container you need to access the Docker daemon. After trying many solutions, reading many posts, this did not work. @Paul Rey, thank you very much for trying all kinds of routes.

A good solution is to get an Ubuntu Virtual Machine and install it via the Oracle VM Virtualbox. It is then VERY IMPORTANT to install Docker via this official description.

Before installing Docker, of course you need to install Curl, Git, etc.