优秀的编程知识分享平台

网站首页 > 技术文章 正文

Docker从入门到精通之Docker命令行参考手册

nanyue 2024-08-18 19:50:39 技术文章 7 ℃

docker ps

查看运行的容器

Option

Name, shorthand

Description

-a , --all

展示所有容器

-f , --filter

按条件过滤,比如status=exited

-q , --quite

仅展示容器id

举例:

$ docker ps -a

$ docker ps -q

docker images

展示所有镜像

语法

docker images [OPTIONS] [REPOSITORY[:TAG]]

Options

Name, shorthand

Description

-a , --all

展示所有镜像

-f , --filter

按条件过滤 dangling:显示tag为空的镜像,值只有true和false label:这个是根据标签进行过滤,其中lable的值,是docker在编译的时候配置的或者在Dockerfile中配置的 before:这个是根据时间来进行过滤,其中before的value表示某个镜像构建时间之前的镜像列表 since:跟before正好相反,表示的是在某个镜像构建之后构建的镜像

--no-trunc

不截断输出

-q , --quite

仅展示镜像id

举例:

列出所有镜像

$ docker images
REPOSITORY      TAG       IMAGE ID       CREATED         SIZE
wh0116/alpine   v1        142964f69674   5 hours ago     5.59MB
alpine          v1        46e27702d3b3   5 hours ago     5.59MB

根据镜像名称和tag列出所有镜像

$ docker images java

$ docker images java:8

显示没打tag的镜像

$ docker images --filter "dangling=true"

docker run

docker run:创建一个新容器并运行

$ docker run [OPTIONS] IMAGE[:TAG|@DIGEST] [COMMAND] [ARG...]

Detached vs foreground (-d)

启动Docker容器时,必须首先决定是在后台以“分离”模式还是以默认的前台模式运行容器:

-d=false: Detached mode: Run container in the background, print new container id

前台模式

  • -a : 指定标准输入输出内容类型,可选 STDIN/STDOUT/STDERR 三项
  • -t : 为容器重新分配一个伪输入终端,通常与 -i 同时使用
  • -i : 以交互模式运行容器,通常与 -t 同时使用
$ docker run -a stdin -a stdout -i -t ubuntu /bin/bash
$ docker run -d  -p 8761:8761 \
--name eureka \
--restart always \
wh0116/eureka:1.0

验证eureka是否部署成功,访问http://ip:8761

--name 名称

为容器指定一个名称,如果不指定,默认会生成一个随机字符作为该容器名称

-p 端口

指定端口映射,格式为:主机(宿主)端口:容器端口

-P 随机端口

随机端口映射,容器内部端口随机映射到主机的端口

-h 主机名

指定容器的hostname

-m 内存

设定容器内存最大值

-v 卷

绑定一个卷

-w 工作目录

指定容器内的工作目录;语法:-w="", --workdir="": Working directory inside the container

-e 环境变量

设置环境变量。语法:-e 变量名="变量值"

--expose 开放端口

开放一个端口或一组端口;语法:--expose=[]

--ip IPv4地址

设置ip地址;语法:--ip=192.168.1.3

--link 链接容器

添加链接到另一个容器

$ docker run -d --name app1 --link app2

--network 网络

为容器设置一个网络;语法:docker run --network=<network-name>

$ docker run -d --network=micro-net busybox

docker attach

将本地输入、输出和错误流附加到正在运行的容器上

语法

$ docker attach [OPTIONS] CONTAINER

举例:

$ docker run -d --name topdemo ubuntu /usr/bin/top -b
$ docker attach topdemo
$ docker run --name test -d -it debian
docker ps -a | grep test

注意

使用CTR+C 或EXIT退出容器,会导致容器关闭

docker exec

在运行的容器中执行命令

语法

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

举例:

创建一个ubuntu_bash容器并启动bash,容器创建后会进入容器

$ docker run --name ubuntu_bash --rm -it ubuntu bash

进入容器

$ docker exec -it ubuntu_bash /bin/sh

$ docker exec -it ubuntu_bash bash

不进入容器执行命令(此处执行date,获取系统时间):

$ docker exec -it ubuntu_bash date

注意:

CTR+C 或exit退出容器,不会停止容器运行

docker create

创建一个新容器,与docker run的区别是docker create只创建,不会运行容器

语法:docker create [OPTIONS] IMAGE [COMMAND] [ARG...]

OPTIONS参考 docker create | Docker Documentation

docker pull

从仓库拉取镜像,默认从docker hub拉取

语法:

docker pull [OPTIONS] NAME[:TAG|@DIGEST]

举例:

$ docker pull debian

docker 会拉取debian的latest版本的镜像

$ docker pull debian:jessie

拉取tag为jessie的debian镜像

docker push

将本地镜像推送到镜像仓库,需要先登录

语法:

$ docker push [OPTIONS] NAME[:TAG]

举例:

把本地镜像alpine推送到远程仓库

  1. 先登录到docker hub
  2. $ docker login
    Login with your Docker ID to push and pull images from Docker Hub. If you don
    't have a Docker ID, head over to https://hub.docker.com to create one.
    Username: 输入dock hub登录用户名
    Password: 输入密码
  3. 推送alpine到远程仓库
  4. $ docker push wh0116/alpine
    Using default tag: latest
    The push refers to repository [docker.io/wh0116/alpine]
    An image does not exist locally with the tag: wh0116/alpine
  5. 上述提示说明本地不存在对应tag的镜像,需先打tag再push
  6. 本地镜像打tag
  7. $ docker tag alpine wh0116/alpine:1.0
  8. 推送对应tag镜像到远程仓库
  9. $ docker push wh0116/alpine:1.0
    The push refers to repository [docker.io/wh0116/alpine]
    8d3ac3489996: Mounted from library/alpine
    1.0: digest: sha256:e7d88de73db3d3fd9b2d63aa7f447a10fd0220b7cbf39803c803f2af9ba256b3 size: 528

给容器打tag,推送到仓库

a. 本地镜像仓库

$ docker container commit 62ae67436291 alpine:v1

b. 远程仓库

$ docker container commit 62ae67436291 wh0116/alpine:v1

docker rm

删除一个或多个容器

语法

$ docker rm [OPTIONS] CONTAINER [CONTAINER...]

Options

Name, shorthand

Description

--force , -f

强制删除正在运行的容器

--link , -l

删除指定的link(容器间网络连接)

--volumes, -v

删除与容器关联的匿名卷

举例:

$ docker rm eureka

强制删除一个容器,可以用容器名称或容器id,如下示例

$ docker rm -f eureka
$ docker rm -f 8a4c66494214

删除两个容器间的网络连接

$ docker rm --link /webapp/redis
/webapp/redis

删除所有容器

$ docker rm $(docker ps -aq)

删除所有停止运行的容器

$ docker rm $(docker ps --filter status=exited -q)

删除一个容器和挂载的卷

$ docker rm -v redis

docker rmi

删除一个或多个镜像,可根据镜像id和镜像名称删除

语法

$ docker rmi [OPTIONS] IMAGE [IMAGE...]

Options

Name, shorthand

Description

--force , -f

强制删除正在运行的容器

--no-prune

不删除为打tag的父项

举例:

$ docker rmi fd484f19954f

$ docker rmi alpine:latest

强制删除镜像

$ docker rmi -f fd484f19954f

$ docker rmi -f alpine:latest

docker info

显示 Docker 系统信息,包括镜像和容器数等

$ docker info
Client:
 Context:    default
 Debug Mode: false
 Plugins:
  app: Docker App (Docker Inc., v0.9.1-beta3)
  buildx: Docker Buildx (Docker Inc., v0.7.1-docker)
  scan: Docker Scan (Docker Inc., v0.8.0)

Server:
 Containers: 1
  Running: 1
  Paused: 0
  Stopped: 0
 Images: 6
 Server Version: 20.10.12
 Storage Driver: overlay2
...................

docker logs

获取容器日志

语法

$ docker logs [OPTIONS] CONTAINER

Options

Name, shorthand

Description

--details

展示更多日志详细信息

--follow,-f

跟踪日志输出

--tail,-n

显示最新的N条日志记录

用例:

$ docker logs eureka

$ docker logs 8a4c66494214

$ docker logs -n 1000 8a4c66494214

$ docker logs -f eureka

docker start

启动一个或多个已停止的容器

语法

$ docker start [OPTIONS] CONTAINER [CONTAINER...]

用例:

$ docker start my_container

docker stop

停止一个或多个运行的容器

语法

$ docker stop [OPTIONS] CONTAINER [CONTAINER...]

用例:

$ docker stop my_container

docker restart

重启一个或多个容器

语法

$ docker restart [OPTIONS] CONTAINER [CONTAINER...]

用例:

$ docker restart my_container

docker stats

实时显示容器资源使用情况

语法

$ docker stats [OPTIONS] [CONTAINER...]

用例:

$ docker stats
CONTAINER ID   NAME      CPU %     MEM USAGE / LIMIT     MEM %     NET I/O       BLOCK I/O   PIDS
8a4c66494214   eureka    0.85%     200.1MiB / 1.946GiB   10.04%    1.47kB / 0B   0B / 0B     85

docker network 网络管理

网络管理,可以用子命令创建、查看、删除、连接、断开网络操作。

网络类型

网络类型

描述

bridge

默认网络,bridge是隔离网络,不能与其它容器通信

host

Host模式下,容器将共享主机的网络堆栈,并且主机的所有接口都可供容器使用.容器的主机名将与主机系统上的主机名匹配,破坏容器隔离性。

none

禁用网络,容器不能进行网络通信

overlay

跨主机容器通信

子命令列表

Command

Description

docker network connect

Connect a container to a network

docker network create

Create a network

docker network disconnect

Disconnect a container from a network

docker network inspect

Display detailed information on one or more networks

docker network ls

List networks

docker network prune

Remove all unused networks

docker network rm

Remove one or more networks

docker network ls

列出所有网络

语法

$ docker network ls [OPTIONS]

docker network create

创建网络

语法

$ docker network create [OPTIONS] NETWORKR

Options

Name, shorthand

Default

Description

--attachable


Enable manual container attachment

--aux-address


Auxiliary IPv4 or IPv6 addresses used by Network driver

--config-from


The network from which to copy the configuration

--config-only


Create a configuration only network

--driver , -d

bridge

Driver to manage the Network

--gateway


IPv4 or IPv6 Gateway for the master subnet

--ingress


Create swarm routing-mesh network

--internal


Restrict external access to the network

--ip-range


Allocate container ip from a sub-range

--ipam-driver


IP Address Management Driver

--ipam-opt


Set IPAM driver specific options

--ipv6


Enable IPv6 networking

--label


Set metadata on a network

--opt , -o


Set driver specific options

--scope


Control the network's scope

--subnet


Subnet in CIDR format that represents a network segment

用例:

创建一个桥接网络

$ docker network create -d bridge my-bridge-network

创建一个overlay网络

创建overlay网络,必须是在swam模式下

$ docker network create -d overlay --attachable --subnet=172.30.0.0/24 mysql_net

创建mysql的bridge网络

$ docker network create -d bridge --attachable --subnet=172.30.0.0/24 mysql_net

docker network connect

将容器连接到一个网络上,可以是容器名或容器ID

语法

$ docker network connect [OPTIONS] NETWORK CONTAINER

Options

Name, shorthand

Description

--alias

为容器添加网络范围的别名

--driver-opt

driver options for the network

--ip

指定IP地址

--ip6

指定IPv6地址

--link

添加链接到另一个容器

--link-local-ip

添加容器的链接到本地地址

用例:

连接一个运行中的容器(container1)到test_net网络

$ docker network connect test_net container1

启动或创建容器时,连接到test_net网络

$ docker run -itd --network=test_net busybox

连接容器到test_net网络,并给容器分配指定ip

$ docker network connect --ip 10.10.36.122 test_net container2

使用--link实现连个容器互连

$ docker network connect --link container1 container2

docker network disconnect

断开容器与网络的连接

语法

$ docker network disconnect [OPTIONS] NETWORK CONTAINER

Name, shorthand

Default

Description

--force , -f


Force the container to disconnect from a network

用例:

$ docker network disconnect test_net container1

docker network inspect

展示一个或多个网络的详细信息

语法

$ docker network inspect [OPTIONS] NETWORK [NETWORK...]

用例:

$ docker network inspect test_net

docker network rm

删除一个或多个网络,NETWORK可以是网络名称,也可以是对应的ID

语法

$ docker network rm NETWORK [NETWORK...]

用例:

删除一个网络

$ docker network rm my-net

删除多个网络

$ docker network rm 3695c422697f my-net

docker tag

给镜像打tag

语法

$ docker tag SOURCE_IMAGE[:TAG] TARGET_IMAGE[:TAG]

用例:

  1. 通过名称镜像名称打tag
$ docker tag alpine alpine:v1
  1. 通过镜像ID打tag
$ docker tag c059bfaa849c alpine:v3
  1. 为推送到仓库的镜像打tag,镜像名称必须是仓库地址和端口号,以下已docker hub私人仓库为例
$ docker tag c059bfaa849c wh0116/alpine:v3

docker volume 数据卷

卷管理,可以用子命令创建、查看、删除数据卷操作。默认地址位于 /var/lib/docker/volumes

$ docker volume --help
Usage:  docker volume COMMAND

Manage volumes

Commands:
  create      Create a volume
  inspect     Display detailed information on one or more volumes
  ls          List volumes
  prune       Remove all unused local volumes
  rm          Remove one or more volumes

docker volume create

创建一个卷

语法

$ docker volume create [OPTIONS] [VOLUME]

Options

Name, shorthand

Default

Description

--driver , -d

local

Specify volume driver name

--label


Set metadata for a volume

--name


Specify volume name

--opt , -o


Set driver specific options

用例:

创建一个卷,并挂载到容器上

$ docker volume create hello
hello
$ docker run -d -v hello:/world alpine ls /world

docker volume rm

删除一个或多个数据卷

语法

$ docker volume rm [OPTIONS] VOLUME [VOLUME...]

Options

Name, shorthand

Description

--force,-f

Force the removal of one or more volumes

用例:

$ docker volume rm hello

docker volume ls

展示本地所有数据卷

语法

$ docker volume ls [OPTIONS]

Options

Name, shorthand

Default

Description

--filter , -f


Provide filter values (e.g. 'dangling=true')

--format


Pretty-print volumes using a Go template

--quiet , -q


Only display volume names

用例:

查看所有卷

$ docker volume ls

docker volume inspect

查看一个或多个卷的详细信息

语法

$ docker volume inspect [OPTIONS] VOLUME [VOLUME...]

用例:

查看hello数据卷的详细信息

$ docker volume inspect hello
[
    {
        "CreatedAt": "2022-01-25T09:51:30Z",
        "Driver": "local",
        "Labels": {},
        "Mountpoint": "/var/lib/docker/volumes/hello/_data",
        "Name": "hello",
        "Options": {},
        "Scope": "local"
    }
]

docker volume prune

移除所有未使用的本地卷

语法

$ docker volume prune [OPTIONS]

用例:

$ docker volume prune

docker build

从Dockerfile构建镜像

语法

$ docker build [OPTIONS] PATH | URL | -

Options

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

--output , -o


Output destination (format: type=local,dest=path)

--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|<id>[=<socket>|<key>[,<key>]])

--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

用例:

当前目录构的Dockerfile构建镜像

$ docker build -t test_app:1.0 .

从具体文件位置构建镜像

$ docker build -t test_app:1.0 -f /app/Dockerfile .

使用URL 的 Dockerfile 创建镜像,url:github.com/creack/docker-firefox

$ docker build github.com/creack/docker-firefox

docker cp

用于容器与主机之间的文件拷贝

语法

$ docker cp [OPTIONS] CONTAINER:SRC_PATH DEST_PATH|-$ docker cp [OPTIONS] SRC_PATH|- CONTAINER:DEST_PATH

用例:

将主机/www/app目录拷贝到容器96f7f14e99ab的/www目录下

$ docker cp /www/app 96f7f14e99ab:/www

将容器96f7f14e99ab的/www目录拷贝到主机的/tmp目录中

$ docker cp 96f7f14e99ab:/www /tmp/

Dockerfile详解

最近发表
标签列表