2 min read

Containerization with Docker: A Practical Guide

Docker’s flexibility and ease of use make it an invaluable tool for modern development workflows. By mastering its commands and orchestration capabilities, you can streamline application deployment and scalability.
Containerization with Docker: A Practical Guide

As modern software development trends shift toward microservices and scalability, containerization has become a cornerstone. Docker, a leading containerization platform, simplifies the process of building, deploying, and managing applications in isolated environments.


What Is Docker?

Docker is a tool that allows developers to package applications and their dependencies into lightweight, portable containers. These containers can run consistently across different environments, from development to production.


Getting Started with Docker

Installation

Begin by installing Docker from the official documentation. Once installed, you can start exploring its capabilities.

Running Your First Container

To spin up an Nginx container:

docker container run --publish 80:80 --detach --name local_nginx nginx
  • --publish 80:80: Maps port 80 on the host to port 80 in the container.
  • --detach: Runs the container in the background.
  • --name: Assigns a custom name to the container.

Verify running containers:

docker container ls

For all containers, including stopped ones:

docker container ls -a

Working with Images

Docker containers are built from images. To fetch the latest Nginx image:

docker image pull nginx:latest

View all downloaded images:

docker image ls

Advanced Container Management

Starting an Ubuntu Container with Interactive Mode

docker container run --name local_ubuntu -it ubuntu
  • -it: Interactive mode with a pseudo-terminal.

To reattach to a running container:

docker container exec -it local_ubuntu bash

Controlling Containers

Restart interactively:

docker container start -ai container_name

Start a stopped container:

docker container start container_name

Stop a container:

docker container stop container_name

Monitoring and Debugging

Inspect details:

docker container inspect container_name

View resource usage:

docker container stats container_name

Building and Publishing Images

Create a Custom Image

To build an image using a Dockerfile:

docker build -t friendlyhello .

Run the container:

docker run -p 4000:80 friendlyhello

Use a Local Docker Registry

Deploy a registry:

docker run -d -p 5000:5000 --restart=always --name registry registry:2

Push an image to the registry:

docker tag friendlyhello localhost:5000/friendlyhello
docker push localhost:5000/friendlyhello

Orchestrating with Docker Compose

Define multi-container setups using docker-compose.yml. Deploy with:

docker stack deploy -c docker-compose.yml friendlyhelloswarm

To manage the stack:

docker service ps friendlyhelloswarm_web
docker stack rm friendlyhelloswarm

Scaling with Docker Swarm

Enable clustering by initializing a Swarm:

docker swarm init

Add nodes:

docker swarm join-token worker

View nodes:

docker node ls

Docker’s flexibility and ease of use make it an invaluable tool for modern development workflows. By mastering its commands and orchestration capabilities, you can streamline application deployment and scalability.