Skip to content

Run Docker container at each restart of my EC2 instance with user-data

0

Hi all,

I am trying to deploy a data annotation tool called label-studio on an EC2 instance via Docker. I managed to do that manually from the instance console, first installing Docker on the instance and then pulling the image and running the container. The tool serves on localhost as a web app.

Now, I would like to automatically stop and start the instance to avoid costs during non-working hours (the tool won't be used). As a consequence, I would need docker to run the container each time the instance is re-started.

I tried passing the following script to my instance's user data, following several official and non-official tutorials I saw online, to no avail.

I tried replacing my docker command by the command from this Knowledge Center article that just creates a text file at instance start and it works fine. On EC2 Instance Connect I just need to run docker run -it -p 80:8080 -v pwd/mydata:/label-studio/data heartexlabs/label-studio:latest, no need to docker start or anything else.

Are any steps missing in my script? Is it a path problem? Can you please guide me to the correct solution?

Content-Type: multipart/mixed; boundary="//"
MIME-Version: 1.0

--//
Content-Type: text/cloud-config; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment; filename="cloud-config.txt"

#cloud-config
cloud_final_modules:
- [scripts-user, always]

--//
Content-Type: text/x-shellscript; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment; filename="userdata.txt"

#!/bin/bash
docker run -it -p 80:8080 -v `pwd`/mydata:/label-studio/data heartexlabs/label-studio:latest
--//--


asked 2 years ago1.8K views

2 Answers
1

I assume you have configure docker daemon to run on OS boot, i.e.

sudo systemctl enable docker

Besides using systemd as suggested, you can explore Docker restart policies

So instead of

docker run -it -p 80:8080 -v `pwd`/mydata:/label-studio/data heartexlabs/label-studio:latest

This may be

docker run -d --restart=always -p 80:8080 -v `pwd`/mydata:/label-studio/data heartexlabs/label-studio:latest

If this is not what you need, update policy to no, i.e.

docker ps
docker update --restart=no <container id>
AWS
EXPERT

answered 2 years ago

EXPERT

reviewed 2 years ago

0
Accepted Answer

To automatically run a Docker container each time your EC2 instance restarts, you'll need to modify your approach slightly. While your current user data script is a good start, there are a few adjustments needed to ensure the Docker container runs reliably on each restart.

Here's what you need to do:

  1. Install Docker: Your user data script should first install Docker if it's not already present on the instance.

  2. Create a systemd service: Instead of directly running the Docker container in the user data script, create a systemd service that will manage the container. This ensures the container starts automatically on each system boot.

  3. Enable and start the service: After creating the systemd service, enable and start it.

Here's an improved version of your user data script:

#!/bin/bash
# Install Docker
amazon-linux-extras install docker
systemctl enable docker
systemctl start docker

# Create a systemd service file for your Docker container
cat << EOF > /etc/systemd/system/label-studio.service
[Unit]
Description=Label Studio Docker Container
Requires=docker.service
After=docker.service

[Service]
Restart=always
ExecStart=/usr/bin/docker run --rm -p 80:8080 -v /home/ec2-user/mydata:/label-studio/data heartexlabs/label-studio:latest
ExecStop=/usr/bin/docker stop label-studio

[Install]
WantedBy=multi-user.target
EOF

# Create the data directory
mkdir -p /home/ec2-user/mydata

# Reload systemd, enable and start the service
systemctl daemon-reload
systemctl enable label-studio.service
systemctl start label-studio.service

This script does the following:

  1. Installs Docker (if not already installed)
  2. Creates a systemd service file for your Label Studio container
  3. Creates the data directory
  4. Enables and starts the Label Studio service

With this setup, the Docker container will automatically start when the EC2 instance boots up, and it will restart if it crashes for any reason.

Make sure to wrap this script in the appropriate MIME headers as you had in your original user data, and it should work as expected.

Remember to adjust the volume mount path (/home/ec2-user/mydata) if needed to match your desired configuration. Also, you may need to modify the script if you're using an operating system other than Amazon Linux 2, as the Docker installation commands might differ.

This approach should solve your issue of running the Docker container automatically each time your EC2 instance restarts.
Sources
Run commands when you launch an EC2 instance with user data input - Amazon Elastic Compute Cloud
Community | Bootstrapping an Amazon EC2 Instance Using User-data to Run a Python Web App
Running a script when you launch an Amazon ECS Linux container instance - Amazon Elastic Container Service

answered 2 years ago

EXPERT

reviewed 2 years ago

  • I works like a charm! Thank you very much

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.