Table Of Content
- Introduction
- Basic Docker commands
- Running an application using Dockerfile
- Docker volume
- Docker network
- Docker compose file
- Clean UP
- Conclusion
Prerequisite
- An AWS account
- A Linux system (Ubuntu 20.04 or Amazon Linux 2 recommended )
- Basic knowledge of Linux commands
Introduction
Docker has become an essential tool for building, shipping, and running applications seamlessly across different environments in today's fast-paced software development landscape. Whether you're a developer, DevOps engineer, or system administrator, understanding Docker can significantly enhance your ability to manage applications efficiently.
This article will guide you through key Docker concepts, including:
✅ Dockerfile – Automating image creation
✅ Docker Network – Enabling seamless communication between containers
✅ Docker Volume – Managing persistent data in containers
✅ Docker Compose – Defining and running multi-container applications effortlessly
Each section includes hands-on examples to help you grasp these concepts in a practical way. By the end of this article, you'll be well-equipped to leverage Docker for your containerized workflows. Let's dive in! 🚀
Step 1: launch an EC2 instance
- click on Launch instance
- Choose your instance name and Amazon Machine Image
- Select your instance type and your key pair
- Allow traffic from SSH, HTTP, HTTPS and click on launch instance
- SSH into the server from your terminal
Step 2: Install Docker
- Update and upgrade your system
sudo apt-get update -y && sudo apt-get upgrade -y
- Install Docker:
sudo apt-get install -y docker.io
- Enable and start docker :
sudo systemctl enable docker && sudo systemctl start docker
- Allow the current user to run Docker commands without sudo (optional)
sudo usermod -aG docker $USER
- Log out and log in again for this change to take effect
Step 3: Explore docker commands
- check if docker is running
sudo sysetmctl status docker
- Pull and run NGINX image from the docker hub
docker pull nginx
- Verify the downloaded image
docker images
- Run a container using NGINX image
docker run -d -p 8080:80 --name my-nginx nginx
- Verify the container is running
docker ps
- Open your browser and navigate to http://localhost:8080. You should see the Nginx welcome page
- Stop the container
docker stop my-nginx
- Remove the container
docker rm my-nginx
Step 4 Create a Custom Docker Image
create a directory
mkdir nginx-image && cd nginx-image
Create a Dockerfile:
vim Dockerfile
Add the following content:
Use the official nginx image as the base image
FROM nginx:latest
Copy a custom HTML file to the nginx document root
COPY index.html /usr/share/nginx/html/index.html
-
Create a custom index.html
vim index.html
Add the following content:
<!DOCTYPE html>
My Custom Nginx
Welcome to My Custom Nginx Container!
Build the docker image
docker build -t ejibode .
- Run the container using the custom image
docker run -d -p 8081:80 --name ejibode ejibode
- Open your browser and navigate to http://localhost:8081. You should see your custom HTML content.
Step 5 Manage Volumes
- Create a volume:
docker volume create my-volume
- Run a container and mount the volume
docker run -d -p 8082:80 --name my-new-nginx -v my-volume:/usr/share/nginx/html nginx
Access the container shell
docker exec -it my-new-nginx
Add a custom HTML
echo "<h1>Persistent Volume Content</h1>" > /usr/share/nginx/html/index.html
- Open your browser and navigate to http://localhost:8082. You should see the updated content.
- Stop and remove the container:
docker stop my-new-nginx && docker rm my-new-nginx
Step 6: Use docker networks
- create a docker network:
docker network create my-network
- Run a container in the network:
docker run -d --name network-nginx --network my-network nginx
- Inspect the network
docker network inspect my-network
- Remove the network
docker network rm my-network
Step 7 :Multicontainer Application Using Docker compose
Install Docker compose
sudo apt-get install -y docker-compose
Create a docker-compose.yml file
vim docker-compose.yml
paste the following code
version: '3'
services:
web:
image: nginx
ports:
- "8080:80"
app:
image: custom-nginx
build:
context: .
Run the container
docker-compose up -d
Access the application at http://localhost:8080
- Stop and remove the application
docker-compose down
Step 8 Clean up
- Remove all the stopped containers
docker container prune
- Remove unused images
docker image prune -a
- Remove unused volumes
docker volume prune
Conclusion
Mastering these concepts enables efficient containerization of applications, improves scalability and streamline deployment processes.
Thank you for reading my article!
Top comments (0)