guidance

Today’s Docker content is full of small liver products, covering the essence of my work and teaching these years. Not to be missed

This article contains: SpringBoot and Docker integration, Docker data volume introduction and use, Docker private server construction, Docker mirroring principle analysis. Docker image backup migration.

1. Let’s talk about Docker

Debug your app, not your environment!

Debug your application, not your development environment;

1.1 What is Docker?

Docker is an open source application container engine, was born in early 2013, based on Go language implementation, dotCloud company produced (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. Docker containers are completely sandboxed, isolated from each other, and have very low performance overhead.

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

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.

1.2 Main functions of Docker:

  • The capacity of high-performance physical hardware is too high
  • Software migration issues across environments (code doesn’t fit)

1.3 Docker 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

What is a Docker data volume? Introduction + Case

2.1 What is a Data Volume?

** Data volumes solve the following problems: ** Considering container isolation:

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?

Essence: A shared directory

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

2.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

The Docker container has three applications deployed

MySQL 3.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.7Copy the code
  1. Create containers and set port mapping and directory mapping
docker run -di --name=c_mysql -p 3306:3306 -v /root/mysql/logs:/logs -v /root/mysql/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD = root mysql: 5.7Copy 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 3.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 3.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

4. Docker image principle analysis

The Centos image is only 200 MB, and the Tomcat image is 500 MB. So here’s the question

  • 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?

What is the nature of Docker mirroring? Docker image is essentially a layered union File system.

  • Because docker images are layered, Tomcat only has more than 10 MB, but it needs to rely on the parent image and the base image. The size of all exposed Tomcat images is more than 500 MB
  • Centos iso image file contains bootfs and rootfs, while Docker Centos image reuse operating system bootfs, only rootfs and other image layers

5. Docker image migration and backup

5.1 Saving Containers as Images

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

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

If the mirror label is not written, the default value is latest

5.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 c_tomcat_bak.tar c_tomcat_bak
#-o: indicates the output file
Copy the code

5.3 Image Restoration 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 c_tomcat_bak.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 c_tomcat_bak:latest
Copy the code

Docker all container script Dockerfile

At present, the images in the official Docker image warehouse are basically built by Dockerfile. It becomes more important to understand Dockfile here

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 value 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. Use -v binding format when starting containers.
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.1 Dockerfile Case SpringBoot packages 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  kaikeba-hero-brother <liuyaxiong91@163.cn>
      Add jar package files to image
      ADDJxshop - admin - 2.2. Jar jxshop. Jar
      The command is executed when the current image starts the container
      CMDJava - jar jxshop. 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 jxshop: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
      #Don't forget the last./ build file path
      Copy the code
  3. Start the container based on the image

    Docker run-di --name=jxshop -p 8000:8000 yshop:1.0Copy the code
  4. test

Note: Don’t forget the last./ build file path

Viii. Docker private server construction

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
  • Seeing {” Repositories “:[]} indicates that 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

That’s all for today’s Docker content. If you are interested in other Docker content but have no time to study it, welcome to discuss it in the comments section.

This article nuggets first, do not forward to other platforms without permission! Please respect the original, thank you