DEV Community

Rodrigo Burgos
Rodrigo Burgos

Posted on

Short: User Data file for Ubuntu based AWS ec2 instance with docker and docker compose.

#!/bin/bash

# Update the system
sudo apt-get update -y
sudo apt-get upgrade -y

# Install Git
sudo apt-get install -y git

# Install Docker
sudo apt-get install -y apt-transport-https ca-certificates curl software-properties-common
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
sudo apt-get update -y
sudo apt-get install -y docker-ce docker-ce-cli containerd.io

# Add the 'ubuntu' and 'ssm-user' to the Docker group
sudo usermod -aG docker ubuntu
sudo usermod -aG docker ssm-user
id ubuntu ssm-user
sudo newgrp docker

# Enable and start Docker
sudo systemctl enable docker.service
sudo systemctl start docker.service

# Install Docker Compose v2
sudo mkdir -p /usr/local/lib/docker/cli-plugins
sudo curl -SL https://github.com/docker/compose/releases/download/v2.23.3/docker-compose-linux-x86_64 -o /usr/local/lib/docker/cli-plugins/docker-compose
sudo chmod +x /usr/local/lib/docker/cli-plugins/docker-compose

# Add swap space
sudo dd if=/dev/zero of=/swapfile bs=128M count=32
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile
echo "/swapfile swap swap defaults 0 0" | sudo tee -a /etc/fstab
Enter fullscreen mode Exit fullscreen mode

Gist link:

https://gist.github.com/burgossrodrigo/790406f04f6bd38d4baee0364fce2f04

Top comments (0)