Start Docker-Compose Automatically on Ec2 Startup

Start docker-compose automatically on EC2 startup

I would recommend using cron for this as it is easy. Most of the corn supports non-standard instructions like @daily, @weekly, @monthly, @reboot.

You can put this either in a shell script and schedule that in crontab as @reboot /path/to/shell/script

or

you can specify the docker-compose file using the absolute path and directly schedule it in crontab as @reboot docker-compose -f /path/to/docker-compose.yml start

Other possibilities:

  1. Create a systemd service and enable it. All the enabled systems services will be started on powering.(difficulty: medium)
  2. Put scripts under init.d and link it to rc*.d directory. These scripts are also started based on the priority.(difficulty: medium)

Bonus:

If you specify restart policy in the docker-compose file for a container it will autostart if you reboot or switch on the server. Reference

Launch docker automatically when starting ec2 server

Using Amazon Linux 2 I tried to replicate the issue. Obviously, I don't have all the dependencies to run your exact docker-compose.yml, thus I used the docker-compose.yml from here for my verification. The file setups wordpress with mysql .

Steps I took were following (executed as ec2-user in home folder):

1. Install docker

sudo yum update -y  
sudo yum install -y docker
sudo systemctl enable docker
sudo systemctl start docker

2. Install docker-compose

sudo curl -L https://github.com/docker/compose/releases/latest/download/docker-compose-$(uname -s)-$(uname -m) -o /usr/bin/docker-compose

sudo chmod +x /usr/bin/docker-compose

3. Create docker-compose.yml

mkdir myapp 

Create file ./myapp/docker-compose.yml:

version: '3.3'

services:
db:
image: mysql:5.7
volumes:
- db_data:/var/lib/mysql
restart: always
environment:
MYSQL_ROOT_PASSWORD: somewordpress
MYSQL_DATABASE: wordpress
MYSQL_USER: wordpress
MYSQL_PASSWORD: wordpress

wordpress:
depends_on:
- db
image: wordpress:latest
ports:
- "8000:80"
restart: always
environment:
WORDPRESS_DB_HOST: db:3306
WORDPRESS_DB_USER: wordpress
WORDPRESS_DB_PASSWORD: wordpress
WORDPRESS_DB_NAME: wordpress
volumes:
db_data: {}

4. Create docker_boot.service

The file is different then yours, as there were few potential issues in your file:

  • not using absolute paths
  • ec2-user may have no permissions to run docker

Create file ./myapp/docker_boot.service:

[Unit]
Description=docker boot
After=docker.service

[Service]
Type=oneshot
RemainAfterExit=yes
WorkingDirectory=/home/ec2-user/myapp
ExecStart=/usr/bin/docker-compose -f /home/ec2-user/myapp/docker-compose.yml up -d --remove-orphans

[Install]
WantedBy=multi-user.target

5. Copy docker_boot.service to systemd

sudo cp -v ./myapp/docker_boot.service /etc/systemd/system

6. Enable and start docker_boot.service

sudo systemctl enable docker_boot.service
sudo systemctl start docker_boot.service

Note: First start may take some time, as it will pull all docker images required. Alternatively start docker-compose manually first to avoid this.

7. Check status of the docker_boot.service

sudo systemctl status docker_boot.service

8. Check if the wordpress is up

curl -L localhost:8000

9. Reboot

Check if the docker_boot.service is running after instance reboot by logging in into the instance and using sudo systemctl status docker_boot.service and/or curl -L localhost:8000.

how can AWS EC2 can write 'docker-compose up' automatically Repeatedly without my control?

You can use ECS which is available in two favors

Serverless (Fargate) where you don't have to manage anything except your docker compose file and second is to use EC2 as launch type for ECS.

Docker and AWS have been collaborating and now there is an in built support for docker compose files since . Please refer to official docs
https://aws.amazon.com/blogs/containers/deploy-applications-on-amazon-ecs-using-docker-compose/
https://www.docker.com/blog/docker-compose-for-amazon-ecs-now-available/

How to launch my docker container automatically on ECS?

I finally found the solution !

On ECS you have to add a entrypoint and not command.

So if you want to launch your docker container when the EC2 is ready, you can use this sample :

{
"family": "familyName",
"containerDefinitions": [
{
"name": "testSample",
"image": "usernameDocker/containerName",
"cpu": 1,
"memory": 500,
"entryPoint": [
"/usr/sbin/apache2ctl",
"-D",
"FOREGROUND"
],
"environment": [],
"command": [],
"portMappings": [
{
"hostPort": 80,
"containerPort": 80,
"protocol": "tcp"
},
{
"hostPort": 2222,
"containerPort": 22,
"protocol": "tcp"
}
],
"volumesFrom": [],
"links": [],
"mountPoints": [],
"essential": true
}
],
"volumes": []
}

Regards,

How properly deploy Docker Compose to Amason EC2 instance?

How you want to design your infrastructure - how many servers, autoscaling, routing, etc - is entirely up to you.

To answer your question though; to deploy a docker-compose.yml file you do so the same as you would on any other server.

docker-compose is a tool made for development and is not made to be used in Production - you should deploy your services with:

docker stack deploy -c your-compose-file.yml your_stack_name

It sounds like this is new to you though; so I should state that there are various options that are available with docker-compose that are not available with docker stack deploy. Often times you can't just use the same compose file as you're using in development.



Related Topics



Leave a reply



Submit