GitLab - Docker inside gitlab/gitlab-ce get errors - docker

I'm running a gitlab/gitlab-ce container on docker. Then , inside it, i want to run a gitlab-runner service, by providing docker as runner. And every single command that i run (e.g docker ps, docker container ..), i get this error:
Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is
the docker daemon running
P.s: i've tried service docker restart, reinstal docker and gitlab-runner.

By default it is not possible to run docker-in-docker (as a security measure).
You can run your Gitlab container in privileged mode, mount the socket (-v /var/run/docker.sock://var/run/docker.sock) and try again.
Also, there is a docker-in-docker image that has been modified for docker-in-docker usage. You can read up on it here and create your own custom gitlab/gitlab-ce image.
In both cases, the end result will be the same as docker-in-docker isn't really docker-in-docker but lets your manage the hosts docker-engine from within a docker container. So just running the Gitlab-ci-runner docker image on the same host has the same result and is a lot easier.

By default the docker container running gitlab does not have access to your docker daemon on your host. The docker client uses a socket connection to communicate to the docker daemon. This socket is not available in your container.
You can use a docker volume to make the socket of your host available in the container:
docker run -v /var/run/docker.sock:/var/run/docker.sock gitlab/gitlab-ce
Afterwards you will be able to use the docker client in your container to communicate with the docker daemon on the host.

Related

Why would it be necessary to give a docker container access to the docker socket?

I am reading a docker run command where it maps /var/run/docker.sock
like:
docker run -it --net=host --rm -v /var/run/docker.sock:/var/run/docker.sock theimage /bin/bash
Why would the container would need access to the socket? (this article says it is a very bad idea.)
What would be one case where the container need access to the socket?
It is not necessary until the container needs to invoke itself the docker daemon, for example, in order to create and run an inner container.
For example, in my CI chain Jenkins builds a docker image to run the build and test process. Inside it we need to create an image to test and then submit it to K8S. In such situation Jenkins, when builds the pipeline container, passes to it the docker socket to allow the container to create other containers using the host server docker daemon.

Rootless-ly Running Docker Daemon inside another Docker container

According to Docker official website: https://docs.docker.com/engine/security/rootless/ it's possible to run Docker Daemon rootless-ly (without root access, no --privileged flag).
However I'm convinced this would not work when running from inside a Docker container. There is no way of getting modprobe inside a Docker container without root access (--privileged). So it's not possible to install the Docker rootless script.
Supposedly there is an official image in Docker hub: docker:dind-rootless image here So I pulled the image and SSH'd into the container, however I'm getting the following error when running dockerd
INFO[2020-07-17T20:50:32.355617100Z] Starting up dockerd needs to be started with root. To see how to run dockerd in rootless mode with unprivileged user, see the documentation
Any suggestions on how to run Docker daemon rootlessly inside another Docker container? I know this is possible with root, but is there a way to do without? I can't get root access as I'm deploying to AWS fargate, which doesn't support privileged access at the container level.
It's been 2 years. There's an image for it now.
https://docs.docker.com/engine/security/rootless/#rootless-docker-in-docker

Running docker command in a Java application executing in a docker container

I am creating a Spring Boot monitoring agent that collects docker metrics. The agent can be attached through POM dependency to any client Spring Boot application that runs inside a docker container.
In the agent, I am trying to programatically run docker stats
But, it fails to execute because the docker container doesn't have docker client installed in it.
So how can I run docker commands in docker container? Please note, I can't make changes to the Dockerfile of client.
You may execute docker commands within the container by defining the docker socket in the container.
run the container and mount the 'docker.sock' in the following manner:
docker run -v /var/run/docker.sock:/var/run/docker.sock ...
so mainly you have to mount docker.sock to order to run docker commands within container.

Docker in docker connection error

I'm trying to run a Java application in a docker container. The application also communicates with docker. So I used docker:latest image and installed the openjdk. Now when I am running the container in interactive mode (privileged) I get the error Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running? when I input any docker command on the command line.
I run the container with docker run --privileged -ti con_name
Have you gone through this link? In there it's mentioned that /var/lib/docker needs to be a volume. In your docker run command, you are not mentioning any volumes. You might give this page a read and make sure everything is correct.

How to use docker inside docker container in a safe way

I have some docker containers running on my docker environment (on a CentOS VM) which need docker inside. So I mount /var/run/docker.sock inside the containers.
Now I'm creating /etc/default/docker in which I put
DOCKER_OPTS="-H tcp://xx.xx.xx.xx:2376"
But now my question is: which IP is xx.xx.xx.xx? Is it the IP of the host or the IP of a container? + Is this the savest way to let a docker container use the socket? (=use docker in docker)
Running docker within docker is not so trivial an you might have a good reason for doing that.
The last time I was doing that, I was using dind (docker in docker) and had to mount the socket (/var/run/docker.sock) and used it in a combination with the --privileged flag. However things might have changed now (see https://github.com/docker/docker/pull/15596) and it should be able to run it without the socket mount:
docker run --privileged -d docker:dind
So be sure to check out this comprehensive guide at https://hub.docker.com/_/docker/
Working with Docker in Docker can be tricky. I would recommend using the official Docker image with the dind tag. You shouldn't need to specify the DOCKER_HOST in options as it will be correctly configured. For example running:
docker run -ti --name docker -v /var/run/docker.sock:/var/run/docker.sock --privileged docker:dind sh
Will drop you to a shell inside the container. Then if your run docker ps you should see a list of containers running on the host machine. Note the --privileged flag is required in this case as we are accessing the Docker daemon outside the container.
Hope this helps!
Dylan
Edit
Drop the --privileged flag from the above command due to security issues highlighted by Alexander in the comments. You also can drop the dind tag as its not required.

Resources