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 ?
Use a restart policy Restart the container if it exits due to an error, which manifests as a non-zero exit code. Optionally, limit the number of times the Docker daemon attempts to restart the container using the :max-retries option.
You do this by exposing the docker socket inside the container, e.g: docker run -v /var/run/docker.
To run docker inside docker, all you have to do it just run docker with the default Unix socket docker. sock as a volume. Just a word of caution: If your container gets access to docker. sock , it means it has more privileges over your docker daemon.
no: Containers won't restart automatically. on-failure[:max-retries]: Restart the container if it exits with a non-zero exit code, and provide a maximum number of attempts for the Docker daemon to restart the container. always: Always restart the container if it stops.
Enter the docker start command with the Container ID in the command line to resume the Container. Note: This preserves environment variables set during the initial docker run statement.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With