Table of Contents
ToggleIntroduction
Welcome to this article ‘Docker tutorial for beginners step by step’ where you will learn the basics of Docker containerization like its installation, how to create a Docker image, and how to run the image as a container. Also, you will see a step-by-step example of how to containerize an application. At the end of this article, you will have a basic understanding of Docker and be proficient in using Docker.
What is Docker and Why Do I Need It?
Before docker, the traditional way of deploying an application was to package the application code into an artifact like a jar, war, etc. After packaging, we were required to install all its dependencies on the server where we wanted to host the application. This required a lot of manual effort, which would be multiplied if we had multiple servers. For each server, we are required to set up the environment. This is where Docker comes in as a savior.
Docker is a platform that allows your application to package as a reusable image that can be deployed and run on any system with Docker installed. This running instance of this package is called Container. This Container is created in such a way that all the application dependencies lie within the container. This Docker image is portable and hence can be deployed as a container in multiple environments without any server dependencies except Docker installation. This means you can just do the application development and testing on one machine and then deploy it in further above environments with minimal effort hence reducing the manual effort. This creates a consistent development environment across multiple servers without worrying about dependencies and version conflicts. That’s the power of Docker.

Creating a Docker Container
Step 1: Installing Docker
The first step to Dockerization is to install the docker on your system. Below are the installation instructions for different OS
- Linux: Run the command `sudo apt-get update` followed by `sudo apt-get install docker.io`. Then, start the service using `sudo systemctl start docker`.
- Windows: Download the Docker Desktop installer from the official website and follow the setup wizard. Once installed, you can access Docker through the Start menu.
- MacOS: Install Docker Desktop from the official website.
Step 2: Create a Dockerfile
Next, We need to create some instructions that Docker software will use to create a Docker image. These instructions are the steps that do the code packaging, dependencies management, and the final command to run the application.
These instructions are written in a file called “Dockerfile”. This name is standard and there is predefined syntax for each step. Below is an example of a very simple Dockerfile.
# Use python image as base OS image
FROM python:3.10-slim
# Set the working directory in the container
WORKDIR /usr/src/app
# Copy all current folder files into the container at /usr/src/app
COPY . .
# Command to run the application
CMD ["python", "./app.py"]
Let’s break down each instruction:
- FROM python:3.10-slim:
`FROM` specifies that this Dockerfile is based on another image.
`python:3.10-slim` is the base image, which contains a minimal Python 3.9 environment.
- WORKDIR /usr/src/app:
`WORKDIR` set `/usr/src/app `as a working directory inside the container.
This ensures that all subsequent instructions will run within this directory.
- COPY …:
`COPY` copies files from one location to another.
In this case, it copies all file from the host machine (i.e., your local system) into the container’s `/usr/src/app’ directory.
- CMD [“python”, “app.py”]:
`CMD` is the command that is executed when the container starts running.
In this example, it runs the `app.py` Python script using the `python` command which displays “Hello World!”
Here is the app.py
# app.py
print("Hello, World!")
Step 3: Building a Docker Image using Docker Build
In the above step, you have created a Dockerfile which is the instructions to create a Docker image. Now next step is to create an image using this file. This can be done using the Docker build command. Just make sure you are running this command in same directory where you have created the Dockerfile. Below is an example
docker build -t my-hello-world.

This will create an image with the tag `my-hello-world`.
Step 4: Running a Docker Container
So far we have created a Docker image for our application. Next, we need to run our application and this can be done by running an instance of this Docker image which is a Docker Container. Again, Docker has a command to do this which is shown below
docker run -p 8000:8000 my-hello-world
The `-p` flag maps port 8000 on your host machine to port 8000 inside the container. This means the application is running inside the container on port 8000 and the application is exposed on 8000 on the host machine.
‘my-hello-world’: this is the tag assigned to the Docker image

Understanding Docker Containers
As mentioned above, a Docker container is a running instance of a Docker image. The Docker container itself is like an isolated environment running on the base OS image mentioned in the first line of the Dockerfile. It contains all the application dependencies, storage, and networking. When a Docker container is run, Docker creates a new process as per the command mentioned as CMD in the Dockerfile
Here are some key concepts to understand about containers:
- Isolation: Containers provide process-level isolation, which means they run as separate processes on your system.
- Resource sharing: Containers can share resources with other containers and the host machine.
- Persistence: Container data is stored in a virtual file system, which persists even when the container is stopped or deleted.
Managing Docker Containers
Above is just one container but in reality, we will be running multiple containers in a system. Below are the basic and useful commands which can be used to manage them properly
- docker ps: Displays a list of all running containers.
- docker stop: Stops a running container by its ID or name.
- docker rm: Deletes a stopped container.
Working with Docker Volumes
Docker volumes provide persistence to the container when started. To create a new volume, run the following command:
docker volume create my-data-volume
Then, you can mount this volume inside a container using:
docker run -v my-data-volume:/app/data my-python-app
Deploying Docker Containers
Now that we’ve covered the basics of Docker containers, it’s time to deploy them. Here are some popular deployment options:
- Docker Compose: A tool for defining and running multiple containers with a single command.
- Kubernetes: An open-source container orchestration system for automating deployments.
Advanced Docker Topics
Congratulations! You’ve made it to the final step of this article ‘Docker Tutorial for Beginners Step by Step’.
Below are some advanced topics that we can explore:
- Docker Networking: Learn how to create custom networks and connect containers using Docker’s built-in networking features.
- Docker Secrets: Discover how to securely store sensitive data, such as database credentials or API keys.
Conclusion for ‘Docker Tutorial for Beginners Step by Step’:
That concludes our comprehensive guide ‘Docker Tutorial for Beginners Step by Step’. By following these steps, you’ve learned the basics of containerization, image building, container running, and management. You’re now equipped with the knowledge to deploy Docker containers in production environments and explore advanced topics like networking and secrets management. If you want to learn more about DevOps, please follow my DevOps blog page.
Happy containerizing!
One Response