docker version
docker --help
docker run --help
docker info
镜像命令
1.docker images #查询本地主机上所有的镜像
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest feb5d9fea6a5 4 months ago 13.3kB
centos latest 5d0da3dc9764 4 months ago 231MB
2.docker images --help
3.docker images -aq #-q只显示ID
1.docker search --help
https://hub.docker.com上面也可以搜索镜像
[root@localhost ~]# docker search mysql
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
mysql MySQL is a widely used, open-source relation鈥 12028 [OK]
mariadb MariaDB Server is a high performing open sou鈥 4607 [OK]
mysql/mysql-server Optimized MySQL Server Docker images. Create鈥 900 [OK]
centos/mysql-57-centos7 MySQL 5.7 SQL database server 92
mysql/mysql-cluster Experimental MySQL Cluster Docker images. Cr鈥 92
centurylink/mysql Image containing mysql. Optimized to be link鈥 59 [OK]
databack/mysql-backup Back up mysql databases to... anywhere! 54
prom/mysqld-exporter 46 [OK]
deitch/mysql-backup REPLACED! Please use http://hub.docker.com/r鈥 40 [OK]
tutum/mysql Base docker image to run a MySQL database se鈥 35
linuxserver/mysql A Mysql container, brought to you by LinuxSe鈥 35
schickling/mysql-backup-s3 Backup MySQL to S3 (supports periodic backup鈥 31 [OK]
mysql/mysql-router MySQL Router provides transparent routing be鈥 23
centos/mysql-56-centos7 MySQL 5.6 SQL database server 21
arey/mysql-client Run a MySQL client from a docker container 20 [OK]
fradelg/mysql-cron-backup MySQL/MariaDB database backup using cron tas鈥 18 [OK]
yloeffler/mysql-backup This image runs mysqldump to backup data usi鈥 7 [OK]
openshift/mysql-55-centos7 DEPRECATED: A Centos7 based MySQL v5.5 image鈥 6
idoall/mysql MySQL is a widely used, open-source relation鈥 3 [OK]
devilbox/mysql Retagged MySQL, MariaDB and PerconaDB offici鈥 3
ansibleplaybookbundle/mysql-apb An APB which deploys RHSCL MySQL 3 [OK]
centos/mysql-80-centos7 MySQL 8.0 SQL database server 2
jelastic/mysql An image of the MySQL database server mainta鈥 2
widdpim/mysql-client Dockerized MySQL Client (5.7) including Curl鈥 1 [OK]
vitess/mysqlctld vitess/mysqlctld 1 [OK]
2.docker search mysql --filter
[root@localhost ~]# docker search mysql --filter=STARS=4000
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
mysql MySQL is a widely used, open-source relation鈥 12028 [OK]
mariadb MariaDB Server is a high performing open sou鈥 4607 [OK]
2.docker pull mysql ==docker pull docker.io/library/mysql:latest
[root@localhost ~]# docker pull mysql
Using default tag: latest #不写tag,默认下载最新的
latest: Pulling from library/mysql
72a69066d2fe: Pull complete #分层下载
93619dbc5b36: Pull complete
99da31dd6142: Pull complete
626033c43d70: Pull complete
37d5d7efb64e: Pull complete
ac563158d721: Pull complete
d2ba16033dad: Pull complete
688ba7d5c01a: Pull complete
00e060b6d11d: Pull complete
1c04857f594f: Pull complete
4d7cfa90e6ea: Pull complete
e0431212d27d: Pull complete
Digest: sha256:e9027fe4d91c0153429607251656806cc784e914937271037f7738bd5b8e7709
Status: Downloaded newer image for mysql:latest
docker.io/library/mysql:latest #真实地址
[root@localhost ~]#
3.docker pull mysql:5.7 #指定版本,注意版本号一定要是官网上有的
[root@localhost ~]# docker pull mysql:5.7
5.7: Pulling from library/mysql
72a69066d2fe: Already exists
93619dbc5b36: Already exists
99da31dd6142: Already exists
626033c43d70: Already exists
37d5d7efb64e: Already exists
ac563158d721: Already exists
d2ba16033dad: Already exists
0ceb82207cd7: Pull complete
37f2405cae96: Pull complete
e2482e017e53: Pull complete
70deed891d42: Pull complete
Digest: sha256:f2ad209efe9c67104167fc609cca6973c8422939491c9345270175a300419f94
Status: Downloaded newer image for mysql:5.7
docker.io/library/mysql:5.7
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mysql 5.7 c20987f18b13 5 weeks ago 448MB
mysql latest 3218b38490ce 5 weeks ago 516MB
hello-world latest feb5d9fea6a5 4 months ago 13.3kB
centos latest 5d0da3dc9764 4 months ago 231MB
4.docker rmi(删除images) -f ID
[root@localhost ~]# docker rmi -f c20987f18b13 #删除mysql 5.7
Untagged: mysql:5.7
Untagged: mysql@sha256:f2ad209efe9c67104167fc609cca6973c8422939491c9345270175a300419f94
Deleted: sha256:c20987f18b130f9d144c9828df630417e2a9523148930dc3963e9d0dab302a76
Deleted: sha256:6567396b065ee734fb2dbb80c8923324a778426dfd01969f091f1ab2d52c7989
Deleted: sha256:0910f12649d514b471f1583a16f672ab67e3d29d9833a15dc2df50dd5536e40f
Deleted: sha256:6682af2fb40555c448b84711c7302d0f86fc716bbe9c7dc7dbd739ef9d757150
Deleted: sha256:5c062c3ac20f576d24454e74781511a5f96739f289edaadf2de934d06e910b92
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mysql latest 3218b38490ce 5 weeks ago 516MB
hello-world latest feb5d9fea6a5 4 months ago 13.3kB
centos latest 5d0da3dc9764 4 months ago 231MB
[root@localhost ~]#
5.docker rmi -f $(docker images -qa) #全部删除镜像 docker images展示所有images
容器命令
1.docker pull centos
2.docker run --help #启动容器
--name="name" 容器名字 tomcat01 tomcat02
-d 后台方式运行
-it 交互式进入容器
-p 指定容器端口,
主机端口:容器端口(常用)
容器端口
ip:主机端口:容器端口
-P 随机指定容器端口
3.docker run -it centos /bin/bash ;启动并进去容器centos
[root@localhost ~]# docker run -it centos /bin/bash
[root@e14d4a35c80b /]#
[root@e14d4a35c80b /]#
[root@e14d4a35c80b /]#
[root@e14d4a35c80b /]# cat /etc/os-release
NAME="CentOS Linux"
VERSION="8"
ID="centos"
ID_LIKE="rhel fedora"
VERSION_ID="8"
PLATFORM_ID="platform:el8"
PRETTY_NAME="CentOS Linux 8"
ANSI_COLOR="0;31"
CPE_NAME="cpe:/o:centos:centos:8"
HOME_URL="https://centos.org/"
BUG_REPORT_URL="https://bugs.centos.org/"
CENTOS_MANTISBT_PROJECT="CentOS-8"
CENTOS_MANTISBT_PROJECT_VERSION="8"
[root@e14d4a35c80b /]#
4.docker ps #展示当前正在运行的容器
docker ps
-a #展示正在运行或者以前运行过的容器
-n=? #显示的个数
-q #只显示容器的标号
[root@localhost ~]# docker ps #当前没有在运行的
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@localhost ~]# docker ps -a #以前运行过的
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e14d4a35c80b centos "/bin/bash" 2 minutes ago Exited (130) 56 seconds ago interesting_bardeen
c1d26ff55f14 centos "/bin/bash" 15 hours ago Exited (0) 15 hours ago keen_payne
5365c82423ff hello-world "/hello" 19 hours ago Exited (0) 19 hours ago clever_ritchie
[root@localhost ~]#
[root@localhost ~]# docker ps -n 2 #只展示最近运行的容器
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e14d4a35c80b centos "/bin/bash" 3 minutes ago Exited (130) About a minute ago interesting_bardeen
c1d26ff55f14 centos "/bin/bash" 15 hours ago Exited (0) 15 hours ago keen_payne
5.退出容器
exit #直接停止并且退出
ctrl + p +q #退出但是保持运行
[root@localhost ~]# docker run -it centos /bin/bash
[root@13c36870224f /]# [root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
13c36870224f centos "/bin/bash" 20 seconds ago Up 20 seconds objective_lamarr
[root@localhost ~]# #ctrl+p+q以后,退出来,centos还在运行
6.docker rm 容器ID #删除指定的容器,正在运行的不能删除
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
13c36870224f centos "/bin/bash" About a minute ago Up About a minute objective_lamarr
e14d4a35c80b centos "/bin/bash" 5 minutes ago Exited (130) 4 minutes ago interesting_bardeen
c1d26ff55f14 centos "/bin/bash" 15 hours ago Exited (0) 15 hours ago keen_payne
5365c82423ff hello-world "/hello" 19 hours ago Exited (0) 19 hours ago clever_ritchie
[root@localhost ~]# docker rm e14d4a35c80b
e14d4a35c80b
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
13c36870224f centos "/bin/bash" 2 minutes ago Up 2 minutes objective_lamarr
c1d26ff55f14 centos "/bin/bash" 15 hours ago Exited (0) 15 hours ago keen_payne
5365c82423ff hello-world "/hello" 19 hours ago Exited (0) 19 hours ago clever_ritchie
[root@localhost ~]#
docker rm -f 容器ID 容器ID 容器ID #可以删除正在运行的容器
docker ps -a -q | xargs docker rm
启动和停止容器的操作
docker start #启动容器
docker restart #重启容器
docker stop #停止正在运行的容器
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
13c36870224f centos "/bin/bash" 3 minutes ago Up 3 minutes objective_lamarr
[root@localhost ~]#
[root@localhost ~]#
[root@localhost ~]# docker stop 13c36870224f
13c36870224f
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@localhost ~]#
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
90d02b1d184f mysql "docker-entrypoint.s鈥 About a minute ago Exited (1) 42 seconds ago practical_burnell
13c36870224f centos "/bin/bash" 6 minutes ago Exited (0) 2 minutes ago objective_lamarr
c1d26ff55f14 centos "/bin/bash" 15 hours ago Exited (0) 15 hours ago keen_payne
5365c82423ff hello-world "/hello" 19 hours ago Exited (0) 2 minutes ago clever_ritchie
[root@localhost ~]# docker start c1d26ff55f14
c1d26ff55f14
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c1d26ff55f14 centos "/bin/bash" 15 hours ago Up 2 seconds keen_payne
docker kill #强制停止运行的容器
常用其他命令
后台启动容器 docker run -d,
1.docker run -d centos
docker run -d centos /bin/sh -c "while true;do echo kuangshen;sleep 1;done"
2.docker ps发现有没有centos
#后台运行的容器,一定要有前台进程,docker发现没有应用就会停止这个容器。
查看日志命令
1.docker logs --help;
-t 时间戳显示,
--tail num显示的行数
-f 打印logs
[root@localhost ~]# docker logs --help
Usage: docker logs [OPTIONS] CONTAINER
Fetch the logs of a container
Options:
--details Show extra details provided to logs
-f, --follow Follow log output
--since string Show logs since timestamp (e.g. 2013-01-02T13:23:37Z) or relative (e.g. 42m for 42 minutes)
-n, --tail string Number of lines to show from the end of the logs (default "all")
-t, --timestamps Show timestamps
--until string Show logs before a timestamp (e.g. 2013-01-02T13:23:37Z) or relative (e.g. 42m for 42 minutes)
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c1d26ff55f14 centos "/bin/bash" 15 hours ago Up 4 minutes keen_payne
[root@localhost ~]# docker logs -f -t --tail 10 c1d26ff55f14
2022-01-27T22:26:39.487890153Z PLATFORM_ID="platform:el8"
2022-01-27T22:26:39.487892499Z PRETTY_NAME="CentOS Linux 8"
2022-01-27T22:26:39.487894767Z ANSI_COLOR="0;31"
2022-01-27T22:26:39.487897095Z CPE_NAME="cpe:/o:centos:centos:8"
2022-01-27T22:26:39.487899431Z HOME_URL="https://centos.org/"
2022-01-27T22:26:39.487901773Z BUG_REPORT_URL="https://bugs.centos.org/"
2022-01-27T22:26:39.487904175Z CENTOS_MANTISBT_PROJECT="CentOS-8"
2022-01-27T22:26:39.487906578Z CENTOS_MANTISBT_PROJECT_VERSION="8"
2022-01-27T22:28:09.774484716Z [root@c1d26ff55f14 /]# exit
2022-01-27T22:28:09.774516022Z exit
查看容器内容的进程信息
[root@localhost ~]# docker top c1d26ff55f14
UID PID PPID C STIME TTY TIME CMD
root 3026 3009 0 21:45 pts/0 00:00:00 /bin/bash
[root@localhost ~]#
查看容器的元数据
[root@localhost ~]# docker inspect --help
Usage: docker inspect [OPTIONS] NAME|ID [NAME|ID...]
Return low-level information on Docker objects
Options:
-f, --format string Format the output using the given Go template
-s, --size Display total file sizes if the type is container
--type string Return JSON for specified type
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
90d02b1d184f mysql "docker-entrypoint.s鈥 14 minutes ago Exited (1) 13 minutes ago practical_burnell
13c36870224f centos "/bin/bash" 19 minutes ago Exited (0) 15 minutes ago objective_lamarr
c1d26ff55f14 centos "/bin/bash" 16 hours ago Up 13 minutes keen_payne
5365c82423ff hello-world "/hello" 19 hours ago Exited (0) 15 minutes ago clever_ritchie
[root@localhost ~]# docker inspect c1d26ff55f14
[
{
"Id": "c1d26ff55f1423ba63419ed1aa16c76c1873a24929a75b2f36909796f83b5145",
"Created": "2022-01-27T22:25:11.445361099Z",
"Path": "/bin/bash",
"Args": [],
"State": {
"Status": "running",
"Running": true,
"Paused": false,
"Restarting": false,
"OOMKilled": false,
"Dead": false,
"Pid": 3026,
"ExitCode": 0,
"Error": "",
"StartedAt": "2022-01-28T13:45:15.940506596Z",
"FinishedAt": "2022-01-27T22:28:09.774810505Z"
},
"Image": "sha256:5d0da3dc976460b72c77d94c8a1ad043720b0416bfc16c52c45d4847e53fadb6",
"ResolvConfPath": "/var/lib/docker/containers/c1d26ff55f1423ba63419ed1aa16c76c1873a24929a75b2f36909796f83b5145/resolv.conf",
"HostnamePath": "/var/lib/docker/containers/c1d26ff55f1423ba63419ed1aa16c76c1873a24929a75b2f36909796f83b5145/hostname",
"HostsPath": "/var/lib/docker/containers/c1d26ff55f1423ba63419ed1aa16c76c1873a24929a75b2f36909796f83b5145/hosts",
"LogPath": "/var/lib/docker/containers/c1d26ff55f1423ba63419ed1aa16c76c1873a24929a75b2f36909796f83b5145/c1d26ff55f1423ba63419ed1aa16c76c1873a24929a75b2f36909796f83b5145-json.log",
"Name": "/keen_payne",
"RestartCount": 0,
"Driver": "overlay2",
"Platform": "linux",
"MountLabel": "",
"ProcessLabel": "",
"AppArmorProfile": "",
"ExecIDs": null,
"HostConfig": {
"Binds": null,
"ContainerIDFile": "",
"LogConfig": {
"Type": "json-file",
"Config": {}
},
"NetworkMode": "default",
"PortBindings": {},
"RestartPolicy": {
"Name": "no",
"MaximumRetryCount": 0
},
"AutoRemove": false,
"VolumeDriver": "",
"VolumesFrom": null,
"CapAdd": null,
"CapDrop": null,
"CgroupnsMode": "host",
"Dns": [],
"DnsOptions": [],
"DnsSearch": [],
"ExtraHosts": null,
"GroupAdd": null,
"IpcMode": "private",
"Cgroup": "",
"Links": null,
"OomScoreAdj": 0,
"PidMode": "",
"Privileged": false,
"PublishAllPorts": false,
"ReadonlyRootfs": false,
"SecurityOpt": null,
"UTSMode": "",
"UsernsMode": "",
"ShmSize": 67108864,
"Runtime": "runc",
"ConsoleSize": [
0,
0
],
"Isolation": "",
"CpuShares": 0,
"Memory": 0,
"NanoCpus": 0,
"CgroupParent": "",
"BlkioWeight": 0,
"BlkioWeightDevice": [],
"BlkioDeviceReadBps": null,
"BlkioDeviceWriteBps": null,
"BlkioDeviceReadIOps": null,
"BlkioDeviceWriteIOps": null,
"CpuPeriod": 0,
"CpuQuota": 0,
"CpuRealtimePeriod": 0,
"CpuRealtimeRuntime": 0,
"CpusetCpus": "",
"CpusetMems": "",
"Devices": [],
"DeviceCgroupRules": null,
"DeviceRequests": null,
"KernelMemory": 0,
"KernelMemoryTCP": 0,
"MemoryReservation": 0,
"MemorySwap": 0,
"MemorySwappiness": null,
"OomKillDisable": false,
"PidsLimit": null,
"Ulimits": null,
"CpuCount": 0,
"CpuPercent": 0,
"IOMaximumIOps": 0,
"IOMaximumBandwidth": 0,
"MaskedPaths": [
"/proc/asound",
"/proc/acpi",
"/proc/kcore",
"/proc/keys",
"/proc/latency_stats",
"/proc/timer_list",
"/proc/timer_stats",
"/proc/sched_debug",
"/proc/scsi",
"/sys/firmware"
],
"ReadonlyPaths": [
"/proc/bus",
"/proc/fs",
"/proc/irq",
"/proc/sys",
"/proc/sysrq-trigger"
]
},
"GraphDriver": {
"Data": {
"LowerDir": "/var/lib/docker/overlay2/2334ef40b9078530d45b7342173665c5d7b55403ceeb0b2d309c071b019b898f-init/diff:/var/lib/docker/overlay2/de570bcebe115a83e2db6a1b68d2c3abdffc0d8f9d36bc8bb138b12b3e3278b7/diff",
"MergedDir": "/var/lib/docker/overlay2/2334ef40b9078530d45b7342173665c5d7b55403ceeb0b2d309c071b019b898f/merged",
"UpperDir": "/var/lib/docker/overlay2/2334ef40b9078530d45b7342173665c5d7b55403ceeb0b2d309c071b019b898f/diff",
"WorkDir": "/var/lib/docker/overlay2/2334ef40b9078530d45b7342173665c5d7b55403ceeb0b2d309c071b019b898f/work"
},
"Name": "overlay2"
},
"Mounts": [],
"Config": {
"Hostname": "c1d26ff55f14",
"Domainname": "",
"User": "",
"AttachStdin": true,
"AttachStdout": true,
"AttachStderr": true,
"Tty": true,
"OpenStdin": true,
"StdinOnce": true,
"Env": [
"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
],
"Cmd": [
"/bin/bash"
],
"Image": "centos",
"Volumes": null,
"WorkingDir": "",
"Entrypoint": null,
"OnBuild": null,
"Labels": {
"org.label-schema.build-date": "20210915",
"org.label-schema.license": "GPLv2",
"org.label-schema.name": "CentOS Base Image",
"org.label-schema.schema-version": "1.0",
"org.label-schema.vendor": "CentOS"
}
},
"NetworkSettings": {
"Bridge": "",
"SandboxID": "ccf263ab728ae3bb8aa97a9c7dde57f61fc543f348fe7159c1cbc5711be4b70c",
"HairpinMode": false,
"LinkLocalIPv6Address": "",
"LinkLocalIPv6PrefixLen": 0,
"Ports": {},
"SandboxKey": "/var/run/docker/netns/ccf263ab728a",
"SecondaryIPAddresses": null,
"SecondaryIPv6Addresses": null,
"EndpointID": "a67f6570f6774f650bac75dcf665b7d252d7eacae17471701c31df7481199ff6",
"Gateway": "172.17.0.1",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"IPAddress": "172.17.0.2",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"MacAddress": "02:42:ac:11:00:02",
"Networks": {
"bridge": {
"IPAMConfig": null,
"Links": null,
"Aliases": null,
"NetworkID": "65f61d1d236de31ba89654b1cb9e718394a8cc60a678e0f98528576b140cba89",
"EndpointID": "a67f6570f6774f650bac75dcf665b7d252d7eacae17471701c31df7481199ff6",
"Gateway": "172.17.0.1",
"IPAddress": "172.17.0.2",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"MacAddress": "02:42:ac:11:00:02",
"DriverOpts": null
}
}
}
}
]
进入当前正在运行的容器
方式一
docker exec -it 容器ID bashshell #进入容器后开启新的终端
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c1d26ff55f14 centos "/bin/bash" 16 hours ago Up 16 minutes keen_payne
[root@localhost ~]# docker exec -it c1d26ff55f14 /bin/bash
[root@c1d26ff55f14 /]#
方式二
docker attach it 容器ID bashshell #进入容器正在执行的终端,不会启动新的终端
[root@localhost ~]# docker attach --help
Usage: docker attach [OPTIONS] CONTAINER
Attach local standard input, output, and error streams to a running container
Options:
--detach-keys string Override the key sequence for detaching a container
--no-stdin Do not attach STDIN
--sig-proxy Proxy all received signals to the process (default true)
[root@localhost ~]# docker attach c1d26ff55f14
[root@c1d26ff55f14 /]#
从容器内拷贝文件到主机
docker cp 容器ID:容器内的地址 目的地址
步骤容器内新建一个文件,然后把它拷贝出来
只要数据在,容器停止了,这个文件也不会消失
docker cp id:/home/test.java /home ;即使容器停止了,也可以拷贝出来。