一、容器生命周期管理
1)run
用法:docker run [OPTIONS] IMAGE [COMMAND] [ARG...]
部分OPTIONS说明:
-d 后台运行容器,并返回容器ID
-h 指定容器的hostname
-e 设置容器的环境变量 格式 -e "key=value"
-i 以交互模式运行容器,通常与 -t 同时使用
-t 为容器重新分配一个伪输入终端,通常与 -i 同时使用
-m 设置容器所有进程分配的内存总量,单位为B,K,M,G
-p 指定容器端口与宿主机端口的映射关系,格式为 hostPort:containerPort
-v 用于挂载一个volume,可以用多个-v指定多个volume,格式为[host-dir]:[container=dir]:[rw|ro]
--name 指定docker run命令启动的容器的名字,若无此选项,docker将为容器随机分配一个名字
--add-host 添加一个host指向到容器中
举例:
docker run -d -p 8080:8080 --name mytomcat -v /data/webapps:/tomcat/webapps registry.xp.com:5000/centos_tomcat:1.0.0
2)stop/start/restart
该命令用于停止/启动/重启一个或者多个容器用法如下
docker stop/start/restart [OPTIONS] CONTAINER [CONTAINER...]
举例:
[root@node1 tmp]# docker stop mytomcat
mytomcat
3)rm
用法:docker rm [OPTIONS] CONTAINER [CONTAINER...]
docker rm用于删除容器,需要注意容器需要处于停止状态下才能删除,否则需要添加-f选项强制删除
举例:
[root@node1 tmp]# docker rm mytomcat
mytomcat
4)pause/unpause
用法:docker pause/unpause [OPTIONS] CONTAINER [CONTAINER...]
docker pause/unpause用于暂停/恢复容器中的所有进程
5)create
用法:docker create [OPTIONS] IMAGE [COMMAND] [ARG...]
docker create 用于创建一个新的容器,但是不启动
举例:
[root@mtceshi2 tomcat]# docker create --name mytest docker.io/ubuntu:16.04
b4fb564b907a6f49b44a6cd874670e124b466baaaeaa587ce333cbd21f296b78
二、容器操作
1)exec
用法:docker exec [OPTIONS] CONTAINER COMMAND [ARG...]
docker exec 可以进入正在运行中的容器中,并且运行命令
部分OPTIONS说明:
-i 即使没有附加也保持STDIN 打开
-t 为容器重新分配一个伪输入终端,通常与 -i 同时使用
通常-it,加上/bin/bash配合使用,用于交互式在容器中运行命令
举例:
[root@mtceshi2 tomcat]# docker exec -it mytomcat /bin/bash
root@0be00e6f223c:/usr/local/tomcat# ls
LICENSE NOTICE RELEASE-NOTES RUNNING.txt bin conf include lib logs native-jni-lib temp webapps work
root@0be00e6f223c:/usr/local/tomcat#
2)ps
用法:docker ps [OPTIONS]
docker ps 用于列出容器,-a选项可以列出所有容器包括不在运行状态的容器
举例:
[root@mtceshi2 tomcat]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
0be00e6f223c ee8 "catalina.sh run" 4 minutes ago Up 4 minutes 8080/tcp mytomcat
b4fb564b907a docker.io/ubuntu:16.04 "/bin/bash" 14 minutes ago Exited (0) 5 minutes ago mytest
3)inspect
用法:docker inspect [OPTIONS] CONTAINER|IMAGE|TASK [CONTAINER|IMAGE|TASK...]
docker inspect可以查看容器以及镜像的详细信息,如果不添加参数则列出所有信息,可以通过-f或者--format=指定输出模板以便输出指定内容
举例:
[root@mtceshi2 tomcat]# docker inspect -f {{.NetworkSettings.Gateway}} mytomcat
172.17.0.1
[root@mtceshi2 tomcat]# docker inspect 168
[
{
"Id": "sha256:168add0e5049c5ee484dfc80e3f265536166eff32485e552a96def49e48fa560",
"RepoTags": [
"registry.xp.com:5000/task_jenkins:2018-04-19-17-33"
],
"RepoDigests": [
"registry.xp.com:5000/task_jenkins@sha256:bba3c3bba9b6ba8573c65b09bc83887ce0faaa74d536caf4ca1d4bc45f6f6e62"
],
"Parent": "sha256:0a696ba0f6994e442fa819904fb0e4e4d915a06fe9bcb41d27fc125390b71e3a",
"Comment": "",
"Created": "2018-04-19T09:33:56.198475973Z",
"Container": "c4edf78bfd15ea12ef6f8b105861cd24892d24f5291ce46ff87dd368a1a2856d",
"ContainerConfig": {
"Hostname": "d7786e11577f",
"Domainname": "",
"User": "",
"AttachStdin": false,
"AttachStdout": false,
"AttachStderr": false,
"ExposedPorts": {
"8080/tcp": {}
......
4)export
用法:docker export [OPTIONS] CONTAINER
docker export 导出一个容器的文件系统并归档为一个tar包,-o指定输出的文件,代替STDOUT。配合import可以进行容器的迁移工作
举例:
[root@mtceshi2 ~]# docker export -o mytomcat.tar mytomcat
[root@mtceshi2 ~]# ls
anaconda-ks.cfg authorized_keys download-70.cgi myshell mytomcat.tar oneinstack-full.tar.gz
5)attach
用法:docker attach [OPTIONS] CONTAINER
docker attach用于连接至正在运行中的容器,需要注意的是attach是到容器的stdin,如果从这个stdin中退出,则会导致容器中止,所以不推荐使用attach连接到容器,而是使用exec
6)stats
用法:docker stats [OPTIONS] [CONTAINER...]
docker stats 显示容器资源使用情况,默认显示运行中的容器
7)top
用法:docker top CONTAINER [ps OPTIONS]
docker top 查看容器运行的进程信息
举例:
[root@mtceshi2 ~]# docker top mytomcat
UID PID PPID C STIME TTY TIME CMD
root 3190 3174 0 10:58 ? 00:00:04 /docker-java-home/jre/bin/java -Djava ... ...
三、系统日志信息
1)events
用法:docker events [OPTIONS]
docker events 从服务中中获取实时时间,主要有下列两个选项
--since 显示指定的时间戳后的事件
--until 显示指定的时间戳前的事件
举例:
[root@mtceshi2 tomcat]# docker events --since="1467302400"
2018-04-25T10:08:46.703703428+08:00 container create b4fb564b907a6f49b44a6cd874670e124b466baaaeaa587ce333cbd21f296b78 (image=docker.io/ubuntu:16.04, name=mytest)
2018-04-25T10:17:47.432217561+08:00 network connect 97029965af651a9224a1d9ce0ef8ad7402abab849f36c178c032084ad35b8863 (container=b4fb564b907a6f49b44a6cd874670e124b466baaaeaa587ce333cbd21f296b78, name=bridge, type=bridge)
2018-04-25T10:17:48.563331357+08:00 container start b4fb564b907a6f49b44a6cd874670e124b466baaaeaa587ce333cbd21f296b78 (image=docker.io/ubuntu:16.04, name=mytest)
2018-04-25T10:17:48.662116226+08:00 container die b4fb564b907a6f49b44a6cd874670e124b466baaaeaa587ce333cbd21f296b78 (exitCode=0, image=docker.io/ubuntu:16.04, name=mytest)
2018-04-25T10:17:48.818063630+08:00 network disconnect 97029965af651a9224a1d9ce0ef8ad7402abab849f36c178c032084ad35b8863 (container=b4fb564b907a6f49b44a6cd874670e124b466baaaeaa587ce333cbd21f296b78, name=bridge, type=bridge)
2018-04-25T10:18:34.597000823+08:00 container create 0be00e6f223c252c4bc6ee7d506b16586bc91571792d5f4022f01240682e1d17 (image=ee8, name=mytomcat)
2018-04-25T10:18:34.755641018+08:00 network connect 97029965af651a9224a1d9ce0ef8ad7402abab849f36c178c032084ad35b8863 (container=0be00e6f223c252c4bc6ee7d50
......
2)history
用法:docker history [OPTIONS] IMAGE
docker history 显示指定镜像的创建历史
举例:
[root@mtceshi2 ~]# docker history docker.io/tomcat:8.0.50-jre8
IMAGE CREATED CREATED BY SIZE COMMENT
ee80ae78d593 13 days ago /bin/sh -c #(nop) CMD ["catalina.sh" "run"] 0 B
<missing> 13 days ago /bin/sh -c #(nop) EXPOSE 8080/tcp 0 B
<missing> 13 days ago /bin/sh -c set -e && nativeLines="$(catalina 0 B
<missing> 13 days ago /bin/sh -c set -eux; savedAptMark="$(apt-ma 22.41 MB
<missing> 13 days ago /bin/sh -c #(nop) ENV TOMCAT_ASC_URLS=https: 0 B
<missing> 13 days ago /bin/sh -c #(nop) ENV TOMCAT_TGZ_URLS=https: 0 B
<missing> 13 days ago /bin/sh -c #(nop) ENV TOMCAT_SHA512=c8923e61 0 B
<missing> 2 weeks ago /bin/sh -c #(nop) ENV TOMCAT_VERSION=8.0.50 0 B
<missing> 2 weeks ago /bin/sh -c #(nop) ENV TOMCAT_MAJOR=8 0 B
<missing> 2 weeks ago /bin/sh -c #(nop) ENV GPG_KEYS=05AB331109497 0 B
<missing> 2 weeks ago /bin/sh -c apt-get update && apt-get install 1.932 MB
<missing> 2 weeks ago /bin/sh -c set -ex; currentVersion="$(dpkg-q 2.834 MB
<missing> 2 weeks ago /bin/sh -c #(nop) ENV OPENSSL_VERSION=1.1.0f 0 B
......
3)logs
用法:docker logs [OPTIONS] CONTAINER
docker logs 用于抓取容器的日志
-f 跟踪日志输出
--since 显示某个时间戳后的日志
举例:
[root@mtceshi2 ~]# docker logs -f mytomcat
25-Apr-2018 02:18:40.430 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server version: Apache Tomcat/8.0.50
25-Apr-2018 02:18:40.433 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server built: Feb 7 2018 20:06:05 UTC
25-Apr-2018 02:18:40.433 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server number: 8.0.50.0
25-Apr-2018 02:18:40.433 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log OS Name: Linux
25-Apr-2018 02:18:40.433 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log OS Version: 3.10.0-514.el7.x86_64
25-Apr-2018 02:18:40.433 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Architecture: amd64
25-Apr-2018 02:18:40.433 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Java Home: /usr/lib/jvm/java-8-openjdk-amd64/jre
25-Apr-2018 02:18:40.433 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log JVM Version: 1.8.0_162-8u162-b12-1~deb9u1-b12
25-Apr-2018 02:18:40.433 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log JVM Vendor: Oracle Corporation
25-Apr-2018 02:18:40.433 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log CATALINA_BASE: /usr/local/tomcat
25-Apr-2018 02:18:40.433 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log CATALINA_HOME: /usr/local/tomcat
......
四、镜像管理
1)build
用法:docker build [OPTIONS] PATH | URL | -
docker build 根据Dockerfile构建镜像,后面的PATH指的是工作目录,通常会将Dockerfile,以及镜像构建过程所需文件都存放于此
常用选项:
-t 指定镜像tag,格式为 -t name:tag
-f 指定Dockerfile文件路径,不指定的时候,默认在PATH路径下面寻找名称为Dockerfile的文件
--no-cache 构建过程中不使用缓存
举例:
[root@node1 tomcat_centos]# docker build -t mytomcat:v1 .
Sending build context to Docker daemon 833.7 MB
Step 1 : FROM registry:5000/centos:7.3.1611
---> 66ee80d59a68
Step 2 : MAINTAINER liyang liyang@pand-auto.com
---> Using cache
---> 60bfa8bbf6fb
Step 3 : RUN echo "Asia/Shanghai" > /etc/timezone
---> Using cache
---> 91d6fcebdb01
Step 4 : ENV JAVA_HOME /usr/local/jdk1.8.0_161
......
2)import
用法:docker import [OPTIONS] file|URL|- [REPOSITORY[:TAG]]
docker import 从归档文件中导入镜像,通常配合export使用进行容器迁移
举例:
[root@node1 ~]# docker import mytomcat.tar
sha256:002f26b80a6853326eb9e8e9d8d61364455cfadae190fe7beca4a7979a440d6a
3)save/load
用法:docker save/load [OPTIONS] IMAGE [IMAGE...]
此命令用于将指定镜像打包或载入tar归档格式文件,可配合使用实现镜像打包迁移;该命令默认输出,输入为STDIN,STDOUT,需要配合重定向使用或者-i,-o选项
举例:
[root@node1 ~]# docker save registry.xp.com:5000/centos_tomcat:1.0.0 > mytest.tar
[root@node1 ~]# ls
anaconda-ks.cfg Centos-7.repo epel-7.repo mytest.tar mytomcat.tar
4)rmi
用法:docker rmi [OPTIONS] IMAGE [IMAGE...]
docker rmi 删除一个或者多个镜像文件
举例:
[root@node1 tomcat_centos]# docker rmi 5fe
Deleted: sha256:5fecab2eb80c4dafc5cd84afb76f447b7654d59dfc3cb11c57714c0c5b930509
Deleted: sha256:fdc1755e5dad870c6d2844ece55f0e4317d1200afb0531f3b6fbcb71c15d7455
Deleted: sha256:28c74e9a384ce0480333f283b9a2ec82b3f95fa53dfe68125f719b93b6a383b9
Deleted: sha256:0b4e6d9813edcf718ab8c2bc0e6f2d36380e21e92475bf5ac35d5e2d1053ed24
Deleted: sha256:cbf92f0bcc62a8f428cb473544a5bcffcf37d345f68537bbcdf98fac0a542705
Deleted: sha256:da47b34f108df0f9103bbe7a1ed06aaadddc03ba69a2ede4dd886bc50316b441
Deleted: sha256:2b96f44b1595ed0151cec4f10187381eef856c87f1fed10e185e5e98ed4829fa
Deleted: sha256:20c4a01d9e29d1417a1f52e956e7b1299353764e1a53160ada6803cfac1d4da1
Deleted: sha256:6ecd665aeb57be32f418fdbe0d026e54ab108d398d10da08f5061459ba36ac19
Deleted: sha256:f630ca444a787e6474b3e15262c23ab4182222e223de61a2e3e5713d878e33b9
......
5)tag
用法:docker tag IMAGE[:TAG] IMAGE[:TAG]
docker tag 标记本地镜像,并归入某一仓库
举例:
##将镜像docker.io/tomcat:8.0标记为registry.xp.com:5000/mytomcat:v1
[root@node1 ~]# docker tag docker.io/tomcat:8.0 registry.xp.com:5000/mytomcat:v1
[root@node1 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
registry.xp.com:5000/centos_tomcat 1.0.0 0e240597b021 6 days ago 585 MB
tomcat_admin 1.0.0 bb537c7aa4ec 3 weeks ago 993.5 MB
centos_ssh 1.0.0 102d164d88d9 3 weeks ago 463 MB
registry:5000/centos_ssh 1.0.0 102d164d88d9 3 weeks ago 463 MB
docker.io/haproxy latest 8e518564f5b3 4 weeks ago 70.99 MB
registry.xp.com:5000/mytomcat v1 7aa9e977bdb9 5 weeks ago 455.7 MB
docker.io/tomcat 8.0 7aa9e977bdb9 5 weeks ago 455.7 MB
registry:5000/centos 7.3.1611 66ee80d59a68 5 months ago 191.8 MB
docker.io/w564791/pod-infrastructure latest f66f4bd9b894 10 months ago 205.8 MB
6)commit
用法:docker commit [OPTIONS] CONTAINER [REPOSITORY[:TAG]]
docker commit 可以将一个容器固化为一个新的镜像,可用于制作特定镜像
举例:
[root@mtceshi2 ~]# docker commit mytomcat mytomcat_test:v1
sha256:7e489a0b57ea74fb99e5cf4eebe0552547f8bb5eb84510216ab2e3c79b637634
[root@mtceshi2 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mytomcat_test v1 7e489a0b57ea 4 seconds ago 553.9 MB
registry.xp.com:5000/task_jenkins 2018-04-19-17-33 168add0e5049 5 days ago 619.7 MB
registry.xp.com:5000/vrbs_jenkins 2018-04-19-17-29 4cc11ed82557 5 days ago 589.8 MB
registry.xp.com:5000/vehicle_jenkins 2018-04-19-17-29 0f0cb95a951b 5 days ago 562 MB
registry.xp.com:5000/sync_jenkins 2018-04-19-17-27 0558533a3c09 5 days ago 596.5 MB
registry.xp.com:5000/mpush_jenkins 2018-04-19-17-27 4efffbea2054 5 days ago 593.4 MB
......
7)images
用法:docker images [OPTIONS] [REPOSITORY[:TAG]]
docker images 用于显示本地镜像
举例:
[root@mtceshi2 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mytomcat_test v1 7e489a0b57ea 4 seconds ago 553.9 MB
registry.xp.com:5000/task_jenkins 2018-04-19-17-33 168add0e5049 5 days ago 619.7 MB
registry.xp.com:5000/vrbs_jenkins 2018-04-19-17-29 4cc11ed82557 5 days ago 589.8 MB
registry.xp.com:5000/vehicle_jenkins 2018-04-19-17-29 0f0cb95a951b 5 days ago 562 MB
registry.xp.com:5000/sync_jenkins 2018-04-19-17-27 0558533a3c09 5 days ago 596.5 MB
registry.xp.com:5000/mpush_jenkins 2018-04-19-17-27 4efffbea2054 5 days ago 593.4 MB
......
五、镜像仓库命令
1)login/logout
用法:docker login [OPTIONS] [SERVER] 或 docker logout [SERVER]
该命令用于登入登出需要认证的docker registry;登入时可以使用-p,-u指定用户名密码
2)pull/push
用法:
docker pull [OPTIONS] NAME[:TAG|@DIGEST]
docker push [OPTIONS] NAME[:TAG]
docker pull用于从docker registry中拉去镜像,docker push则用于将本地镜像推送到docker registry中
举例:
[root@node1 ~]# docker pull redis
Using default tag: latest
Trying to pull repository docker.io/library/redis ...
latest: Pulling from docker.io/library/redis
b0568b191983: Already exists
6637dc5b29fe: Already exists
2fd86759b5ff: Pull complete
0f04862b5a3b: Pull complete
2db0056aa977: Pull complete
Digest: sha256:6b9f935e89af002225c0dcdadf1fd74245b4cc1e3e91222f7e4769c236cf80d4
3)search
用法:docker search [OPTIONS] TERM
docker search 用于搜索docker hub上的镜像
举例:
[root@node1 ~]# docker search tomcat
INDEX NAME DESCRIPTION STARS OFFICIAL AUTOMATED
docker.io docker.io/tomcat Apache Tomcat is an open source implementa... 1812 [OK]
docker.io docker.io/tomee Apache TomEE is an all-Apache Java EE cert... 50 [OK]
docker.io docker.io/dordoka/tomcat Ubuntu 14.04, Oracle JDK 8 and Tomcat 8 ba... 48 [OK]
docker.io docker.io/davidcaste/alpine-tomcat Apache Tomcat 7/8 using Oracle Java 7/8 wi... 24 [OK]
docker.io docker.io/consol/tomcat-7.0 Tomcat 7.0.57, 8080, "admin/admin" 16 [OK]
docker.io docker.io/cloudesire/tomcat Tomcat server, 6/7/8 15 [OK]
docker.io docker.io/bitnami/tomcat Bitnami Tomcat Docker Image 13 [OK]
docker.io docker.io/tutum/tomcat Base docker image to run a Tomcat applicat... 9
docker.io docker.io/jeanblanchard/tomcat Minimal Docker image with Apache Tomcat 8
docker.io docker.io/meirwa/spring-boot-tomcat-mysql-app a sample spring-boot app using tomcat and ... 8 [OK]
docker.io docker.io/aallam/tomcat-mysql Debian, Oracle JDK, Tomcat & MySQL 6 [OK]
docker.io docker.io/rightctrl/tomcat CentOS , Oracle Java, tomcat application s... 3 [OK]
docker.io docker.io/fabric8/tomcat-8 Fabric8 Tomcat 8 Image 2 [OK]
docker.io docker.io/99taxis/tomcat7 Tomcat7 1 [OK]
......
六、docker环境信息
1)info
docker info 命令可用于检查docker是否安装正确,如果安装正确,该命令会输出docker的相关配置信息
举例:
[root@mtceshi2 docker]# docker info
Containers: 0
Running: 0
Paused: 0
Stopped: 0
Images: 59
Server Version: 1.12.6
Storage Driver: devicemapper
Pool Name: docker-252:17-68151391-pool
Pool Blocksize: 65.54 kB
Base Device Size: 10.74 GB
Backing Filesystem: xfs
Data file: /dev/loop0
Metadata file: /dev/loop1
Data Space Used: 3.136 GB
Data Space Total: 107.4 GB
Data Space Available: 41.44 GB
Metadata Space Used: 5.751 MB
......
2)version
docker version 命令一般结合docker info使用提取详细的docker环境信息
举例:
[root@mtceshi2 docker]# docker version
Client:
Version: 1.12.6
API version: 1.24
Package version: docker-1.12.6-71.git3e8e77d.el7.centos.1.x86_64
Go version: go1.8.3
Git commit: 3e8e77d/1.12.6
Built: Tue Jan 30 09:17:00 2018
OS/Arch: linux/amd64
Server:
Version: 1.12.6
API version: 1.24
Package version: docker-1.12.6-71.git3e8e77d.el7.centos.1.x86_64
Go version: go1.8.3
Git commit: 3e8e77d/1.12.6
Built: Tue Jan 30 09:17:00 2018
OS/Arch: linux/amd64
网友评论