¿Cómo puedo hacer que un contenedor de Docker se inicie automáticamente en el arranque del sistema?


Se supone que tengo un contenedor Docker que quiero ejecutar, entonces puedo llamar a

$ docker run ...

Y todo está bien. ¿Hay una forma integrada de ejecutar un contenedor de manera que se reinicie automáticamente, si el sistema se bloquea y se reinicia?

Si es así, ¿también está disponible en Docker Compose?

Author: Golo Roden, 2015-05-26

4 answers

Sí, docker tiene políticas de reinicio como docker run --restart=always que manejarán esto. Esto también está disponible en la composición .yml config file como restart: always.

 36
Author: Peter Lyons,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-02-17 06:45:15

Si desea que el contenedor se inicie incluso si ningún usuario ha realizado un inicio de sesión (como la VM VirtualBox que solo inicio y no quiero iniciar sesión cada vez). Estos son los pasos que realicé para Ubuntu 16.04 LTS. Como ejemplo, instalé un contenedor de oracle db:

$ docker pull alexeiled/docker-oracle-xe-11g
$ docker run -d --name=MYPROJECT_oracle_db --shm-size=2g -p 1521:1521 -p 8080:8080 alexeiled/docker-oracle-xe-11g
$ vim /etc/systemd/system/docker-MYPROJECT-oracle_db.service

Y añádase el siguiente contenido:

[Unit]
Description=Redis container
Requires=docker.service
After=docker.service

[Service]
Restart=always
ExecStart=/usr/bin/docker start -a MYPROJECT_oracle_db
ExecStop=/usr/bin/docker stop -t 2 MYPROJECT_oracle_db

[Install]
WantedBy=default.target

Y habilitar el servicio al inicio

sudo systemctl enable docker-MYPROJECT-oracle_db.service

Para más información https://docs.docker.com/engine/admin/host_integration /

 39
Author: kon,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-05-24 07:33:26

La política de reinicio predeterminada es no.

Para los contenedores creados use docker update para actualizar la directiva de reinicio.

docker update --restart=always 0576df221c0b
 2
Author: Edward Young,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-07-27 01:25:07

Más modo "suave"de la documentación:

docker run -dit --restart unless-stopped <image_name>
 0
Author: Rib47,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-07-13 12:01:54