Containerization is a must-have skill for DevOps engineers and software developers alike. It is essential for creating robust, deployable applications. In this article, I will explain the Docker concepts every developer needs to know and provide a hands-on example.
Table of Contents
- Prerequisites
- What is Docker?
- Why is Docker Important
- Idea of How Docker Works
- What is a Docker Image?
- Dockerfile Commands Explanation
- Running the Image
- What is a Docker Container?
- How to Run a Docker Container
- Docker Volumes
- Final Thoughts
Prerequisites:
to follow along this article be sure to
- Download and install Docker Desktop.
- Ensure Docker is running (especially on macOS or Windows) while practicing.
What is docker:
Docker is a software platform that helps package and run applications on any machine with the exact environment you have configured.
Teams often face the "it works on my machine" issue, where an application runs locally but crashes when a teammate tries to run it. This happens due to:
- Mismatched environment or library versions.
- Differences in Operating Systems (OS) that don't support specific features.
Configuring every environment to match can be time-consuming and slows down development. Docker solves this by "containerizing" the environment.
Why is docker important:
As a developer, you shouldn't waste time troubleshooting environment setups. Docker allows you to:
- Configure the environment once and reuse it throughout the development process.
- Run multiple copies of the same application with different environments without conflicts.
- Eliminate library version mismatches.
Idea of how docker works:
Imagine you have a minivan that you use as a home, a business venture, and transport all at once. You can drive anywhere, and everything you need is already inside. You don't need to find a new bed or kitchen every time you move; it's all in the van.
That is the idea of Docker. It contains all the essentials needed to run an application in a single container. This container can be used by anyone, anywhere, without further configuration.
What is a docker image:
Docker image is a blueprint that describes how our application environment will look like and how it will be built from the ground up and after it is created it cannot be modified.
with docker image we describe how docker should allocate and arrange our application environment this process happens in a file called Dockerfile.
A Dockerfile defines the layers (stages) required to run the container, starting from the OS up to your codebase.
#this creates a node runtime of version 18 and OS of alpine
From node:18-alpine
#this specifies the path where docker will store you application codes this could be any name of path you want
WORKDIR /app
#copies the package file to docker files directory so that when npm can have packages to install
COPY package.json
#this will run and install all the packages you are using in your package file
RUN npm run install
#copies all the codebase in your directory to the docker files
COPY . .
#define the port that will be used when testing the application
EXPOSE 3000
# if you are using nodemon you write a command to start your application like this
CMD ["npm", "ru", "dev"]
# if you are using node to execute the code you use
CMD ["node", "name of the file.js"]
Docker file commands Explanation
From specifies which kind of runtime you are using in my case I am using JavaScript, but you can use whatever based on your project setup and the colon helps in specifying the version and the hyphen to specify the OS system the system will be built on top of.
WORKDIR this command specifies the directory in which docker will store your application codes generally you can specify any path you want but /app is the convenient one.
COPY this command copies a specific file from your project files to docker
EXPOSE this command specifies the port in which you are application is running on and also helps to define a port your application will run on when starting a container
CMD this command is the one that runs you application after it have been initialized.
Note: for window users and macOS when running your application using nodemon package inside your run script file in package.json file place -L flag before your file for it to run, without it your application won’t get started unless you are a Linux user
Running the image
After setting your docker file you can create an image by running the build command in your terminal using the docker command
docker build -t my-first-docker-image:version-one .
The build command is the one used to build the image for your docker, and the -t flag us used to specify the tag of the image.
Image tag helps to differentiate images with the same name, docker image cannot be modified after they are built to edit it you would need to create a new image, but you may want to keep the same image but change its versions so you can track your changes and version over time and to do that the -t flag helps create a tag that you can refer as a version of the image, to specify the tag or version of your image you use double colon(: )
e.g.
docker build -t my-first-docker-image:version-two .
If you didn’t define the tag name by default docker will assign it the latest as the default tag name
e.g.
docker build -t my-first-docker-image .
without -t flag it would hard to track down the changes of the image when we are constantly making changes and rebuilding the image
. tells docker where to look for files to copy
What is docker container:
Docker container is the implementation of a plan created in docker image, now all of the specifications you’ve specified inside your Dockerfile will be executed one by one and create a container of your application codes and also create the underlying OS which is alpine and a runtime environment of node.js below is the command to run and create a container.
How to run a docker container
docker run --name container_name -p image-name
Let’s start with knowing each command step by step
run command is used to start a new container.
--name flag specifies the name for your container, the name you would like your container to have
-p flag is referred as port mapping this should be used if you have specified EXPOSE command in your Dockerfile it helps map any port you specify to the exposed port in docker file you put you preferred port on the left and the exposed port on the right and separate them using a colon.
e.g.
-p prefferedPort:exposedPort
image-name and then you have to specify the name of image in which you want to create a container from.
running a container from the image we’ve created above we can something like this
docker run --name container_one -p 3000:3000 my-first-docker-image:version-one
Docker Volumes
Docker images are "read-only." If you change your code on your computer, the running container won't see those changes unless you rebuild the image. Docker Volumes solve this.
Volumes act as a bridge between your local development environment and the running container.
docker run --name container_one -p 3000:3000 -v /our apps location:./our docker specified path
the command is the one we’ve been using to create a container before but The -v flag binds your local folder to the folder inside the container (e.g., /app). Now, any changes you make in your code editor will instantly reflect inside the running container!
So, the real command can look something like this:
docker run --name container_one -p 3000:3000 -v /C:\Users\arnol\OneDrive\Documentos\newContent\client:./app
Final Thoughts
Docker makes development faster and smoother. Whether you are aiming to be a DevOps engineer or a Full-stack developer, understanding these basics is essential for modern software engineering.
Thank you for reading! If you learned something new or have suggestions, please let me know in the comments below.

Top comments (0)