Introduction and installation of docker in Docker series

Table of contents

1. Three services of cloud computing

1.LaaS (Infrastructure as a Service)

2.PaaS (Platform as a Service, middleware is ready)

3.SaaS (Software as a Service)

2. Basic management of Docker

1.Introduction and concepts of Docker

    ①What is Docker?

    ②The difference between Docker containers and virtual machines

    ③Usage scenarios of Docker 

3. The core concepts and installation methods of Docker

 1.Docker core concepts

     ①Image: a read-only template for the docker container engine

     ②Container: A running instance created from an image

     ③Repository: a place where images are stored centrally

 2. Advantages of containers

4. Install and deploy Docker containers

 1. First turn off the firewall

 2. Install dependency packages

 3. Set up Alibaba Cloud image source

 4. Install Docker-CE (starts automatically after booting)

 5. Check docker version information 

5. Docker container image operation 

1. Search for images

2. Get the image

3. Mirror acceleration 

4.Basic commands for Docker images

①Mirror log file location

②View the downloaded image file information

③View all images downloaded locally 

④ Obtain detailed image information based on the unique identification ID number of the image.

⑤Add a new label to the local image

⑥Delete image

⑦Save image: Save the image as a local file

​⑧Load image: Import the image file into the image library

⑨Upload image

5. Network optimization

6. Docker container operation

1. Create a container

2. Check the running status of the container 

3. Start the container 

4. Run the startup container 

5. View running containers 

6. Enter inside the container 

7. Delete the container (before deleting the container, you need to stop the container first) 

8. Start the container

9.Exit the container

10. Container import and export


1. Three services of cloud computing

1.LaaS (Infrastructure as a Service)

        Infrastructure-as-a-Service (Infrastructure as a Service), sometimes also called Hardware-as-a-Service. In the past, if you wanted to run some enterprise applications in the office or company website, you needed to buy a server. , or other expensive hardware to control local applications and allow enterprise business to run.
        But now with IaaS, companies can outsource their hardware to other places. laas. The company will provide off-site servers, storage and network hardware for rent. Saving maintenance costs and office space, companies can leverage the hardware to run their applications at any time.

2.PaaS (Platform as a Service, middleware is ready)

        PaaS: Platform-as-a-Service (Platform as a Service), sometimes also called middleware. All the company's development can be carried out on this layer, saving time and resources.
        PaaS companies provide various solutions for developing and distributing applications online, such as virtual servers and operating systems. This saves you money on paperwork and makes collaboration between dispersed studios easier. Web application management, application design, application virtual machine, storage, security and application development collaboration tools, etc.

3.SaaS (Software as a Service)

        The SaaS layer is the layer that comes into contact with daily life, and is mostly accessed through a web browser. Any application on a remote server can be run over the network, which is SaaS.
        The services you consume are completely entered into these categories from web pages such as Netfix, MOG, GoogleApps, Dropbox or Apple's iCloud. Although these web services are used for business, entertainment or both, they are also part of cloud technology. Domestic next-second data Nexadata can quickly access new data (especially SaaS systems), accurately accumulate detailed data, form accurate master data, and quickly modify business changes.

2. Basic management of Docker

1.DockerIntroduction and concepts

    ①What is Docker?

             a. It is a lightweight “virtual machine”

             b. Open source tools for running applications in Linux containers

             ​ ​ ​ ​ c.Docker’s logo is designed as a blue whale, dragging many containers. The whale can be seen as a host machine and a container.

                It can be understood as containers that are isolated from each other, and each container contains its own application.

               Docker has aimed to provide a standardized runtime environment from the beginning, and has truly achieved "Build, Ship and Run any APP, Angwhere", and can use the same build version for development, testing, pre-release, production, etc. environment, and achieves decoupling from the underlying operating system. On this basis, Caas was further developed

               (Containers as a Service) technology.

    ②The difference between Docker containers and virtual machines

Options virtual machine docker container
Start Time slow Fast (millisecond startup)
capacity big Small
system logical isolation Depends on the kernel
safety powerful

weak


    ③Usage scenarios of Docker 

          Packaged applications simplify deployment

          It can be migrated arbitrarily without the underlying hardware (for example: the server is migrated from Tencent Cloud to Alibaba Cloud)

3. The core concepts and installation methods of Docker

 1.Docker core concepts

     ①Image: a read-only template for the docker container engine

          Contains various environments and services (tomcat, mysql, redis, etc.), which is equivalent to a template

          The image is the basis for running the container and contains everything needed to run the program.

     ②Container: A running instance created from an image

          The object container is the instance after the image is run. The container can be regarded as a simple Linux environment container.

     ③Repository: a place where images are stored centrally

         A warehouse is a place where images are stored. The largest public warehouse is the Docker Hub private warehouse harbor

Supplement: Two ways to install Docker on CentOS

              a. Use CURL to obtain the Docker installation script for installation

              ​ ​ ​ ​ b. Use the YUM repository to install Docker

 2. Advantages of containers

      Flexible: Even complex applications can be containerized
      Lightweight: Containers leverage and share the host kernel.
      Interchangeable: updates and upgrades can be deployed on the fly
      Portable: can be built locally, deployed to the cloud, and run anywhere< a i=4> Scalable: container replicas can be added and automatically distributed       Stackable: services can be stacked vertically and instantly

4. Install and deploy Docker containers

 1. First turn off the firewall

systemctl stop firewalld.service
systemctl disable firewalld.service
setenforce 0

 2. Install dependency packages

yum install -y yum-utils device-mapper-persistent-data lvm2

3. Set up Alibaba Cloud image source

yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo 

 4. Install Docker-CE (starts automatically after booting)

yum install -y docker-ce
systemctl start docker
systemctl enable docker

 5. Check docker version information 

docker version

5. Docker container image operation 

1. Search for images

Format: docker search keyword

2. Get the image

Format: docker pull warehouse name[:label]
#If you do not specify a label when downloading the image, the latest version of the image in the warehouse will be downloaded by default, that is, the label is selected as the latest label .

3. Mirror acceleration 

Browser access Alibaba Cloud login - Welcome to Alibaba Cloud, a safe and stable cloud computing service platform Get image accelerator configuration

(Ali container mirror image acceleration)

mkdir -p /etc/docker
tee /etc/docker/daemon.json <<-'EOF'
{
  "registry-mirrors": ["https://1fam3t0i.mirror.aliyuncs.com"]
}
EOF
systemctl daemon-reload
systemctl restart docker

After doing this step, the download image will not be stuck, but the speed will take off immediately! Huh~huh~huh~

4.Basic commands for Docker images

①Mirror log file location

After the image is downloaded, the log is stored in /var/lib/docker

②View the downloaded image file information

cat /var/lib/docker/image/overlay2/repositories.json

③View all images downloaded locally 

docker images
 
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest ae2feff98a0c 9 days ago 133MB
--- -------------------------------------------------- ---------------------------------------
REPOSITORY: The warehouse to which the image belongs;
TAG: The tag information of the image, marking different images in the same warehouse;
IMAGE ID: The unique ID number of the image, a unique identification An image;
CREATED: Image creation time;
VIRTUAL SIZE: Image size;
-------- -------------------------------------------------- --------------------------

④ Obtain detailed image information based on the unique identification ID number of the image.

Format: docker inspect image ID number

⑤Add a new label to the local image

Format: docker tag name: [tag] new name: [new tag]

⑥Delete image

Format:
docker rmi warehouse name: label #When an image has multiple labels, just delete the specified label
or a>
docker rmi image ID number #The image will be completely deleted
 
Note: If the image is already used by a container, the correct approach is to first delete all containers that depend on the image. Then delete the image.

⑦Save image: Save the image as a local file

Format: docker save -o storage file name stored image

 ⑧Load image: Import the image file into the image library

Format:
docker load < saved file
or
docker load -i saved File

⑨Upload image

is uploaded to the docker Hub official public warehouse by default, and you need to register an account to use the public warehouse. https://hub.docker.com
You can use the docker login command to enter your username, password and email to complete registration and login.
Before uploading the image, you need to add a new tag to the local image, and then use the docker push command to upload it.
 

docker tag     #Add a new tag
docker login   #Log in to the public warehouse
Username:   #User
password : #Password
docker push #Upload image

5. Network optimization

vim /etc/sysctl.conf
#添加 net.ipv4.ip_forward=1

sysctl -p
service network restart
systemctl restart docker

 6. Docker container operation

1. Create a container

The newly created container is stopped by default and does not run any programs. You need to initiate a process in it to start the container.

The docker create command can create a container based on an image. (It is the process of loading the image into the container)

The effect of this command is similar to docker run -d, which creates a container that will run in the background of the system.

However, unlike docker run -d, the container created by docker create is not actually started. You also need to execute the docker start command or the docker run command to start the container.

In fact, the docker create command is often used to make necessary settings before starting a container.

Format: docker create [options] image
Common options:
-i: Keep the input of the container open
-t: Let Docker allocate a pseudo terminal

Note:

-it is equal to -i and -t. The function of these two parameters is to create a pseudo terminal for the docker, so that you can enter the interactive mode of the container (that is, directly enter the container)

The function of /bin/bash behind is to run bash after loading the container. Docker must keep a process running, otherwise the entire container will kill itself immediately after starting. This /bin/bash means starting after starting the container. bash


2. Check the running status of the container 

docker ps ## View running containers
docker ps -a View all containers

-a lists all containers, including those that are not running

3. Start the container 

You can directly execute the docker run command, which is equivalent to executing the docker create command first and then the docker start command.

Note: A container is a terminal that coexists with the shell command running in it. The command to run the container runs, and the command to end the container exits.

When using docker run to create a container, the standard running process of Docker in the background is:

(1) Check whether the specified image exists locally. When the image does not exist, it will be downloaded from the public repository;

(2) Use the image to create and start a container;

(3) Allocate a file system to the container and mount a read-write layer outside the read-only image layer;

(4) Bridge a virtual machine interface to the container from the bridge interface configured on the host host;

(5) Assign an IP address in an address pool to the container;

(6) Execute the application specified by the user. After the execution is completed, the container is terminated.

docker run centos:7 /usr/bin/bash -c ls /
docker ps -a #You will find that a new container is created and a shell command is started, and then Stopped

run command

From no image to generating a running container is summarized in three steps

1. Download the image
2. Create the container
3. Start the container

But how convenient is it in a production environment?
The run command will combine it with three in one
So the one I use more is run< /span>

docker run -d nginx /bin/bash

-d is daemon to start the daemon process. The container cannot be opened without adding -d here.

 #Simple operation
docker run image identification|image name[:tag]
#Commonly used parameters
docker run -d -p host port: container port --name container name image ID | image name [:tag]
Options:
-d: background running Container
-p: Host port: Container port: To map the current Linux port and the container port
--name container name: Specify the name of the container < /span>  -p: Randomly specify the port -P: Specify the container port
--it Run in interactive mode and enter the container to view the content

Example: docker run --name mynginx -p 80:80 nginx #Console running
    docker run -di --name mynginx -p 80:80 nginx #Background running< /span>

4. Run the startup container 

 #Simple operation
docker run image identification|image name[:tag]
#Commonly used parameters
docker run -d -p host port: container port --name container name image ID | image name [:tag]
Options:
-d: background running Container
-p: Host port: Container port: To map the current Linux port and the container port
--name container name: Specify the name of the container < /span>  -p: Randomly specify the port -P: Specify the container port
--it Run in interactive mode and enter the container to view the content

Example: docker run --name mynginx -p 80:80 nginx #Console running
    docker run -di --name mynginx -p 80:80 nginx #Background running< /span>

5. View running containers 

docker ps 
Options:
-f: Filter    
-a: View all containers, including none Running
-q: Only view the identity of the container
-n=?: Display the recently created container
-l: View the last created container

Example: docker ps -f status=exited #Filter containers that already exist but are stopped
    docker ps -n 5 #List the recently created n containers

6. Enter inside the container 

docker exec -it container id/container name /bin/bash        
docker attach container id

Difference:
docker exec: After entering the container, open a new terminal and you can operate in it (commonly used)
docker attach: Enter the container and it is The executing terminal will not start a new process

7. Delete the container (before deleting the container, you need to stop the container first) 

docker stop container id                                                                                                                                                                               #Stop the specified container

Docker RM container name/container ID #delete the specified container. Before deleting the container needs to be stopped Docker RM $ (docker ps -qa) # Container docker ps -a | awk '{print "docker rm "$1}'|bash #Delete all containers except the status up

8. Start the container

docker start container id/container name

9.Exit the container

EXIT#Direct container stops and exit
Ctrl+P+Q#container does not stop exiting 

10. Container import and export

#Export format: docker export container ID/name> file name
docker export 2592d3fad0fb > centos7.tar
 
#Import format: cat File name | docker import - Image name: label
cat centos7.tar | docker import - centos7:test #After importing, the image will be generated, but the container will not be created


 

Guess you like

Origin blog.csdn.net/weixin_52269501/article/details/130312298