Setting up an EC2 instance to run a Docker container
To run a Docker container on an EC2 instance, we first have to set up an instance with a Docker installation and add an ec2-user
to the Docker group so that we can execute Docker commands with an ec2-user
rather than as a root
user, which we will be covering in this recipe.
How to do it…
- Switch to the
root
user from theec2-user
user by executing the following command:
[ec2-user@ip-172-31-34-99 ~]$ sudo su [root@ip-172-31-34-99 ec2-user]#
- Install
Docker
and update an EC2 instance by executing the following commands:
[root@ip-172-31-34-99 ec2-user] yum install -y docker [root@ip-172-31-34-99 ec2-user] yum update -y
- Start
Docker
as a service on an EC2 instance by executing the following command:
[root@ip-172-31-34-99 ec2-user] service docker start
- Add
ec2-user
to thedocker
group so that you can execute Docker commands without usingsudo
, as follows:
[root@ip-172-31-34-99 ec2-user] usermod -a -G docker ec2-user
- Log out of the EC2 instance by executing...