In my last blog post, we compared virtualization and containerization. As I said at the end of that blog post, I am going to write about Docker architecture and some of the most common commands to containerize your applications. Let’s dive in.
Docker is a containerization tool. Under the hood, Docker uses namespaces and cgroups to create independent processes on your host OS without having to install VMs. Docker helps us manage some low-level computer processes like namespaces and cgroups.
Like other well-known applications, Docker employs a client-server architecture using a REST API. You might wonder why Docker needs a client-server architecture. Does it send requests to some servers? It actually does, but not quite like you think.
Docker Architecture
Docker architecture consists of a client (Docker Desktop or CMD) and a server (Docker Daemon). The Docker daemon executes commands issued by the client by translating them into actionable operations within the Docker environment. The Docker daemon also manages containers and images.
You might have heard about “Docker Engine” at least once in your career; it is essentially the Docker daemon. You can easily install Docker Desktop and start your Docker engine. At this point, Docker Desktop acts as both the client and the server. The engine runs in the background and waits for commands from the client.
Managing Containers and Images
Here is a simple roadmap if you would like to dockerize your application:
-
Create a Dockerfile in your application’s repo:
touch Dockerfile -
Build the image from this Dockerfile:
docker build . -
Create and run a container that contains your application:
docker run <image_name>
Docker instructions are in the Dockerfile to explain how the image will be created. You can check running containers in the Docker Desktop application or via CLI:
docker ps
Example Dockerfile
What is Docker Hub?
Docker Hub is a container registry designed for developers and open-source contributors to find, use, and share their container images. You can deploy your images or pull (download) other images created by developers. For this aspect, I compare Docker Hub to GitHub.
You can download Docker images from the hub with:
docker pull <image_name>
Or you can push your images to the hub, but first, you have to log in:
docker login
Then:
docker push myusername/myapp:latest
What is Docker Compose?
Docker Compose helps us run multiple containers with a single command, docker-compose up and docker-compose down. To define Docker Compose, you must create a docker-compose.yml file. The most important feature of Docker Compose is that it automatically creates a network between connected containers.
For example, if you dockerized both your application and the database, you can use docker-compose up to run these containers without having to create a distinct network for them.
Example docker-compose.yml File
You can run multiple containers (frontend and backend) using this docker-compose.yml with:
docker-compose up
Additional Docker Commands
Here are some other Docker commands you might find useful:
-
Run an interactive shell in the Ubuntu image:
docker run -it ubuntu -
Run the Postgres image in the background:
docker run -d postgres -
List running containers and images:
docker ps -
Follow logs of a specific container:
docker logs -f <container_id>