By default, when my Docker host restarts, my containers are not started …

Currently, we have 3 containers :

[root@docker ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
9343acf270ed        ubuntu              "/bin/bash"         21 hours ago        Up 2 hours                              Ubuntu
ebae479717ac        centos              "/bin/bash"         22 hours ago        Up 2 hours                              CentOS
3182473dc136        fedora              "/bin/bash"         22 hours ago        Up 2 hours                              fedora

But none of them starts after system reboot :

[root@docker ~]# docker inspect --format '{{ .HostConfig.RestartPolicy.Name }}' $(docker ps -a --format "{{.Names}}")

You forgot to set –restart=”always” option first time you started the container !

In this case, you have to edit the JSON configuration file “/var/lib/docker/containers/ID*/hostconfig.json” :

For our 3 container :

[root@docker ~]# docker ps -a --format "{{.ID}}: {{.Names}}"
9343acf270ed: Ubuntu
ebae479717ac: CentOS
3182473dc136: fedora

We have those 3 files :

[root@docker ~]# eval ls -l /var/lib/docker/containers/{$(docker ps -a --format "{{.ID}}" | xargs | sed -e "s/ /*,/g;s/$/*/")}/hostconfig.json
-rw-r--r--. 1 root root 651 Jan 16 14:17 /var/lib/docker/containers/3182473dc136356415957ea0c4cb59bbfe990cb70454f4f54c9cd7e581edd6e1/hostconfig.json
-rw-r--r--. 1 root root 651 Jan 16 12:16 /var/lib/docker/containers/9343acf270ed4fee981647f03e73012c1570d69b7b16257e55c776df976f42f4/hostconfig.json
-rw-r--r--. 1 root root 651 Jan 16 12:16 /var/lib/docker/containers/ebae479717acfcbfa71e80224aac6bd8b41cf1e5a10e7e7dc0eba4879bdc7961/hostconfig.json

Each file is in minimized JSON format that is not easy to edit. So, use your “vim” prefered editor :

:%!python -m json.tool

And within “vim”, launch “%!python -m json.tool” command, this this awesome, your JSON file is now easy to edit :

    "RestartPolicy": {
        "MaximumRetryCount": 0,
        "Name": "always"

Set “Name field to “always”, save your file and it’s done !

Reboot your server, and your containers are from now up & running :

[root@docker ~]# docker ps -a --no-trunc --format "{{.ID}}: {{.Names}}: {{.Status}}"
9343acf270ed4fee981647f03e73012c1570d69b7b16257e55c776df976f42f4: Ubuntu: Up 5 minutes
ebae479717acfcbfa71e80224aac6bd8b41cf1e5a10e7e7dc0eba4879bdc7961: CentOS: Up 5 minutes
3182473dc136356415957ea0c4cb59bbfe990cb70454f4f54c9cd7e581edd6e1: fedora: Up 5 minutes    Send article as PDF   

Leave a Reply

Your email address will not be published. Required fields are marked *