docker命令

docker logs

docker logs -f -t --since="2017-05-31" --tail=10 container
  • 说明:
    --since : 指定输出日志开始日期。
    -f : 查看实时日志
    -t : 查看日志产生的时间戳
    -tail=10 : 查看最后的10条日志。
    container : 容器名
docker logs -f --until=2s
  • 说明:
    相对时间(relative):2013-01-02T13:23:37
    绝对时间(timestamp):42m for 42 minutes
docker logs [OPTIONS] CONTAINER

Options

Name, shorthand Default Description
--details Show extra details provided to logs
--follow , -f Follow log output
--since Show logs since timestamp (e.g. 2013-01-02T13:23:37) or relative (e.g. 42m for 42 minutes)
--tail all Number of lines to show from the end of the logs
--timestamps , -t Show timestamps
--until Show logs before a timestamp (e.g. 2013-01-02T13:23:37) or relative (e.g. 42m for 42 minutes)

docker history

docker history -q IMAGE
  • 说明:
Name, shorthand Default Description
--format Pretty-print images using a Go template
--human , -H true Print sizes and dates in human readable format
--no-trunc Don’t truncate output
--quiet , -q Only show numeric IDs

truncate 缩短、删节

docker build

docker build [OPTIONS] PATH | URL | -
  • 说明:
Name, shorthand Default Description
--add-host Add a custom host-to-IP mapping (host:ip)
--build-arg Set build-time variables
--cache-from Images to consider as cache sources
--cgroup-parent Optional parent cgroup for the container
--compress Compress the build context using gzip
--cpu-period Limit the CPU CFS (Completely Fair Scheduler) period
--cpu-quota Limit the CPU CFS (Completely Fair Scheduler) quota
--cpu-shares , -c CPU shares (relative weight)
--cpuset-cpus CPUs in which to allow execution (0-3, 0,1)
--cpuset-mems MEMs in which to allow execution (0-3, 0,1)
--disable-content-trust true Skip image verification
--file , -f Name of the Dockerfile (Default is ‘PATH/Dockerfile’)
--force-rm Always remove intermediate containers
--iidfile Write the image ID to the file
--isolation Container isolation technology
--label Set metadata for an image
--memory , -m Memory limit
--memory-swap Swap limit equal to memory plus swap: ‘-1’ to enable unlimited swap
--network Set the networking mode for the RUN instructions during build
--no-cache Do not use cache when building the image
--platform Set platform if server is multi-platform capable
--progress auto Set type of progress output (auto, plain, tty). Use plain to show container output
--pull Always attempt to pull a newer version of the image
--quiet , -q Suppress the build output and print image ID on success
--rm true Remove intermediate containers after a successful build
--secret Secret file to expose to the build (only if BuildKit enabled): id=mysecret,src=/local/secret
--security-opt Security options
--shm-size Size of /dev/shm
--squash Squash newly built layers into a single new layer
--ssh SSH agent socket or keys to expose to the build (only if BuildKit enabled) (format: default|[=|[,]])
--stream Stream attaches to server to negotiate build context
--tag , -t Name and optionally a tag in the ‘name:tag’ format
--target Set the target build stage to build.
--ulimit Ulimit options

docker run

$ docker run [OPTIONS] IMAGE[:TAG|@DIGEST] [COMMAND] [ARG...]
  • 说明:
    -a stdin: 指定标准输入输出内容类型,可选 STDIN/STDOUT/STDERR 三项;
    -i: 以交互模式运行容器,通常与 -t 同时使用-it;
    -t: 为容器重新分配一个伪输入终端,通常与 -i 同时使用-it;
    -P: 随机端口映射,容器内部端口随机映射到主机的高端口;
    -p: 指定端口映射,格式为:主机(宿主)端口:容器端口;
    --name="nginx-lb": 为容器指定一个名称;
    --dns 8.8.8.8: 指定容器使用的DNS服务器,默认和宿主一致;
    --dns-search example.com: 指定容器DNS搜索域名,默认和宿主一致;
    -h "mars": 指定容器的hostname;
    -e username="ritchie": 设置环境变量;
    --env-file=[]: 从指定文件读入环境变量;
    --cpuset="0-2" or --cpuset="0,1,2": 绑定容器到指定CPU运行;
    -m :设置容器使用内存最大值;
    --net="bridge": 指定容器的网络连接类型,支持 bridge/host/none/container: 四种类型;
    --link=[]: 添加链接到另一个容器;
    --expose=[]: 开放一个端口或一组端口;
$ docker run -d -p 80:80 my_image service nginx start
  • 说明:
    后台创建容器,返回容器ID,并在容器内启动nginx
$ docker run -a stdin -a stdout -it ubuntu /bin/bash
  • 说明:
    -a stdin: 指定标准输入输出内容类型,可选 STDIN/STDOUT/STDERR 三项;
$ docker run alpine@sha256:9cacb71397b640eca97488cf08582ae4e4068513101088e9f96c9814bfda95e0 date
  • 说明:
    Image[@digest]

PID settings (--pid)

--pid=""  : Set the PID (Process) Namespace mode for the container,
             'container:<name|id>': joins another container's PID namespace
             'host': use the host's PID namespace inside the container
  • 说明:
    -- 默认情况下,所有容器都启用了PID名称空间。
    -- PID命名空间提供进程分离。PID命名空间删除了系统进程的视图,并允许包括PID 1在内的进程id被重用。
    -- 在某些情况下,您希望容器共享主机的进程名称空间,基本上允许容器内的进程查看系统上的所有进程。

Example: run htop inside a container

创建Dockerfile:

FROM alpine:latest
RUN apk add --update htop && rm -rf /var/cache/apk/*
CMD ["htop"]

依据dockerfile创建镜像myhtop:

$ docker build -t myhtop .

Use the following command to run htop inside a container:

$ docker run -it --rm --pid=host myhtop

加入另一个容器的pid名称空间可用于调试该容器。

docker run Usage

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

Options

Name, shorthand Default Description
--add-host Add a custom host-to-IP mapping (host:ip)
--attach , -a Attach to STDIN, STDOUT or STDERR
--blkio-weight Block IO (relative weight), between 10 and 1000, or 0 to disable (default 0)
--blkio-weight-device Block IO weight (relative device weight)
--cap-add Add Linux capabilities
--cap-drop Drop Linux capabilities
--cgroup-parent Optional parent cgroup for the container
--cidfile Write the container ID to the file
--cpu-count CPU count (Windows only)
--cpu-percent CPU percent (Windows only)
--cpu-period Limit CPU CFS (Completely Fair Scheduler) period
--cpu-quota Limit CPU CFS (Completely Fair Scheduler) quota
--cpu-rt-period Limit CPU real-time period in microseconds
--cpu-rt-runtime Limit CPU real-time runtime in microseconds
--cpu-shares , -c CPU shares (relative weight)
--cpus Number of CPUs
--cpuset-cpus CPUs in which to allow execution (0-3, 0,1)
--cpuset-mems MEMs in which to allow execution (0-3, 0,1)
--detach , -d Run container in background and print container ID
--detach-keys Override the key sequence for detaching a container
--device Add a host device to the container
--device-cgroup-rule Add a rule to the cgroup allowed devices list
--device-read-bps Limit read rate (bytes per second) from a device
--device-read-iops Limit read rate (IO per second) from a device
--device-write-bps Limit write rate (bytes per second) to a device
--device-write-iops Limit write rate (IO per second) to a device
--disable-content-trust true Skip image verification
--dns Set custom DNS servers
--dns-opt Set DNS options
--dns-option Set DNS options
--dns-search Set custom DNS search domains
--entrypoint Overwrite the default ENTRYPOINT of the image
--env , -e Set environment variables
--env-file Read in a file of environment variables
--expose Expose a port or a range of ports
--group-add Add additional groups to join
--health-cmd Command to run to check health
--health-interval Time between running the check (ms|s|m|h) (default 0s)
--health-retries Consecutive failures needed to report unhealthy
--health-start-period Start period for the container to initialize before starting health-retries countdown (ms|s|m|h) (default 0s)
--health-timeout Maximum time to allow one check to run (ms|s|m|h) (default 0s)
--help Print usage
--hostname , -h Container host name
--init Run an init inside the container that forwards signals and reaps processes
--interactive , -i Keep STDIN open even if not attached
--io-maxbandwidth Maximum IO bandwidth limit for the system drive (Windows only)
--io-maxiops Maximum IOps limit for the system drive (Windows only)
--ip IPv4 address (e.g., 172.30.100.104)
--ip6 IPv6 address (e.g., 2001:db8::33)
--ipc IPC mode to use
--isolation Container isolation technology
--kernel-memory Kernel memory limit
--label , -l Set meta data on a container
--label-file Read in a line delimited file of labels
--link Add link to another container
--link-local-ip Container IPv4/IPv6 link-local addresses
--log-driver Logging driver for the container
--log-opt Log driver options
--mac-address Container MAC address (e.g., 92:d0:c6:0a:29:33)
--memory , -m Memory limit
--memory-reservation Memory soft limit
--memory-swap Swap limit equal to memory plus swap: ‘-1’ to enable unlimited swap
--memory-swappiness -1 Tune container memory swappiness (0 to 100)
--mount Attach a filesystem mount to the container
--name Assign a name to the container
--net Connect a container to a network
--net-alias Add network-scoped alias for the container
--network Connect a container to a network
--network-alias Add network-scoped alias for the container
--no-healthcheck Disable any container-specified HEALTHCHECK
--oom-kill-disable Disable OOM Killer
--oom-score-adj Tune host’s OOM preferences (-1000 to 1000)
--pid PID namespace to use
--pids-limit Tune container pids limit (set -1 for unlimited)
--platform Set platform if server is multi-platform capable
--privileged Give extended privileges to this container
--publish , -p Publish a container’s port(s) to the host
--publish-all , -P Publish all exposed ports to random ports
--read-only Mount the container’s root filesystem as read only
--restart no Restart policy to apply when a container exits
--rm Automatically remove the container when it exits
--runtime Runtime to use for this container
--security-opt Security Options
--shm-size Size of /dev/shm
--sig-proxy true Proxy received signals to the process
--stop-signal SIGTERM Signal to stop a container
--stop-timeout Timeout (in seconds) to stop a container
--storage-opt Storage driver options for the container
--sysctl Sysctl options
--tmpfs Mount a tmpfs directory
--tty , -t Allocate a pseudo-TTY
--ulimit Ulimit options
--user , -u Username or UID (format: <name|uid>[:<group|gid>])
--userns User namespace to use
--uts UTS namespace to use
--volume , -v Bind mount a volume
--volume-driver Optional volume driver for the container
--volumes-from Mount volumes from the specified container(s)
--workdir , -w Working directory inside the container
$ docker  run -w /path/to/dir/ -i -t  ubuntu pwd
  • 说明
    The -w lets the command being executed inside directory given, here /path/to/dir/. If the path does not exist it is created inside the container.

docker exec Usage

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

  • 说明:
Name, shorthand Default Description
--detach , -d Detached mode: run command in the background
--detach-keys Override the key sequence for detaching a container
--env , -e Set environment variables
--interactive , -i Keep STDIN open even if not attached
--privileged Give extended privileges to the command
--tty , -t Allocate a pseudo-TTY
--user , -u Username or UID (format: <name|uid>[:<group|gid>])
--workdir , -w Working directory inside the container
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 217,406评论 6 503
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,732评论 3 393
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 163,711评论 0 353
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,380评论 1 293
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,432评论 6 392
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,301评论 1 301
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,145评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,008评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,443评论 1 314
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,649评论 3 334
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,795评论 1 347
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,501评论 5 345
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,119评论 3 328
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,731评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,865评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,899评论 2 370
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,724评论 2 354

推荐阅读更多精彩内容

  • 额外补充 查看容器运行状态 进入容器内进行命令行操作 在容器mynginx中开启一个交互模式的终端 容器生命周期管...
    tianmac阅读 4,526评论 1 2
  • Docker命令分类 容器生命周期管理 容器操作 容器rootfs命令 镜像仓库 本地镜像管理 版本信息 Dock...
    garyond阅读 2,770评论 0 4
  • docker命令集容器生命周期管理 容器操作 容器文件系统操作 镜像仓库操作 镜像管理 集群管理 其他命令 命令参...
    yichen_china阅读 374评论 0 0
  • Docker学习总结之Run命令介绍 - vikings`s blog - 博客园http://www.cnblo...
    葡萄喃喃呓语阅读 1,192评论 0 1
  • 一、Docker 简介 Docker 两个主要部件:Docker: 开源的容器虚拟化平台Docker Hub: 用...
    R_X阅读 4,386评论 0 27