Table of contents
4. Common operations of Docker
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 dependencies into a lightweight, portable container, which can then be distributed to any popular Linux machine, and can also be virtualized.
The container is completely using the sandbox mechanism, and there will be no interface between them (similar to iPhone apps). More importantly, the performance overhead of the container is extremely low.
Docker supports compiling software into images; in the images, various software can be configured and released, and other users can directly use the configured images. The running image is called a container, and the container starts very quickly. Similar to the packaged Windows system, it can be installed directly through the U disk, without the need for system configuration software.
Application scenarios of Docker:
- Automatic packaging and publishing of web applications;
- Automated testing and continuous integration, release;
- Deploy and tune database or other background applications in a service environment;
- Build your own PaaS environment by building from scratch or extending an existing OpenShift or Cloud Foundry platform.
Advantages of Docker:
- Fast, consistent delivery of applications
- Responsive deployment and scaling
- Make full use of virtual machine resources
2. The core concept of Docker
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): an instance after the image is started, an application or a group of applications that run independently;
Docker image (Image): packaged software, used to create a template for Docker containers;
Docker warehouse (Respository): used to save the packaged software image;
Relationship diagram:
The basic usage of Docker:
① Install Docker in the machine;
② Find the image corresponding to this software in the Docker repository;
③ Use Docker to run the image and generate a Docker container;
④ The start or stop of the container is equivalent to the start and stop of the software;
3. 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 URL of Alibaba Cloud Mirror Accelerator: Alibaba Cloud Mirror Accelerator
4. Common operations of Docker
1. Mirror operation
1.1 Operate remote mirroring
① Query remote warehouse mirroring
docker search [OPTIONS]
Description of OPTIONS:
--automated
: Only list images of the automated build type;--no-trunc
: Display the complete image description;-s
: List the images whose favorites are not less than the specified value.
Query the MySQL in the docker remote warehouse
[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... 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 image in the remote warehouse
docker pull [OPTIONS] NAME[:TAG|@DIGEST]
Description of OPTIONS:
-a
: Pull all tagged images--disable-content-trust
: Ignore the verification of the image, it is enabled by defaultTAG
: used to specify the version number
Pull tomcat 7.0 image
[root@izuf6h7blj36dmxbmcj15wz ~]# 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
复制代码
The docker official mirror warehouse will introduce the usage method in the official docker mirror warehouse, and you can follow the instructions in the official document.
1.2 Manage local mirrors
① View the local mirror list
docker images [OPTIONS] [REPOSITORY[:TAG]]
Description of OPTIONS:
-a
: List all local images (including intermediate image layers, by default, filter out intermediate image layers);--digests
: Display the summary information of the image;f
: Display images that meet the conditions;--format
: specify the template file of the return value;--no-trunc
: Display complete image information;-q
: Only display the image ID.
View the mirror of the local warehouse
[root@izuf6h7blj36dmxbmcj15wz ~]# 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...]
Description of OPTIONS:
-f
: force delete;--no-prune
: Do not remove the process image of the image, which is removed by default;
Delete tomcat in the local warehouse
[root@izuf6h7blj36dmxbmcj15wz ~]# docker rmi tomcat:7.0
Untagged: 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 MB
复制代码
2. Container operations
2.1 Container life cycle management
① Create a container and run the command
docker run [OPTIONS] IMAGE [COMMAND] [ARG...]
Description of OPTIONS:
-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
: Reassign a pseudo-input terminal for the container, usually used together with -i;--name="nginx-lb"
: Specify a name for the container;--dns 8.8.8.8
: Specifies the DNS server used by the container, which is the same as the host by default;--dns-search example.com
: Specifies 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 used by 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, and can run at the same time
② Start, stop, refresh 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
复制代码
Stop the running container mysql01
[root@izuf6h7blj36dmxbmcj15wz ~]# docker stop mysql01
复制代码
Restart the container mysql01shell
[root@izuf6h7blj36dmxbmcj15wz ~]# docker restart mysql01
复制代码
③ Delete container
docker rm [OPTIONS] CONTAINER [CONTAINER...]
Description of OPTIONS:
-f
: Forcefully delete a running container via the 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 the command in the running container
docker exec [OPTIONS] CONTAINER COMMAND [ARG...]
Description of OPTIONS:
-d
:detached mode: run in background-i
: keep STDIN open even if not attached-t
: Allocate a pseudo-terminal
Enter into MySQL to operate
[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>
复制代码
2.2 Container Management
① List all containers
docker ps [OPTIONS]
Description of OPTIONS:
-a
: Show all containers, including non-running ones.-f
: Filter the displayed content according to the condition.--format
: Specifies the template file for the return value.-l
: Displays the most recently created containers.-n
: List the most recently created n containers.--no-trunc
: Do not truncate output.-q
: Silent mode, only display the container number.-s
: Display the total file size.
② View the container operation log
docker logs [OPTIONS] CONTAINER
Description of OPTIONS:
-f
: Trace log output--since
: Display all logs from a certain start time-t
: display timestamp--tail
: Only list the latest N container logs
Check the tomcat operation log
[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.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
[root@izuf6h7blj36dmxbmcj15wz ~]# 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
[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-test
复制代码
Docker operation command: Docker command encyclopedia
3. Install MySQL
① Query 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... 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 the MySQL image
You can specify the version number of the installation image, if not specified, the latest version will be 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
复制代码
View the acquired local mirror warehouse
[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 MB
复制代码
③ Create and run 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 ~]# 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 the environment variable
-d
: Run the container in the background and return the ID
④ View running containers
[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-test
复制代码
⑤ Enter the MySQL container operation
[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>
复制代码
If operating in the cloud service, pay attention to open the server port 3306
Use the navicat client to connect after deployment in the server
Using Docker to install other images is basically similar to installing MySQL. Congratulations, you have learned Docker operationsヾ(≧▽≦*)o