You don’t want to get caught with your pants down when your server reboots, so how do you ensure that your Docker containers start automatically? There are two ways to go about this; the first way is to ensure your Docker image is built with restart policies, while the second way allows you to manage Docker containers’ startup order in more detail. Let’s take a look at both of these methods.
If you’re using Docker containers in your development or production workflow, it’s important to ensure that they automatically start up when your server reboots. Otherwise, you’ll have to manually restart each container every time the server comes back online. Fortunately, there are some tools out there to help automate this process. The two most popular are keepalived and ha-dns.
Keepalived is more of a program for monitoring the health of individual services on your system (such as the network connection). Ha-dns is more focused on DNS service discovery rather than other services. The way it works is by running an agent on each node and a daemon with in-built DNS server on one node which creates host records for all nodes running agents, hence providing full cluster availability for applications even if all nodes were offline at once.
Reboot the Server
- Log into the server as the root user.
- Type in the following command to restart the server: shutdown -r now
- Once the server has rebooted, log back in as the root user.
- Check that Docker is running by typing in the following command: docker ps -a
- If Docker is not running, type in the following command to start it: dockerd
- Now that Docker is running, you can type in the following command to start your container: docker run
- Your container should now be up and running! The commands for stopping and removing a container are listed below:
- To stop a running container, type in the following command: docker stop
- To remove a stopped or running container, type in the following command: docker rm 10. When finished with your container, use one of these commands to remove it from the list of active containers: docker rm
- Note that when you remove a container, all data inside will be deleted. Be sure to save any important files before doing so!
Disable Runtime Reconfiguration
If you want to make sure your Docker containers always start up when your server reboots, you’ll need to disable runtime reconfiguration. With runtime reconfiguration disabled, your container will only start up if it’s manually started by a user or process. To disable runtime reconfiguration, edit the file /etc/default/docker and set the line DOCKER_OPTS=–disable-runtime-reconfiguration .
Once you’ve made this change, restart the Docker daemon for the changes to take effect. Next, add the following to your system’s startup routine: service docker stop && service docker start && update-rc.d docker defaults (add any
other startup routines that might be necessary). Now anytime you reboot your server, all of your Docker containers should come back online without any additional work on your part.
It’s worth noting that this does not prevent people from stopping and starting containers outside of Docker with sudo docker stop $(sudo docker ps -aq) && sudo docker start $(sudo docker ps -aq) so you’ll still have to monitor your processes closely in order to keep things running smoothly. This can also cause problems with continuous integration setups since servers won’t necessarily be coming back up every time an integration test runs. However, if you don’t want to worry about monitoring the services themselves or breaking your CI setup, runtime reconfiguration is the best option available.
Bootstrap Your Containers
If you want to ensure that your containers always start up when your server reboots, you’ll need to use a bootstrapping tool like systemd, upstart, or supervisor. Bootstrapping tools monitor the system’s init system and then start the process that they’re configured to watch over.
For example, if you’re using systemd and you want it to manage your container processes (that is, make sure they’re started after a reboot), then configure the system-wide Unit file as follows WantedBy=multi-user.target Note: The –name parameter in the command should be replaced with whatever name you gave your container in its docker run command in Step 3. Now, let’s go ahead and create the next one by running docker run -p 8000:8000 openjdk . If this doesn’t work for some reason, try typing docker ps instead of docker run . It will list all currently running containers on your machine. You can also type sudo systemctl start servicename where servicename is the name of the service from step 5 to get it going manually.
When finished creating your new containers, you’ll see that there are now two of them listed under CONTAINERS IN RUNNING STATE on the main page of DOCKER HOSTS AND CONTAINERS. And these two containers will now automatically restart every time you reboot this machine!