Docker 为我们提供了大量的命令,直接在终端运行 docker --help
即可查看 Docker 支持的命令。如果需要查看具体命令的使用方式,可以使用 docker COMMAND --help
。Docker 提供了 55 条命令,由于篇幅的原因,这里将介绍 Docker 常用的命令,其他的可以参见 Docker 官方文档。
查看 Docker 版本和系统信息
我们可以分别使用 docker version
和 docker info
查看 Docker 版本和系统信息
[iteblog@iteblog.com ~]$ docker version Client: Docker Engine - Community Version: 19.03.1 API version: 1.40 Go version: go1.12.5 Git commit: 74b1e89 Built: Thu Jul 25 21:18:17 2019 OS/Arch: darwin/amd64 Experimental: false Server: Docker Engine - Community Engine: Version: 19.03.1 API version: 1.40 (minimum version 1.12) Go version: go1.12.5 Git commit: 74b1e89 Built: Thu Jul 25 21:17:52 2019 OS/Arch: linux/amd64 Experimental: false containerd: Version: v1.2.6 GitCommit: 894b81a4b802e4eb2a91d1ce216b8817763c29fb runc: Version: 1.0.0-rc8 GitCommit: 425e105d5a03fabd737a126ad93d62a9eeede87f docker-init: Version: 0.18.0 GitCommit: fec3683 [iteblog@iteblog.com ~]$ docker info Client: Debug Mode: false Server: Containers: 35 Running: 0 Paused: 0 Stopped: 35 Images: 23 Server Version: 19.03.1 Storage Driver: overlay2 Backing Filesystem: extfs Supports d_type: true Native Overlay Diff: true Logging Driver: json-file Cgroup Driver: cgroupfs Plugins: Volume: local Network: bridge host ipvlan macvlan null overlay Log: awslogs fluentd gcplogs gelf journald json-file local logentries splunk syslog Swarm: inactive Runtimes: runc Default Runtime: runc Init Binary: docker-init containerd version: 894b81a4b802e4eb2a91d1ce216b8817763c29fb runc version: 425e105d5a03fabd737a126ad93d62a9eeede87f init version: fec3683 Security Options: seccomp Profile: default ..... Live Restore Enabled: false Product License: Community Engine
搜索镜像
有时候,我们想到 Docker 仓库下载自己想要的镜像,但是仓库里面的镜像太多,我们很难记住完整镜像名字,这时候,我们可以使用下面命令去仓库搜索镜像:
[iteblog@iteblog.com ~]$ docker search --format "table {{.Name}}\t{{.Description}}\t{{.IsOfficial}}" nginx NAME DESCRIPTION OFFICIAL nginx Official build of Nginx. [OK] jwilder/nginx-proxy Automated Nginx reverse proxy for docker con… richarvey/nginx-php-fpm Container running Nginx + PHP-FPM capable of… linuxserver/nginx An Nginx container, brought to you by LinuxS… bitnami/nginx Bitnami nginx Docker Image tiangolo/nginx-rtmp Docker image with Nginx using the nginx-rtmp… jc21/nginx-proxy-manager Docker container for managing Nginx proxy ho… nginxdemos/hello NGINX webserver that serves a simple page co… nginx/unit NGINX Unit is a dynamic web and application … jlesage/nginx-proxy-manager Docker container for Nginx Proxy Manager nginx/nginx-ingress NGINX Ingress Controller for Kubernetes privatebin/nginx-fpm-alpine PrivateBin running on an Nginx, php-fpm & Al… schmunk42/nginx-redirect A very simple container to redirect HTTP tra… blacklabelops/nginx Dockerized Nginx Reverse Proxy Server. centos/nginx-112-centos7 Platform for running nginx 1.12 or building … centos/nginx-18-centos7 Platform for running nginx 1.8 or building n… nginxinc/nginx-unprivileged Unprivileged NGINX Dockerfiles raulr/nginx-wordpress Nginx front-end for the official wordpress:f… nginx/nginx-prometheus-exporter NGINX Prometheus Exporter sophos/nginx-vts-exporter Simple server that scrapes Nginx vts stats a… mailu/nginx Mailu nginx frontend pebbletech/nginx-proxy nginx-proxy sets up a container running ngin… ansibleplaybookbundle/nginx-apb An APB to deploy NGINX centos/nginx-110-centos7 Platform for running nginx 1.10 or building … wodby/nginx Generic nginx
docker search
的详细使用请参见这里。
下载镜像
从 Docker 仓库下载镜像到本地可以使用 docker image pull
或 docker pull
,具体如下:
[iteblog@iteblog.com ~]$ docker image pull nginx Using default tag: latest latest: Pulling from library/nginx 8ec398bc0356: Pull complete a53c868fbde7: Pull complete 79daf9dd140d: Pull complete Digest: sha256:70821e443be75ea38bdf52a974fd2271babd5875b2b1964f05025981c75a6717 Status: Downloaded newer image for nginx:latest docker.io/library/nginx:latest 或者 [iteblog@iteblog.com ~]$ docker pull nginx Using default tag: latest latest: Pulling from library/nginx 8ec398bc0356: Pull complete a53c868fbde7: Pull complete 79daf9dd140d: Pull complete Digest: sha256:70821e443be75ea38bdf52a974fd2271babd5875b2b1964f05025981c75a6717 Status: Downloaded newer image for nginx:latest docker.io/library/nginx:latest
下载镜像的用法为 docker pull [OPTIONS] NAME[:TAG|@DIGEST]
或者 docker image pull [OPTIONS] NAME[:TAG|@DIGEST]
,如果你没指定 TAG
,则默认使用 latest
,如果需要使用特定的 tag,可以如下使用:
[iteblog@iteblog.com ~]$ docker pull nginx:stable-alpine-perl
查看下载的所有镜像
可以使用 docker images
或者 docker image ls
查看本机下载的所有镜像:
[iteblog@iteblog.com ~]$ docker images REPOSITORY TAG IMAGE ID CREATED SIZE nginx latest 5ad3bd0e67a9 10 days ago 127MB ubuntu latest ccc6e87d482b 2 weeks ago 64.2MB iteblog/hbase-phoenix-docker 1.0 978d21a23ec3 3 months ago 1.55GB hello-world latest fce289e99eb9 13 months ago 1.84kB [iteblog@iteblog.com ~]$ docker image ls REPOSITORY TAG IMAGE ID CREATED SIZE nginx latest 5ad3bd0e67a9 10 days ago 127MB ubuntu latest ccc6e87d482b 2 weeks ago 64.2MB iteblog/hbase-phoenix-docker 1.0 978d21a23ec3 3 months ago 1.55GB hello-world latest fce289e99eb9 13 months ago 1.84kB
删除本地镜像
可以使用 docker image rm [OPTIONS] IMAGE [IMAGE...]
删除本机下载的镜像:
[iteblog@iteblog.com ~]$ docker image rm 655180bd3625 7a5a0eeb30df 0d27b6964665
运行容器
假设我们想使用 Docker 运行刚刚下载的 nginx 服务器,可运行以下命令:
[iteblog@iteblog.com ~]$ docker run -p 8888:80 nginx
运行上面的命令之后,我们就启动了一个 nginx 服务器,-p 8888:80
会告诉 Docker 将本机端口 8888 映射到 Docker 容器端口 80,这时候我们就可以通过本机的 http://localhost:8888 访问刚刚启动的 nginx 服务器。
上面命令直接在命令行上运行容器的,如果命令行关闭,这个容器也自然关闭了。我们可以通过加上 -d
参数以分离模式启动容器:
[iteblog@iteblog.com ~]$ docker run -p 8888:80 -d nginx d2acc1eaa8f964bbdf64c6c84df58d35ac04ef88cc40f0bdefb7dee59a5b6a91
注意:docker run = docker creat + docker start
列出所有正在运行的容器
可以使用 docker ps
命令列出所有正在运行的容器:
[iteblog@iteblog.com ~]$ docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES d2acc1eaa8f9 nginx "nginx -g 'daemon of…" 3 minutes ago Up 3 minutes 0.0.0.0:8888->80/tcp goofy_mcnulty
上面命令列出了容器的 ID、镜像名字、创建时间、以及容器名字。在这个例子里面,容器的名字为 goofy_mcnulty,我们可以在启动容器的时候指定我们自定义的名字:
[iteblog@iteblog.com ~]$ docker run --name iteblog_nginx -p 8889:80 -d nginx 76a85168b716c17c7c002912fa05ee8c6af1fd6e3d59e9215543d971dc619971 [iteblog@iteblog.com ~]$ docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 76a85168b716 nginx "nginx -g 'daemon of…" 4 seconds ago Up 3 seconds 0.0.0.0:8889->80/tcp iteblog_nginx d2acc1eaa8f9 nginx "nginx -g 'daemon of…" 9 minutes ago Up 9 minutes 0.0.0.0:8888->80/tcp goofy_mcnulty
可以看出,容器的名字已经变成 iteblog_nginx 了。
启动,停止,重启和杀死容器
要停止一个运行的容器,可以使用 docker stop
,并且指定要停止的容器 ID 或者容器名称,也就是上面 docker ps
命令显示的 CONTAINER ID 或 NAMES:
[iteblog@iteblog.com ~]$ docker stop iteblog_nginx iteblog_nginx [iteblog@iteblog.com ~]$ docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES d2acc1eaa8f9 nginx "nginx -g 'daemon of…" 12 minutes ago Up 12 minutes 0.0.0.0:8888->80/tcp goofy_mcnulty
可以看出,名为 iteblog_nginx 的容器已经停止运行了。
可以使用 docker start/restart
分别启动/重启动容器:
[iteblog@iteblog.com ~]$ docker start iteblog_nginx iteblog_nginx [iteblog@iteblog.com ~]$ docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 76a85168b716 nginx "nginx -g 'daemon of…" 5 minutes ago Up 3 seconds 0.0.0.0:8889->80/tcp iteblog_nginx d2acc1eaa8f9 nginx "nginx -g 'daemon of…" 14 minutes ago Up 14 minutes 0.0.0.0:8888->80/tcp goofy_mcnulty [iteblog@iteblog.com ~]$ docker restart iteblog_nginx iteblog_nginx [iteblog@iteblog.com ~]$ docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 76a85168b716 nginx "nginx -g 'daemon of…" 5 minutes ago Up 1 second 0.0.0.0:8889->80/tcp iteblog_nginx d2acc1eaa8f9 nginx "nginx -g 'daemon of…" 14 minutes ago Up 14 minutes 0.0.0.0:8888->80/tcp goofy_mcnulty
可以使用 docker kill
杀死正在运行的容器:
[iteblog@iteblog.com ~]$ docker kill iteblog_nginx iteblog_nginx [iteblog@iteblog.com ~]$ docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES d2acc1eaa8f9 nginx "nginx -g 'daemon of…" 15 minutes ago Up 15 minutes 0.0.0.0:8888->80/tcp goofy_mcnulty
本文仅仅列出 Docker 一部分命令,还有大量的命令并未涉及,如果有需要可以去 Docker 官方文档去查阅,这里就不再介绍了。
本博客文章除特别声明,全部都是原创!原创文章版权归过往记忆大数据(过往记忆)所有,未经许可不得转载。
本文链接: 【Docker 入门教程:常用命令介绍】(https://www.iteblog.com/archives/9775.html)