๐ Understanding Docker: A Practical Guide

Docker has become one of the most popular tools for building, packaging, and running applications in a lightweight, consistent, and isolated environment called containers.
๐ก Virtualization vs. Containers
Traditional virtualization uses a hypervisor, which sits on top of the operating system or directly on hardware to run multiple virtual machines.
On the other hand, Docker builds a lightweight layer on top of the operating system, allowing you to run multiple isolated applications without the heavy overhead of full virtual machines.
โก Why use Docker?
Virtualization: Runs applications in isolated environments without affecting the host system.
Lightweight: Uses fewer resources compared to virtual machines.
Reusability: Easily reuse and share images across teams and environments.
๐ณ Installing Docker on Ubuntu (Quick Steps)
sudo apt update
sudo apt install docker.io
sudo systemctl start docker
sudo systemctl enable docker
Then, add your user to the Docker group to run Docker without sudo
:
sudo usermod -a -G docker $USER
โ
This command adds your current user to the docker
group, allowing you to run Docker commands without root permissions.
๐ Dockerfile: The Blueprint
A Dockerfile contains instructions that define everything needed to build a container for a specific application, including its code, dependencies, and setup steps.
In short, it is used to create a Docker image, which is then used to spin up containers on the Docker engine.
Dockerfile syntax basics
FROM # Base image
WORKDIR # Set working directory
COPY # Copy files into image
ENV # Set environment variables
EXPOSE # Expose ports
RUN # Run commands
CMD # Command to run when container starts
๐ฆ Working with Images and Containers
Pull an image from Docker Hub
docker pull image_name
Build an image from a Dockerfile
docker build . -t tag/name
Run a container
docker run -d --name my-db -e MYSQL_ROOT_PASSWORD=password mysql:latest
docker run
: Starts and runs a new container.-d
: Detached mode (runs in background).--name my-db
: Names the container "my-db".-e
: Sets environment variables.MYSQL_ROOT_PASSWORD=password
: Sets the MySQL root password.mysql:latest
: Uses the latest MySQL image.
๐ Useful Docker Commands
Stop a container
docker stop container_id
Remove a container
docker rm container_id
Open a Bash shell inside a running container
docker exec -it container_id bash
This allows you to look around or run commands directly inside the container.
Run MySQL client directly in a container
sudo docker exec -it <container_id> mysql -u root -p
โ Instead of first opening bash, this directly opens the MySQL client inside the container.
โ Conclusion
Docker simplifies the process of packaging and running applications, making it easy to create consistent environments across development, testing, and production. By learning to work with Dockerfiles, images, and containers, you can make your applications more portable and efficient.
Subscribe to my newsletter
Read articles from Samyog Ghimire directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
