Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Restart container from inside

Tags:

docker

centos

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 ?

like image 648
quantCode Avatar asked Apr 07 '18 23:04

quantCode


People also ask

Can you restart a docker container?

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.

How can we start stop and restart containers from inside another container?

You do this by exposing the docker socket inside the container, e.g: docker run -v /var/run/docker.

How do I start docker service inside a container?

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.

Can a container restart by itself?

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.

How do I resume a docker container?

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.


1 Answers

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
like image 160
Weike Avatar answered Sep 20 '22 16:09

Weike