Installing Docker on Amazon Linux EC2 Instance
Welcome back! Now that we can connect to our server using SSH, the next step is to install Docker so we can run Docker containers on our Amazon Linux instance. Let’s walk through the process step by step.
Step 1: Updating Existing Packages
Amazon Linux provides a package manager called Yum. It’s similar to npm
for Node.js and allows us to install and update applications and packages.
First, we need to update the existing packages installed on our EC2 instance to ensure that we’re using the latest versions with all the latest security updates. To do this, run:
sudo yum update -y
sudo
: Runs the command as an administrative user with full privileges.yum update
: Updates all installed packages.-y
: Automatically confirms the update process.
Step 2: Installing Docker
Now that the packages are updated, we’ll install Docker using the Yum install command. Run:
sudo yum install docker -y
This installs the Docker package on your Amazon Linux EC2 instance.
During the process, Yum will show the size of the package and confirm installation. The
-y
flag automatically confirms it.
Step 3: Starting the Docker Service
Docker runs as a background service. To start the Docker service, use:
sudo service docker start
This command starts the Docker service on your EC2 instance, enabling you to run Docker commands.
Step 4: Running Docker Commands
You can now try running a Docker command to verify the installation. For example:
sudo docker info
If you don’t use sudo
, you’ll see a permission denied error. This is because Docker commands require administrative privileges by default.
Step 5: Enabling Docker Commands Without “Sudo”
To make it easier to use Docker, we’ll add the ec2-user
to the Docker group. This allows us to run Docker commands without using sudo
every time.
Run the following command:
sudo usermod -aG docker ec2-user
usermod
: Modifies the user account.-aG docker
: Adds the user to the Docker group.ec2-user
: The default user for EC2 instances.
Step 6: Logging Out and Back In
For the changes to take effect, log out of the EC2 instance and then log back in using SSH. You can log out by typing:
exit
After reconnecting to the EC2 instance, verify that you can run Docker commands without sudo
. For example:
docker info
You should now see Docker information without any errors.
Summary
You’ve successfully installed Docker on your Amazon Linux EC2 instance! Here’s what we accomplished:
Updated existing packages using
yum update
.Installed Docker using
yum install
.Started the Docker service.
Enabled non-administrative users (like
ec2-user
) to run Docker commands withoutsudo
.
We’re now ready to deploy and run our Docker containers. Let’s move on to the next step of deploying our project!