This is the 20th day of my participation in the August Text Challenge.More challenges in August

๐ŸŒˆ Past review

Thank you for reading, I hope to help you, if there is a flaw in the blog, please leave a message in the comment area or add my private chat in the home page, thank you for every little partner generous advice. I’m XiaoLin, a boy who can both write bugs and sing rap

  • ๐Ÿ’–5 minutes with you to run Docker๐Ÿ’–Docker series of introductory tutorials
  • ๐Ÿ’– take 20 minutes per day to get you started on ElasticSearch 4๏ธ discount
  • ๐Ÿ’– take 20 minutes per day to get you started on ElasticSearch 3๏ธ discount

3. Initial experience of Docker

3.1 Operation mechanism of Docker

Docker is not a container, but just an engine to manage containers. The underlying operation principle of Docker is as follows: Docker service starts โ†’ download image โ†’ start the image to get a container โ†’ the container runs the program we want.

3.2. Run the Docker container

When we start the Docker container, we need to download an image. Before Docker runs a container, it needs to have the corresponding image locally. If the image does not exist locally, Docker will download it from the image repository (the default is the repository in the Docker Hub public registry server).

Search for the image to be used from the Docker Hub official website. You can also search for the image to be used in the command line, such as Docker Search tomcat, and then download the required image.

#Download the Tomncat image
docker pull tomcat

#To run an image, it is run foreground by default. If it is run background, it needs to add a parameter -d
docker run tomcat

#Check whether the Tomcat image successfully starts the container
ps -ef | grep tomcat 
Copy the code

We can use the command to view existing local images.

docker images
Copy the code

In the listing information, you can see several fields:

  1. REPOSITORY: Which REPOSITORY comes from.
  2. TAG: indicates the TAG of the mirror.
  3. IMAGE ID: indicates the IMAGE ID (unique).
  4. CREATED: CREATED time.
  5. SIZE: indicates the mirror SIZE.

3.3 Enter the Docker container

#The command formatDocker exec -it container id bash
#Out of the container
exit
Copy the code

Where I stands for interactive, that is, keeping the standard input stream open. T indicates virtual console, which is assigned to a virtual console

3.4. The client accesses the container

Port mapping is required to access the container from the client. Docker container communicates with the host by default in bridge mode, and the IP port of the host needs to be mapped to the IP port of the container.

#Start the containerDocker run -d -p Host port: container port Container name or container ID
#Stop the containerDocker stop Container ID/ nameCopy the code

4. Docker core components

4.1 Docker architecture

Docker uses a client-server (C/S) architecture pattern that uses remote apis to manage and create Docker containers.

Docker containers are created by Docker images. The relationship between images and containers is similar to that between classes and objects in object-oriented programming.

Docker object-oriented
The mirror class
The container object

4.2 Docker core elements

Docker consists of three core elements:

  1. Image
  2. Container
  3. Repository

4.3, mirror

4.3.1 Overview of mirroring

A Docker image is a read-only template that can be used to create Docker containers.

For example, an image can contain a full centos operating system environment with only mysql or other required applications installed. Docker provides a very simple mechanism to create images or update existing images, and users can even download a ready-made image from someone else to use directly.

4.3.2 Composition of mirror image

The bottom layer is a boot file system bootfs, and the second layer is a root file system rootfs. The root file system is usually an operating system of some kind, such as centos and Ubuntu, and there are many layers of file systems on top of the root file system. These file systems are stacked together to form the image in Docker.

4.3.3 Mirroring Operations

Download mirror

#Download a 1Redis image
docker pull redis:latest
Copy the code

Reids is the name of the queried mirror, and latest is the tag of the mirror. You can obtain an image in either of the following ways:

  1. One is downloaded from the official mirror repository.
  2. The other is to build it yourself from a Dockerfile file.

If there is an official image, we don’t need to build it ourselves in Dockerfile, unless there is no official image, we will build it ourselves in Dockerfile.

Lists the images that have been downloaded

#Docker images [OPTIONS] [REPOSITORY]
#-a: lists all mirrors. By default, mirror 1 of the middle layer is not displayed
#-q: only the unique ID 1 of the mirror is displayed
docker images
#Or is it
docker images redis
Copy the code

Run the mirror

#-d indicates that the system is running in the background
docker run -d redis
Copy the code

View the Redis process

 ps -ef | grep redis 
Copy the code

Check the container image status

docker ps
Copy the code

Into the container

Docker exec -itCopy the code

Remove the mirror

docker rmi redis:latest 
Copy the code

4.3.4. Build an Image

Building an image allows us to save changes to the container and use them again. Provides the ability to customize images, packaged as software, and method services and their runtime environment. Docker provides two ways to build an image:

  1. Docker commit to build the image from the container.
  2. Docker build mode, built by DockerFile file.

4.3.4.1. Build an Image in Commit mode

The syntax for building an image with commit is:

docker commit [OPTIONS] CONTAINER [REPOSITORY[:TAG]]
Copy the code

It has several parameters:

  1. -a: specifies the author of the mirror.
  2. -m: indicates the message submitted by the mirror.
  3. -p: By default, server 1 is paused when the image is built using commit.

Steps to build an image:

Create a container

 #Create a container with port 80 and name commit_test, and specify the base image as centos
 docker run -itd --name commit_test -p 80 centos /bin/bash
Copy the code

Install nginx inside the container

yum install -y epel-release
yum install -y nginx
Copy the code

Submit mirror

#Syntax: Docker commit -a'Author Information' -m 'Mirror name'Name of the image to build Name of the image to build: version
docker commit -a 'xiaolin' -m 'nginx_mirror' commit_test nginx_mirror:v1
Copy the code

To view

docker images
Copy the code

Start the container

 #The container ID should be replaced according to the actual situation
 docker run -itd --name nginx_test2 -p 80 36210c82236d
Copy the code

Into the container

#The container ID should be replaced according to the actual situation
docker exec -it e231f1dfea62 bash
Copy the code

4.3.4.2. Through the DockerFile file

More on this later

4.4, container

4.4.1 Overview of containers

A container is a running instance created from an image. It can be started, stopped, and deleted. Each container is an isolated, secure platform. Think of it as a simplified Version of the Linux environment, including root user permissions, process space, user space, and network space and the applications that run in them.

Docker uses containers to run applications, and the image is read-only.

4.4.2 Operation of containers

Start the container with an image

The command will check if the image to be run exists locally. If not, it will automatically pull down and run again. After running, a container will be started, and a file system will be started.

You can create a folder directly inside the container, run a specified program, and when the program exits, the container exits.

Run-time containers cannot be deleted, only after being stopped or forcibly deleted with the -f argument

#The syntax format is docker Run container name
docker run -d redis
Copy the code

Start the daemon container

# i --interactive=true|falseThe default isfalse: allows you to interact with standard input (STDIN) inside the container
# t --tty=true|falseThe default isfalse: Specifies a dummy terminal or terminal in the new containerDocker run -i -t Container name /bin/bash
#demonstration
docker run -i -t centos /bin/bash
Copy the code

Exit the daemon container

exit
Copy the code

View the running container

#-a Lists all containers
#-i lists the recently created containers
docker ps [-a] [-l]
Copy the code

View all containers

docker ps -a
Copy the code

View all container ids

docker ps -aq
Copy the code

Stop the container

Docker stop Container ID or container nameCopy the code

Restart the container

Docker start Container ID or container nameCopy the code

Remove the container

#When deleting a container, the container must be stopped; otherwise, an error will be reportedDocker RM Container ID or container nameCopy the code

Into the container

Docker exec -it container id or container name bashCopy the code

View more information about containers

Docker inspect Container ID or container nameCopy the code

Stop all running containers

docker stop $(docker ps -q)
Copy the code

Delete all containers

docker rm $(docker ps -aq)
Copy the code

Deactivate and delete containers

docker stop $(docker ps -q) & docker rm -f $(docker ps -aq)
Copy the code

4.4.3 Guardian container

A daemon container is a container that can run for a long time, and most of the containers we created were closed as soon as they were created. Advantages of a daemon container:

  1. Can run for a long time.
  2. Suitable for running applications and services.

Run the container as a daemon

Docker run -i -t Container name /bin/bash#Example (Create a container and specify the base image)
docker run -i -t centos /bin/bash
Copy the code

Exits but does not close the container

Ctrl + P and Ctrl + QCopy the code

Attach to a running container

Docker Attach Container nameCopy the code

Start the daemon container

Docker run -d specifies the image nameCopy the code

View container run logs

#-f Consistently tracks log changes and returns a result
#-t by defaultfalseAdd a timestamp to the returned result
#--tail = "all" returns the last few lines of log dataDocker logs [-f] [-t] [--tailCopy the code

View process 1 in the container

Docker top Container nameCopy the code

Stop the daemon process

#Sends a signal to the container and waits for the container to closeDocker stop container name#Direct stop containerDocker kill container nameCopy the code

4.5, warehouse

4.5.1 Overview of the warehouse

A repository is a place where image files are stored centrally, and sometimes the repository and Registry are considered the same thing and not strictly distinguished. In fact, the repository registry server often hosts multiple repositories, each containing multiple images, each with a different tag.

The warehouse is divided into two forms: Public warehouse and Private warehouse. The largest Public warehouse is Docker Hub, which stores a large number of images for users to download. The user can also create a private repository on the local network. After creating his own image, the user can upload it to the public or private repository using the push command, so that the next time the image is used on another machine, he only needs to pull it from the repository.

The concept of a Docker repository is similar to Git, and the registry server is similar to hosting services like GitHub.

4.5.2 Warehouse operation

Search the mirror

docker search redis
Copy the code

The concept of a Docker repository is similar to Git, and the registry server is similar to hosting services like GitHub. The parameters from left to right are mirror name, mirror description, number of mirror stars, official creation, and automatic creation.

The concept of a Docker repository is similar to Git, and the registry server is similar to hosting services like GitHub. An official image description is created and maintained by the official project team. The AUTOMATED resource allows users to verify the source and content of an image.

The concept of a Docker repository is similar to Git, and the registry server is similar to hosting services like GitHub. Image resources can be classified into two types according to whether they are officially provided or not:

  1. Basic images like Centos, which are created, validated, supported and provided by Docker. Such images tend to use single words as names.
  2. The other is similar to the Bitnami/Redis image, which is created by Docker user 1 and maintained by 1, often with a username prefix.

Download mirror

Docker pull redis # docker pull redis4.xCopy the code

The concept of a Docker repository is similar to Git, and the registry server is similar to hosting services like GitHub. We found that the download speed of the image was surprisingly slow, so we needed to configure Ali’s image accelerator to download it.

sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<-'EOF'
{
  "registry-mirrors": ["https://175i7vum.mirror.aliyuncs.com"]
}
EOF
sudo systemctl daemon-reload
sudo systemctl restart docker
Copy the code