Docker常用命令
Posted MangataTS
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Docker常用命令相关的知识,希望对你有一定的参考价值。
文章目录
Docker命令
帮助命令
我们输入 docker
就能查看所有选项和命令:
操作选项
Options:
--config string Location of client config files (default "/root/.docker")
-c, --context string Name of the context to use to connect to the daemon (overrides DOCKER_HOST env var and default context set with "docker context use")
-D, --debug Enable debug mode
-H, --host list Daemon socket(s) to connect to
-l, --log-level string Set the logging level ("debug"|"info"|"warn"|"error"|"fatal") (default "info")
--tls Use TLS; implied by --tlsverify
--tlscacert string Trust certs signed only by this CA (default "/root/.docker/ca.pem")
--tlscert string Path to TLS certificate file (default "/root/.docker/cert.pem")
--tlskey string Path to TLS key file (default "/root/.docker/key.pem")
--tlsverify Use TLS and verify the remote
-v, --version Print version information and quit
管理命令
Management Commands:
app* Docker App (Docker Inc., v0.9.1-beta3)
builder Manage builds
buildx* Docker Buildx (Docker Inc., v0.8.2-docker)
config Manage Docker configs
container Manage containers
context Manage contexts
image Manage images
manifest Manage Docker image manifests and manifest lists
network Manage networks
node Manage Swarm nodes
plugin Manage plugins
scan* Docker Scan (Docker Inc., v0.17.0)
secret Manage Docker secrets
service Manage services
stack Manage Docker stacks
swarm Manage Swarm
system Manage Docker
trust Manage trust on Docker images
volume Manage volumes
普通命令
Commands:
attach Attach local standard input, output, and error streams to a running container
build Build an image from a Dockerfile
commit Create a new image from a container's changes
cp Copy files/folders between a container and the local filesystem
create Create a new container
diff Inspect changes to files or directories on a container's filesystem
events Get real time events from the server
exec Run a command in a running container
export Export a container's filesystem as a tar archive
history Show the history of an image
images List images
import Import the contents from a tarball to create a filesystem image
info Display system-wide information
inspect Return low-level information on Docker objects
kill Kill one or more running containers
load Load an image from a tar archive or STDIN
login Log in to a Docker registry
logout Log out from a Docker registry
logs Fetch the logs of a container
pause Pause all processes within one or more containers
port List port mappings or a specific mapping for the container
ps List containers
pull Pull an image or a repository from a registry
push Push an image or a repository to a registry
rename Rename a container
restart Restart one or more containers
rm Remove one or more containers
rmi Remove one or more images
run Run a command in a new container
save Save one or more images to a tar archive (streamed to STDOUT by default)
search Search the Docker Hub for images
start Start one or more stopped containers
stats Display a live stream of container(s) resource usage statistics
stop Stop one or more running containers
tag Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE
top Display the running processes of a container
unpause Unpause all processes within one or more containers
update Update configuration of one or more containers
version Show the Docker version information
wait Block until one or more containers stop, then print their exit codes
当然对于某一个命令我们可以通过 docker command --help
命令来查看对应的详细用法,例如:docker ps --help
当前的终端就会给你反馈:
Usage: docker ps [OPTIONS]
List containers
Options:
-a, --all Show all containers (default shows just running)
-f, --filter filter Filter output based on conditions provided
--format string Pretty-print containers using a Go template
-n, --last int Show n last created containers (includes all states) (default -1)
-l, --latest Show the latest created container (includes all states)
--no-trunc Don't truncate output
-q, --quiet Only display container IDs
-s, --size Display total file sizes
docker中使用镜像并创建容器的过程
- 获取镜像
对于一个镜像可以自己用Dockerfile
脚本构建,当然也可以去拉去镜像库中的文件,假设我们拉去文件,则先输入 docker search xxx
去查找我们需要的镜像
root@mangata:/home/mangata# docker search nginx
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
nginx Official build of Nginx. 16986 [OK]
linuxserver/nginx An Nginx container, brought to you by LinuxS… 169
bitnami/nginx Bitnami nginx Docker Image 131 [OK]
ubuntu/nginx Nginx, a high-performance reverse proxy & we… 52
bitnami/nginx-ingress-controller Bitnami Docker Image for NGINX Ingress Contr… 18 [OK]
rancher/nginx-ingress-controller 10
clearlinux/nginx Nginx reverse proxy server with the benefits… 4
ibmcom/nginx-ingress-controller Docker Image for IBM Cloud Private-CE (Commu… 4
bitnami/nginx-ldap-auth-daemon 3
rancher/nginx 2
bitnami/nginx-exporter 2
vmware/nginx 2
rancher/nginx-ingress-controller-defaultbackend 2
circleci/nginx This image is for internal use 2
vmware/nginx-photon 1
rapidfort/nginx RapidFort optimized, hardened image for NGINX 1
wallarm/nginx-ingress-controller Kubernetes Ingress Controller with Wallarm e… 1
bitnami/nginx-intel 1
kasmweb/nginx An Nginx image based off nginx:alpine and in… 1
rancher/nginx-conf 0
ibmcom/nginx-ingress-controller-ppc64le Docker Image for IBM Cloud Private-CE (Commu… 0
continuumio/nginx-ingress-ws 0
rancher/nginx-ingress-controller-amd64 0
ibmcom/nginx-ppc64le Docker image for nginx-ppc64le 0
rancher/nginx-ssl 0
然后通过 docker image pull xxx
去拉去下载我们需要的镜像
root@mangata:/home/mangata# docker image pull nginx
Using default tag: latest
latest: Pulling from library/nginx
b85a868b505f: Pull complete
f4407ba1f103: Pull complete
4a7307612456: Pull complete
935cecace2a0: Pull complete
8f46223e4234: Pull complete
fe0ef4c895f5: Pull complete
Digest: sha256:10f14ffa93f8dedf1057897b745e5ac72ac5655c299dade0aa434c71557697ea
Status: Downloaded newer image for nginx:latest
docker.io/library/nginx:latest
- 查看本地镜像
我们通过 docker image ls
可以查看当前本地存在的镜像
例如:
root@mangata:/home/mangata# docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest 55f4b40fe486 34 hours ago 142MB
hello-world latest feb5d9fea6a5 9 months ago 13.3kB
ubuntu 15.10 9b9cb95443b5 5 years ago 137MB
- 删除镜像
有安装镜像就有删除镜像,我们可以通过 docker rmi 镜像ID
去删除对应的镜像文件
- 运行镜像,生成容器运行
我们通过 docker run
命令来运行镜像生成一个新的容器,注意一下 run
是通过运行镜像 生成容器 并 运行容器 ,而 start
是 运行 一个容器,这个容器也许是之前被停止了的,如果只是 docker container ps
只会显示当前运行中的容器,但是我们可以给这个命令加一个参数 -a
显示所有容器信息
root@mangata:/home/mangata# docker run -d -p 80:80 nginx
48e19849b516f0b71e234e1899734ee9de6f22e8ef31f274674f37432a5f2386
在屏幕中打印的这一串字符是该容器对应的ID
- 停止运行
既然有开始,那么一定有结束,我们可以通过 docker stop 容器ID
来关闭指定ID的容器的服务
root@mangata:/home/mangata# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
48e19849b516 nginx "/docker-entrypoint.…" 6 seconds ago Up 5 seconds 0.0.0.0:80->80/tcp, :::80->80/tcp dazzling_gould
root@mangata:/home/mangata# docker stop 48e19849b516
48e19849b516
- 暂停进程
有的时候我们并不想直接终止进程,而是需要暂停,那么我们可以通过 docker pause CONTAINER 容器ID
对指定的容器进行暂停,此时我们通过 docker container ls -a
可以看到容器的状态变成了Pause
- 恢复暂停进程
我们通过 docker unpause CONTAINER 容器ID
就能让暂停的容器服务恢复正常
- 查看容器or镜像
我们可以通过 docker ps
或者通过 docker container ps
查看容器,通过 docker image ls
查看本地镜像文件
- 删除容器
通过 docker container rm 容器ID
删除指定的容器
那么以上就是一个简单的容器运行的流程了
细谈docker run
我们通过 docker run --help
能够查看启动命令的形式以及参数
当然这个命令的作用也显而易见,就是创建一个新的容器,然后将镜像放入运行
Usage: docker run [OPTIONS] IMAGE [COMMAND] [ARG...]
Run a command in a new container
Options:
--add-host list Add a custom host-to-IP mapping (host:ip)
-a, --attach list Attach to STDIN, STDOUT or STDERR
--blkio-weight uint16 Block 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
--cgroupns string Cgroup namespace to use (host|private)
'host': Run the container in the Docker host's cgroup namespace
'private': Run the container in its own private cgroup namespace
'': Use the cgroup namespace as configured by the
default-cgroupns-mode option on the daemon (default)
--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, --detach Run container in background and print container ID
--detach-keys string Override the key sequence for detaching a container
--device list Add 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 list Set environment variables
--env-file list Read in a file of environment variables
--expose list Expose 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 duration Maximum time to allow one check to run (ms|s|m|h) (default 0s)
--help Print usage
-h, --hostname string Container host name
--init Run an init inside the container that forwards signals and reaps processes
-i, --interactive Keep STDIN open even if not attached
--ip string IPv4 address (e.g., 172.30.100.104)
--ip6 string IPv6 address (e.g., 2001:db8::33)
--ipc string IPC mode to use
--isolation string Container isolation technology
--kernel-memory bytes Kernel memory limit
-l, --label list Set meta data on a container
--label-file list Read in a line delimited file of labels
--link list Add link to another container
--link-local-ip list Container IPv4/IPv6 link-local addresses
--log-driver string Logging driver for the container
--log-opt list Log driver options
--mac-address string Container 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 int Tune 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-healthcheck Disable any container-specified HEALTHCHECK
--oom-kill-disable Disable OOM Killer
--oom-score-adj int Tune host's OOM preferences (-1000 to 1000)
--pid string PID namespace to use
--pids-limit int Tune container pids limit (set -1 for unlimited)
--platform string Set platform if server is multi-platform capable
--privileged Give extended privileges to this container
-p, --publish list Publish a container's port(s) to the host
-P, --publish-all Publish all exposed ports to random ports
--pull string Pull image before running ("always"|"missing"|"never") (default "missing")
--read-only Mount the container's root filesystem as read only
--restart string Restart policy to apply when a container exits (default "no")
--rm Automatically remove the container when it exits
--runtime string Runtime to use for this container
--security-opt list Security Options
--shm-size bytes Size 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 int Timeout (in seconds) to stop a container
--storage-opt list Storage driver options for the container
--sysctl map Sysctl options (default map[])
--tmpfs list Mount a tmpfs directory
-t, --tty Allocate a pseudo-TTY
--ulimit ulimit Ulimit options (default [])
-u, --user string Username or UID (format: <name|uid>[:<group|gid>])
--userns string User namespace to use
--uts string UTS namespace to use
-v, --volume list Bind mount a volume
--volume-driver string Optional volume driver for the container
--volumes-from list Mount volumes from the specified container(s)
-w, --workdir string Working directory inside the container
-d 选项 后台运行
-d, --detach Run container in background and print container ID
--detach-keys string Override the key sequence for detaching a container
--device list Add 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)
我们可以看到 -d
其实也被细分为了很多种,如果是-d
那么就是默认--detach
,其作用就是在 后台运行 一个容器,并且将容器的 ID 打印到终端
-p 80:80 端口映射 ,宿主机端口:容器端口
-p, --publish list Publish a container's port(s) to the host
该选项将容器内的端口映射到宿主机上
以上是关于Docker常用命令的主要内容,如果未能解决你的问题,请参考以下文章