Answer the question
In order to leave comments, you need to log in
How to run a command from a container in a "neighbor" (docker-compose) container?
There is, for example, docker-compose which describes three containers, for example named nginx , php and cron .
How can a cron container execute a command on an adjacent container, such as an nginx container ?
From the host everything is clear:
docker-compose exec nginx service nginx restart
Answer the question
In order to leave comments, you need to log in
Add sshd to the containers you need and, if necessary, connect from one container to ssh to another and do what you need there. This is the best way, in my opinion.
You can also install docker into the container from which you want to execute commands in other containers and pass the docker socket from the host to the container. The side effect here is that your container will suddenly crash, all other running containers on the host machine may crash.
I would add cron to the host system.
Since this is not the task, I would make access from the cron container via SSH to the host system and execute the command on it. For example:
ssh [email protected] docker-compose exec nginx service nginx restart
I proceeded from the fact that all containers will be as independent of the host as possible, so that when transferring containers there is no need to configure something a lot (including cron).
Probably the most optimal option would be the one proposed by Maxim Kudryavtsev , with dropping the docker itself into the container. Alternatively, it was possible to install the entire docker in a container, but the final image was 200-300mb. But it turned out that there are already separate docker clients.
So, the configuration turned out like this:
Add the cron section to docker-compose.yml:
cron:
image: cron
build:
context: './images/cron'.
volumes:
#конфиги для крона
- ./config/cron:/etc/cron.d
#пробрасываем sock в readonly-режиме, чтобы docker внутри контейнера видел всё, что на хосте
- /var/run/docker.sock:/var/run/docker.sock:ro
#используем сборку alpine с кроном - https://github.com/xordiv/docker-alpine-cron
FROM xordiv/docker-alpine-cron
#скачиваем и устанавливаем сборку с докер клиентом
ARG DOCKER_CLI_VERSION="18.06.3-ce"
ENV DOWNLOAD_URL="https://download.docker.com/linux/static/stable/x86_64/docker-$DOCKER_CLI_VERSION.tgz"
# install docker client
RUN mkdir -p /tmp/download \
&& curl -L $DOWNLOAD_URL | tar -xz -C /tmp/download \
&& mv /tmp/download/docker/docker /usr/bin/ \
&& rm -rf /tmp/download \
&& rm -rf /var/cache/apk/*
[email protected]:~$ docker-compose exec cron /usr/bin/docker exec user1_nginx_1 nginx -v
nginx version: nginx/1.15.10
[email protected]:~$ docker-compose images
Container Repository Tag Image Id Size
------------------------------------------------------------------------
user1_certbot_1 certbot/certbot latest 4bdc1514009b 108 MB
user1_crond_1 cron latest 5e632bfd8eb0 42.1 MB
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question