Docker, which has 54k stars on Github, what exactly is it?

Many developers should know about Docker. Even if they have never used it, they should have heard about it. This open source tool became popular once it came out in 2013 and is very popular in microservices, project migration, cloud, etc. Now its github project has reached 54k stars. What exactly is Docker? What are its advantages that make it so popular? How to use it? This article will elaborate on the above points. I hope that this article can give everyone a clear understanding of Docker.

Programmer’s treasure trove:https://github.com/Jackpopc/DevWeekly

What is Docker?

Docker is a project initiated by dotCloud and open sourced in 2013. It has become very popular since it was open sourced. Its main projects are now available on github.54kA star. It is developed and implemented using the Go language. It encapsulates and isolates processes based on technologies such as Linux kernel cgroups, namespaces, and AUFS classes. It is a virtualization technology at the operating system level. After that, further development began to use runC and containerd to further encapsulate and isolate everything from the file system to network interconnection and then to the process, which greatly simplified the creation and maintenance of containers, making Docker lighter and faster than virtual machines.

Why use Docker?

Docker, like traditional virtual machines, is a virtualization technology, but it has many advantages that virtual machines cannot match:

  • Continuous delivery and deployment
  • Faster migration
  • Utilize system resources more efficiently
  • Faster boot time
  • Consistent operating environment
  • Easier maintenance and expansion

The details of the comparison between containers and virtual machines are as follows:

For most developers, the first two points are the most felt:Continuous delivery and deploymentFaster migration

I think this is a headache for many developers. During the development process, they will encounter this kind of complaint: “Can it run on my computer? Why can’t it work on another computer?”

Although the emergence of maven, nodejs’s package.json, and Python’s requirements make migration simple, they are more about making the migration of third-party tool packages simple, but not in terms of systems and development environments. What effect. Docker ensures the consistency of the direct environment and can run on multiple platforms, making application migration easier. In addition, Docker uses layered storage and mirroring technology to make it easier to reuse repeated parts of applications, and can make more expansions based on basic mirrors, making system maintenance easier.

basic concept

The following 3 concepts are most exposed when using docker:

  • Mirror: image
  • Container: container
  • Warehouse: repository

Understand these three concepts and you will have an understanding of the entire life cycle of the container. Here, I use simple language to describe the above three concepts

**Image: **It is equivalent to a simplified file system. For example, the official Ubuntu image only contains a minimized root file system.

container:A container is a process with its own root file system, its own network configuration, and its own namespace. Images and containers are like programmingkindandExample, the image is statically defined, and the entity when the image is run is the container. What are classes and instances? Let’s give a programming example to illustrate:

# kind
class HelloWorld:
def __init__(self, x, y):
self.x = x
self.y = y

def add(self):
return self.x + self.y

# Example
hello_world = HelloWorld(2, 3)
print(hello_world.add())

# output
>>> 5

HelloWorld is a class and hello_world is an instance. By analogy, you can understand the relationship between containers and images.

**Warehouse: **The docker image warehouse is just like the github code warehouse. When a person builds a project and wants to run the project on other computers, then he clones the project from the code warehouse. The same is true for the docker image warehouse. After building an image, if you want to use this image on other servers, you need a centralized storage and distribution service. The warehouse is such a service. The official image warehouse is DockerHub, which stores a wealth of images. However, the speed of pulling images in China is slow, so domestic image warehouses can be used instead, such as Alibaba Cloud Image Warehouse, NetEase Cloud Image Warehouse, DaoCloud Image Market, etc.

Installation configuration

Docker currently supports Linux, Windows 10, and macOS. Here is a Linux installation as an example:

APT installation

First install the HTTPS package and CV certificate,

$ sudo apt-get update
$ sudo apt-get install \
    apt-transport-https \
    ca-certificates \
    curl \
    software-properties-common

Add software source GPG key,

$ curl -fsSL https://mirrors.ustc.edu.cn/docker-ce/linux/ubuntu/gpg | sudo apt-key add

Add docker software source,

$ sudo add-apt-repository \
    "deb [arch=amd64] https://mirrors.ustc.edu.cn/docker-ce/linux/ubuntu \
    $(lsb_release -cs) \
    stable"

Install docker ce,

$ sudo apt-get update
$ sudo apt-get install docker-ce

Add user group

The docker command uses Unix socket to communicate with the docker engine, so it requires root privileges every time it is used. It is troublesome to add sudo before the command. In order to avoid this trouble, you can create a docker group and add the current user to the docker user group.

$ sudo groupadd docker
$ sudo usermod -aG docker $USER

Start, exit, and restart docker

$ systemctl start docker
$ systemctl stop docker
$ systemctl restart docker

can also be used,

$ service docker start
$ service docker stop
$ service docker restart

Dockerfile

Understand some basic concepts in docker and complete the docker installation. The next step is to learn how to use docker. For most developers, the most core part of using docker is the Dockerfile.

Dockerfile is a text file that contains some instructions. The construction of the docker image is completed through each instruction in the Dockerfile. In other words, to build an image, you need a Dockerfile, and then configure some instruction sets according to your own needs. Let’s take a look at some instructions used in the Dockerfile.

FROM: Specify the base image

To customize our image, we need to base it on an image, which is a basic image, such as Ubuntu, nginx, postgres, mysql, etc., for example,FROM Ubuntu: 16.04, if there is an Ubuntu base image locally, the local base image will be used. If not, it will be pulled from the official image warehouse.16.04Is the image version number. If not specified, the lastest will be pulled.

RUN: execute command

RUN specifies the commands we need to execute when building the image, such as apt-get install to install a certain software, pip install to install Python dependency packages, configure software sources, configure time zones, etc., for example,RUN apt-get install python3

ADD and COPY: file operations

ADD and COPY are two instructions with similar functions. COPY is generally used first. It is more transparent than ADD. Its function is to copy local files into the container. For example,COPY ./ /home/jackpop

WORKDIR: Specify the working path

Specify the working path of the image’s runtime, for example,WORKDIR /home/jackpop

ENTRYPOINT: Set the mirror main command

Specify the command to run if the image is to be run, for example,ENTRYPOINT [“python”, “-m, “main”]

LABEL: add a label

You can add tags to images to help organize images, record licensing information, assist in automated builds, etc.

CMD: Execute the software contained in the target image

If the purpose of creating an image is to deploy a certain service, some form of command may be executed, which may include parameters.

EXPOSE: Specify the listening port

Specify the access port for external access.

ENV: environment variable

In order for the program to run, it is sometimes necessary to update the environment variables.

VOLUME: Expose database storage files

USER: Specify the current user

The most commonly used commands areFROMCOPYWORKDIRENTRYPOINTRUN

Common commands

Now that we understand the common instructions of Dockerfile, how do we operate images and containers? Let’s learn some commonly used commands of docker:

**Note:** Since I have added the current user to the docker user group, sudo is not added to the following command. If the user group is not added, sudo docker needs to be used.

View local mirror

$ docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
ubuntu 16.04 ****** 10 days ago 119MB

View container

$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
*** *** *** *** *** *** *** ***

Start, stop, and restart containers

$ docker start $container_id
$ docker stop $container_id
$ docker restart $container_id

Exit and enter mirroring

$ exit
$ docker exec $container_id /bin/bash

boot image

$ docker run $image_id

You can use -p and -dns to specify the port and dns to configure the network.

container_id is the container ID, image_id is the image ID.

Pull image

$ docker image pull ubuntu

Create image from Dockerfile

$ docker build

Create an image from a modified container

$ docker commit

Copy files between container and local

$ docker cp

Push image

$ docker push

Tag the image

$ docker tag

double naming container

$ docker rename

Delete container

$ docker rm

Delete image

$ docker rmi

Search for images

$ docker search

Summary of common Docker commands:

Hands

Create project

Test/
├── Dockerfile
└── main.py

Write a simple test program

# main.py

import logging
from time import sleep
import numpy as np

logging.basicConfig(level=logging.DEBUG,
          format="'%(asctime)s - "               "%(filename)s[line:%(lineno)d] - "
               "%(levelname)s: %(message)s")


def main():
  for i in range(10):
    logging.debug(np.random.randint(0, 5))
    sleep(0.1)


if __name__ == '__main__':
  main()

Dockerfile

This is the key part of building an image,

FROM ubuntu:16.04

COPY ./ /home/Test_docker
WORKDIR /home/Test_docker

RUN apt-get update && apt-get install -y python3 python3-pip \
&& ln -s pip3 /usr/bin/pip \
&& ln -sf /usr/bin/python3 /usr/bin/python \
&& rm -rf ls /var/cache/apt/* \

ENTRYPOINT ["python3", "-m", "main"]

Enter the project root directory

$ cd Test

Start creating an image

$ docker build test:v1.0 .

test specifies the name of the built image, and v1.0 specifies the image label. If not specified, the image name and label will be displayed.

Run image

$ docker run $image_id
'2019-06-29 12:26:38,298 - main.py[line:13] - DEBUG: 0
'2019-06-29 12:26:38,399 - main.py[line:13] - DEBUG: 2
'2019-06-29 12:26:38,499 - main.py[line:13] - DEBUG: 1
'2019-06-29 12:26:38,599 - main.py[line:13] - DEBUG: 3
'2019-06-29 12:26:38,699 - main.py[line:13] - DEBUG: 0
'2019-06-29 12:26:38,799 - main.py[line:13] - DEBUG: 4
'2019-06-29 12:26:38,900 - main.py[line:13] - DEBUG: 4
'2019-06-29 12:26:39,000 - main.py[line:13] - DEBUG: 4
'2019-06-29 12:26:39,100 - main.py[line:13] - DEBUG: 4
'2019-06-29 12:26:39,200 - main.py[line:13] - DEBUG: 2

Of course, we can also modify the base image to create our image. For example, we pull an Ubuntu base image, start the image, install the software and environment we need, and then use **docker commit [OPTIONS] CONTAINER [REPO [:TAG]]** to create a new image.

Further reading

In addition to basic docker, there are also some advanced docker open source tools. The more well-known ones are as follows:

  • docker compose
  • docker machine
  • docker swarm

indocker composeIt is one of the official orchestration projects and is used to quickly deploy distributed applications in clusters.docker machineIt is also one of the official orchestration projects, responsible for quickly installing docker environments on multiple platforms.docker swarmProviding docker container cluster services is Docker’s official core solution to support the container cloud ecosystem.

In addition, there are some well-known cluster management systems, such as,

  • Mesos
  • Kubernetes

inMesosIt is an open source project for cluster resource management from UC Berkeley, which allows users to easily implement automated scheduling of distributed applications.KubernetesIt is an open source container cluster management system for docker initiated and maintained by the Google team. It is widely used. It not only supports the cloud platform of the scene, but also supports the internal data center.

Learning Resources

The common commands and instruction meanings mentioned above are sufficient for daily development and use. If you are interested in more in-depth content of Docker, such as data management, security, underlying implementation, containers and cloud computing, you can choose other learning materials. . Here I recommend a learning material that I think is good, which is a detailed docker tutorial-docker_practice open sourced by yeasy on github. Currently, the docker_practice project has 13.7k stars on github. If you want to learn more, you can check out the github project.

Docker — from entry to practice github.com/yeasy/docker_practice

Preface – Docker – from entry to practice

You can also check out gitbooks,

Docker – from getting started to practice yeasy.gitbook.io/docker_practice/

Related Posts

Docker learning: start the container and mount the directory

Java Date time zone settings (GMT and CST)

ES ElasticSearch Connection reset by peer problem solving

Mockito detailed tutorial

Install docker on Mac (easy installation solution)

DataSourceTransactionManager Summary

[Wuji Low Code] Low code platform development diary, low code platform sql programming

[Exception]The field file exceeds its maximum permitted size of 1,048,576 bytes.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

*