I. Introduction to Docker

1.1 Virtualization Technology

Virtualization technology is a resource management technology, is the computer’s various physical resources, such as server, network, memory and storage, etc., to abstract, transformed after the presentation, break the entity structure can not be cut barriers, users can be better than the original configuration to apply these resources.

Main functions of virtualization technology: high performance physical hardware overcapacity, software migration across the environment (code adaptation)

1.2 What is Docker

Docker is an open source application container engine

Born in early 2013, based on Go language, dotCloud company (later renamed Docker Inc);

Docker allows developers to package their applications and dependencies into a lightweight, portable container that can then be distributed to any popular Linux machine.

Containers are completely sandboxed, isolated from each other, and have very low container performance overhead.

Docker is divided into CE (Community Edition) and EE (Enterprise Edition) after version 17.03.

Official website: www.Docker.com

In popular terms, Docker is a high-performance virtual machine in the server. It can isolate one physical machine from N virtual machines and do not affect each other.

Features:

  • Containers package applications into standardized units for delivery and deployment.
  • Containers contain all the environments that the software needs to run, and are very lightweight
  • Containerized applications that can run consistently in any Linux environment
  • Containerized applications with isolation so that multiple teams can share the same Linux system resources

1.3 Comparison between Containers and VMS

The following image compares the difference between Docker and traditional virtualization. It can be seen that container virtualization is implemented at the operating system level and directly reuse the local host operating system, while traditional virtualization is implemented at the hardware level.

features The container The virtual machine
Start the Second level Minutes of class
The hard disk to use Generally for MB Generally for GB
performance Close to native hardware Weak chicken
System support Single machine can run dozens of containers Several virtual OS in a single machine
Runtime environment Mainly in Linux Mainly in the window

Same: Both containers and VMS are virtualization technologies, providing advantages in resource isolation and allocation

Different:

  • Containers virtualize operating systems, and VMS virtualize hardware
  • Traditional virtual machines can run different operating systems, while containers mainly run the same operating system (Linux).

1.4 Basic Concepts of Docker

  • Host: Linux server with Docker daemon installed, called host; (equivalent to the Vmware host)
  • Image: similar to the iso Image file of the installation system.
  • Container: represents an installed VM. Containers can be created, started, stopped, deleted, paused, and so on.
  • Repository: A Repository can be thought of as a control center for storing images.

Docker installation and startup

How to install Docker on CentOS

2.1 Automatic Installation using the official installation script

The installation command is as follows:

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

You can also use the domestic daoCloud one-click install command:

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

You can also install it manually, so I won’t expand it here

2.2 Docker image acceleration

The Docker image acceleration used to configure the repository image source can also be directly referred to the related related image source provided by the tutorial

2.3 Docker Daemon-related Commands

The systemctl command is a system service manager directive

Start the docker:

systemctl start docker
Copy the code

Stop the docker:

systemctl stop docker
Copy the code

Restart the docker:

systemctl restart docker
Copy the code

Check docker status:

systemctl status docker
Copy the code

Startup:

systemctl enable docker
Copy the code

View the Docker summary

docker info
Copy the code

Check out the Docker help documentation

docker --help
Copy the code

Common Docker commands

3.1 Commands related to Mirrors

3.1.1 Viewing a Mirror

View all local mirrors

docker images
Copy the code

These images are stored in the /var/lib/docker host directory

3.1.2 Searching for mirrors

To search for an image on the network, run the following command: Note that you must ensure that your current system is networked

Docker Search Image nameCopy the code

3.1.3 Pulling a Mirror

Pull image: Download the image from the Docker repository to the local PC. The image name format is name: version number. If the version number is not specified, it is the latest version. If you do not know the version of the image, you can go to the Docker Hub to search for the image.

Docker pull image nameCopy the code

For example, I want to download the Centos7 image

docker pull centos:7
Copy the code

3.1.4 Deleting a Mirror

Delete a mirror by its ID

Docker RMI image IDCopy the code

Deleting all Mirrors

docker rmi `docker images -q`
Copy the code

3.2 Container-related Commands

3.2.1 Viewing containers

View the running container

docker ps
Copy the code

View all containers

Docker ps - aCopy the code

View the container that was last run

Docker ps - lCopy the code

View the stopped container

docker ps -f status=exited
Copy the code

3.2.2 Creating and Starting containers

Create container command:

Docker run parameter Image name: image label /bin/bashCopy the code

Common parameters for creating containers are as follows:

## Command parameter description-i: runs the container. -t: displays the command line after the container is started. After adding these two parameters, the container creation can be logged in. Assign a dummy terminal (if you only add it, it will automatically enter the container). -d: If run is followed by -d, a daemon container is created to run in the background (so that the container is not logged in automatically after being created). --name: Name the created container. -v: indicates directory mapping (the former is a host directory, and the latter is a directory mapped to the host). You can use multiple -v to map multiple directories or files. Note: It is best to do directory mapping, make changes on the host, and then share them on the container. -p: indicates port mapping. The former is a host port, and the latter is a mapped port in the container. You can use multiple -p to map multiple ports. After entering the container, run the following command to initialize: /bin/bash; You can write without writingCopy the code

(1) Interactive container

Docker runit --name= container name Image name: tag /bin/bashCopy the code

At this time, we use the ps command to check, and we find that we can see the started container, which is in the started state

Exit the current container

exit
Copy the code

(2) Guardian container:

Docker run-di --name= container name Image name: label /bin/bashCopy the code

Login container:

Docker exec-it container name (or container ID) /bin/bashCopy the code

Note: here the script /bin/bash must be written after logging into the container

3.2.3 Stopping and starting containers

Stop container:

Docker stop container name (container ID)Copy the code

Start container:

Docker start docker startCopy the code

After the container is started, it is run in background mode by default, so you need to use the docker exec command mentioned above to start it

3.2.4 File Copy

We can use cp if we need to copy the file to the container

Docker cp File or directory to copy Container name: container directoryCopy the code

You can also copy files out of the container

Docker cp Container name: container directory The file or directory to be copiedCopy the code

In general, the container is used to mount data volumes, and the external storage is bidirectionally bound to the storage inside the container. However, the binding method is only temporary. The specific data volume usage is described later

3.2.6 Viewing the CONTAINER IP Address

We can view the various data that the container is running with the following command

Docker inspect Container nameCopy the code

You can also run the following command to output the IP address

Docker inspect - format = '{{. NetworkSettings. IPAddress}}' container name (ID) containerCopy the code

3.2.7 Deleting a Container

Deletes the specified container. Running containers cannot be deleted

Docker RM Container name (ID)Copy the code

Docker data volume

4.1 Data Volume Overview

Problems solved:

Considering the isolation of containers:

Will the data generated in the Docker container still exist after the container is deleted?

Can Docker containers and external machines exchange files directly?

Data interaction between containers?

A data volume is a directory or file on the host. After a container directory is bound to a data volume directory, the modification of the container directory is immediately synchronized. A data volume can be mounted by multiple containers at the same time, and a container can be mounted by multiple data volumes

Data Volume function

  • Container data persistence
  • The external machine communicates indirectly with the container
  • Data exchange between containers

4.2 Data Volume Configuration Mode

(1). One container mounts one data volume

When creating a startup container, use the -v parameter to set the data volume

docker run ... -v Host directory (file): directory (file) in the container...Copy the code

Matters needing attention:

1. The directory must be an absolute pathCopy the code
  1. If the host directory does not exist, it will be created automatically
  2. Multiple data volumes can be mounted

Case study:

docker run -di --name=c1 -v /root/host_data1:/root/c1_data centos:7 /bin/bash
Copy the code

(2). View the data volumes attached to the container

You can run the following command to view the data volumes mounted in the container

Docker inspect Container nameCopy the code

(3). One container mounts multiple data volumes

You can run the following command to mount multiple data volumes

docker run -di --name=c1 -v /root/host_data1:/root/c1_data1 -v /root/host_data2:/root/c1_data2 centos:7 /bin/bash
Copy the code

(4). Attach one data volume to multiple containers

Multiple containers attach one data volume to implement data sharing

docker run -di --name=c2  -v /root/host_data_common:/root/c2_data centos:7
docker run -di --name=c3  -v /root/host_data_common:/root/c3_data centos:7
Copy the code

Multiple containers mount 1 container (this container mounts 1 data volume)

#Create and start the c3 data volume container, set the data volume with the -v parameter
docker run -it --name=c3 -v /root/host_data_common:/root/c3_data centos:7 /bin/bash
#Run the -- volumes-from command to configure volumes
docker run -it --name=c1 --volumes-from c3 centos:7 /bin/bash
docker run -it --name=c2 --volumes-from c3 centos:7 /bin/bash
Copy the code

Deploy the application in Docker

MySQL 5.1 deployment

Implementation steps:

  1. Search for MySQL mirror
  2. Pull MySQL image
  3. Create containers, set port mappings, and set data volumes
  4. Enter the container to operate mysql
  5. Connect to MySQL using Navicat

Implementation process:

  1. Search for mysql mirror
docker search mysql
Copy the code
  1. Pull mysql image
Docker pull mysql: 5.6Copy the code
  1. Create containers and set port mapping and directory mapping
docker run -di --name=c_mysql -p 3307:3306 -v /root/mysql/logs:/logs -v /root/mysql/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD = 123456 mysql: 5.6Copy the code
  • Parameter Description:

    • -p 3307:3306: maps port 3306 of a container to port 3307 of a host.
    • -v /root/mysql/logs:/logs: Mounts the logs directory under the host directory (/root/mysql) to /logs in the container. Log directory
    • – v/root/mysql/data: / var/lib/mysql: will host directory (/ root/mysql) under the data directory mounted to the container of/var/lib/mysql. The data directory
    • **-e MYSQL_ROOT_PASSWORD=123456: ** Initialize the password of user root.
  1. Enter the container and operate mysql
Docker exec -- it c_mysql /bin/bashCopy the code
  1. Connect to mysql in the container using Navicat

Tomcat 5.2 deployment

Implementation steps:

  1. Searching for Tomcat Images
  2. Pull the Tomcat image
  3. Create containers, set port mappings, and set data volumes
  4. Deploy the service to Tomcat
  5. Use an external machine to access Tomcat to test the deployment service

Implementation process:

  1. Searching for tomcat Images
docker search tomcat
Copy the code
  1. Pull the Tomcat image
docker pull tomcat:8-jdk8
Copy the code
  1. Create containers and set port mapping and directory mapping
docker run -id --name=c_tomcat -p 8080:8080 -v /root/tomcat/webapps:/usr/local/tomcat/webapps tomcat:8-jdk8
Copy the code
  • Parameter Description:

    • **-p 8080:8080: ** Maps port 8080 of the container to port 8080 of the host

      * * – v/root/tomcat/webapps: / usr/local/tomcat/webapps: * * will host directory (/ root/tomcat/webapps) mounted to the container webapps

  1. Deploy the service to Tomcat using the FinalShell file upload
  2. Use an external machine to access Tomcat to test the deployment service

Redis 5.3 deployment

Implementation steps:

  1. Search for Redis images
  2. Pull the Redis image
  3. Create containers and set port mappings
  4. Using an external machine to connect to Redis, test

Implementation process:

  1. Search for redis images
docker search redis
Copy the code
  1. Pull the Redis image
Docker pull redis: 5.0Copy the code
  1. Create a container and set port mapping
Docker run-id --name=c_redis -p 6379:6379 redis:5.0Copy the code
  1. Using an external machine to connect to Redis, test

6. Make Docker images

6.1 Mirroring Principles

What is the nature of Docker mirroring?

Why a centos image in Docker is only 200MB, but a centos OPERATING system ISO file to several G?

Why is a tomcat image in Docker 500MB, while a tomcat installation package is only 70 + MB?

Check the PPT

Docker image is essentially a layered union File system.

6.2 Migration and Backup

1. Save the container as an image

We can save the container as an image by using the following command

Docker commit {running container name} {mirror name}#For example,
docker commit c_tomcat mywar_tomcat
Copy the code

2. Image backup

We can save the image as a tar file by using the following command

Docker save -o docker save -o#For example,
docker save -o mywar_tomcat.tar mywar_tomcat
#-o: indicates the output file
Copy the code

3. Mirror recovery and migration

First we delete the mynginx_img image and then execute this command to restore it

Docker load -i {backup image file}#For example,
docker load -i mywar_tomcat.tar
#-i: specifies the file to be imported
Copy the code

After executing the command, you can view the mirror again and see that the mirror has been restored. You can run the test again

docker run -di --name=mytomcat -p 8081:8080 -v /root/tomcat/webapps/:/usr/local/tomcat/webapps mywar_tomcat:latest
Copy the code

6.3 Dockerfile profile

Dockerfile is a text file that contains instructions for building image files. Each instruction builds a layer based on the underlying image, and eventually builds a new image.

  • For developers: a completely consistent development environment can be provided for the development team
  • For testers: Take the image you built during development or build a new image from a Dockerfile file and get started
  • For operations: Seamless migration of applications can be achieved at deployment time

Key words:

The keyword role note
FROM Specify parent mirror Specifies that the dockerfile is built on that image
MAINTAINER The author information Used to indicate who wrote the dockerfile
LABEL The label Labels used to Label dockerfiles can be used instead of Maintainer and are ultimately viewable in the Basic Docker image information
RUN Execute the command The default format is /bin/sh. RUN command or RUN [” command “, “param1”, “param2”]
CMD Container start command Provides the default commands for starting containers to work with ENTRYPOINT. Format CMD command param1 param2 or CMD [” command “, “param1”, “param2”]
ENTRYPOINT The entrance This is usually used when making containers that are closed on execution
COPY Copy the file Copy files to image during build
ADD Add files Adding a file to the image at build time is not limited to the current build context that can come from a remote service
ENV The environment variable ENV name=value specifies that build environment variables can be overridden with -e when the container is started
ARG Build parameters Build arguments are only used at build time if ENV is present then the value of ENV with the same name always overrides arg arguments
VOLUME Defines data volumes that can be mounted externally Which directories can be mounted to the file system on startup.
EXPOSE Exposure to port Define the port that the container listens on when it runs. Start the container using -p to bind the EXPOSE port format: EXPOSE 8080 or EXPOSE 8080/ UDP
WORKDIR Working directory Specifies that the working directory inside the container is automatically created if it is not created
USER Specify execution user Specifies the user to be used during the RUN CMD ENTRYPONT execution during build or startup
HEALTHCHECK Health check The command specifying health monitoring for the current container is basically useless because many times the application has its own health monitoring mechanism
ONBUILD The trigger The ONBUILD command will be executed after executing FROM, but it does not affect the current image
STOPSIGNAL Send semaphore to host machine The STOPSIGNAL directive sets the system call signal to be sent to the container to exit.
SHELL Specifies the shell to execute the script Specifies the shell used when the RUN CMD ENTRYPOINT command is executed

6.4 Making the Springboot executable JAR package into a Docker image

Goal: Publish the Springboot project into the Docker container

Note: Build docker image is Dockerfile file, Dockerfile and SpringBoot file must be stored in the same directory;

Implementation steps:

  1. Edit the Dockerfile file in IDEA

    • Defining the base image
    • Define author information
    • Add jar package files to the image
    • Executes the command when defining the current image to start the container
  2. In the host, build the image, using the docker command

  3. Start the container based on the image

  4. test

Implementation process:

  1. Edit the Dockerfile file in IDEA

    • Define the base image
      FROM java:8
      # Define author information
      MAINTAINER  itheima-hero-brother <[email protected]>
      Add jar package files to image
      ADD springboot.jar app.jar
      The command is executed when the current image starts the container
      CMDJava - jar app. The jar
      Copy the code
  2. On the host machine, build the image

    • Docker bulid -f {dockerfile file path} -t {image name: version} {build file path}#For example,Docker build -f Dockerfile -t mySpringboot :1.0./#-f: specifies the Dockerfile path to use.
      #-t: indicates the name and label of the image. It is usually in the format of name:tag or Name and is not latest
      Copy the code
  3. Start the container based on the image

    Docker run-di --name= mySpringboot -p 9000:8080 mySpringboot :1.0Copy the code
  4. test

7. Build Docker private server

Docker official Docker Hub (hub.docker.com) is a repository for managing public images, we can pull images from above to local, we can also push our own images up. However, sometimes our server cannot access the Internet, or you do not want to put your mirror image on the public network, then we need to build our own private warehouse to store and manage our image.

8.1 Private warehouse construction

Implementation steps:

1. Pull private warehouse image

2. Start the private repository container

3. Test whether the private image repository is set up successfully

4. Configure private repositories

5. Restart the Docker service

Implementation process:

1. Pull private warehouse image

docker pull registry
Copy the code

2. Start the private repository container

docker run -di --name=my_registry -p 5000:5000 registry:latest
Copy the code

3. Test whether the private image repository is set up successfully

  • Open a browser Enter the address http://192.168.200.128:5000/v2/_catalog
  • If {” repositories “:[]} is seen, the private repository is successfully constructed

4. Configure private repositories

#Changes the daemon. Json   
vim /etc/docker/daemon.json    
#Add a key to the above file, save and exit
#This step is used to make Docker trust the private repository address; Change the IP address of the private warehouse server to the real IP address of the private warehouse server{"insecure-registries":[" private repository IP :5000"]}Copy the code
#For example,{" insecure - registries: "[]" 192.168.200.128:5000 "}Copy the code

5. Restart the Docker service

systemctl restart docker
docker start my_registry
Copy the code

8.2 Uploading an Image to a Private Vault

1. Mark the image as a private repository image

#Docker tag {host:port} host:port}
#For example,Docker tag myspringboot: 1.0 192.168.200.128:5000 / springboot: 1.0Copy the code

2. Upload the image of the tag

#Docker push {private repository host:port}/{private repository host:port}
#For example,Docker push 192.168.200.128:5000 / springboot: 1.0Copy the code

8.3 Pulling an Image from a Private Vault

#Pull the mirrorDocker pull {host:port}/{host:port}#For example,Docker pull 192.168.200.128:5000 / springboot: 1.0Copy the code