I wrote an API, dockerized it and ran on an AWS EC2 instance. I can make requests through Postman while container is running but how can I make sure that my docker container is always running in the background on AWS EC2 instance?
I don’t know whether container is already running always in background or not.
2
Answers
You can decouple your container instance(s) with an Application Load Balancer and set the health check.
But, if you want to be sure that your container is always in an healthy state, you should use ECS in EC2 mode, coupled with the Load Balancer, and let him manage the container status.
To ensure that your docker container is always running in the background on an AWS EC2 instance, you can use a process manager like systemd or supervise to automatically start and monitor your container.
Here are the steps you can follow:
SSH into your EC2 instance.
Check if your container is already running in the background. You can do this by running the command docker ps. If you see your container listed, it means it is already running.
If your container is not running, start it with the command
Install a process manager like systemd or supervise on your EC2 instance. This will help you start and monitor your container automatically.
Create a service file for your docker container using your preferred process manager. For example, if you are using systemd, create a service file
In the service file, specify the command to start your docker container and any necessary options. Here is an example service file for a docker container:
makefile
Once you have created the service file, start the service with the command
(or equivalent command for your process manager).
Check that the service is running with the command
Test your docker container by making requests to it through Postman or any other client.
With these steps, your docker container should be running in the background on your AWS EC2 instance and will automatically restart if it crashes or if the EC2 instance is restarted.