I am using docker-compose for deployment.
I want to restart my "centos-1" container from "centos-2" container. Both containers are running on the same host.
Please suggest, How could I achieve this in a simplest and automated way?
I followed How to run shell script on host from docker container? and tried to run a script on Host from "centos-2" container, but the script is executing inside a container and not on the host.
Script:
#!/bin/bash
sudo docker container restart centos-1
Error:
line 2: docker: command not found
(Docker isn't installed inside any centos-2 container)
You need:
Install docker CLI (command line interface) on second container. Do not confuse with full scale installation - you dont need docker daemon, only command line tool (docker executable)
Share you host's docker daemon (service) to make it accessible in second container. That is achieved with simply sharing /var/run/docker.sock when launching 2nd container, example:
docker run ... -v "/var/run/docker.sock:/var/run/docker.sock" container2 ...
Now you can execute any docker command, like docker stop from second container and these commands are happily passed to your main (and the only) docker daemon.
There is a approach from the CI-context to control the Docker Daemon on System from a running container called Docker-out-of-Docker (DooD):
you have to install docker inside your container
Map you docker installation from your system inside your container using volumes
-v /var/run/docker.sock:/var/run/docker.sock
Now each docker command inside your container are execute on the system docker installation. E.g. if you type docker image list inside your container there should be the same list as if your type the command on your system.
Related
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.
I have a kafka cluster installed in my local windows machine, and I would like to access this cluster from my spring boot application deployed as a container in docker toolbox, here is my application.properties file.
kafka.bootstrapAddress = 127.0.0.1:9092
And when I launch the container I use the host network but it doesn't work.
docker run spring-app:latest --network host
So how can i access this cluster. ?
Thank you in advance.
From the docker run reference, the docker run command usage is like this:
$ docker run [OPTIONS] IMAGE[:TAG|#DIGEST] [COMMAND] [ARG...]
You are not providing the --network option correctly. The option must come before the image name and whatever comes after the image name will be passed to the created container as the command and arguments.
Here is how you should invoke the command to correct your issue:
$ docker run --network host spring-app:latest
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.
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.
There is possibility to install docker in docker container.
How to control docker host service from it's container (manage another containers)?
If execute docker run --privileged=true -v /var/run/docker.sock:/var/run/docker.sock -v $(which docker):$(which docker) -ti debian and enter docker error appears:
docker: error while loading shared libraries: libapparmor.so.1: cannot open shared object file: No such file
The error you're seeing seems very clear: the docker binary requires a shared library that is not present inside the container.
Is your container running the same distribution and version as your host? If it is, you simply need to determine which packages provide the necessary dependencies and install them inside the container.
If not, you will probably have better luck simply installing docker inside the container, rather than trying to bind-mount it from the host. There is probably a source of recent Docker versions available for Debian.
if your host is a linux based machine, you dont need to install docker inside container, you can just mount docker into container and whatever you do with that inside your container is just like doing it on host. I have tested it on a Ubuntu machine (image: https://github.com/mohamnag/ubuntu-git.git) by mounting /usr/bin/docker from host into /bin/docker inside container. then inside that container you can literally do (build, stop, list ...) whatever you may have done with docker inside host.