Políticas de reinicialização do Docker
O Docker fornece um recurso que resolve esse problema chamado política de reinicialização do Docker :
Docker provides restart policies to control whether your containers start automatically when they exit, or when Docker restarts. Restart policies ensure that linked containers are started in the correct order. Docker recommends that you use restart policies, and avoid using process managers to start containers.
Os tipos de políticas de reinicialização:
no Do not automatically restart the container. (the default)
on-failure Restart the container if it exits due to an error, which
manifests as a non-zero exit code.
unless-stopped Restart the container unless it is explicitly stopped or
Docker itself is stopped or restarted.
always Always restart the container if it stops.
Uso
Você pode então usar a política de reinicialização da seguinte forma:
$ docker run -dit --restart always redis
Considerações
A restart policy only takes effect after a container starts successfully. In this case, starting successfully means that the container is up for at least 10 seconds and Docker has started monitoring it. This prevents a container which does not start at all from going into a restart loop.
If you manually stop a container, its restart policy is ignored until the Docker daemon restarts or the container is manually restarted. This is another attempt to prevent a restart loop.
Restart policies only apply to containers. Restart policies for swarm services are configured differently. See the flags related to service restart.
Exemplo
Aqui, vamos usar o container Docker hello-world para ilustrar como isso funciona.
Para começar, removemos:
$ docker pull hello-world
Using default tag: latest
Trying to pull repository docker.io/library/hello-world ...
latest: Pulling from docker.io/library/hello-world
9db2ca6ccae0: Pull complete
Digest: sha256:4b8ff392a12ed9ea17784bd3c9a8b1fa3299cac44aca35a85c90c5e3c7afacdc
Status: Downloaded newer image for docker.io/hello-world:latest
$
Com o nosso contêiner baixado, vamos executá-lo:
$ docker run -dit --restart always hello-world
0f6a61dddd3b667727a20df2c198941b2202653a71b6c86b8ace7b236bafc974
E se executarmos um par de docker ps
, veremos a reinicialização de novo e de novo:
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
0f6a61dddd3b hello-world "/hello" 27 seconds ago Up Less than a second upbeat_brown
...time passes...
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
0f6a61dddd3b hello-world "/hello" 5 minutes ago Restarting (0) About a minute ago upbeat_brown
...time passes...
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
0f6a61dddd3b hello-world "/hello" 10 minutes ago Restarting (0) 3 minutes ago upbeat_brown
Olhando os registros do Docker, podemos ver que ele foi reiniciado várias vezes:
$ docker logs 0f6a61dddd3b -f | grep "Hello from Docker"
Hello from Docker!
Hello from Docker!
Hello from Docker!
Hello from Docker!
Hello from Docker!
Hello from Docker!
Hello from Docker!
Hello from Docker!
Hello from Docker!
Hello from Docker!
Hello from Docker!
Hello from Docker!
Hello from Docker!
Quando tudo estiver concluído, interrompa o contêiner para que ele não seja reiniciado para sempre:
$ docker stop 0f6
0f6
Sua correção
Para resolver o problema, basta executar o contêiner do HBase Docker da seguinte maneira:
$ docker run -d --hostname hbase-db --name hbase --restart always \
-p 2181:2181 -p 8080:8080 -p 8085:8085 -p 9090:9090 -p 9095:9095 \
-p 16000:16000 -p 16010:16010 -p 16201:16201 -p 16301:16301 \
harisekhon/hbase