本文概述
- 查看版本
- 下载镜像
- 镜像
- 运行镜像
- 查看容器
- ps -a
- docker exec
- 删除容器
- 删除镜像
- 重新启动Docker
- 停止Docker
- 启动Docker
- docker kill
- docker commit
- docker login
- docker push
- docker network
- docker info
- 复制文件
- 查看历史
- 检查日志
- 搜索镜像
- 更新配置
- 创建卷
- 安装插件
- docker logout
Docker是一个容器化系统, 它将依赖于容器的应用程序打包并运行在容器中。使用Docker时, 你必须了解几个docker命令。本文就是关于这一点的。
如果你不知道Docker是什么, 那么你可以参加Udemy入门课程。
查看版本你要了解的第一件事是如何找到已安装的Docker版本。
[email
protected]:/home/geekflare$ docker --versionDocker version 18.09.6, build 481bc77
下载镜像假设你需要从dockerhub(docker存储库)中提取docker映像。下面的示例提取Apache HTTP服务器映像。
[email
protected]:/home/geekflare$ docker pull httpdUsing default tag: latestlatest: Pulling from library/httpdf5d23c7fed46: Pull completeb083c5fd185b: Pull completebf5100a89e78: Pull complete98f47fcaa52f: Pull complete622a9dd8cfed: Pull completeDigest: sha256:8bd76c050761610773b484e411612a31f299dbf7273763103edbda82acd73642Status: Downloaded newer image for httpd:latest[email
protected]:/home/geekflare$
镜像列出所有拉到系统上的docker映像, 以及映像详细信息, 例如TAG / IMAGE ID / SIZE等。
[email
protected]:/home/geekflare$ docker imagesREPOSITORY
TAG
IMAGE ID
CREATED
SIZEhttpd
latest
ee39f68eb241
2 days ago
154MBhello-world
latest
fce289e99eb9
6 months ago
1.84kBsequenceiq/hadoop-docker
2.7.0
789fa0a3b911
4 years ago
1.76GB
运行镜像运行命令中提到的docker镜像。此命令将创建一个将在其中运行Apache HTTP服务器的docker容器。
[email
protected]:/home/geekflare$ docker run -it -d httpd09ca6feb6efc0578951a3e2557ed5855b2edda39a795d9703eb54d975930fe6e
查看容器ps列出了所有正在运行的docker容器以及容器详细信息。
[email
protected]:/home/geekflare$ docker psCONTAINER ID
IMAGE
COMMAND
CREATED
STATUS
PORTS
NAMES09ca6feb6efc
httpd
"httpd-foreground"
36 seconds ago
Up 33 seconds
80/tcp
suspicious_bell
如你所见, Apache服务器正在此docker容器中运行。
ps -a列出所有正在运行/已退出/已停止的Docker容器以及容器详细信息。
[email
protected]:/home/geekflare$ docker ps -aCONTAINER ID
IMAGE
COMMAND
CREATED
STATUS
PORTS
NAMES09ca6feb6efc
httpd
"httpd-foreground"
51 seconds ago
Up 49 seconds
80/tcp
suspicious_bell2f6fb3381078
sequenceiq/hadoop-docker:2.7.0
"/etc/bootstrap.sh -d"
2 weeks ago
Exited (137) 9 days ago
quizzical_raman9f397feb3a46
sequenceiq/hadoop-docker:2.7.0
"/etc/bootstrap.sh -…"
2 weeks ago
Exited (255) 2 weeks ago
2122/tcp, 8030-8033/tcp, 8040/tcp, 8042/tcp, 8088/tcp, 19888/tcp, 49707/tcp, 50010/tcp, 50020/tcp, 50070/tcp, 50075/tcp, 50090/tcp
determined_ritchie9b6343d3b5a0
hello-world
"/hello"
2 weeks ago
Exited (0) 2 weeks ago
peaceful_mclean
docker exec访问docker容器并在容器内运行命令。在此示例中, 我正在访问apache服务器容器。
[email
protected]:/home/geekflare$ docker exec -it 09ca6feb6efc bash[email
protected]:/usr/local/apache2# lsbin
build
cgi-bin
conf
error
htdocs
icons
include
logs
modules[email
protected]:/usr/local/apache2#
键入exit, 然后按Enter键以退出容器。
删除容器删除命令中提到的具有容器ID的Docker容器。
[email
protected]:/home/geekflare$ docker rm 9b6343d3b5a09b6343d3b5a0
运行以下命令以检查是否已卸下容器。
[email
protected]:/home/geekflare$ docker ps -aCONTAINER ID
IMAGE
COMMAND
CREATED
STATUS
PORTS
NAMES09ca6feb6efc
httpd
"httpd-foreground"
About a minute ago
Up About a minute
80/tcp
suspicious_bell2f6fb3381078
sequenceiq/hadoop-docker:2.7.0
"/etc/bootstrap.sh -d"
2 weeks ago
Exited (137) 9 days ago
quizzical_raman9f397feb3a46
sequenceiq/hadoop-docker:2.7.0
"/etc/bootstrap.sh -…"
2 weeks ago
Exited (255) 2 weeks ago
2122/tcp, 8030-8033/tcp, 8040/tcp, 8042/tcp, 8088/tcp, 19888/tcp, 49707/tcp, 50010/tcp, 50020/tcp, 50070/tcp, 50075/tcp, 50090/tcp
determined_ritchie
删除镜像使用命令中提到的docker image ID删除docker image
[email
protected]:/home/geekflare$ docker rmi fce289e99eb9Untagged: hello-world:latestUntagged: [email
protected]:41a65640635299bab090f783209c1e3a3f11934cf7756b09cb2f1e02147c6ed8Deleted: sha256:fce289e99eb9bca977dae136fbe2a82b6b7d4c372474c9235adc1741675f587eDeleted: sha256:af0b15c8625bb1938f1d7b17081031f649fd14e6b233688eea3c5483994a66a3[email
protected]:/home/geekflare$
重新启动Docker使用命令中提到的容器ID重新启动Docker容器。
[email
protected]:/home/geekflare$ docker restart 09ca6feb6efc09ca6feb6efc
运行以下命令, 并检查STATUS参数以验证容器是否最近启动。
[email
protected]:/home/geekflare$ docker psCONTAINER ID
IMAGE
COMMAND
CREATED
STATUS
PORTS
NAMES09ca6feb6efc
httpd
"httpd-foreground"
6 minutes ago
Up 9 seconds
80/tcp
suspicious_bell
停止Docker停止具有命令中提到的容器ID的容器。
[email
protected]:/home/geekflare$ docker stop 09ca6feb6efc09ca6feb6efc
运行以下命令以检查容器是否仍在运行或已停止。
[email
protected]:/home/geekflare$ docker psCONTAINER ID
IMAGE
COMMAND
CREATED
STATUS
PORTS
NAMES
启动Dockerdocker中的此命令使用命令中提到的容器ID启动docker容器。
[email
protected]:/home/geekflare$ docker start 09ca6feb6efc09ca6feb6efc
运行以下命令以检查容器是否已启动。
[email
protected]:/home/geekflare$ docker psCONTAINER ID
IMAGE
COMMAND
CREATED
STATUS
PORTS
NAMES09ca6feb6efc
httpd
"httpd-foreground"
8 minutes ago
Up 3 seconds
80/tcp
suspicious_bell
docker kill立即停止docker容器。 Docker stop命令可以优雅地停止容器, 这就是kill和stop命令之间的区别。
[email
protected]:/home/geekflare$ docker kill 09ca6feb6efc09ca6feb6efc
运行以下命令以查看容器是否被杀死。
[email
protected]:/home/geekflare$ docker psCONTAINER ID
IMAGE
COMMAND
CREATED
STATUS
PORTS
NAMES
docker commit使用本地系统中命令中提到的容器ID保存新的docker镜像。在下面的示例中, geekflare是用户名, 而httpd_image是镜像名称。
[email
protected]:/home/geekflare$ docker commit 09ca6feb6efc geekflare/httpd_imagesha256:d1933506f4c1686ab1a1ec601b1a03a17b41decbc21d8acd893db090a09bb31c
docker login登录到Docker Hub。系统将要求你输入Docker Hub凭据进行登录。
[email
protected]:/home/geekflare$ docker loginLogin 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: geekflarePassword:Configure a credential helper to remove this warning. Seehttps://docs.docker.com/engine/reference/commandline/login/#credentials-storeLogin Succeeded
docker push在dockerhub上上传命令中提到的映像名称的docker映像。
[email
protected]:/home/geekflare$ docker push geekflare/httpd_imageThe push refers to repository [docker.io/geekflare/httpd_image]734d9104a6a2: Pushed635721fc6973: Mounted from library/httpdbea448567d6c: Mounted from library/httpdbfaa5f9c3b51: Mounted from library/httpd9d542ac296cc: Mounted from library/httpdd8a33133e477: Mounted from library/httpdlatest: digest: sha256:3904662761df9d76ef04ddfa5cfab764b85e3eedaf10071cfbe2bf77254679ac size: 1574
docker networkdocker中的以下命令列出了群集中所有网络的详细信息。
[email
protected]:/home/geekflare$ docker network lsNETWORK ID
NAME
DRIVER
SCOPE85083e766f04
bridge
bridge
localf51d1f3379e0
host
host
local5e5d9a192c00
none
null
local
还有其他几个docker network命令。
[email
protected]:/home/geekflare$ docker networkUsage:
docker network COMMANDManage networksCommands:connect
Connect a container to a networkcreate
Create a networkdisconnect
Disconnect a container from a networkinspect
Display detailed information on one or more networksls
List networksprune
Remove all unused networksrm
Remove one or more networksRun 'docker network COMMAND --help' for more information on a command.
docker info获取有关系统上安装的docker的详细信息, 包括内核版本, 容器和映像的数量等。
[email
protected]:/home/geekflare$ docker infoContainers: 3Running: 1Paused: 0Stopped: 2Images: 3Server Version: 18.09.6Storage Driver: overlay2Backing Filesystem: extfsSupports d_type: trueNative Overlay Diff: trueLogging Driver: json-fileCgroup Driver: cgroupfsPlugins:Volume: localNetwork: bridge host macvlan null overlayLog: awslogs fluentd gcplogs gelf journald json-file local logentries splunk syslogSwarm: inactiveRuntimes: runcDefault Runtime: runcInit Binary: docker-initcontainerd version: bb71b10fd8f58240ca47fbb579b9d1028eea7c84runc version: 2b18fe1d885ee5083ef9f0838fee39b62d653e30init version: fec3683Security Options:apparmorseccompProfile: defaultKernel Version: 4.18.0-25-genericOperating System: Ubuntu 18.10OSType: linuxArchitecture: x86_64CPUs: 1Total Memory: 4.982GiBName: geekflareID: RBCP:YGAP:QG6H:B6XH:JCT2:DTI5:AYJA:M44Z:ETRP:6TO6:OPAY:KLNJDocker Root Dir: /var/lib/dockerDebug Mode (client): falseDebug Mode (server): falseUsername: geekflareRegistry: https://index.docker.io/v1/Labels:Experimental: falseInsecure Registries:127.0.0.0/8Live Restore Enabled: falseProduct License: Community Engine
复制文件将文件从Docker容器复制到本地系统。
在此示例中, 我将ID为09ca6feb6efc的docker容器内的httpd.pid文件复制到/ home / geekflare /
[email
protected]:/home/geekflare$ sudo docker cp 09ca6feb6efc:/usr/local/apache2/logs/httpd.pid /home/geekflare/[sudo] password for geekflare:
运行以下命令以检查文件是否已复制。
[email
protected]:/home/geekflare$ lsDesktop
Documents
example
examples.desktop
httpd.pid
nginx_new.yml
nginx.yml
查看历史使用命令中提到的映像名称显示docker映像的历史记录。
[email
protected]:/home/geekflare$ docker history httpdIMAGE
CREATED
CREATED BY
SIZE
COMMENTee39f68eb241
2 days ago
/bin/sh -c #(nop)
CMD ["httpd-foreground"]
0B<
missing>
2 days ago
/bin/sh -c #(nop)
EXPOSE 80
0B<
missing>
2 days ago
/bin/sh -c #(nop) COPY file:c432ff61c4993ecd…
138B<
missing>
4 days ago
/bin/sh -c set -eux;
savedAptMark="$(apt-m…
49.1MB<
missing>
4 days ago
/bin/sh -c #(nop)
ENV HTTPD_PATCHES=
0B<
missing>
4 days ago
/bin/sh -c #(nop)
ENV HTTPD_SHA256=b4ca9d05…
0B<
missing>
4 days ago
/bin/sh -c #(nop)
ENV HTTPD_VERSION=2.4.39
0B<
missing>
4 days ago
/bin/sh -c set -eux;
apt-get update;
apt-g…
35.4MB<
missing>
4 days ago
/bin/sh -c #(nop) WORKDIR /usr/local/apache2
0B<
missing>
4 days ago
/bin/sh -c mkdir -p "$HTTPD_PREFIX"
&
&
chow…
0B<
missing>
4 days ago
/bin/sh -c #(nop)
ENV PATH=/usr/local/apach…
0B<
missing>
4 days ago
/bin/sh -c #(nop)
ENV HTTPD_PREFIX=/usr/loc…
0B<
missing>
5 days ago
/bin/sh -c #(nop)
CMD ["bash"]
0B<
missing>
5 days ago
/bin/sh -c #(nop) ADD file:71ac26257198ecf6a…
69.2MB
检查日志显示命令中提到的包含ID的Docker容器的日志。
[email
protected]:/home/geekflare$ docker logs 09ca6feb6efcAH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 172.17.0.2. Set the 'ServerName' directive globally to suppress this messageAH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 172.17.0.2. Set the 'ServerName' directive globally to suppress this message[Mon Jul 15 14:01:55.400472 2019] [mpm_event:notice] [pid 1:tid 140299791516800] AH00489: Apache/2.4.39 (Unix) configured -- resuming normal operations[Mon Jul 15 14:01:55.400615 2019] [core:notice] [pid 1:tid 140299791516800] AH00094: Command line: 'httpd -D FOREGROUND'[Mon Jul 15 14:08:36.798229 2019] [mpm_event:notice] [pid 1:tid 140299791516800] AH00491: caught SIGTERM, shutting downAH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 172.17.0.2. Set the 'ServerName' directive globally to suppress this messageAH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 172.17.0.2. Set the 'ServerName' directive globally to suppress this message[Mon Jul 15 14:08:38.259870 2019] [mpm_event:notice] [pid 1:tid 139974087980160] AH00489: Apache/2.4.39 (Unix) configured -- resuming normal operations[Mon Jul 15 14:08:38.260007 2019] [core:notice] [pid 1:tid 139974087980160] AH00094: Command line: 'httpd -D FOREGROUND'[Mon Jul 15 14:09:01.540647 2019] [mpm_event:notice] [pid 1:tid 139974087980160] AH00491: caught SIGTERM, shutting downAH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 172.17.0.2. Set the 'ServerName' directive globally to suppress this messageAH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 172.17.0.2. Set the 'ServerName' directive globally to suppress this message[Mon Jul 15 14:10:43.782606 2019] [mpm_event:notice] [pid 1:tid 140281554879616] AH00489: Apache/2.4.39 (Unix) configured -- resuming normal operations[Mon Jul 15 14:10:43.782737 2019] [core:notice] [pid 1:tid 140281554879616] AH00094: Command line: 'httpd -D FOREGROUND'AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 172.17.0.2. Set the 'ServerName' directive globally to suppress this messageAH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 172.17.0.2. Set the 'ServerName' directive globally to suppress this message[Mon Jul 15 14:14:08.270906 2019] [mpm_event:notice] [pid 1:tid 140595254346880] AH00489: Apache/2.4.39 (Unix) configured -- resuming normal operations[Mon Jul 15 14:14:08.272628 2019] [core:notice] [pid 1:tid 140595254346880] AH00094: Command line: 'httpd -D FOREGROUND'
搜索镜像在dockerhub上搜索命令中提到的名称的docker镜像。
[email
protected]:/home/geekflare$ docker search hadoopNAME
DESCRIPTION
STARS
OFFICIAL
AUTOMATEDsequenceiq/hadoop-docker
An easy way to try Hadoop
611
[OK]uhopper/hadoop
Base Hadoop image with dynamic configuration…
98
[OK]harisekhon/hadoop
Apache Hadoop (HDFS + Yarn, tags 2.2 - 2.8)
54
[OK]bde2020/hadoop-namenode
Hadoop namenode of a hadoop cluster
22
[OK]kiwenlau/hadoop
Run Hadoop Cluster in Docker Containers
19izone/hadoop
Hadoop 2.8.5 Ecosystem fully distributed, Ju…
14
[OK]uhopper/hadoop-namenode
Hadoop namenode
9
[OK]bde2020/hadoop-datanode
Hadoop datanode of a hadoop cluster
9
[OK]singularities/hadoop
Apache Hadoop
8
[OK]uhopper/hadoop-datanode
Hadoop datanode
7
[OK]harisekhon/hadoop-dev
Apache Hadoop (HDFS + Yarn) + Dev Tools + Gi…
6
[OK]
更新配置更新容器配置。这将显示所有更新选项。
[email
protected]:/home/geekflare$ docker update --helpUsage:
docker update [OPTIONS] CONTAINER [CONTAINER...]Update configuration of one or more containersOptions:--blkio-weight uint16
Block IO (relative weight), between 10 and 1000, or 0 to disable(default 0)--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 the CPU real-time period in microseconds--cpu-rt-runtime int
Limit the 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)--kernel-memory bytes
Kernel memory limit-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--restart string
Restart policy to apply when a container exits
运行以下命令以命令中提到的容器ID更新Docker容器的CPU配置。
[email
protected]:/home/geekflare$ docker update -c 1 2f6fb33810782f6fb3381078
创建卷创建一个卷, Docker容器将使用该卷来存储数据。
[email
protected]:/home/geekflare$ docker volume create7e7bc886f69bb24dbdbf19402e31102a25db91bb29c56cca3ea8b0c611fd9ad0
如果未创建该卷, 请运行以下命令。
[email
protected]:/home/geekflare$ docker volume lsDRIVER
VOLUME NAMElocal
7e7bc886f69bb24dbdbf19402e31102a25db91bb29c56cca3ea8b0c611fd9ad0
安装插件安装调试环境设置为1.的docker插件vieux / sshfs。
[email
protected]:/home/geekflare$ docker plugin install vieux/sshfs DEBUG=1Plugin "vieux/sshfs" is requesting the following privileges:- network: [host]- mount: [/var/lib/docker/plugins/]- mount: []- device: [/dev/fuse]- capabilities: [CAP_SYS_ADMIN]Do you grant the above permissions? [y/N] ylatest: Pulling from vieux/sshfs52d435ada6a4: Download completeDigest: sha256:1d3c3e42c12138da5ef7873b97f7f32cf99fb6edde75fa4f0bcf9ed277855811Status: Downloaded newer image for vieux/sshfs:latestInstalled plugin vieux/sshfsRun the below command to list the docker plugins.[email
protected]:/home/geekflare$ docker plugin lsID
NAME
DESCRIPTION
ENABLED2a32d1fb95af
vieux/sshfs:latest
sshFS plugin for Docker
true
docker logout从dockerhub注销。
[email
protected]:/home/geekflare$ docker logoutRemoving login credentials for https://index.docker.io/v1/
总结
希望你现在对docker命令有一个不错的了解。在你的开发或实验室环境中尝试这些命令以进行练习和学习。
【26个带有示例的Docker命令】如果你有兴趣学习Docker和Kubernetes, 请查看此在线课程。
推荐阅读
- 如何安装Docker Compose和安装程序()
- 如何安装Puppet Bolt自动执行Sysadmin任务()
- 使用这些顶级工具自动化应用程序测试
- 什么是Dockerfile以及如何创建Docker镜像()
- Android开发—错误记录1(W/System.err: java.net.ConnectException: Connection refused)
- 刷题42. Trapping Rain Water
- Azure App Service-多语言/高可用/自动缩放的Web托管服务
- mybatis框架(入门方法,dao层原始开发方法,mapper代理开发)(sqlserver数据库)
- .NET(c#) 移动APP开发平台之Smobiler开发