Docker tutorial - the basics
Tags: webdev, programming, docker, beginners
Published: 2022-12-19 16:30:09

## Content 
- [Disclaimer](#disclaimer)
- [What is Docker](#what-is-docker)
- [Why we should use it?](#why-we-should-use-it?)
- [The basic concept](#the-basic-concept)
- [The first run](#the-first-run)
- [Conclusion](#conclusion)
- [Links](#links)

## Disclaimer
This is not another random Docker article. I had a difficult time understanding the concept of Docker in early career. There are a ton of documents to refer to, those covered too much for the first timers though. I bet there will be many others who struggle with Docker. So, I tried to cover the gist of Docker and how to run it for newcomers in this post.

## What is Docker
You've heard the term Docker from here and there if you are a developer at any level. Yet, do you know what it's about exactly? According to official docs, Docker is an open source platform that enables developers to build, deploy, run, update and manage containers — standardized, executable components that combine application source code with the operating system (OS) libraries and dependencies required to run that code in any environment.

## Why we should use it?
Here are a few benefits I found useful.
Easy dependency management

Software engineers struggled to set up the right development environment. Many times, the local versions are different from the ones a software requires. Let's say you are using Node v14 and a project requires Node v16. To run it, you should switch the node version. That's not a big deal if you have only one project. Yet, what if you have many projects and they need different versions for all dependencies? Then it is a nightmare. Containers provide isolated environments so running different versions of dependencies is safe.
Easy deployment and scale

Containers make continuous integration and delivery easier. For every deployment, we can run tests on containers to make sure nothing breaks, for example. Scaling becomes easier. Thanks to the consistent environments, we only need to add or reduce the number of containers.
Os independent

Each team might use a different OS - Linux, macOS, or Windows. If so, there might be extra steps to set up the projects, which makes the team stressful. As Docker is platform agnostic, teams can work on a consistent environment.
It's not only for devops

I saw people who think Docker is only for devops. No. Regardless of your hat, it's beneficial to know about it. It's used in most of software delivery process. Of course, the devops team will take care of the deployment process a lot. Yet, their job is not to debug the whole process when you are stuck in the middle of it. So, learn to catch a fish.

## The basic concept
Let's try to understand important terms and concepts first.
Image vs Container

People might use them interchangeably, but they are different. The image is a template. The container is a running environment you can use to run your image.
Registry

Docker registry is like Github for Docker images. There will be both public and private images. Ubuntu, Redis, or Postgres are the public ones that everyone can use for their development. Private ones are only accessible to allowed users.
Cli

You can interact with Docker with the command line. Docker CLI is well documented. You can find most of them with the --help. I will cover common commands in another post.
How does it work?


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/a0ff6etglaot1b9l8h7g.png)

[Image by official Docker docs](https://docs.docker.com/get-started/)
The Image from the official docs shows one of the typical flows when using Docker. Let's say I want to run Postgres. I will run docker pull postgres. This CLI command will check if there is an image in our machine. If not, it will try to fetch from the Docker registry. Since Postgres is public in the registry, we can pull it down. Then you can start it by running docker run postgres. That's it!

## The first run
Now you understand the basic concepts and flow of Docker, it's time to run it by yourself. Make sure you install Docker in your system. Let's use Postgres. Try to run

docker run --name postgres-db -e POSTGRES_PASSWORD=password -p 5432:5432 -d postgres

--name is the option for assigning a name to the container. -e is the option for passing your environment variable. Here I set the POSTGRES_PASSWORD to password. -p is the option for mapping your port. -d is the option for running in the background.

Then, try to run docker ps

It will display the containers with status. That's it!

What if you want to run your software with Docker? To do so, we need Dockerfile, which is a recipe to build an image. I will cover Dockerfile and more in next chapter.

## Conclusion
So far, I've covered what Docker is and why we want to learn about it. I also cover the core concept of Docker and how to run applications with Docker. I can't stay from it as a backend engineer. You will be. So, start today! Stay tuned for the next articles :)

## Links
- [Official docs](https://docs.docker.com/get-started/)