docke-03-常用命令
Posted 型男一枚
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了docke-03-常用命令相关的知识,希望对你有一定的参考价值。
一、帮助命令
1、docker version
docker version 查看docker相关的版本信息
[root@javaEE ~]# docker version
Client: Docker Engine - Community
Version: 20.10.8
API version: 1.41
Go version: go1.16.6
Git commit: 3967b7d
Built: Fri Jul 30 19:55:49 2021
OS/Arch: linux/amd64
Context: default
Experimental: true
Server: Docker Engine - Community
Engine:
Version: 20.10.8
API version: 1.41 (minimum version 1.12)
Go version: go1.16.6
Git commit: 75249d8
Built: Fri Jul 30 19:54:13 2021
OS/Arch: linux/amd64
Experimental: false
containerd:
Version: 1.4.9
GitCommit: e25210fe30a0a703442421b0f60afac609f950a3
runc:
Version: 1.0.1
GitCommit: v1.0.1-0-g4144b63
docker-init:
Version: 0.19.0
GitCommit: de40ad0
2、dcoker info
查看docker的相关的信息
[root@javaEE ~]# docker info
Client:
Context: default
Debug Mode: false
Plugins:
app: Docker App (Docker Inc., v0.9.1-beta3)
buildx: Build with BuildKit (Docker Inc., v0.6.1-docker)
scan: Docker Scan (Docker Inc., v0.8.0)
Server:
Containers: 7
Running: 1
Paused: 0
Stopped: 6
Images: 4
Server Version: 20.10.8
Storage Driver: overlay2
Backing Filesystem: extfs
Supports d_type: true
Native Overlay Diff: true
userxattr: false
Logging Driver: json-file
Cgroup Driver: cgroupfs
Cgroup Version: 1
Plugins:
Volume: local
Network: bridge host ipvlan macvlan null overlay
Log: awslogs fluentd gcplogs gelf journald json-file local logentries splunk syslog
Swarm: inactive
Runtimes: io.containerd.runc.v2 io.containerd.runtime.v1.linux runc
Default Runtime: runc
Init Binary: docker-init
containerd version: e25210fe30a0a703442421b0f60afac609f950a3
runc version: v1.0.1-0-g4144b63
init version: de40ad0
Security Options:
seccomp
Profile: default
Kernel Version: 3.10.0-1160.el7.x86_64
Operating System: CentOS Linux 7 (Core)
OSType: linux
Architecture: x86_64
CPUs: 2
Total Memory: 1.777GiB
Name: javaEE
ID: K2PJ:CAII:ZUSA:JDWI:WJ77:MT7U:D5IO:ARFW:LEW5:6TML:IFZL:KS7H
Docker Root Dir: /var/lib/docker
Debug Mode: false
Registry: https://index.docker.io/v1/
Labels:
Experimental: false
Insecure Registries:
127.0.0.0/8
Registry Mirrors:
https://06wo2xso.mirror.aliyuncs.com/
Live Restore Enabled: false
3、docker help
查看docker的相关的帮助信息,特别是对docker指令的帮助、
二、镜像的命令
1、docker images [-options]
docker images 啥都不写,列出docker在本机上的所有的镜像
[root@javaEE ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
tomcat latest 710ec5c56683 33 hours ago 668MB
nginx latest 08b152afcfae 2 weeks ago 133MB
hello-world latest d1165f221234 5 months ago 13.3kB
centos latest 300e315adb2f 8 months ago 209MB
``
各个选项说明:`
REPOSITORY:表示镜像的仓库源
TAG:镜像的标签
IMAGE ID:镜像ID
CREATED:镜像创建时间
SIZE:镜像大小
同一仓库源可以有多个 TAG,代表这个仓库源的不同个版本,我们使用 REPOSITORY:TAG 来定义不同的镜像
如果你不指定一个镜像的版本标签,例如你只使用 ubuntu,docker 将默认使用 ubuntu:latest 镜像
docker images -a
列出本地所有的镜像(含中间映像层),docker images默认就是docker images -a
[root@javaEE ~]# docker images -a
REPOSITORY TAG IMAGE ID CREATED SIZE
tomcat latest 710ec5c56683 33 hours ago 668MB
nginx latest 08b152afcfae 2 weeks ago 133MB
hello-world latest d1165f221234 5 months ago 13.3kB
centos latest 300e315adb2f 8 months ago 209MB
[root@javaEE ~]#
docker images -q
只列出镜像的ID
[root@javaEE ~]# docker images -q
710ec5c56683
08b152afcfae
d1165f221234
300e315adb2f
docker images --digests
显示镜像的摘要信息
docker images --no-trunc
显示完整的镜像信息
2、docker search [-option] name
搜素指定名字的镜像
[root@javaEE ~]# docker search mysql
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
mysql MySQL is a widely used, open-source relation… 11236 [OK]
mariadb MariaDB Server is a high performing open sou… 4272 [OK]
mysql/mysql-server Optimized MySQL Server Docker images. Create… 835 [OK]
centos/mysql-57-centos7 MySQL 5.7 SQL database server 91
mysql/mysql-cluster Experimental MySQL Cluster Docker images. Cr… 88
centurylink/mysql Image containing mysql. Optimized to be link… 59 [OK]
databack/mysql-backup Back up mysql databases to... anywhere! 45
deitch/mysql-backup REPLACED! Please use http://hub.docker.com/r… 41 [OK]
prom/mysqld-exporter 40 [OK]
tutum/mysql Base docker image to run a MySQL database se… 35
linuxserver/mysql A Mysql container, brought to you by LinuxSe… 30
schickling/mysql-backup-s3 Backup MySQL to S3 (supports periodic backup… 30 [OK]
mysql/mysql-router MySQL Router provides transparent routing be… 21
centos/mysql-56-centos7 MySQL 5.6 SQL database server 20
arey/mysql-client Run a MySQL client from a docker container 18 [OK]
fradelg/mysql-cron-backup MySQL/MariaDB database backup using cron tas… 16 [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
devilbox/mysql Retagged MySQL, MariaDB and PerconaDB offici… 3
ansibleplaybookbundle/mysql-apb An APB which deploys RHSCL MySQL 2 [OK]
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]
centos/mysql-80-centos7 MySQL 8.0 SQL database server 1
monasca/mysql-init A minimal decoupled init container for mysql 0
options 说明
- –no-trunc : 显示完整的镜像描述
- -s列出收藏数不小于指定值的镜像。
- –automated : 只列出 automated build类型的镜像;
[root@javaEE ~]# docker search --no-trunc mysql
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
mysql MySQL is a widely used, open-source relational database management system (RDBMS). 11236 [OK]
mariadb MariaDB Server is a high performing open source relational database, forked from MySQL. 4272 [OK]
mysql/mysql-server Optimized MySQL Server Docker images. Created, maintained and supported by the MySQL team at Oracle 835 [OK]
centos/mysql-57-centos7 MySQL 5.7 SQL database server 91
mysql/mysql-cluster Experimental MySQL Cluster Docker images. Created by the MySQL team at Oracle 88
centurylink/mysql Image containing mysql. Optimized to be linked to another image/container. 59 [OK]
databack/mysql-backup Back up mysql databases to... anywhere! 45
deitch/mysql-backup REPLACED! Please use http://hub.docker.com/r/databack/mysql-backup instead. 41 [OK]
prom/mysqld-exporter 40 [OK]
tutum/mysql Base docker image to run a MySQL database server 35
schickling/mysql-backup-s3 Backup MySQL to S3 (supports periodic backups) 30 [OK]
linuxserver/mysql A Mysql container, brought to you by LinuxServer.io. 30
mysql/mysql-router MySQL Router provides transparent routing between your application and back-end MySQL Servers. 21
centos/mysql-56-centos7 MySQL 5.6 SQL database server 20
arey/mysql-client Run a MySQL client from a docker container 18 [OK]
fradelg/mysql-cron-backup MySQL/MariaDB database backup using cron task manager 16 [OK]
genschsa/mysql-employees MySQL Employee Sample Database 7 [OK]
yloeffler/mysql-backup This image runs mysqldump to backup data using cronjob. Based on tutumcloud/mysql-backup 7 [OK]
openshift/mysql-55-centos7 DEPRECATED: A Centos7 based MySQL v5.5 image for use with OpenShift v3 6
cytopia/mysql-5.5 MySQL 5.5 on CentOS 7 4 [OK]
devilbox/mysql Retagged MySQL, MariaDB and PerconaDB official Docker images 3
jelastic/mysql An image of the MySQL database server maintained by Jelastic PaaS. 2
ansibleplaybookbundle/mysql-apb An APB which deploys RHSCL MySQL 2 [OK]
widdpim/mysql-client Dockerized MySQL Client (5.7) including Curl to automate MySQL related jobs 1 [OK]
monasca/mysql-init A minimal decoupled init container for mysql 0
[root@javaEE ~]#
3、docker pull 镜像名字[tag]
docker pull 镜像名字[:TAG],下载镜像,tag 指定版本,不写默认为最新版本
4、docker rmi [option] 镜像名字ID
删除镜像,指定的id或者指定名字的镜像
注意当我们的镜像存在实例容器时,即使容器没有运行,也要先删除容器,在删除镜像。否则无法删除
[root@javaEE ~]# docker rmi d1165f221234
Untagged: hello-world:latest
Untagged: hello-world@sha256:df5f5184104426b65967e016ff2ac0bfcd44ad7899ca3bbcf8e44e4461491a9e
Deleted: sha256:d1165f2212346b2bab48cb01c1e39ee8ad1be46b87873d9ca7a4e434980a7726
Deleted: sha256:f22b99068db93900abe17f7f5e09ec775c2826ecfe9db961fea68293744144bd
[root@javaEE ~]# docker iamges
docker: 'iamges' is not a docker command.
See 'docker --help'
[root@javaEE ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
tomcat latest 710ec5c56683 33 hours ago 668MB
nginx latest 08b152afcfae 2 weeks ago 133MB
centos latest 300e315adb2f 8 months ago 209MB
[root@javaEE ~]#
options 说明
1.删除单个,默认 docker rmi -f 镜像ID
2. 删除多个,docker rmi -f 镜像名1:TAG 镜像名2:TAG
3. 删除全部,docker rmi -f $(docker images -qa)、
三、容器指令(普通)
镜像创建运行的容器实例
我们首先要有一个镜像才可以创建容器,使用centos镜像容器演示。
首先拉取centos的镜像,docker pull centos
1、新建并启动容器
docker run [OPTIONS] IMAGE [COMMAND] [ARG...]
options说明
OPTIONS说明(常用):有些是一个减号,有些是两个减号
--name="容器新名字": 为容器指定一个名称;
-d: 后台运行容器,并返回容器ID,也即启动守护式容器;
-i:以交互模式运行容器,通常与 -t 同时使用;
-t:为容器重新分配一个伪输入终端,通常与 -i 同时使用;
-P: 随机端口映射;
-p: 指定端口映射,有以下四种格式
ip:hostPort:containerPort
ip::containerPort
hostPort:containerPort
containerPort
示例:
[root@javaEE ~]# docker run --name mysce centos
[root@javaEE ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f2ff14ffbe4b centos "/bin/bash" 11 hours ago Up 11 hours adoring_volhard
-it options结合,启动交互式容器
使用镜像centos:latest以交互模式启动一个容器,在容器内执行/bin/bash命令。
docker run -it centos /bin/bash (-it 默认执行)
[root@javaEE ~]# docker run -it --name mycentos1 centos
[root@8cb650713436 /]# ls
bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var
[root@8cb650713436 /]# ll
bash: ll: command not found
[root@8cb650713436 /]#
2、列出当前正在运行的容器docekr ps
docekr ps
[root@javaEE ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f2ff14ffbe4b centos "/bin/bash" 11 hours ago Up 11 hours adoring_volhard
options说明
-a :列出当前所有正在运行的容器+历史上运行过的
-l :显示最近创建的容器。
-n:显示最近n个创建的容器。
-q :静默模式,只显示容器编号。
–no-trunc :不截断输出。
[root@javaEE ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
8cb650713436 centos "/bin/bash" 14 minutes ago Exited (127) About a minute ago mycentos1
b3a774026c3e centos "/bin/bash" 16 minutes ago Exited (0) 16 minutes ago mysce
f2ff14ffbe4b centos "/bin/bash" 11 hours ago Up 11 hours adoring_volhard
1ce8c5158464 centos "/bin/bash" 11 hours ago Exited (0) 11 hours ago kind_poitras
82432affd6f2 centos "/bin/bash" 12 hours ago Exited (137) 11 hours ago mycentos
ff134dbfc051 centos "/bin/bash" 12 hours ago Exited (0) 12 hours ago silly_keller
f6882eebf058 centos "/bin/bash" 24 hours ago Exited (0) 24 hours ago youthful_wescoff
3、退出容器
第一种:exit
退出容器,并且停止容器
第二种:ctrl+P+Q
退出容器,但是容器不会停止
4、启动我们停止的容器
docker start 容器ID或者容器名
eg:
[root@javaEE ~]# docker start 8cb650713436
5、重启容器
docker restart 容器ID或者容器名
[root@javaEE ~]# docker restart 8cb650713436
8cb650713436
[root@javaEE ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
8cb650713436 centos "/bin/bash" 21 minutes ago Up 4 seconds mycentos1
f2ff14ffbe4b centos "/bin/bash" 11 hours ago Up 11 hours adoring_volhard
[root@javaEE ~]#
6、停止容器
docker stop 容器ID或者容器名
[root@javaEE ~]# docker stop 8cb650713436
8cb650713436
7、强行停止容器
docker kill 容器ID或者容器名
[root@javaEE ~]# docker kill f2ff14ffbe4b
f2ff14ffbe4b
[root@javaEE ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@javaEE ~]#
8、删除已经停止的容器[docker rm 容器ID]
上述指令是删除单个
删除多个容器
docker rm -f $(docker ps -a -q)
或者
docker ps -a -q | xargs docker rm
四、容器指令(重要)
1、启动守护式进程
#使用镜像centos:latest以后台模式启动一个容器
docker run -d centos
问题:然后docker ps -a 进行查看, 会发现容器已经退出
很重要的要说明的一点: Docker容器后台运行,就必须有一个前台进程.
容器运行的命令如果不是那些一直挂起的命令(比如运行top,tail),就是会自动退出的。
*这个是docker的机制问题,比如你的web容器,我们以nginx为例,正常情况下,我们配置启动服务只需要启动响应的service即可。例如
service nginx start
但是,这样做,nginx为后台进程模式运行,就导致docker前台没有运行的应用,
这样的容器后台启动后,会立即自杀因为他觉得他没事可做了.
所以,最佳的解决方案是,将你要运行的程序以前台进程的形式运行*
2、查看容器的日志
docker logs -f -t --tail 容器ID
* -t 是加入时间戳
* -f 跟随最新的日志打印
* --tail 数字 显示最后多少条
[root@javaEE ~]# docker logs -f -t --tail 5 8cb650713436
2021-08-08T12:04:02.941055306Z bash: docker: command not found
2021-08-08T12:04:09.447141165Z [root@8cb650713436 /]# exit
2021-08-08T12:04:09.447158293Z exit
[root@8cb650713436 /]# exit
2021-08-08T12:13:48.445379297Z [root@8cb650713436 /]# exit
3、查看容器内运行的进程
docker top 容器ID
[root@javaEE ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
53586b8a3a5b centos "/bin/bash" 15 seconds ago Up 15 seconds naughty_turing
[root@javaEE ~]# docker top 53586b8a3a5b
UID PID PPID C STIME TTY TIME CMD
root 11133 11112 0 20:26 ? 00:00:00 /bin/bash
[root@javaEE ~]#
4、查看容器内部的细节,以json格式展示数据
docker inspect 容器ID
[root@javaEE ~]# docker inspect 53586b8a3a5b
[
{
"Id": "53586b8a3a5b9fc6bd1d7493d51a9a21f339096b80bc228df9ae0c45371c0112",
"Created": "2021-08-08T12:26:34.038597872Z",
"Path": "/bin/bash",
"Args": [],
"State": {
"Status": "running",
"Running": true,
"Paused": false,
"Restarting": false,
"OOMKilled": false,
"Dead": false,
"Pid": 11133,
"ExitCode": 0,
"Error": "",
"StartedAt": "2021-08-08T12:26:34.712132614Z",
"FinishedAt": "0001-01-01T00:00:00Z"
},
"Image": "sha256:300e315adb2f96afe5f0b2780b87f28ae95231fe3bdd1e16b9ba606307728f55",
"ResolvConfPath": "/var/lib/docker/containers/53586b8a3a5b9fc6bd1d7493d51a9a21f339096b80bc228df9ae0c45371c0112/resolv.conf",
"HostnamePath": "/var/lib/docker/containers/53586b8a3a5b9fc6bd1d7493d51a9a21f339096b80bc228df9ae0c45371c0112/hostname",
"HostsPath": "/var/lib/docker/containers/53586b8a3a5b9fc6bd1d7493d51a9a21f339096b80bc228df9ae0c45371c0112/hosts",
"LogPath": "/var/lib/docker/containers/53586b8a3a5b9fc6bd1d7493d51a9a21f339096b80bc228df9ae0c45371c0112/53586b8a3a5b9fc6bd1d7493d51a9a21f339096b80bc228df9ae0c45371c0112-json.log",
"Name": "/naughty_turing",
"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以上是关于docke-03-常用命令的主要内容,如果未能解决你的问题,请参考以下文章