We're going to start by creating a script that outputs, "Hello, World!" and then, kills itself after three seconds. We will create a Docker file which simply adds this script to our image, and executes it. Let's build this into an image named, Hello, World!
Then, run this with a standard Docker run command with a name of Test One. If we check the status with docker ps and filter to show only our Test One image, we can see that the container is exited and is no longer running. Let's try starting another container, this time with the -- restart flag and pass in the value of always.
This will restart the container whenever it exits to ensure it's always running. Let's name this container, Test Two. If we go ahead and inspect it now, we will see that the container is still running. Another way to ensure high availability with your containers is to use docker-compose.
Create a docker-compose YAML file, and we will define a simple configuration for our Hello, World! App. Make sure to add a restart flag with the value of Always to your configuration. Next, we can easily scale this app with docker-compose up followed by the scale flag. Let's tell compose to start Hello, World! With three instances.
We will see that there are still three containers available and always restarting.