DEV Community

Ejibode Ibraheem
Ejibode Ibraheem

Posted on

Introduction to Docker: Simplifying Containerized Applications

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

launch-instance

  • Choose your instance name and Amazon Machine Image

amazon-ami

  • Select your instance type and your key pair

Instance-type

  • Allow traffic from SSH, HTTP, HTTPS and click on launch instance

network

  • SSH into the server from your terminal

ssh-server

Step 2: Install Docker

  • Update and upgrade your system sudo apt-get update -y && sudo apt-get upgrade -y

ubuntu-update

  • Install Docker: sudo apt-get install -y docker.io

docker-installed

  • Enable and start docker : sudo systemctl enable docker && sudo systemctl start docker

enable-docker

  • Allow the current user to run Docker commands without sudo (optional) sudo usermod -aG docker $USER

docker

  • 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

docker-status

  • Pull and run NGINX image from the docker hub docker pull nginx

nginx

  • Verify the downloaded image docker images

docker-image

  • Run a container using NGINX image docker run -d -p 8080:80 --name my-nginx nginx

nginx-image

  • Verify the container is running docker ps

nginx-running

nginx-page

  • Stop the container docker stop my-nginx

my-nginx

  • Remove the container docker rm my-nginx

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 .

docker-build

  • Run the container using the custom image docker run -d -p 8081:80 --name ejibode ejibode

ejibode-nginx

ejibode-ibraheem

Step 5 Manage Volumes

  • Create a volume: docker volume create my-volume

docker-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

ejibode

  • 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

volume-exec

persistent-volume

  • Stop and remove the container: docker stop my-new-nginx && docker rm my-new-nginx

docker

Step 6: Use docker networks

  • create a docker network: docker network create my-network

docker-network

  • Run a container in the network: docker run -d --name network-nginx --network my-network nginx

docker-network

  • Inspect the network docker network inspect my-network

docker-inspect

  • 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: .

 nginx

  • Stop and remove the application docker-compose down

docker-compose

Step 8 Clean up

  • Remove all the stopped containers docker container prune

docker-prune

  • Remove unused images docker image prune -a

docker-image

  • Remove unused volumes docker volume prune

docker-vol-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)