编程人 cdmana.com

Quick start to docker

Original address : Liang Guizhao's blog

Blog address :blog.720ui.com

One 、 Welcome to Docker The world

1. Docker And virtualization

In the absence of Docker Era , We'll use hardware virtualization ( virtual machine ) To provide isolation . here , Virtual machine builds an intermediate virtual software layer on the operating system Hypervisor , And use the resources of the physical machine to create multiple virtual hardware environments to share the resources of the host , The applications run on the virtual machine kernel . however , There is a bottleneck in the utilization of hardware by virtual machines , Because it is difficult for a virtual machine to dynamically adjust its hardware resources according to the current traffic , So containerization became popular . among ,Docker Is an open source application container engine , Allows developers to package their applications and dependencies into a portable container , Then post to any popular Linux On the machine .

https://user-gold-cdn.xitu.io/2019/4/9/16a02cdab48514e3?imageView2/0/w/1280/h/960/format/webp/ignore-error/1

Docker Containers don't use hardware virtualization , Its daemon is a process on the host , let me put it another way , The application runs directly on the host kernel . Because there is no extra middle layer between the program running in the container and the operating system of the computer , No resources are wasted by running redundant software or simulating virtual hardware .

Docker It's not just that , Let's compare .

Untitled

2. Mirror image 、 Containers and warehouses

Docker By mirror (Image)、 Containers (Container)、 Warehouse (Repository) Three parts .

Docker The mirror image of can be simply compared to the system disk used to install the system for the computer , Including the operating system , And the necessary software . for example , A mirror image can contain a complete centos Operating system environment , And installed Nginx and Tomcat The server . Pay attention to is , The mirror image is read-only . It's easy to understand , Just like the system disk we burn is actually readable . We can use  docker images  To see the list of local images .

Docker Can be simply understood as providing a system hardware environment , It's a real running program 、 Consume machine resources 、 Things that provide services . for example , We can think of the container as a Linux Our computer , It can run directly . that , The container is image based , And every container is isolated from each other . Pay attention to is , When the container starts, it creates a writable layer based on the image as the top layer . We can use  docker ps -a  View locally run containers .

Docker Our warehouse is used to store images . This point , and Git Very similar . We can download images from the central warehouse , It can also be downloaded from the self built warehouse . meanwhile , We can make a good image commit To local , then push To remote warehouse . The warehouse is divided into public warehouse and private warehouse , The largest open warehouse is the official warehouse Dock Hub, There are also many choices for domestic open warehouses , For example, alicloud, etc .

https://user-gold-cdn.xitu.io/2019/4/9/16a02cdab4a554d0?imageView2/0/w/1280/h/960/format/webp/ignore-error/1

3. Docker Drive development process change

The author thinks ,Docker The impact on the development process is to standardize the environment . for example , It turns out that we have three environments : Development ( daily ) Environmental Science 、 Test environment 、 Production environment . here , We need to deploy the same software for each environment 、 Scripts and running programs , As shown in the figure . in fact , The content of the startup script is consistent , But there is no unified maintenance , There are always problems . Besides , For running programs , If the underlying runtime environment is inconsistent , It can also cause problems and anomalies .

https://user-gold-cdn.xitu.io/2019/4/9/16a02cdab4959cfc?imageView2/0/w/1280/h/960/format/webp/ignore-error/1

Now? , We introduce Docker after , We just need to maintain one Docker  Mirror image . let me put it another way , Multiple environments , A mirror image , Implement a system level build and run everywhere . here , We standardized running scripts , Mirror the underlying software , Then implement standardized deployment for the same program to be deployed . therefore ,Docker It provides us with a standardized operation and maintenance mode , And solidify the operation and maintenance steps and processes .

https://user-gold-cdn.xitu.io/2019/4/9/16a02cdab5b3597f?imageView2/0/w/1280/h/960/format/webp/ignore-error/1

Through the improvement of this process , It's easier for us to achieve DevOps The goal of , Because our image can run on any system , And quickly deploy . Besides , Use Docker A lot of motivation is based on Docker Achieve flexible scheduling , To make full use of machine resources , Cost savings .

ha-ha , I'm using Docker In the process , And found some great revenue points , For example, when we release a rollback, we just need to switch TAG And restart it . Also like , We're interested in upgrading the environment , You only need to upgrade the basic image , So the newly built application image , The new version is automatically referenced .( Welcome to add ~~~)

Two 、 From build Web The server starts to talk about

1. Environment first , install Docker

Now? , We need to install the following steps to install Docker.

  • Sign up for an account : stay  hub.docker.com/  Registered account .
  • Download and install

Official download address :(Mac):download.docker.com/mac/stable/…  Alicloud download address (Mac):mirrors.aliyun.com/docker-tool…  Alicloud download address (Windows): mirrors.aliyun.com/docker-tool…

Start after installation , Mac An icon appears in the top navigation bar , It can be done through the menu docker Configuration and exit operations .

https://user-gold-cdn.xitu.io/2019/4/9/16a02cdabcd61dd8?imageView2/0/w/1280/h/960/format/webp/ignore-error/1

Official Guide :docs.docker.com/install/  Alibaba cloud guide (Linux):yq.aliyun.com/articles/11…

  • Set up acceleration services

There are many acceleration service providers on the market , Such as :DaoCloud, Alicloud, etc . here , I use Alibaba cloud .( Pay attention to is , The author's operating system is  Mac, For other operation series, please refer to the alicloud operation document )

https://user-gold-cdn.xitu.io/2019/4/9/16a02cdb64ac1921?imageView2/0/w/1280/h/960/format/webp/ignore-error/1

Right click... On the top bar of the desktop docker Icon , choice Preferences , stay Daemon label (Docker 17.03 The previous version was Advanced label ) Under the Registry mirrors In the list https://xxx.mirror.aliyuncs.com  Add to "registry-mirrors" In the array of , Click on Apply & Restart Button , wait for Docker Restart and apply the configured image Accelerator .

https://user-gold-cdn.xitu.io/2019/4/9/16a02cdb68211a09?imageView2/0/w/1280/h/960/format/webp/ignore-error/1

Alicloud operation document :cr.console.aliyun.com/cn-hangzhou…

  • View version

thus , We've installed it . here , Let's look at the version .

docker version
 Copy code 

View results , As shown below .

https://user-gold-cdn.xitu.io/2019/4/9/16a02cdba08c75e1?imageView2/0/w/1280/h/960/format/webp/ignore-error/1

2. The doers , From build Web The server starts

We as doers , So let's build a Web Server . then , I will take you to understand this process , What did you do . First , We need to pull centos Mirror image .

docker run -p 80 --name web -i -t centos /bin/bash
 Copy code 

Then , We installed nginx The server , Execute the following command :

rpm -ivh <http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm>
 Copy code 

installed Nginx After source , You can install it Nginx 了 .

yum install -y nginx
 Copy code 

thus , Let's type in  whereis nginx  Command to see the installation path . Last , We also need to Nginx Run .

nginx
 Copy code 

Now? , We execute  ctrl + P +  Q  Switch to the background . then , adopt  docker ps -a  To see the randomly assigned ports .

https://user-gold-cdn.xitu.io/2019/4/9/16a02cdba0915005?imageView2/0/w/1280/h/960/format/webp/ignore-error/1

here , The port assigned by the author is  32769 , So access it through a browser  http://127.0.0.1:32769  that will do .

https://user-gold-cdn.xitu.io/2019/4/9/16a02cdba5b85ed0?imageView2/0/w/1280/h/960/format/webp/ignore-error/1

Be accomplished , Ha ha ha ~

3. Understand the whole process

Now? , Let's understand the process . First , We type in  docker run -p 80 --name web -i -t centos /bin/bash  The command runs the interactive container , among  -i  Options tell Docker The container keeps the standard input stream open to the container , Even if the container has no terminal connection , the other one  -t  Options tell Docker Assign a virtual terminal to the container , So that we can install Nginx The server .( The author's remarks :Docker Input is also supported  -d  Options tell Docker Running the container's daemons in the background )

Docker A random name will be automatically generated for each container we create . in fact , It's convenient , But the readability is poor , And the cost of understanding our later maintenance will be relatively large . therefore , We go through  --name web  Options tell Docker Create a name that is  web  The container of . Besides , We go through  -p 80  tell Docker to open up 80 port , that , Nginx Only by visiting and serving the outside world . however , Our host machine will do port mapping automatically , For example, the port assigned above is  32769 , Pay attention to is , If you shut down or restart , This port has changed , So how to solve the problem of fixed port , The author will analyze in detail and take you to the actual combat later .

here , There is also a very important knowledge point  docker run .Docker adopt run Command to start a new container .Docker First, look for the image in the local machine , If not installed ,Docker stay Docker Hub Find the image on the computer and download it , Last Docker Create a new container and start the program .

https://user-gold-cdn.xitu.io/2019/4/9/16a02cdba66ba246?imageView2/0/w/1280/h/960/format/webp/ignore-error/1

however , When the second execution   docker run  when , because  Docker The image is already installed on this machine , therefore Docker A new container will be created directly and the program will be started .

[data:image/svg+xml;utf8,<?xml version="1.0"?><svg xmlns="http://www.w3.org/2000/svg" version="1.1" width="579" height="360"></svg>](data:image/svg+xml;utf8,<?xml version="1.0"?><svg xmlns="http://www.w3.org/2000/svg" version="1.1" width="579" height="360"></svg>)

Pay attention to is ,docker run  Each use creates a new container , therefore , When we start this container again later , Just use the command  docker start  that will do . here , docker start  To restart an existing image , and docker run  Contains putting the image into the container  docker create , Then start the container  docker start , As shown in the figure .

[data:image/svg+xml;utf8,<?xml version="1.0"?><svg xmlns="http://www.w3.org/2000/svg" version="1.1" width="929" height="282"></svg>](data:image/svg+xml;utf8,<?xml version="1.0"?><svg xmlns="http://www.w3.org/2000/svg" version="1.1" width="929" height="282"></svg>)

Now? , We can build on the above case , adopt  exit  Order to shut down Docker Containers . Of course , If we are running the daemons in the background , We can also pass  docker stop web  To stop . Pay attention to is ,docker stop  and  docker kill  It's a little different ,docker stop  send out SIGTERM The signal , and  docker kill  send out SIGKILL The signal . then , We use  docker start  Restart it .

docker start web
 Copy code 

Docker The container will be used after restart  docker run  Command to run , however , At this point, it's still running in the background . We have to pass  docker attach  Command to run the interactive container .

docker attach web
 Copy code 

4. More than that , There are more orders

Docker Provides a very rich command . A picture is worth a thousand words , We can learn a lot of information and their previous uses from the pictures below .( You can just skip reading , Recommended collection , Easy to expand reading )

https://user-gold-cdn.xitu.io/2019/4/9/16a02cdbf14142a0?imageView2/0/w/1280/h/960/format/webp/ignore-error/1

If you want more information , You can read the official use documents .

Untitled

Official reading Links :docs.docker.com/engine/refe…

5. Advanced : Simplification of warehouse and software installation

I still remember what I introduced at the beginning of the article 「 Mirror image 、 Containers and warehouses 」 Do you ?Docker Our warehouse is used to store images . We can download images from the central warehouse , It can also be downloaded from the self built warehouse . meanwhile , We can push the finished image from local to remote warehouse .

First , The author first introduces a knowledge point :Docker Its image is its file system , One image can be placed on top of another , So the lower level is its parent image . therefore ,Docker There will be many mirror layers , Each mirror layer is read-only , And will not change . When we create a new container ,Docker Will build a stack of mirrors , And add a read-write layer at the top of the stack , As shown in the figure .

[data:image/svg+xml;utf8,<?xml version="1.0"?><svg xmlns="http://www.w3.org/2000/svg" version="1.1" width="610" height="862"></svg>](data:image/svg+xml;utf8,<?xml version="1.0"?><svg xmlns="http://www.w3.org/2000/svg" version="1.1" width="610" height="862"></svg>)

Now? , We can go through  docker images  Command to view the local image .

docker images
 Copy code 

Query results , As shown in the figure .

[data:image/svg+xml;utf8,<?xml version="1.0"?><svg xmlns="http://www.w3.org/2000/svg" version="1.1" width="641" height="71"></svg>](data:image/svg+xml;utf8,<?xml version="1.0"?><svg xmlns="http://www.w3.org/2000/svg" version="1.1" width="641" height="71"></svg>)

here , Explain the meaning of some nouns .

  • REPOSITORY: Warehouse name .
  • TAG: Image label , among lastest It means the latest version of . Pay attention to is , A single image can have multiple tags , Then we can manage useful version and function tags through tags .
  • IMAGE ID : The mirror image is unique ID.
  • CREATED : Creation time .
  • SIZE : Image size .

that , If the first time we pass  docker pull centos:latest  Pull the mirror image , So when we execute  docker run -p 80 --name web -i -t centos /bin/bash  when , It's not going to get it remotely anymore , Because the image is already installed on this machine , therefore Docker A new container will be created directly and the program will be started .

in fact , It's officially available for installation Nginx Mirror image , We can use it directly . Now? , We're building a new one by pulling the mirror Web The server . First , We go through  docker search  To find the image . We get Nginx Image list of .

docker search nginx
 Copy code 

Add up , We can also visit Docker Hub (hub.docker.com/) Search warehouse , that star More , The more reliable it is , Safe to use .

[data:image/svg+xml;utf8,<?xml version="1.0"?><svg xmlns="http://www.w3.org/2000/svg" version="1.1" width="1280" height="575"></svg>](data:image/svg+xml;utf8,<?xml version="1.0"?><svg xmlns="http://www.w3.org/2000/svg" version="1.1" width="1280" height="575"></svg>)

Now? , We go through  docker pull nginx  Pull the latest  Nginx Mirror image . Of course , We can also pass  docker pull nginx:latest  To operate .

docker pull nginx
 Copy code 

then , We create and run a container . The difference from the front is , We go through  -d  Options tell Docker Running the container's daemons in the background . also , adopt  8080:80  tell Docker 8080 Ports are open to the outside world ,80 Port the open port is mapped to the port number in the container .

docker run -p 8080:80 -d --name nginx nginx
 Copy code 

We'll pass it again  docker ps -a  Check it out. , Found that the container has been running in the background , And it was executed in the background nginx command , And open to the outside world 8080 port .

[data:image/svg+xml;utf8,<?xml version="1.0"?><svg xmlns="http://www.w3.org/2000/svg" version="1.1" width="958" height="80"></svg>](data:image/svg+xml;utf8,<?xml version="1.0"?><svg xmlns="http://www.w3.org/2000/svg" version="1.1" width="958" height="80"></svg>)

therefore , Access... Through a browser  http://127.0.0.1:8080  that will do .

[data:image/svg+xml;utf8,<?xml version="1.0"?><svg xmlns="http://www.w3.org/2000/svg" version="1.1" width="747" height="214"></svg>](data:image/svg+xml;utf8,<?xml version="1.0"?><svg xmlns="http://www.w3.org/2000/svg" version="1.1" width="747" height="214"></svg>)

6. Other options , Use an alternate registration server

Docker Hub Not the only source of software , We can also switch to other domestic alternative registration servers , For example, Alibaba cloud . We can log in  cr.console.aliyun.com  Search for , And pull open images .

[data:image/svg+xml;utf8,<?xml version="1.0"?><svg xmlns="http://www.w3.org/2000/svg" version="1.1" width="1280" height="559"></svg>](data:image/svg+xml;utf8,<?xml version="1.0"?><svg xmlns="http://www.w3.org/2000/svg" version="1.1" width="1280" height="559"></svg>)

[data:image/svg+xml;utf8,<?xml version="1.0"?><svg xmlns="http://www.w3.org/2000/svg" version="1.1" width="1280" height="579"></svg>](data:image/svg+xml;utf8,<?xml version="1.0"?><svg xmlns="http://www.w3.org/2000/svg" version="1.1" width="1280" height="579"></svg>)

Now? , We type in  docker pull  Command to pull .

docker pull registry.cn-hangzhou.aliyuncs.com/qp_oraclejava/orackejava:8u172_DCEVM_HOTSWAPAGEN_JCE
 Copy code 

here , I continue to add a knowledge point : The address of the registration server . in fact , There is a set of specifications for the address of the registration server . The complete format is :[ Warehouse host /][ user name /] Container short name [: label ]. here , The warehouse host is  registry.cn-hangzhou.aliyuncs.com, User name is  qp_oraclejava, The short name of the container is  orackejava, The tag name is 8u172_DCEVM_HOTSWAPAGEN_JCE. in fact , We go up through  docker pull centos:latest  Pull the mirror image , amount to  docker pull registry.hub.docker.com/centos:latest .

3、 ... and 、 Build my image

Learn from the above , I believe you have been right Docker I have a general understanding of the use of , It's like we go through VMware A system has been installed , And let it run , So we can be in this Linux System (CentOS perhaps Ubuntu ) Anything we want to work on it . in fact , We'll always install our VMware System snapshot backup and clone to meet our next rapid replication . here ,Docker You can also build custom content Docker Mirror image , For example, above we use the official installation Nginx Of Docker Mirror image . Pay attention to is , We use the existing basic image , It's just a way to build a new image by adding an image layer to it .

To sum up ,Docker Provides the ability to customize images , It allows us to save changes to the underlying image , And use... Again . that , We can put the operating system 、 Running environment 、 Scripts and programs are packaged together , And provide services on the host .

Docker There are two ways to build an image , One way is to use  docker commit  command , Another way to use  docker build  Command and  Dockerfile  file . among , It is not recommended to use  docker commit  Command to build , Because it doesn't standardize the whole process , therefore , It is more recommended to use in the enterprise  docker build  Command and  Dockerfile  File to build our image . We use Dockerfile  Files make building images more repeatable , At the same time, ensure the standardization of startup script and running program .

1. Build the first Dockerfile file

Now? , We continue to fight . here , We're going to build it from the beginning Web The server builds a mirror . First , We need to create an empty Dokcerfile file .

mkdir dockerfile_test
cd dockerfile_test/
touch Dockerfile
nano Dockerfile
 Copy code 

Then , We need to write a Dockerfile file , The code list is as follows

FROM centos:7
MAINTAINER LiangGzone "lianggzone@163.com"
RUN rpm -ivh <http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm>
RUN yum install -y nginx
EXPOSE 80
 Copy code 

Last , We go through  docker build  Command to build .

docker build -t="lianggzone/nginx_demo:v1" .
 Copy code 

Now? , Let's go through  docker images  Take a look at our new image .

https://user-gold-cdn.xitu.io/2019/4/9/16a02cdc42a0f63c?imageView2/0/w/1280/h/960/format/webp/ignore-error/1

2. understand Dockerfile Whole process

wow , We write a Dockerfile The file successfully builds a new image . The process is incredibly simple . Now? , Let's understand the whole process . First , FROM centos:7  yes Dockerfile The first step that has to be taken , It will run a container from an existing image , let me put it another way ,Docker You need to rely on a base image to build . here , We specify centos As a basic mirror , Its version is 7 (CentOS 7). then , We go through  MAINTAINER LiangGzone "lianggzone@163.com"  Specifies that the author of the image is  LiangGzone, Email is  lianggzone@163.com. This helps to tell the user its author and contact information . next , We perform two RUN Order to proceed Nginx Download and install , Finally through   EXPOSE 80  expose Dokcer Container of 80 port . Pay attention to is ,Docker The order of execution is from top to bottom , So we need to know the execution sequence of the whole process . besides ,Docker After each instruction is executed, a new mirror layer is created and submitted .

We use   docker build  Command to build , Appoint  - t  tell Docker The name and version of the image . Pay attention to is , If no label is specified ,Docker It will automatically set a lastest label . And a little bit more , We have one last  .  It's to make Docker Go to the current local directory to find Dockerfile file . Pay attention to is ,Docker At each step of the build, the result is submitted as a mirror , Then think of the previous mirror layer as a cache , So when we rebuild a similar image layer, we will directly reuse the previous image . If we need to skip , have access to  --no-cache  Options tell Docker No caching .

3. Dockerfile Instruction,

Dockerfile It provides a lot of instructions . I've compiled a list here , It is suggested to collect and check .

https://user-gold-cdn.xitu.io/2019/4/9/16a02cdc42dd43f0?imageView2/0/w/1280/h/960/format/webp/ignore-error/1

Official address :docs.docker.com/engine/refe…

Command identification one :RUN、CMD、ENTRYPOINT

RUN 、 CMD 、 ENTRYPOINT  The purpose of the three instructions is very familiar , Difference is that ,RUN  An instruction is a command that runs when the container is built , and CMD 、 ENTRYPOINT  Is executed when the container is started shell command , and  RUN  Will be  docker run  Command override , however   ENTRYPOINT  Will not be covered . in fact ,docker run  Any parameters specified by the command are passed back to as parameters  ENTRYPOINT  Instructions .CMD 、 ENTRYPOINT  Two instructions can also be used together . for example , We have access to  ENTRYPOINT  Of exec Form to set fixed default commands and parameters , Then use any form of  CMD  To set other default values that may change .

FROM ubuntu
ENTRYPOINT ["top", "-b"]
CMD ["-c"]
 Copy code 

Command discrimination two :ADD、COPY

ADD 、 COPY  The instructions are used the same way , The only difference is  ADD  Support for filing files (tar, gzip, bzip2, etc) Do extraction and decompression operations . Pay attention to is ,COPY  The directory to be copied must be in Dockerfile Under the same level directory of the file .

4. Push the image to the remote warehouse

Remote warehouse :Docker Hub

After the image is built , We can upload it to Docker Hub above . First , We need to pass  docker login  Make sure we're logged in . Then , We use  docker push  Command to push .

docker push lianggzone/nginx_demo:v1
 Copy code 

here , Let's see how it works , The format is  docker push [OPTIONS] NAME[:TAG] , among , I set up NAME yes  lianggzone/nginx_demo,TAG yes v1. ( The authors note : push Docker Hub slowly , Wait patiently ) Last , Visit after upload :hub.docker.com/u/lianggzon…/, As shown in the figure .

https://user-gold-cdn.xitu.io/2019/4/9/16a02cdc42cae7e8?imageView2/0/w/1280/h/960/format/webp/ignore-error/1

Remote warehouse : Alibaba cloud

meanwhile , We can also use domestic warehouses , For example, Alibaba cloud . First , Enter the access credentials in the terminal , Sign in Registry example . If you don't know which , You can visit  cr.console.aliyun.com/cn-hangzhou….

docker login --username= Account number  registry.cn-hangzhou.aliyuncs.com
 Copy code 

Now? , Push the image to alicloud image warehouse . among , docker tag [IMAGE_ID] registry.cn-hangzhou.aliyuncs.com/[ Namespace ]/[ Image name ]:[ edition ]  and  docker push registry.cn-hangzhou.aliyuncs.com/[ Namespace ]/[ Image name ]:[ edition ]  The command is used as follows .

docker tag 794c07361565 registry.cn-hangzhou.aliyuncs.com/lianggzone/nginx_demo:v1
docker push registry.cn-hangzhou.aliyuncs.com/lianggzone/nginx_demo:v1
 Copy code 

Last , Visit after upload :cr.console.aliyun.com/cn-hangzhou…, As shown in the figure .

https://user-gold-cdn.xitu.io/2019/4/9/16a02cdc42f17db4?imageView2/0/w/1280/h/960/format/webp/ignore-error/1

5. Dockerfile Of Github Source code address

here , I have attached the Dockerfile The warehouse of . Back , I will update some commonly used files one after another , welcome star Focus on .

github.com/lianggzone/…

attach : Reference material

( End , Please indicate the author and source of reprint .)

Scroll to Top