Is there any proper way of restarting an entire docker compose
stack from within one of its containers?
One workaround involves mounting the docker socket:
volumes:
- /var/run/docker.sock:/var/run/docker.sock
and then use the Docker Engine SDKs (https://docs.docker.com/engine/api/sdk/examples/).
However, this solution only allows restarting the containers itselves. There seems to be no way to send compose commands, like docker compose restart
, docker compose up
, etc.
The only solution I’ve found to send docker compose
commands is to open a terminal on the host from the container using ssh, like this: access host's ssh tunnel from docker container
This is partly related to How to run shell script on host from docker container? , but I’m actually looking for a more specific solution to only send docker compose
commands.
2
Answers
As mentioned by @BMitch in the comments, compose project name was the reason why I wasn't able to run docker compose commands inside the running container.
By default the compose project name is set to the directory name, so if the
docker-compose.yml
is run from a host directory namedfolder1
, then the commands inside the container should be run as:So now, for example, restarting the stack works:
Just as a reference, a fixed project name for your compose can be set using
name: ...
as a top-level attribute of the.yml
file, but requires docker compose v2.3.3 : Set $PROJECT_NAME in docker-compose fileI tried with this simple docker-compose.yml file
Then I started a
docker
container usingThen, inside the container, I did
and it started the container up on the host.
Please note that you have an error in your socket mapping. It needs to be
(you have a period instead of a slash at one point)