DEV Community

Romero Dias
Romero Dias

Posted on

Install Docker and Docker Compose on an AWS EC2 instance using User Data

To install Docker and Docker Compose on an AWS EC2 instance using User Data, you'll need a script that automatically updates the system, installs Docker, starts the Docker service, adds the user to the Docker group, installs Docker Compose, and enables the Docker service to start on boot.
Here's a User Data script for Amazon Linux 2 and Amazon Linux 2023:

#!/bin/bash
# Update the system
sudo yum update -y

# Install Docker
# sudo amazon-linux-extras install docker -y  # For Amazon Linux 2
sudo yum install -y docker                  # For Amazon Linux 2023

# Start the Docker service
sudo service docker start

# Add the ec2-user to the docker group
sudo usermod -a -G docker ec2-user

# Install Docker Compose
sudo curl -L "https://github.com/docker/compose/releases/latest/download/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose

# Enable Docker to start on boot
sudo systemctl enable docker

# Verify Docker Compose installation
docker-compose version

Enter fullscreen mode Exit fullscreen mode

Top comments (1)

Some comments may only be visible to logged-in visitors. Sign in to view all comments.