How to start docker container automatically after server rebooted?

I add RestartPolicy: always in to the container and I expect container start automatically in all cases.

Case 1:
Container down or docker service restarted, container will restart automatically EXPECTED

Case 2:
Server reboot, container will not start NOT EXPECTED

Are there to way start container automatically after server reboot ?

Restart policy should work unless there is an other issue while the system is booting.
You could also conigure the containers as systemd services or use Kubernetes or Docker Swarm to make sure the containers are in the required state.

Newtratrip, yours is not an uncommon question. To be clear, the the Docker docs on restartpolicy indicate how that setting “controls whether your containers start automatically when they exit, or when Docker restarts” (emphasis mine). So that last point is about how DOCKER ensures a container runs on Docker restart.

Your question is instead about how your OS ensures Docker runs on a server restart, and that will depend on various things
starting with what OS you’re using (Linux, Windows, macOS, etc.), as each has its own way to control the startup of a process like Docker on reboot.

It will depend also on how you installed (and run) Docker itself.

For instance, if you may be on Windows or Mac and are using Docker Desktop, that offers a setting to “start Docker Desktop when you log in” (as shown in the docs for mac or win), which might suit your needs. I’m pretty sure it’s disable by default.

If you may still have issues after reading the above, please let us know what OS you’re using and how you installed and run Docker.

2 Likes

My OS is Ubuntu V.14.04, I installed docker following this document Install Docker Engine on Ubuntu | Docker Docs

Well, there’s this in that document:

“The docker service starts automatically on Debian based distributions. On RPM based distributions, such as CentOS, Fedora, RHEL or SLES, you need to start it manually using the appropriate systemctl or service command. As the message indicates, non-root users cannot run Docker commands by default.”

Perhaps it would be better if that said, “should start automatically”, as it doesn’t start for you. As for why it does not, we need to know first what mechanism on your Ubuntu 14 manages such auto-start apps, which are generically known as" services".

It would be useful for you to see this blog post which explains the 3 ways that might work (system d, upstart, and system v), as has varied over different distros and even different Ubuntu versions: https://linoxide.com/enable-disable-services-ubuntu-systemd-upstart/

Try these:
service docker status
systemctl status docker
initctl status docker

Once we know which it is your system is using, we can then focus on why it’s not starting on reboot.

1 Like

@newtratip, how did things sort out?

I need to start docker containers when starting windows 10 pro without user logon.
Previously I was able to start “C:\Program Files\Docker\Docker\Docker Desktop.exe” with the Windows task scheduler application
Now after a new installation of windows it is not working.
Not even trying to start a powershell script that launches service and application.
Something starts and I notice it from the task manager but not the containers.
Thanks in advance for the help