Talking about containerization Technology (docker usage)

Yang Linwei 2022-01-26 19:49:19 阅读数:795

talking containerization technology docker usage

01 introduction

In front of the blog , About containerization Technology , I wrote a related blog , Interested students can refer to the following :

This article is mainly about docker Use , From installation to command set .

02 docker install

stay 《 Talk about containerization Technology (docker Principles )》 First we need to know ,docker Not a general container tool , It depends on existing and running Linux Kernel environment .

therefore , install docker Must be installed first Linux!

2.1 Windows Lower installation docker

If you want to in Windows install docker, Then we have to start in Windows Install virtual machine , Then install the virtual machine Linux Environmental Science , Then install in the virtual machine Linux Environment re installation Docker, This process is very troublesome . not so bad docker The official website provides us with docker Of Windows The desktop version , It simplifies our installation operation , Specific download address :https://hub.docker.com/editions/community/docker-ce-desktop-windows
 Insert picture description here

It will not be detailed here Windows Lower installation docker 了 , Click next all the time to install successfully !

2.2 Mac Lower installation docker

And Windows It's installed in the same way , We can also do that docker Download and install the installation package on the official website docker, Official website address :https://docs.docker.com/desktop/mac/install/
 Insert picture description here
Of course , You can also use homebrew Command to install :

brew install --cask --appdir=/Applications docker

2.3 Linux Lower installation docker

You can use the official installation script to automatically install , The installation command is as follows :

curl -fsSL https://get.docker.com | bash -s docker --mirror Aliyun

You can also use domestic daocloud One click Install command :

curl -sSL https://get.daocloud.io/docker | sh

Of course, there are manual installation methods , This article will not elaborate on , To baidu .

2.4 Mirror to accelerate

From home DockerHub Pulling images can sometimes be slow , Image accelerator can be configured , The mainstream configuration is as follows :

Here are the configuration methods under different operating systems , The script code to check whether the configuration is successful is as follows , If you can see that the image address is changed to your own configuration , The configuration is successful :

$ docker info
Registry Mirrors:
https://reg-mirror.qiniu.com

2.4.1 Linux Configuration mode under

1. stay /etc/docker/daemon.json Write the following in ( If the file does not exist, please create a new one ):

{
"registry-mirrors":["https://reg-mirror.qiniu.com/"]}

2. Restart the service after configuration :

$ sudo systemctl daemon-reload
$ sudo systemctl restart docker

2.4.2 Windows Configuration mode under

open docker Desktop software , Open Settings Settings, Then configure Registrymirrors Fill in the accelerator address in the column :
 Insert picture description here

2.4.3 Mac Configuration mode under

Follow Windows The configuration is similar to , Click in the taskbar Docker for mac Application icon -> Perferences...-> Daemon-> Registrymirrors, Fill in the list with the accelerator address :
 Insert picture description here

03 docker Relevant concepts

docker Before practice , You need to be familiar with the following concepts :

Concept english explain
Machine Docker Machine A simplification Docker Command line tools installed , It can be installed on the corresponding platform through a simple command line Docker
host Docker Host A physical or virtual machine is used to execute Docker Daemons and containers .
client Docker Client Docker The client uses the command line or other tools Docker SDK(https://docs.docker.com/develop/sdk/) And Docker Of the daemons .
Warehouse Repository Docker The repository is used to hold images , It can be understood as a code repository in code control .
Mirror image Image Docker Images are used to create Docker Templates for containers , such as Ubuntu System .
Containers Container A container is an application or group of applications that run independently , It's the entity of the mirror runtime .

 Insert picture description here

Tips : You can think of mirror images as java Class class Templates , The container is treated as an instance object new Object()

04 docker command

4.1 docker Command template

I sorted out docker Command template , Let's start with an example docker command :

docker run -i -t ubuntu:15.10 /bin/bash

You can see that the composition format of the command is as follows , Now let's explain the contents of each component :

Format docker( Declare as docker command )+ operation (run) + Parameter modifiers (-i -t) + Action object ubuntu:15.10)+ The contents of the object (/bin/bash

4.1 docker operation

docker The command is followed by run operation , There are also the following methods , It is arranged as follows :

4.1.1 Mirror related

operation analysis Example command
images Lists the images on the localhost docker images
rmi delete mirror docker rmi hello-world
pull Download mirroring docker pull ubuntu:13.10
run Start a container docker run -it ubuntu /bin/bash
search Find image docker search httpd
commit Update image docker commit -m=“has update” -a=“runoob” e218edb10161 runoob/ubuntu:v2
build Build a mirror image docker build -t runoob/centos:6.7
tag Set image label docker tag 860c279d2fec runoob/centos:dev

4.1.2 Container related

By command docker command --help Learn more about the designated docker Command usage .

operation analysis Example command
command Understand the specified Docker Command usage docker command --help
ps List of containers docker ps -a
start Start a stopped container docker start b750bbbcfd88
restart Restart container docker restart b750bbbcfd88
stop Stop container docker stop b750bbbcfd88
exec、attach Into the container , Recommended exec docker exec -it 243c32535da7 /bin/bash
export Export container docker export 1e560fca3906 > ubuntu.tar
import Import container snapshot Snapshot file ubuntu.tar Import to image test/ubuntu:v1:cat docker/ubuntu.tar | docker import - test/ubuntu:v1
rm Delete container docker rm -f 1e560fca3906
update Update container Set up abebf7571666 Container restart strategy :docker update --restart=always abebf7571666
logs View container log docker logs -f bf08b7f2cd89
stats Monitoring container docker stats —no-stream
port Check the binding of the port docker port adoring_stonebraker
network New network docker network create -d bridge test-net
network New network docker network create -d bridge test-net

4.1.3 Warehouse related

operation analysis Example command
login Sign in docker login
logout sign out docker logout
push Push your image to Docker Hub docker push username/ubuntu:18.04

4.2 docker Modifier

Modifier analysis Example command
-t Specify a pseudo terminal or terminal in the new container docker run -i -t ubuntu:15.10 /bin/bash
-i Allows you to enter standard input into the container (STDIN) Interact docker run -i -t ubuntu:15.10 /bin/bash
-d Background operation mode docker run -itd --name ubuntu-test ubuntu /bin/bash
-f Foreground operation mode docker logs -f bf08b7f2cd89
-f Foreground operation mode docker logs -f bf08b7f2cd89
-e Set the environment variable docker run -itd --name mysql-test -p 3306:3306 -e MYSQL_ROOT_PASSWORD=123456 mysql
-P Randomly map the network ports used in the container to the hosts we use docker run -d -P training/webapp python app.py
-a View all docker ps -a
Combination order , Such as -itd Interactive 、 terminal 、 Background operation docker run -itd --name ubuntu-test ubuntu /bin/bash

4.3 docker Action object

Action object analysis Example command
Mirror image The image pulled from the warehouse images docker pull ubuntu:13.10
Containers Containers created based on images docker run -it ubuntu /bin/bash

4.4 docker Object content

Object content analysis Example command
Execute commands in the container Commands executed in the boot container docker run ubuntu:15.10 /bin/echo “Hello world”
Container directory Enter the container root directory docker exec -it 243c32535da7 /bin/bash
environment variable Set the environment variable docker run -itd --name mysql-test -p 3306:3306 -e MYSQL_ROOT_PASSWORD=123456 mysql
Operation of other contents Operation of other contents nothing

05 summary

This paper starts from docker Start with installation , Let's talk about docker The concept of 、 Instructions , This knowledge should be enough to get us started , If you want to go deep , We have to practice more , Practice can test knowledge .

This article is through reading 《 Novice tutorial 》 Organized notes , There may be a mistake , Welcome to leave a message to point out and pay attention to . The end of this paper !

copyright:author[Yang Linwei],Please bring the original link to reprint, thank you. https://en.javamana.com/2022/01/202201261949160451.html