- Newest
- Most votes
- Most comments
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>
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:
-
Install Docker: Your user data script should first install Docker if it's not already present on the instance.
-
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.
-
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:
- Installs Docker (if not already installed)
- Creates a systemd service file for your Label Studio container
- Creates the data directory
- 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
Relevant content
asked 3 years ago

I works like a charm! Thank you very much