AWS Docker Activity
Before getting started, look in the docker-compose.yml and make sure that the backend container includes and environment variable that sets the PORT:
backend:
depends_on:
- mysqldb
networks:
- project-x-network
build:
dockerfile: ./Dockerfile
context: .
restart: unless-stopped
ports:
- 8888:8888
environment:
- DB_HOST=mysqldb
- DB_USER=root
- DB_PASSWORD=
- DB_NAME=project_x
- DB_PORT=3306
- PORT=8888 <<<<<<<<<<<<<<< MAKE SURE THIS IS HERE (AND INDENTED PROPERLY)
Log into the AWS console and make sure you are in the us-east-1 region (N.Virginia)
Create a security group:
- Go to the EC2 dashboard
- Click Security Groups in the left nav
- Click Create Security Group
- Name it docker-sg
- For the Description, enter Docker server secruity group
- Add the following inbound rules:
- Allow SSH traffic from Anywhere (set the Source dropdown to Anywhere-IPv4)
- Allow HTTP traffic from Anywhere
- Allow HTTPS traffic from Anywhere
- Allow Custom TCP traffic, set the Port Range to 8888 from Anywhere (this is the port that exposes the node container)
- Add the following outbound rule
- Allow All Traffic and set the Destination dropdown to Anywhere-IPv4
- Click Create Security Group
Launch an Ubuntu EC2 instance:
- Click on Instances in the left nav
- Name it docker-server
- Choose Ubuntu 24.04
- Create a new key pair (name id docker-key)
- Click Select an existing security group and then select the docker-sg that we created in the previous step
- Click Launch Instance
Note the public IP address of the instance, you'll need it in the next step.
Install Docker and Docker Compose
- Put the docker-key file in the project folder (you should ignore it) Open a GitBash terminal in the project folder and use the docker-key to log into the instance by entering this command (use your instance's public IP):
ssh -i docker-key.pem ubuntu@YOUR-PUBLIC-IP-ADDRESS-GOES-HERE
- Enter yes when prompt to trus the key
- Run the following commands to install Docker:
# Update your server
sudo apt update && sudo apt upgrade -y
# Install Docker
sudo apt install docker.io -y
# Start Docker
sudo systemctl start docker
# Enable it to start automatically
sudo systemctl enable docker
# Allow your user run Docker without typing 'sudo' every time
sudo usermod -aG docker $USER
# Check the version to confirm the install
docker --version
# Download 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
# Make it executable
sudo chmod +x /usr/local/bin/docker-compose
# Check the version to confirm the install
docker-compose --version
Install the Nginx Weberver
In another activity, we may set up Nginx as a reverse proxy so that you can run multiple docker compose apps on the same server.
# install it:
sudo apt install nginx -y
# check the version
nginx -v
# make sure it's running
systemctl status nginx
# You can press 'q' to return to the terminal
You should now be able to open a browser tab and enter the public IP of your instance to see the Nginx welcome page, but make sure you are not using HTTPS. Uset HTTP because we have not installed an SSL certificate on the server and it does not support HTTPS.
Copy your project to the instance
While still logged into the EC2 instance, run this command to create a folder for the api project file:
mkdir web4-api
Now enter exit to log out of the EC2 instance
Run this command to copy the project files folder to the EC2 instance (use the public IP of the instance):
scp -i docker-key.pem -r ./* ubuntu@YOUR-PUBLIC-IP-ADDRESS-GOES-HERE:~/web4-api/
Run the containers
Log back into the EC2 instance and verify that the project files are there:
ssh -i docker-key.pem ubuntu@YOUR-PUBLIC-IP-ADDRESS-GOES-HERE
cd web4-api
ls
Run this command to start the containers:
docker-compose up -d
Now you should be able to visit the api in the browser by entering your instances public IP and setting the port to 8888 (remember that we configured the security group to allow inbound TCP traffic on this port from any source)
Here are some docker commands you could try out while you're logged into your instance:
# shows running container info, include each container's id
docker ps
# log into a container
docker exec -it CONTAINERID sh
# view the logs for a container
docker logs CONTAINERID
# stop the containers
docker-compose stop
# remove the containers
docker-compose down
# view the images (include the image ID)
docker images
# remove an image (all containers made from the image must be removed first)
docker rmi IMAGEID