Docker Summary for Basic Usage
1. What is Docker?
Docker is not only a name of a company, but also a name of a technology. Docker is a containerization platform. It is used to build, ship, and run applications by using containers. Containers are isolated environments that contain everything an application needs to run. Therefore, Docker is a tool that can help us to build, ship, and run applications in containers.
Why do we need containers?
TL;DR Containers are lightweight, portable, and isolated. They help us to deploy and run applications in a more efficient way.
For example, if we want to run a web application on different servers, we need to install the same environment on each server.
It’s hard to solve environment problem if we just share the code of the application. However, if we use containers, we can just share the container image of the application. Then we can run the application on different servers without worrying about the environment problem.
2. How to install Docker?
Docker can run on all major operating systems, althought it is based on Linux kernel. For macOS and Windows users, we can install Docker Desktop. For Linux users, we can install Docker Engine.
After installing Docker, we can use docker version
to check whether Docker is installed successfully. If it is installed successfully, we can see the version of Docker Client and Docker Server.
1 | docker version |
In fact, docker for macOS or for Windows runs on a Linux virtual machine. We can see the docker server’s OS/Arch is always linux/arm64
or linux/amd64
.
3. Docker Basic Commands
To run a container from docker hub, a public docker image registry. We can use this command:
1 | docker run -d -p 27017:27017 \ |
docker
run is used to run a container from an image. -d
means run the container in background.
The terminal will be blocked if there is no -d
flag.-p
is used to map the port of the container to the port of the host. -p 8000:4000
means that
we can access the container’s port 4000 by using host’s port 8000. 4000
is used by application running in container.-e
is used to set environment variables. It’s easy to look at docker hub profile to know how to config environment variables.--name
is used to set the name of the container. It is recommanded to set the name of the container in order to track the container easily.--net
is used to set the network of the container. We can create a network by using docker network create
command.mongo
is the name of the image. We can use docker pull
command to pull the image from docker hub.
If we don’t have the image in local, docker will pull the image from docker hub automatically.
1 | docker network create mongo-network |
This command is used to create a network named mongo-network
.
Docker network is used to connect containers. Containers in the same network can communicate with each other by using container’s name as host name. I used to deploy mongo
, a dbms, and mongo-express
, a ui client, in the same network
so that mongo-express
can connect to mongo
by using mongo
as host name.
1 | docker-compose -f docker-compose.yaml up/down |
This command is used to start/stop a compose. Docker compose is used to deploy multiple containers at once. By default, all
services in a docker compose share the same docker network. So we don’t need to create a network manually.
1 | docker run -v name:container_path |
This command is used to mount a volume to a container. Docker volume is used to store data. The data in containers will be lost
when the container is restarted. However, the data in volumes will not be lost. We can mount a volume to a container to persist data.
It is recommended to use name:container_path
to give a name to the volume. In most cases, we don’t need to declare where we store the volume locally. Docker can manage the volume for us.
docker ps
is used to look at all running containers. docker exec -it container_id
is used to enter the container.
4. Dockerfile
Dockerfile tells docker how to build an image from scratch. We can create our images to wrap our applications by using Dockerfile.
Here is an example.
1 | FROM node:13-alpine |
FROM
is used to set the base image. ENV
is used to set environment variables. RUN
is used to run commands. COPY
is used to copy files from host to container. WORKDIR
is used to set the default directory, very like cd
in linux. CMD
is used to set the default command.
5. Docker Compose
Docker compose should be defined by a .yaml
file. Here is an example.
1 | version: '3' |
6. What’s more
After learning this low-level container technology, developers should learn kubernates. It is a high-level container technology. It is used to manage clusters, which contain many containers.
About this Post
This post is written by Chen Li, licensed under CC BY-NC 4.0.