Title: Mastering Docker Installation: A Step-by-Step Guide with Real-Time Testing
Docker has revolutionized how we manage applications by offering a lightweight, portable way to package and run software. This guide takes you through the installation and testing of Docker on various platforms, such as Windows, Amazon Linux 2, and Ubuntu, ensuring you can deploy containerized applications seamlessly.
1. Installing Docker on Windows
To install Docker Desktop on Windows, follow the official Docker documentation for step-by-step instructions:
Installation Guide for Docker Desktop on Windows: Install Docker Desktop on Windows
After installation, you can start running Docker containers using the Docker CLI.
2. Installing Docker on Amazon Linux 2
Here are the steps to install Docker on Amazon Linux 2:
Step 1: Update Software Repositories
$ sudo yum update -y
Step 2: Install Docker
$ sudo yum install docker -y
Step 3: Start Docker Service
$ sudo service docker start
Step 4: Add User to Docker Group
After installation, running Docker commands may produce permission denied errors. To fix this, add the ec2-user to the docker group:
$ sudo usermod -aG docker ec2-user
Step 5: Restart the Session
Exit and restart the session for the changes to take effect:
$ exit
If using MobaXterm, press 'R' to restart the session.
Step 6: Verify Docker Installation
Check the Docker version and other details:
$ docker -v
$ docker info
3. Installing Docker on Ubuntu
Step 1: Update Software Repositories
Set a hostname for the VM and update the system:
$ sudo hostnamectl set-hostname Docker-VM
$ bash
$ sudo apt update -y
Step 2: Install Docker
$ sudo apt install docker.io -y
Step 3: Enable and Start Docker
Ensure Docker starts automatically on boot:
$ sudo systemctl enable docker
$ sudo systemctl start docker
Step 4: Verify Docker Installation
Check Docker version:
$ docker --version
Step 5: Allow Executing Docker Commands Without sudo
To avoid permission errors, add the current user to the Docker group:
$ sudo usermod -aG docker $USER
or
$ sudo usermod -aG docker ubuntu
Step 6: Restart Docker Service
Restart the Docker service to apply the changes:
$ sudo service docker restart
Step 7: Verify Docker Service Status
$ sudo systemctl status docker
To unblock the terminal:
$ ctrl + c
4. Testing Docker Installation
Once Docker is installed, it's essential to test the installation using various containers and commands.
Test 1: Pull and Run an Nginx Image
Step 1: Pull the Nginx Image
$ docker pull nginx
Step 2: Verify Image
$ docker images
Step 3: Run the Nginx Container
$ docker run -d -it --name africa-nginx -p 1000:80 nginx
Step 4: Verify Running Container
$ docker container ls
$ docker ps
Step 5: Access the Nginx Page
In your browser, access the Nginx default page using:
http://<your-server-public-ip>:1000
Test 2: Pull and Run a Custom Image from Docker Hub
Step 1: Pull the Image
$ sudo docker pull ekangaki/netflix
Step 2: Run the Container
$ sudo docker run -it -d -p 8080:80 ekangaki/netflix
Step 3: Verify the Running Container
$ sudo docker ps
Step 4: Access the Application
Open your browser and navigate to:
http://<your-server-public-ip>:8080
Test 3: Run a Container Directly Without Pulling an Image
You can run the hello-world container without pulling the image first:
$ docker run hello-world
If the image is not available locally, Docker will pull it automatically and display a confirmation message that your installation works.
Test 4: Running a Tomcat Container
Step 1: Pull the Tomcat Image
$ sudo docker pull tomcat
Step 2: Run the Tomcat Container
$ sudo docker run -d -p 80:8080 tomcat
Step 3: Access the Tomcat Application
Open your browser and navigate to:
http://<your-server-public-ip>
Test 5: Run a Spring Boot REST API Container
Step 1: Pull the Spring Boot Image
$ docker pull ekangaki/sb-app:latest
Step 2: Run the Container with Port Mapping
$ docker run -p 9090:9090 -it -d ekangaki/sb-app:latest
Step 3: Access the Application
In your browser, use:
http://<your-server-public-ip>:9090/welcome/Ekangaki
5. Docker Commands Cheat Sheet
Here are some essential Docker commands to manage your containers and images effectively:
Pull an Image: docker pull <image-name>
List Docker Images: $ docker images
Run a Docker Container: $ docker run -d -t -p <host-port>:<container-port> <image-name>
List Running Containers: $ docker ps
Stop All Containers: $ docker stop $(docker ps -a -q)
Remove All Containers: $ docker rm $(docker ps -a -q)
Remove an Image: $ docker rmi <image-name>
Clean Up Unused Resources: $ docker system prune
Conclusion
By following this guide, you've learned how to install Docker on Windows, Amazon Linux 2, and Ubuntu, as well as how to verify your installation through various tests. With Docker, you can create isolated environments for your applications, ensuring consistency and portability across different platforms.
Get started with Docker today and take your containerization skills to the next level!