Introduction and use of docker

                                  **Docker**

1. Introduction to docker
1. What is docker?
The most widely used open source container engine. An operating system-level virtualization technology that relies on Linux kernel features: Namespace (resource isolation) and Cgroups (resource restriction). A simple application packaging tool.

2. The design goals of docker

(1) Rapid delivery and deployment: Using docker, developers can use mirrors to quickly build a standard development environment; after development is completed, test and operation and maintenance personnel can directly use the same environment to deploy code. Docker can quickly create and delete containers, achieve rapid iteration, and save a lot of time in development, testing, and deployment. Moreover, each step has clear configuration and operation, and the entire process is visible, making it easier for the team to understand the application creation and working process.
(2) Efficient utilization of resources: The operation of docker containers does not require additional virtualization hypervisor support. It only requires kernel-level virtualization, which can achieve higher performance while requiring very little additional resources. Easy migration and expansion: can run on almost any platform, including physical machines, virtual machines, public clouds, private clouds, personal computers, etc. This compatibility allows users to easily migrate apps between different platforms.
(3) Simple update management: Using Dockerfile, you only need to modify the configuration to replace a large amount of update work. And all modifications are distributed and updated in an incremental manner, thereby achieving automated and efficient container management.

3. The composition of docker

Docker Client(client) The Docker client is the user interface of Docker, which can accept user commands and configuration identifiers, and communicate with the Docker daemon. In the picture, docker build, etc. are all related commands of Docker.
docker Daemon (daemon) Docker daemon is a background process running on the host (DOCKER-HOST). It can be communicated with via Docker client.
Docker Images A Docker image is a read-only template that contains instructions for creating a Docker container. It is somewhat similar to the system installation CD. You can use the system installation CD to install the system. In the same way, you can use the Docker image to run the programs in the Docker image.
Docker Container (container) A container is a runnable instance of an image. The relationship between images and containers is somewhat similar to the relationship between classes and objects in object-oriented technology. Containers can be started, stopped, moved, and deleted through Docker API or CLI commands.
Docker Registry (mirror warehouse) Docker Registry is a service that centrally stores and distributes images. After building the Docker image, it can be run on the current host. But if you want to run this image on other machines, you need to copy it manually. At this time, you can use Docker Registry to avoid manual copying of images. A Docker Registry can contain multiple Docker repositories, each repository can contain multiple image tags, and each tag corresponds to a Docker image.

Insert image description here

4. Comparison between docker containers and traditional virtualization

Insert image description here

Insert image description here

5. Docker application scenarios
(1), application packaging and publishing
(2), application isolation
(3), continuous integration
(4), deploying microservices
(5), quickly building a test environment
(6), and providing PaaS products (Platform as a Service)

6. The three core concepts of docker
(1), image (container installed in the image)
(2), container (various services)
(3) warehouse (image installed)

2. Docker installation (yum)

1. Linux installation dependent software
yum install -y yum-utils device-mapper-persistent-data lvm2
Insert image description here

2. Update docker source
yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
Insert image description here

3、Install docker
yum install -y docker-ce docker-ce-cli containerd.id
Insert image description here

4. Start docker and set it to auto-start
systemctl start docker #Start docker
systemctl enable docker #Set docker to auto-start at boot
Insert image description here

5. Add Docker domestic image
Docker official image warehouse network speed is poor, we need to set up domestic image service:
refer to Alibaba Cloud's image acceleration document: https://cr.console.aliyun.com/cn-hangzhou/instances/mirrors

sudo tee /etc/docker/daemon.json <<-‘EOF’
{
“registry-mirrors”: [“https://akchsmlh.mirror.aliyuncs.com”]
}
EOF

##Reload the file
sudo systemctl daemon-reload
##Restart docker
sudo systemctl restart docker

3. Mirror

1. Mirror: Generally speaking, a mirror is a read-only file that contains the environment and code necessary for the program to run. It uses a layered file system to add changes to each layer to the original read-only file in the form of a read-write layer. File-wise, this is a bit like an onion. Different containers can be created through the base image.

2. Image architecture
The bottom layer of the image is a boot file system (bootfs) image. The upper image of bootfs is called the root image. Generally speaking, the root image is an operating system, such as Ubuntu, CentOS, etc. The user's image must be built On top of the root image, users can build a variety of other images on top of the root image. The essence of an image is actually a collection of files. The layer-by-layer structure is somewhat similar to Git, and also somewhat similar to onions in life.

3. The write-copy mechanism of the image.
When you specify a container to create an image through the docker run command, an empty read-write file system level is actually created on top of the image. This file system level can be regarded as a temporary image. treated, and the template image referred to in the command can be called the parent image. The contents of the parent image are mounted in a read-only manner. The container will read the contents of the shared parent image. All modifications made by the user are in the file system and will not have any impact on the parent image. In short, an image is a fixed template file that does not change. The container is created based on this template. The container will make some modifications based on the template. These modifications themselves will not affect the template. We can also Create more containers based on templates (mirrors)

Insert image description here

3. Docker network mode
(1), bridge mode - net=bridge (default) specifies port mapping through -p.
(2) Host mode--net=host The container and the host share the Network namespace.
(3), none mode--net=none The container has an independent Network namespace
Insert image description here

4. Docker usage commands

1. Docker image management command
(1), docker command parameters

instruction illustrate
Build List images
Images View image
Search Search for images
pull Download image
rmi or rm Delete image
tag Tag the image
load Import an image package
save Export an image package

(2) Common docker commands

Search image docker search [image_name]

#Download the image docker pull [image_name]
#View the image docker images
#View the detailed information of the image docker inspect [image_id/image_name]
#Delete the image docker rmi [image_name]
#Tag the image docker tag [docker_id] [image_name]
#Export one Image docker save [image.tar.gz] [image_name]

2. Docker container usage commands

instruction illustrate
-i Interactive
-t Tty allocates a pseudo terminal
-d Detach runs the container into the background
-e -env sets environment variables
-p Publish list publishes the container port to the host
-name String specifies the container name
-h Hostname sets the container hostname
-ip String specifies the container IP, which can only be used for custom networks
-network Connect the container to a network
-mount Mount attaches a file system to a container
-v Volume list bind mounts a volume
-restart Restart policy when container exits

(1), docker run command
command description

(2) Commonly used commands are
#Download an nginx image docker pull nginx
#Run a container (frontend) docker run -p 80:80 nginx
#Run a container (backend) docker -d -p 80:80 nginx
#Delete container ( Need to stop before deleting) docker stop nginx &&docker rm nginx
#Specify the port and container name for the container docker run -d -p 82:80 –name “nginx123” nginx
#Enter the container after running docker run -it –name “nginx123” nginx /bin/bash
#List all containers: docker ps -a
#Export the container as an image Docker export [container_name] -o [container_name.tar]
#Export an image docker import [container_name.tar] [image_name:version]
#Clean up Stopped container: docker container prune -f

5. Docker storage
(1), data volume data volume
(2), data volume dontainers data volume container (general)
docekr provides three ways to mount data from the host to the container
volumes: docker manages the host file system Part of (/var/lib/docker/volumes) The best way to save data is
bind mounts: Mount files or directories anywhere on the host into the container.

Volume command
1. docker volume create name #Create volume
2. docker volume ls #View volume
3. Create a volume and mount it to the nginx container
docker run -d -it -name “nginx-01” --mount src= nginx-vol,dst=/usr/share/nginx/html/ -p 80:80 nginx
4. Features of volume
1) Sharing data between multiple running containers, multiple containers can mount the same volume at the same time
2) . When the container is stopped or removed, the modified volume still exists
3). The volume will be deleted only when the volume is explicitly deleted
4). Store the container data on the remote host or other storage (indirectly)
5). When migrating from one docker host to another, stop the container first, and then back up the volume directory. The path is (/var/lib/docker/volumes/)

bind mounts command
1. First create the directory that needs to be shared with the container. For example: mkdir -p /opt/mounts/
2. Run the container
docker -run -d -it -name “nginx-bind” -p 80:80 --mount type=bind,src=/opt/mounts,dst=/usr/share/nginx/html nginx
3. Features of bind mounts
1) Sharing configuration files from the host to the container, the host /etc/resolv is mounted by default .conf to each container to provide DNS resolution.
2) Share code between the development environment and the container on the docker host, and the container can directly share the code directory.
3) When the file or directory structure of the docker host is consistent with the binding and mounting required by the container.

6. Writing dockerfile
1. Introduction to dockerfile: Dockerfile is a text file used to build an image. Using the docker image build command in Docker will read the Dockerfile and containerize the application.

2. Basic syntax of dockerfile

instruction illustrate
FROM Basic image, everything starts from here
MAINTAINER Who wrote the mirror, name + email
RUN Commands that need to be run when building the image
ADD Steps, tomcat image, this tomcat decompression package!
WORKDIR Mirror's working directory
VOLUME Mounted directory
EXPOSE Exposed port configuration
CMD Specify the command to be run when this container starts. Only the last one will take effect and can be replaced.
ENTRYPOINT Specify the command to be run when this container starts. You can append the command
ONBUILD When building an inherited Dockerfile, the ONBUILD command will be run.
COPY Similar to ADD, copy our files to the mirror
ENV Set environment pass when building

Insert image description here

七、使用docker运行一个tomcat镜像

docker search tomcat #搜索tomcat镜像
Insert image description here

docker pull tomcat #下载tomcat
Insert image description here
Insert image description here
运行tomcat
docker run -it -d -p 8080:8080 tomcat

八、使用dockerfile写一个tomcat镜像

1、准备一个tomcat和jdk压缩包
Insert image description here

2、编写dockerfile
Vim dockerfile

FROM centos:7
MAINTAINER 彦祖强的[email protected]

ADD jdk1.8.0_191.tar.gz /usr/local/
ADD apache-tomcat-9.0.19.tar.gz /usr/local/

RUN yum -y install vim
ENV MYPATH /usr/local
WORKDIR $MYPATH

ENV JAVA_HOME /usr/local/jdk1.8.0_191
ENV CLASSPATH $JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
ENV CATALINA_HOME /usr/local/apache-tomcat-9.0.19
ENV CATALINA_BASE /usr/local/apache-tomcat-9.0.19
ENV PATH $PATH:$JAVA_HOME/bin:$CLASSPATH:$CATALINA_HOME/lib:$CATALINA_HOME/bin

EXPOSE 8080
CMD /usr/local/apache-tomcat-9.0.19/bin/startup.sh && tail -f /usr/local/apache-tomcat-9.0.19/logs/catalina.out
Insert image description here

3. Build the image
docker build -t tomcat9.
Insert image description here

Docker images
Insert image description here

4. Run the image
docker run -d -p 9090:8080 tomcat9
Insert image description here

Visit this machine http://ip:9090

Insert image description here
Insert image description here

5. Package the image
Docker save tomcat > /root/docker/tomcat9.tar.gz
Insert image description here

Guess you like

Origin blog.csdn.net/m0_57207884/article/details/130185411