I. Introduction to Docker

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

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

The container is completely sandbox, there is no interface between each other (like iPhone app) and more importantly, the container has very low performance overhead.

Docker supports compiling software into images; You can configure and publish all kinds of software in the image. Other users can directly use the configured image. A running image is called a container, and containers start up quickly. Similar to the packaged Windows system, it can be installed directly through the USB flash drive, without the need for system configuration software.

Docker application scenarios:

  • Automated packaging and publishing of Web applications;
  • Automated testing and continuous integration, release;
  • Deploy tuning databases or other backend applications in a service-oriented environment;
  • Build from scratch or extend existing OpenShift or Cloud Foundry platforms to build your own PaaS environment.

Docker benefits:

  • Fast, consistent delivery of applications
  • Responsive deployment and scaling
  • Make full use of VM resources

Ii. Core concepts of Docker

Docker Host: a machine on which Docker programs are installed (Docker is directly installed in the operating system)

Docker Client: Connects to Docker host for operation;

A Docker Container is an instance of an image that runs independently of one or a group of applications.

Docker Image: packaged software used to create templates for Docker containers;

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

Relationship diagram:

Basic usage of Docker:

① Install Docker in the machine;

(2) Find the corresponding image of the software in the Docker warehouse;

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

(4) the start or stop of the container is equivalent to the start and stop of the software;

Docker installation

Steps to install Docker in Linux

1. Check the Linux kernel version, which must be later than 3.10. Uname -r If the Linux kernel version does not meet requirements, upgrade the software package and kernel yum update 2. Yum install docker 3 Check whether the docker installation is successful. Docker -v 4. Start the docker container systemctl start docker 5. Systemctl enable Docker 6. Stop docker systemctl Stop dockerCopy the code

Set up Docker to use Ali Cloud image acceleration

Visit Ali Cloud image accelerator website: Ali cloud image acceleration

Docker common operations

1. Mirror operation

1.1 Operating a Remote Mirror

① Querying remote warehouse mirrors

docker search [OPTIONS]

The OPTIONS:

  • --automated: Lists only the images of the automated Build type.
  • --no-trunc: Displays the complete image description.
  • -s: Lists mirrors whose collections are at least the specified value.

MySQL > select * from docker

[root@izuf6h7blj36dmxbmcj15wz ~]# 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... 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] ......Copy the code

② Pull the mirror from the remote warehouse

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

The OPTIONS:

  • -a: Pulls all tagged images
  • --disable-content-trust: Indicates that mirror verification is ignored and enabled by default
  • TAG: Specifies the version number

Pull the Tomcat 7.0 image

[root@izuf6h7blj36dmxbmcj15wz ~]# docker pull tomcat:7.0 Trying to pull repository docker. IO /library/tomcat... 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.0Copy the code

Docker official image warehouse docker official image warehouse will introduce how to use, you can follow the instructions of the official document to operate.

1.2 Managing a Local Mirror

① View the list of local mirrors

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

The OPTIONS:

  • -a: lists all local images (including the intermediate image layer, which is filtered by default).
  • --digests: Displays summary information about the mirror.
  • f: Displays images that meet the conditions.
  • --format: specifies the return value of the template file;
  • --no-trunc: Displays complete image information.
  • -q: Only the mirror ID is displayed.

View the mirror of the local repository

[root@izuf6h7blj36dmxbmcj15wz ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE docker.io/memcached latest 804901da629f 38 hours ago 802.3 MB docker. IO /tomcat 7.0 ae42daf81b8c 2 days ago 533 MB docker. IO /mysql 5.7.32 1b12f2e9257b 6 days ago 448 MBCopy the code

② Delete the list of local mirrors

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

The OPTIONS:

  • -f: Forcible deletion.
  • --no-prune : The process mirror of the mirror is removed by default.

Delete Tomcat from the local repository

[root@izuf6h7blj36dmxbmcj15wz ~]# docker rmi tomcat:7.0 Untagged: docker.io/tomcat@sha256: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 [root@izuf6h7blj36dmxbmcj15wz ~]# 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 MBCopy the code

2. Container operations

2.1 Container lifecycle Management

① Create a container and run the command

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

The OPTIONS:

  • -a stdin: Specifies the standard input and output types. The options are STDIN, STDOUT, or STDERR.
  • -d: Runs the container in the background and returns the container ID.
  • -i: Runs the container in interactive mode, usually with -t;
  • -P: random port mapping, internal container portsrandomPort mapped to the host
  • -p: Specifies the port mapping in the following format:Host (host) port: container port
  • -t: Reassigns a pseudo-input terminal to the container, usually used in conjunction with -i;
  • --name="nginx-lb": Specifies a name for the container.
  • --dns 8.8.8.8: Specifies the DNS server used by the container. The default DNS server is the same as the host server.
  • --dns-search example.com: Specifies the container DNS search domain name, which is the same as the host by default.
  • -h "mars": Specifies the hostname of the container.
  • -e username="ritchie": Sets environment variables.
  • --env-file=[]: reads environment variables from the specified file;
  • --cpuset="0-2" or --cpuset="0,1,2": binds the container to a specified CPU to run.
  • -m : Sets the maximum memory used by the container.
  • --net="bridge"Specifies the network connection type, container support bridge/host/none/container: four types;
  • --link=[]: Adds a link to another container
  • --expose=[]: Open a port or group of ports;
  • --volume , -v: Binds a volume

Docker can create multiple containers from a single image, 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

[root@izuf6h7blj36dmxbmcj15wz ~]# docker start mysql01
Copy the code

Stop the running container mysql01

[root@izuf6h7blj36dmxbmcj15wz ~]# docker stop mysql01
Copy the code

Restart the mysql01shell container

[root@izuf6h7blj36dmxbmcj15wz ~]# docker restart mysql01
Copy the code

③ Delete containers

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

The OPTIONS:

  • -f: Forcibly deletes a running container by SIGKILL signal.
  • -l: Removes network connections between containers, not the containers themselves.
  • -v: Deletes the volume associated with the container.

④ Create a container but do not start it

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

The syntax is similar to that of run

⑤ Execute commands in a running container

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

The OPTIONS:

  • -d: Split mode: Runs in the background
  • -i: Keep STDIN open even without attachments
  • -t: Assigns a dummy terminal

Access the MySQL database

[root@izuf6h7blj36dmxbmcj15wz ~]# docker exec -it mysql-test /bin/bash root@86afbc58bd27:/# 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> 
Copy the code

2.2 Container Management

① List all containers

docker ps [OPTIONS]

The OPTIONS:

  • -a: Displays all containers, including those that are not running.
  • -f: Filters the displayed content based on conditions.
  • --format: Specifies the template file that returns the value.
  • -l: Displays the recently created container.
  • -n: lists the n containers recently created.
  • --no-trunc: Does not truncate the output.
  • -q: In silent mode, only container numbers are displayed.
  • -s: Displays the total file size.

② View container run logs

docker logs [OPTIONS] CONTAINER

The OPTIONS:

  • -f: Tracks log output
  • --since: Displays all logs at a start time
  • -t: Displays the timestamp
  • --tail: Lists only the latest N container logs

View tomcat run logs

[root@izuf6h7blj36dmxbmcj15wz ~]# 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.0f7d14d73ec4a 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 [root@izuf6h7blj36dmxbmcj15wz ~]# Docker logs Tomcat9.0 2020-11-030t01: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-030T01: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-030t01:45:26.636942000z 03-nov-2020 01:45:26.634 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: Jialin =java.base/java.lang= all-unnamed 2020-11-03:45:26.637118000z 03-Nov-2020 01:45:26.634 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: Jialin =java.base/java. IO = all-unnamed 2020-11-030T01: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. The rmi. Transport = ALL - UNNAMED T01 2020-11-03: they. 03 - Nov 637307000 z - 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-03 T01: they. 03 - Nov 637411000 z - 2020 01:45:26. 634 INFO [main] org. Apache. Catalina. Startup. VersionLoggerListener. Log Command line argument: - Djava. Util. Logging. Manager = org. Apache. Juli. ClassLoaderLogManager T01 2020-11-03: they. 03 - Nov 637510000 z - 2020 01:45:26. 634 INFO [main] org. Apache. Catalina. Startup. VersionLoggerListener. Log Command line argument: - Djdk. TLS. EphemeralDHKeySize = 2048 2020-11-03 T01: they. 03 - Nov 637603000 z - 2020 01:45:26. 634 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: - Djava. Protocol. The handler. PKGS = org. Apache. Catalina. Webresources T01 2020-11-03: they. 03-637691000 z 01:45:26 Nov - 2020. 634 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: - Dorg. Apache. Catalina. Security. SecurityListener. UMASK = 0027 2020-11-03 T01: they. 03-637780000 z 01:45:26 Nov - 2020. 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-030t01: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-03: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-03:45:26.654483000z 03-Nov 2020 01:45:26.652 INFO [main] Org. Apache. Catalina. Core. AprLifecycleListener. LifecycleEvent the Loaded apache Tomcat Native library [1.2.25] using APR Version [1.6.5]....Copy the code

View all containers

[root@izuf6h7blj36dmxbmcj15wz ~]# 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-testCopy the code

Docker operation command: docker command

3. The installation of MySQL

① Query the remote MySQL mirror

[root@izuf6h7blj36dmxbmcj15wz ~]# 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... 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... 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 to be 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... 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 0Copy the code

② Pull MySQL image

You can specify the version number of the installed image, otherwise the latest version is pulled by default

[root@izuf6h7blj36dmxbmcj15wz ~]# 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
Copy the code

View the obtained local mirror repository

[root@izuf6h7blj36dmxbmcj15wz ~]# 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 MBCopy the code

③ Create and run the MySQL container

[root@izuf6h7blj36dmxbmcj15wz ~]# docker run --name=mysql-test -it -p 3306:3306 -e MYSQL_ROOT_PASSWORD=123456 -d Mysql: 5.7.32 root @ izuf6h7blj36dmxbmcj15wz ~ # 86 afbc58bd27Copy the code

–name: Specifies the container name

-it: Creates a terminal and runs the container in interactive mode

-p: specifies the port mapping format as host (host) port: container port

-e: Sets environment variables

-d: Runs the container in the background and returns the ID

④ Check the running container

[root@izuf6h7blj36dmxbmcj15wz ~]# 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-testCopy the code

⑤ Access the MySQL container

[root@izuf6h7blj36dmxbmcj15wz ~]# docker exec -it mysql-test /bin/bash root@86afbc58bd27:/# 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>
Copy the code

Enable port 3306 on the server if you use cloud services

Connect using the Navicat client after deployment on the server

Use Docker install other image and install MySQL operation basic similar, congratulations you learned Docker operation ヾ(≧▽≦*)o