Docker常用命令

目录

镜像相关命令

查看镜像

搜索镜像

拉取镜像

删除镜像

容器相关命令

查看容器

创建与启动容器

交互方式创建容器

守护方式创建容器

停止与启动容器

停止容器

启动容器

文件拷贝

将宿主机的文件拷贝到容器内

将容器内的文件拷贝到宿主机

目录挂载

查看容器IP地址

查看容器运行的各种数据

输出IP地址

删除指定容器


镜像相关命令

查看镜像

docker images
结果列名描述
REPOSITORY镜像名称
TAG镜像标签
IMAGE ID镜像ID
CREATED镜像的创建日期(不是获取该镜像的日期)
SIZE镜像大小

这些镜像都是在宿主机的 /var/lib/docker 目录下

搜索镜像

docker search [OPTIONS] TERM
OPTIONS
选项简写选项全称说明
-f--filter filterFilter output based on conditions provided
--format stringPretty-print search using a Go template
--limit intMax number of search results (default 25)
--no-truncDon't truncate output

拉取镜像

docker pull [OPTIONS] NAME[:TAG|@DIGEST]
OPTIONS
选项简写选项全称说明
-a--all-tagsDownload all tagged images in the repository
 --disable-content-trustSkip image verification (default true)
 --platform stringSet platform if server is multi-platform capable
-q--quietSuppress verbose output

删除镜像

docker rmi [OPTIONS] IMAGE [IMAGE...]
OPTIONS
选项简写选项全称说明
-f--forceForce removal of the image
 --no-pruneDo not delete untagged parents

容器相关命令

查看容器

docker ps [OPTIONS]
OPTIONS
选项简写选项全称说明
-a--allShow all containers (default shows just running)
-f--filter filterFilter output based on conditions provided
--format stringPretty-print containers using a Go template
-n--last intShow n last created containers (includes all states) (default -1)
-l--latestShow the latest created container (includes all states)
--no-truncDon't truncate output
-q--quietOnly display numeric IDs
-s--sizeDisplay total file sizes
结果列名说明
CONTAINER ID容器的ID
IMAGE容器使用的镜像
COMMAND命令
CREATED容器的创建日期
STATUS容器的状态
PORTS容器的端口
NAMES容器的名称

创建与启动容器

docker run [OPTIONS] IMAGE [COMMAND] [ARG...]
OPTIONS
选项简写选项全称说明
--add-host list Add a custom host-to-IP mapping (host:ip)
-a  --attach listAttach to STDIN, STDOUT or STDERR 
 --blkio-weight uint16Block IO (relative weight), between 10 and 1000, or 0 to disable (default 0) 
--blkio-weight-device list  Block IO weight (relative device weight) (default [])
--cap-add list Add Linux capabilities 
 --cap-drop list Drop Linux capabilities
--cgroup-parent string  Optional parent cgroup for the container
--cidfile string Write the container ID to the file 
 --cpu-period int Limit CPU CFS (Completely Fair Scheduler) period
 --cpu-quota int Limit CPU CFS (Completely Fair Scheduler) quota
--cpu-rt-period int Limit CPU real-time period in microseconds 
--cpu-rt-runtime int  Limit CPU real-time runtime in microseconds
 -c--cpu-shares int CPU shares (relative weight) 
--cpus decimal  Number of CPUs
--cpuset-cpus string  CPUs in which to allow execution (0-3, 0,1)
--cpuset-mems string  MEMs in which to allow execution (0-3, 0,1)
-d  --detachRun container in background and print container ID 
 --detach-keys stringOverride the key sequence for detaching a container 
 --device listAdd a host device to the container 
--device-cgroup-rule list Add a rule to the cgroup allowed devices list 
--device-read-bps list Limit read rate (bytes per second) from a device (default []) 
 --device-read-iops list Limit read rate (IO per second) from a device (default [])
--device-write-bps list  Limit write rate (bytes per second) to a device (default [])
--device-write-iops list Limit write rate (IO per second) to a device (default []) 
--disable-content-trust Skip image verification (default true) 
 --dns list Set custom DNS servers
 --dns-option list Set DNS options
--dns-search list  Set custom DNS search domains
--domainname string Container NIS domain name 
--entrypoint string Overwrite the default ENTRYPOINT of the image 
 -e --env listSet environment variables 
 --env-file listRead in a file of environment variables 
 --expose listExpose a port or a range of ports 
 --gpus gpu-request GPU devices to add to the container ('all' to pass all GPUs)
--group-add list Add additional groups to join 
--health-cmd string Command to run to check health 
--health-interval duration Time between running the check (ms|s|m|h) (default 0s) 
--health-retries int  Consecutive failures needed to report unhealthy
--health-start-period duration  Start period for the container to initialize before starting health-retries countdown (ms|s|m|h) (default 0s)
--health-timeout durationMaximum time to allow one check to run (ms|s|m|h) (default 0s)
--helpPrint usage
-h--hostname stringContainer host name
--initRun an init inside the container that forwards signals and reaps processes
-i--interactiveKeep STDIN open even if not attached
--ip stringIPv4 address (e.g., 172.30.100.104)
--ip6 stringIPv6 address (e.g., 2001:db8::33)
--ipc stringIPC mode to use
--isolation stringContainer isolation technology
--kernel-memory bytesKernel memory limit
-l--label listSet meta data on a container
--label-file listRead in a line delimited file of labels
--link listAdd link to another container
--link-local-ip listContainer IPv4/IPv6 link-local addresses
--log-driver stringLogging driver for the container
--log-opt listLog driver options
--mac-address stringContainer MAC address (e.g., 92:d0:c6:0a:29:33)
 -m --memory bytes Memory limit
 --memory-reservation bytes Memory soft limit
 --memory-swap bytes Swap limit equal to memory plus swap: '-1' to enable unlimited swap
 --memory-swappiness intTune container memory swappiness (0 to 100) (default -1) 
 --mount mount Attach a filesystem mount to the container
 --name string Assign a name to the container
 --network network Connect a container to a network
 --network-alias list Add network-scoped alias for the container
--no-healthcheckDisable any container-specified HEALTHCHECK
--oom-kill-disableDisable OOM Killer
--oom-score-adj intTune host's OOM preferences (-1000 to 1000)
--pid stringPID namespace to use
--pids-limit intTune container pids limit (set -1 for unlimited)
--platform stringSet platform if server is multi-platform capable
--privilegedGive extended privileges to this container
-p--publish listPublish a container's port(s) to the host
-p--publish-allPublish all exposed ports to random ports
--read-onlyMount the container's root filesystem as read only
--restart stringRestart policy to apply when a container exits (default "no")
--rmAutomatically remove the container when it exits
--runtime stringRuntime to use for this container
--security-opt listSecurity Options
--shm-size bytesSize of /dev/shm
--sig-proxy  Proxy received signals to the process (default true)
 --stop-signal string Signal to stop a container (default "SIGTERM")
 --stop-timeout intTimeout (in seconds) to stop a container 
--storage-opt listStorage driver options for the container
--sysctl mapSysctl options (default map[])
--tmpfs listMount a tmpfs directory
-t--ttyAllocate a pseudo-TTY
--ulimit ulimitUlimit options (default [])
-u--user stringUsername or UID (format: <name|uid>[:<group|gid>])
--userns stringUser namespace to use
--uts stringUTS namespace to use
-v--volume listBind mount a volume
--volume-driver stringOptional volume driver for the container
--volumes-from listMount volumes from the specified container(s)
-w--workdir stringWorking directory inside the container
常用OPTIONS
选项简写说明
-i表示运行容器
-t表示容器启动后会进入其命令行。加入这两个参数后,容器创建就能登录进去。即分配一个伪终端。
--name表示给创建的容器命名
-v表示目录映射关系(前者是宿主机目录,后者是映射到宿主机上的目录),可以使用多个 -v 做多个目录或文件映射。注意:最好做目录映射,在宿主机上做修改,然后共享到容器上。
-d在run后面加上-d参数,则会创建一个守护式容器在后台运行(这样创建容器后不会自动登录容器,如果只加 -i -t 两个参数,创建后就会自动进去容器)
-p表示端口映射,前者是宿主机端口,后者是容器内的映射端口。可以使用多个 -p 做多个端口映射

交互方式创建容器

# 创建
docker run -it --name=容器名称 镜像名称:标签 /bin/bash
# 退出当前容器
exit

守护方式创建容器

# 创建
docker run -di --name=容器名称 镜像名称:标签
# 进入容器
docker exec -it 容器名称 (或者容器ID) /bin/bash

停止与启动容器

停止容器

docker stop 容器名称或者容器ID

启动容器

docker start 容器名称或者容器ID

文件拷贝

将宿主机的文件拷贝到容器内

docker cp 需要拷贝的文件或目录 容器名称:容器目录

将容器内的文件拷贝到宿主机

docker cp 容器名称:容器目录 需要拷贝的文件或目录

目录挂载

我们可以在创建容器的时候,将宿主机的目录与容器内的目录进行映射,这样我们就可以通过修改宿主机某个目录的文件从而去影响容器。

创建容器时,添加 -v 参数,后边为宿主机目录:容器目录。

如:

docker run -di -v /usr/local/myHtml:/usr/local/myHtml --name=mycentos3 centos:7

如果你共享的是多级的目录,可能会出现权限不足的提示。

这是因为 CentOS7 中的安全模块 selinux 把权限禁掉了,我们需要添加参数 --privileged=true 来解决挂载的目录没有权限的问题。

查看容器IP地址

查看容器运行的各种数据

docker inspect 容器名称或容器ID

输出IP地址

docker inspect --format='{{.NetworkSettings.IPAddress}}' 容器名称或容器ID

删除指定容器

docker rm 容器名称或容器ID
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

猫咪老师QAQ

赏一点猫粮叭

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值