Contents

How to Install Docker in AWS EC2 Instances

Contents

Once you have created an AWS EC2 instance, you probably want to deploy your app on it using Docker. So, you might need to install Docker in the EC2 instance first.

  1. Firstly, you need to connect to your instance via SSH. Once connected, you will see the command line in your terminal: https://raw.githubusercontent.com/williamyuaus/blog-img/master/20210726100843.png

  2. Before installing Docker, you may need to update the installed packages and package cache on your instance. sudo yum update -y https://raw.githubusercontent.com/williamyuaus/blog-img/master/20210726101139.png

  3. If your instance is Amazon Linux 2, using the command to install the most recent Docker Engine package. sudo amazon-linux-extras install docker https://raw.githubusercontent.com/williamyuaus/blog-img/master/20210726101429.png

    You will see the info about the installation successfully. https://raw.githubusercontent.com/williamyuaus/blog-img/master/20210726101550.png

  4. Now, you can start the Docker service. sudo service docker start

  5. It is recommended to add the ec2-user to the docker group, in order to execute Docker commands without using sudo. sudo usermod -a -G docker ec2-user

  6. However, if you want to verify the Docker commands now, you may get this error.https://raw.githubusercontent.com/williamyuaus/blog-img/master/20210726102122.png

  7. To solve this issue, it is required to log out and log back in again, so new docker group permissions can be picked up. To achieve this, just close your current SSH terminal window and reconnect to your instance. The new session can have the permissions. https://raw.githubusercontent.com/williamyuaus/blog-img/master/20210726102505.png

Now, you successfully install docker in your AWS EC2 instances, you can play with Docker now.