Table of contents
- Installing Docker on AWS EC2
- Step 1: Connect to your EC2 Instance
- Step 2: Update the Package Index
- Step 3: Install Docker
- Start the Docker service and enable it to start on boot:
- Step 4: Test Docker Installation
- Run a simple "Hello World" container to verify Docker is working:
- Basic Docker Commands
- 1. Pulling Docker Images
- 2. Listing Docker Images
- 3. Running a Docker Container
- 4. Listing Running Containers
- 5. Stopping a Container
- 6. Removing a Container
- 7. Executing Commands in a Container
- 8. Removing an Image
Docker Installation and Basic Commands on AWS EC2 Instance
Docker has revolutionized the way applications are deployed and managed by providing a lightweight and efficient solution for containerization. In this blog, we'll guide you through installing Docker on an AWS EC2 instance and cover some basic commands for managing Docker images and containers.
Installing Docker on AWS EC2
Follow these steps to install Docker on your AWS EC2 instance:
Step 1: Connect to your EC2 Instance
Open your terminal.
Use the following command to connect to your EC2 instssh -i /path/to/your/key.pem ec2-user@your-instance-ip
Step 2: Update the Package Index
Run the following command to update the package index on your EC2 instant
sudo yum update -y
Step 3: Install Docker
Install the required packages to set up the Docker repository:
sudo yum install -y docker
Start the Docker service and enable it to start on boot:
sudo systemctl start docker sudo systemctl enable docker
Step 4: Test Docker Installation
Check the Docker version to ensure it's installed correctly:
docker --version
Run a simple "Hello World" container to verify Docker is working:
docker run hello-world
Basic Docker Commands
Now that Docker is installed, let's explore some basic Docker commands to work with images and containers:
1. Pulling Docker Images
To pull a Docker image from Docker Hub, use the following command:
docker pull image-name:tag
Replace image-name
with the name of the image and tag
with the desired version tag.
2. Listing Docker Images
To list all the Docker images on your system, execute:
docker images
3. Running a Docker Container
To run a Docker container from an image, use the following command:
docker run -d --name container-name image-name:tag
Replace container-name
with your desired container name and provide the appropriate image-name:tag
.
4. Listing Running Containers
To see the list of running containers, use:
docker ps
5. Stopping a Container
To stop a running container, run:
docker stop container-name/container-id
6. Removing a Container
To remove a stopped container, use:
docker rm container-name/container-id
7. Executing Commands in a Container
You can execute commands inside a running container:
edocker exec -it container-name/container-id command
8. Removing an Image
To remove a Docker image, execute:
docker rmi image-name:tag
9. Building a Docker Image If you have a Dockerfile, you can build an image using:
docker build -t image-name:tag /path/to/Dockerfile-directory
Docker simplifies the process of managing applications and their dependencies, allowing for consistent deployments across various environments. With these installation and basic command instructions, you're now ready to explore Docker's capabilities on your AWS EC2 instance.