skip to Main Content

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


  1. 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.

    Login or Signup to reply.
  2. 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

    docker run -d .

    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

    /etc/systemd/system/my-container.service.

    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

    [Unit]
    Description=My Docker Container
    Requires=docker.service
    After=docker.service
    
    [Service]
    Restart=always
    ExecStart=/usr/bin/docker run --name my-container -p 8080:8080 my-image
    
    [Install]
    WantedBy=multi-user.target
    

    Once you have created the service file, start the service with the command

    systemctl start my-container.service

    (or equivalent command for your process manager).

    Check that the service is running with the command

    systemctl status my-container.service.

    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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search