AWS — Install Docker & Run Containers on EC2
1 min readAug 9, 2021
I won’t explain how to create a new EC2 instance.
Install Docker
// update
sudo yum update -y
// install most recent package
sudo amazon-linux-extras install docker
// start the service docker
sudo service docker start
// add the ec2-docker user to the group
sudo usermod -a -G docker ec2-user
// you need to logout to take affect
logout
// login again
ssh -i "****.pem" ec2-user@ec2-******.us-east-2.compute.amazonaws.com
// check the docker version
docker --version
Run Docker Container on EC2
// pull the image
docker pull bbachin1/node-api
// list the images
docker images ls
// run the conatiner
docker run -d -p 80:3000 --name nodeapi bbachin1/node-api
// if you want the container always restart, then use this
// docker run -d --restart always -p 80:3000 --name nodeapi bbachin1/node-api
// check the running container
docker container ls
// exec into docker container
docker exec -it nodeapi /bin/sh
If you want to control Docker with systemd, please refer to https://docs.docker.com/config/daemon/systemd/