A Fundamental Guide To Docker For Beginners
Tags: docker, dockercompose, containers, devops
Published: 2022-03-18 16:16:23

In my [previous article](https://medium.com/codex/what-is-docker-and-why-do-we-need-it-7dedc616366e), I had gone over what is Docker and why do we need it. I recommend you go over that article to understand what Docker is and the need to containerize your applications using Docker

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1shinhhgg3ifokjrsi43.png)
### Docker
To begin with, **Docker** is a tool used for developing, shipping, and running applications inside loosely coupled isolated environments called **containers**. Containers are lightweight processes that include all the dependencies or libraries that your application needs to run without interfering with the other containers running on the same host machine. The advantage of using containers is that your application runs the same way on any machine irrespective of its OS or conflicting dependencies on the host machine.

Now, let's discuss the main components that Docker comprises of

---

### The Docker Architecture
**Docker Engine**
The **Docker Engine** is the core containerization technology responsible for creating and managing all the containers and other Docker objects. It acts as a client-server application consisting of:
- The **Docker Daemon**, which is a daemon process that runs in the background, keeps listening for any API requests and manages the Docker objects accordingly
- A set of **APIs** to communicate with the Docker daemon
- The **Docker CLI client** helps users to communicate with the docker daemon and carry out the user's requests by using these Docker APIs

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

### Docker Objects
All the files and individual components that are managed by the Docker engine like images, containers, dockerfiles, volumes, and networks are called objects in the Docker context. Let's discuss each one of these objects and understand what they represent

**Dockerfile**: A Dockerfile is a text file with instructions and commands that a user intends to perform as part of building a docker image. Dockerfile is first used to build an image, which is then used to create the container itself. A simple Dockerfile would look something like this
```
FROM ubuntu
RUN apt-get update
RUN apt-get install –y nginx
CMD ["echo","Image created"]
```
**Docker Image**: A Docker image is just a template that includes all the set of instructions and commands from the Dockerfile, and this in turn is used to create the actual container. We can use the same docker image to run the same set of instructions and create multiple docker containers on the same host given they run on different ports
**Docker Container**: A Docker container is the actual instance that is based on the docker image. It packages all the dependencies, libraries that an application needs and runs it in loosely coupled isolation.

Now that we understand what dockerfiles, images, and containers stand for, let's understand what the Dockerfile cited above does

- `FROM ubuntu`: This statement tells docker which base image to use while building your current image. In this example, we are the ubuntu image, which is pulled from the configured docker registry while building the image. It is required that you specify the FROM command to tell docker which base image your current image is based on
- `RUN apt-get update`: Runs the update command on the ubuntu system
- `RUN apt-get install -y nginx`: Installs the nginx server on the system
- `CMD ["echo","Image created"]`: Prints "Image created" message on the terminal

**Docker Volume**: By default, the data related to a container is not persisted when the container is terminated or restarted. Volumes in docker help store the container-related data inside the host machine's file system which is then managed by Docker
**Docker Network**: For the individual containers to communicate with each other or to communicate with the host machine, docker provides the concept of networking. The different network drivers that can be used are - bridge (default), host, overlay, ipvlan, macvlan, and none (disables all networking)

---

### Docker Registries
Docker registries are a place where you can store all your docker images. The official registry of Docker i.e; Docker Hub is a public registry that allows any user to pull or push images to the registry. It is also possible to create your own private registry. Docker Hub is set as the default registry when you first install Docker and all the pull and push commands you run in order to upload or download your images use Docker Hub

---

### Docker Compose
So you have created a few dockerfiles, built images based on these dockerfiles, and also ran containers using these images. But using this approach, it becomes difficult to manage all the containers, their configuration, and the network links between individual containers to establish communication, this is where Docker compose comes into the picture.

**Docker Compose** is a container orchestration tool that is used to run multi-container applications on a single host machine. Docker compose helps users to start multiple dependent containers by using just a single file and a single command. This single file is used to maintain all the configuration, network links, volume, and port mappings for the required containers of your application and is usually written in YAML format

---

### Docker Swarm
**Docker Swarm**, similar to Docker compose is a container orchestration tool that makes it simpler to manage multiple containers that run on multiple host machines. This group of machines is called a cluster and each of them needs to have Docker running on them. Individual machines that make up the cluster are referred to as nodes and all the activities of the cluster are controlled by a node which is referred to as the swarm manager

More Docker articles you can check out:
- [What Is Docker And Why Do We Need It?](https://medium.com/codex/what-is-docker-and-why-do-we-need-it-7dedc616366e)

---

### References
- [https://docs.docker.com/](https://docs.docker.com/)
- [https://www.tutorialspoint.com/docker/index.htm](https://www.tutorialspoint.com/docker/index.htm)

_Follow for more articles related to Docker and Software Engineering in general :)_