Reputation: 565
On a batch job, Am doing a large number of operations inside a docker.
Is there to send a command from inside so docker can come back as if it were just started ?
Upvotes: 16
Views: 25720
Reputation: 3341
This is a nice old unanswered question. Personally I like to drop a file into a directory and have my entry point script detect it to exit the container. This will work as long as the container is set to restart. e.g. restart: unless-stopped
#!/bin/bash
# Begin long running process, add a `&` to do so in the background
/app/scripts/long_running.sh &
# Wait for a signal to restart
while true; do
if [ -f /app/restart.txt ]; then
echo "Restarting container..."
# Remove the restart flag
rm -f /app/restart.txt
# Do any additional cleaning up if you need to.
# exit the container - exit code is optional
exit 3010
fi
sleep 5
done
In your container simply touch
/app/restart.txt to trigger a container restart
Upvotes: 4
Reputation: 118
reboot command works from withing container. I use this in my Go code inside docker
out, err = exec.Command("reboot").Output()
Upvotes: -3
Reputation: 1270
You just need to install docker client when building your docker images and map /var/run/docker.sock
when running a new container to enable docker client inside the container to connect the docker daemon on the host, then you can use docker
command just like on the host.
First, add commands to install docker-ce in your Dockerfile
:
FROM centos:7.8.2003
ENV DOCKER_VERSION='19.03.8'
RUN set -ex \
&& DOCKER_FILENAME=https://download.docker.com/linux/static/stable/x86_64/docker-${DOCKER_VERSION}.tgz \
&& curl -L ${DOCKER_FILENAME} | tar -C /usr/bin/ -xzf - --strip-components 1 docker/docker
Then, build a new image and run a new container using it:
$ docker build --tag docker-in-docker:v1 .
$ docker run -dit \
--name docker-in-docker \
-v /var/run/docker.sock:/var/run/docker.sock:ro \
docker-in-docker:v1 bash
Now, you can operate docker-daemon (on the host) inside docker container.
$ docker exec -it docker-in-docker docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
bdc2d81b2227 docker-in-docker:v1 "bash" 8 seconds ago Up 7 seconds docker-in-docker
# just restart the container docker-in-docker in the container docker-in-docker:
$ docker exec docker-in-docker docker restart docker-in-docker
Upvotes: 14