After three days of death, I summarized the Docker Xiaobai getting started operation guide (recommended collection)

1. Introduction to Docker

Docker is an open source application container engine, based on the Go language and open source in compliance with the Apache2.0 protocol.

Docker allows developers to package their applications and dependent packages into a lightweight, portable container, and then publish it to any popular Linux machine, which can also be virtualized.

Containers use the sandbox mechanism completely, and there will be no interfaces between them (apps similar to iPhone). What's more, the container performance overhead is extremely low.

Docker supports the compilation of software into mirrors; supports various software configurations and releases in mirrors, and other users can directly use the configured mirrors. The running image is called a container, and the container starts quickly. Similar to the packaged Windows system, it can be installed directly via a USB flash drive, without the need for system configuration software.

Application scenarios of Docker:

  • Automated packaging and publishing of Web applications;
  • Automated testing and continuous integration and release;
  • Deploy and adjust databases or other background applications in a service-oriented environment;
  • Compile or extend the existing OpenShift or Cloud Foundry platform from scratch to build your own PaaS environment.

Advantages of Docker:

  • Fast and consistent delivery of applications
  • Responsive deployment and expansion
  • Make full use of virtual machine resources

Two, Docker core concept

Docker host (Host): the machine where the Docker program is installed (Docker is installed directly in the operating system)

Docker client (Client): connect to the Docker host for operation;

Docker container (Container): the instance after the image is started, one or a group of applications running independently;

Docker image (Image): packaged software, used to create a template for Docker containers;

Docker repository (Respository): used to store packaged software images;

Relationship diagram:

Basic usage of Docker:

① Install Docker in the machine;

② Look for the mirror image corresponding to this software in the Docker warehouse;

③ Use Docker to run the image and generate a Docker container;

④ Starting or stopping the container is equivalent to starting and stopping the software;

Three, Docker installation

Steps to install Docker in Linux

1.检查Linux内核版本,必须是3.10以上
uname -r
若Linux版本内核不符合则需要升级软件包和内核
yum update

2.安装docker
yum install docker

3.查看docker是否安装成功
docker -v

4.启动docker容器
systemctl start docker

5.设置docker开机启用
systemctl enable docker

6.停止docker
systemctl stop docker

Set docker to use Alibaba Cloud image acceleration

Visit the website of Alibaba Cloud Mirror Accelerator: Alibaba Cloud Mirror Accelerator

Four, Docker common operations

1. Mirror operation

1.1 Operating remote mirroring

① Query remote warehouse mirror

docker search [OPTIONS]

OPTIONS description:

  • --automated: Only list the images of the automated build type;
  • --no-trunc : Display the complete mirror description;
  • -s : List the mirrors whose number of favorites is not less than the specified value.

Query MySQL in docker remote warehouse

[[email protected] ~]# docker search mysql
INDEX       NAME                                        DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
docker.io   docker.io/mysql                             MySQL is a widely used, open-source relati...   10111     [OK]       
docker.io   docker.io/mariadb                           MariaDB is a community-developed fork of M...   3711      [OK]       
docker.io   docker.io/mysql/mysql-server                Optimized MySQL Server Docker images. Crea...   738                  [OK]
docker.io   docker.io/percona                           Percona Server is a fork of the MySQL rela...   511       [OK]       
docker.io   docker.io/centos/mysql-57-centos7           MySQL 5.7 SQL database server                   84                   
docker.io   docker.io/mysql/mysql-cluster               Experimental MySQL Cluster Docker images. ...   77                   
docker.io   docker.io/centurylink/mysql                 Image containing mysql. Optimized to be li...   60                   [OK]
docker.io   docker.io/bitnami/mysql                     Bitnami MySQL Docker Image                      45                   [OK]
docker.io   docker.io/deitch/mysql-backup               REPLACED! Please use http://hub.docker.com...   41                   [OK]
......

② Pull the mirror image in the remote warehouse

docker pull [OPTIONS] NAME[:TAG|@DIGEST]

OPTIONS description:

  • -a : Pull all tagged mirrors
  • --disable-content-trust :Ignore the verification of mirroring, turn on by default
  • TAG: Used to specify the version number

Pull tomcat 7.0 image

[[email protected] ~]# docker pull tomcat:7.0
Trying to pull repository docker.io/library/tomcat ... 
7.0: Pulling from docker.io/library/tomcat
e4c3d3e4f7b0: Pull complete 
101c41d0463b: Pull complete 
8275efcd805f: Pull complete 
751620502a7a: Pull complete 
a59da3a7d0e7: Pull complete 
5ad32ac1e527: Pull complete 
27c13473e3bd: Pull complete 
717370f5d395: Pull complete 
84d544041c3b: Pull complete 
4cce74688a50: Pull complete 
Digest: sha256:9c86a5a1af6f7bdc946a28a39b7f05044abd5eee2eb8cf56b55938c8618c85cb
Status: Downloaded newer image for docker.io/tomcat:7.0
Docker official mirror warehouse Docker official mirror warehouse will introduce how to use it, you can follow the instructions in the official document.

1.2 Manage local mirrors

① View the local mirror list

docker images [OPTIONS] [REPOSITORY[:TAG]]

OPTIONS description:

  • -a : List all local mirrors (including the intermediate image layer, by default, filter out the intermediate image layer);
  • --digests : Display summary information of the mirror;
  • f : Display the mirror image that meets the conditions;
  • --format : Specify the template file of the return value;
  • --no-trunc : Display complete mirror information;
  • -q : Only the mirror ID is displayed.

View the mirror image of the local warehouse

[[email protected] ~]# docker images
REPOSITORY            TAG                 IMAGE ID            CREATED             SIZE
docker.io/memcached   latest              804901da629f        38 hours ago        82.3 MB
docker.io/tomcat      7.0                 ae42daf81b8c        2 days ago          533 MB
docker.io/mysql       5.7.32              1b12f2e9257b        6 days ago          448 MB

② Delete the local mirror list

docker rmi [OPTIONS] IMAGE [IMAGE...]

OPTIONS description:

  • -f : Forced deletion;
  • --no-prune : Do not remove the process image of the image, it is removed by default;

Delete the tomcat in the local warehouse

[[email protected] ~]# docker rmi tomcat:7.0
Untagged: tomcat:7.0
Untagged: docker.io/[email protected]:9c86a5a1af6f7bdc946a28a39b7f05044abd5eee2eb8cf56b55938c8618c85cb
Deleted: sha256:ae42daf81b8c5fd4b2f009d277d062681171d8d84c9f174aba08f6dbd47ed857
Deleted: sha256:3f394bd52132d1cf758651758ac12ebf02b721921b524123ead1e874a1ed9bd6
Deleted: sha256:1174c29e4d812f6dd04484ae909d5c4451fc7d4ecb8cd0934930876ad68985de
Deleted: sha256:24fdd84b4adf4b922767e2b9d336c96fb31cab90c84fb34f9164f6587473e565
Deleted: sha256:984e8d35fe94a287a0bc4a45e8757c75e17e80c3f4fed0b9f9b0ec6b4b457ebb
Deleted: sha256:254cf71da09bbc16e304f925dee4270ac9069649dee8576423a6a77186ee0061
Deleted: sha256:3383e45b22b2b665b1a2604e375c1e7dc007c540e77d6367b1d7ba79ef0030d8
Deleted: sha256:d5d618196ec30fcdba1d210f135b4911b9dbdc79fdd729a9a9c3683a6a9b9308
Deleted: sha256:4bb57adf9037e4da45f2dbc0f84a7e9d8da6847bef3e63d09c8a9d8465f4d2c6
Deleted: sha256:5173011923d00f0fe606ecceda93ea88f17dc77e0b026df802c9aeb31bc1eac6
Deleted: sha256:9780f6d83e45878749497a6297ed9906c19ee0cc48cc88dc63827564bb8768fd
[[email protected] ~]# docker images
REPOSITORY            TAG                 IMAGE ID            CREATED             SIZE
docker.io/memcached   latest              804901da629f        38 hours ago        82.3 MB
docker.io/mysql       5.7.32              1b12f2e9257b        6 days ago          448 MB

2. Container operation

2.1 Container lifecycle management

① Create a container and run the command

docker run [OPTIONS] IMAGE [COMMAND] [ARG...]

OPTIONS description:

  • -a stdin: Specify the standard input and output content type, optional STDIN/STDOUT/STDERR three items;
  • -d: Run the container in the background and return the container ID;
  • -i: Run the container in interactive mode, usually used together with -t;
  • -P: Random port mapping, the internal port of the container is randomly mapped to the port of the host
  • -p: Specify port mapping, the format is: host (host) port: container port
  • -t: Re-allocate a pseudo input terminal for the container, usually used with -i at the same time;
  • --name="nginx-lb": Specify a name for the container;
  • --dns 8.8.8.8: Specify the DNS server used by the container, which is the same as the host by default;
  • --dns-search example.com: Specify the container DNS search domain name, which is the same as the host by default;
  • -h "mars": Specify the hostname of the container;
  • -e username="ritchie": Set environment variables;
  • --env-file=[]: Read environment variables from the specified file;
  • --cpuset="0-2" or --cpuset="0,1,2": Bind the container to the specified CPU to run;
  • -m : Set the maximum memory usage of the container;
  • --net="bridge": Specify the network connection type of the container, support bridge/host/none/container: four types;
  • --link=[]: Add a link to another container;
  • --expose=[]: Open a port or a group of ports;
  • --volume , -v: Bind a volume
Docker can create multiple containers through a mirror, each container does not interfere with each other, can run at the same time

② Start, stop, and refresh the container

docker start [OPTIONS] CONTAINER [CONTAINER...]

docker stop [OPTIONS] CONTAINER [CONTAINER...]

docker restart [OPTIONS] CONTAINER [CONTAINER...]

Start the stopped container mysql01

[[email protected] ~]# docker start mysql01

Stop the running container mysql01

[[email protected] ~]# docker stop mysql01

Restart the container mysql01shell

[[email protected] ~]# docker restart mysql01

③ Delete the container

docker rm [OPTIONS] CONTAINER [CONTAINER...]

OPTIONS description:

  • -f: Use the SIGKILL signal to forcefully delete a running container.
  • -l: Remove the network connection between the containers, not the container itself.
  • -v: Delete the volume associated with the container.

④ Create a container but do not start

docker create [OPTIONS] IMAGE [COMMAND] [ARG...]

The syntax is similar to that of run

⑤ Execute commands in the running container

docker exec [OPTIONS] CONTAINER COMMAND [ARG...]

OPTIONS description:

  • -d: Separate mode: run in the background
  • -i: Keep STDIN open even if it is not attached
  • -t: Assign a pseudo terminal

Enter into MySQL operation

[[email protected] ~]# docker exec -it mysql-test /bin/bash
[email protected]:/# mysql -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 13
Server version: 5.7.32 MySQL Community Server (GPL)

Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> 

2.2 Container management

① List all containers

docker ps [OPTIONS]

OPTIONS description:

  • -a: Show all containers, including those that are not running.
  • -f: Filter the displayed content based on conditions.
  • --format: Specify the template file of the return value.
  • -l: Display the recently created container.
  • -n: List the recently created n containers.
  • --no-trunc: Does not cut off the output.
  • -q: Silent mode, only the container number is displayed.
  • -s: Display the total file size.

② View the container running log

docker logs [OPTIONS] CONTAINER

OPTIONS description:

  • -f: Trace log output
  • --since: Display all logs of a certain start time
  • -t: Show time stamp
  • --tail: Only list the latest N container logs

View tomcat running log

[[email protected] ~]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                               NAMES
b88d05801048        tomcat:9.0.39       "catalina.sh run"        17 minutes ago      Up 10 minutes       0.0.0.0:8090->8080/tcp              tomcat9.0
f7d14d73ec4a        memcached           "docker-entrypoint..."   5 days ago          Up 5 days           0.0.0.0:11211->11211/tcp            memcache
86afbc58bd27        mysql:5.7.32        "docker-entrypoint..."   5 days ago          Up 5 days           0.0.0.0:3306->3306/tcp, 33060/tcp   mysql-test
[[email protected] ~]# docker logs tomcat9.0
2020-11-03T01:45:26.595757000Z 03-Nov-2020 01:45:26.593 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Java Home:             /usr/local/openjdk-11
2020-11-03T01:45:26.595845000Z 03-Nov-2020 01:45:26.593 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log JVM Version:           11.0.9+11
2020-11-03T01:45:26.595933000Z 03-Nov-2020 01:45:26.593 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log JVM Vendor:            Oracle Corporation
2020-11-03T01:45:26.596033000Z 03-Nov-2020 01:45:26.593 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log CATALINA_BASE:         /usr/local/tomcat
2020-11-03T01:45:26.605849000Z 03-Nov-2020 01:45:26.600 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log CATALINA_HOME:         /usr/local/tomcat
2020-11-03T01:45:26.636942000Z 03-Nov-2020 01:45:26.634 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: --add-opens=java.base/java.lang=ALL-UNNAMED
2020-11-03T01:45:26.637118000Z 03-Nov-2020 01:45:26.634 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: --add-opens=java.base/java.io=ALL-UNNAMED
2020-11-03T01:45:26.637215000Z 03-Nov-2020 01:45:26.634 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: --add-opens=java.rmi/sun.rmi.transport=ALL-UNNAMED
2020-11-03T01:45:26.637307000Z 03-Nov-2020 01:45:26.634 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.util.logging.config.file=/usr/local/tomcat/conf/logging.properties
2020-11-03T01:45:26.637411000Z 03-Nov-2020 01:45:26.634 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager
2020-11-03T01:45:26.637510000Z 03-Nov-2020 01:45:26.634 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djdk.tls.ephemeralDHKeySize=2048
2020-11-03T01:45:26.637603000Z 03-Nov-2020 01:45:26.634 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.protocol.handler.pkgs=org.apache.catalina.webresources
2020-11-03T01:45:26.637691000Z 03-Nov-2020 01:45:26.634 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dorg.apache.catalina.security.SecurityListener.UMASK=0027
2020-11-03T01:45:26.637780000Z 03-Nov-2020 01:45:26.635 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dignore.endorsed.dirs=
2020-11-03T01:45:26.637897000Z 03-Nov-2020 01:45:26.635 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dcatalina.base=/usr/local/tomcat
2020-11-03T01:45:26.638002000Z 03-Nov-2020 01:45:26.635 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dcatalina.home=/usr/local/tomcat
2020-11-03T01:45:26.638094000Z 03-Nov-2020 01:45:26.635 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.io.tmpdir=/usr/local/tomcat/temp
2020-11-03T01:45:26.654483000Z 03-Nov-2020 01:45:26.652 INFO [main] org.apache.catalina.core.AprLifecycleListener.lifecycleEvent Loaded Apache Tomcat Native library [1.2.25] using APR version [1.6.5].
......

View all containers

[[email protected] ~]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                               NAMES
f7d14d73ec4a        memcached           "docker-entrypoint..."   43 hours ago        Up 43 hours         0.0.0.0:11211->11211/tcp            memcache
86afbc58bd27        mysql:5.7.32        "docker-entrypoint..."   46 hours ago        Up 45 hours         0.0.0.0:3306->3306/tcp, 33060/tcp   mysql-test

Docker operation command: Docker command encyclopedia

3. Install MySQL

① Query remote MySQL mirror

[[email protected] ~]# docker search mysql
INDEX       NAME                                        DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
docker.io   docker.io/mysql                             MySQL is a widely used, open-source relati...   10111     [OK]       
docker.io   docker.io/mariadb                           MariaDB is a community-developed fork of M...   3711      [OK]       
docker.io   docker.io/mysql/mysql-server                Optimized MySQL Server Docker images. Crea...   739                  [OK]
docker.io   docker.io/percona                           Percona Server is a fork of the MySQL rela...   511       [OK]       
docker.io   docker.io/centos/mysql-57-centos7           MySQL 5.7 SQL database server                   84                   
docker.io   docker.io/mysql/mysql-cluster               Experimental MySQL Cluster Docker images. ...   77                   
docker.io   docker.io/centurylink/mysql                 Image containing mysql. Optimized to be li...   60                   [OK]
docker.io   docker.io/bitnami/mysql                     Bitnami MySQL Docker Image                      45                   [OK]
docker.io   docker.io/deitch/mysql-backup               REPLACED! Please use http://hub.docker.com...   41                   [OK]
docker.io   docker.io/tutum/mysql                       Base docker image to run a MySQL database ...   35                   
docker.io   docker.io/databack/mysql-backup             Back up mysql databases to... anywhere!         31                   
docker.io   docker.io/prom/mysqld-exporter                                                              31                   [OK]
docker.io   docker.io/schickling/mysql-backup-s3        Backup MySQL to S3 (supports periodic back...   29                   [OK]
docker.io   docker.io/linuxserver/mysql                 A Mysql container, brought to you by Linux...   26                   
docker.io   docker.io/centos/mysql-56-centos7           MySQL 5.6 SQL database server                   20                   
docker.io   docker.io/circleci/mysql                    MySQL is a widely used, open-source relati...   19                   
docker.io   docker.io/mysql/mysql-router                MySQL Router provides transparent routing ...   17                   
docker.io   docker.io/arey/mysql-client                 Run a MySQL client from a docker container      15                   [OK]
docker.io   docker.io/fradelg/mysql-cron-backup         MySQL/MariaDB database backup using cron t...   10                   [OK]
docker.io   docker.io/openshift/mysql-55-centos7        DEPRECATED: A Centos7 based MySQL v5.5 ima...   6                    
docker.io   docker.io/devilbox/mysql                    Retagged MySQL, MariaDB and PerconaDB offi...   3                    
docker.io   docker.io/ansibleplaybookbundle/mysql-apb   An APB which deploys RHSCL MySQL                2                    [OK]
docker.io   docker.io/jelastic/mysql                    An image of the MySQL database server main...   1                    
docker.io   docker.io/widdpim/mysql-client              Dockerized MySQL Client (5.7) including Cu...   1                    [OK]
docker.io   docker.io/monasca/mysql-init                A minimal decoupled init container for mysql    0 

② Pull MySQL mirror

You can specify the version number of the installation image, if not specified, the latest version will be pulled by default

[[email protected] ~]# docker pull mysql
Using default tag: latest
Trying to pull repository docker.io/library/mysql ... 
latest: Pulling from docker.io/library/mysql
bb79b6b2107f: Already exists 
49e22f6fb9f7: Already exists 
842b1255668c: Already exists 
9f48d1f43000: Already exists 
c693f0615bce: Already exists 
8a621b9dbed2: Already exists 
0807d32aef13: Already exists 
a56aca0feb17: Pull complete 
de9d45fd0f07: Pull complete 
1d68a49161cc: Pull complete 
d16d318b774e: Pull complete 
49e112c55976: Pull complete 
Digest: sha256:8c17271df53ee3b843d6e16d46cff13f22c9c04d6982eb15a9a47bd5c9ac7e2d
Status: Downloaded newer image for docker.io/mysql:latest

View the local mirror warehouse that has been obtained

[[email protected] ~]# docker images
REPOSITORY            TAG                 IMAGE ID            CREATED             SIZE
docker.io/memcached   latest              804901da629f        2 days ago          82.3 MB
docker.io/mysql       5.7.32              1b12f2e9257b        7 days ago          448 MB
docker.io/mysql       latest              db2b37ec6181        7 days ago          545 MB

③ Create and run MySQL container

[[email protected] ~]# docker run --name=mysql-test -it -p 3306:3306 -e MYSQL_ROOT_PASSWORD=123456 -d mysql:5.7.32
[[email protected] ~]# 86afbc58bd27

--name: Specify the container name

-it: Create a terminal and run the container in interactive mode

-p: Specify the port mapping format as 主机(宿主)端口:容器端口

-e: Set environment variables

-d: Run the container in the background and return the ID

④ View the running container

[[email protected] ~]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                               NAMES
f7d14d73ec4a        memcached           "docker-entrypoint..."   45 hours ago        Up 45 hours         0.0.0.0:11211->11211/tcp            memcache
86afbc58bd27        mysql:5.7.32        "docker-entrypoint..."   47 hours ago        Up 47 hours         0.0.0.0:3306->3306/tcp, 33060/tcp   mysql-test

⑤ Enter MySQL container operation

[[email protected] ~]# docker exec -it mysql-test /bin/bash
[email protected]:/# mysql -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 28
Server version: 5.7.32 MySQL Community Server (GPL)

Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>

references

If operating in the cloud service, pay attention to open the server port 3306

Use Navicat client to connect after deployment in the server

Using Docker to install other images is basically similar to installing MySQL. Congratulations on learning how to operate Docker.