本文概述
- 查看版本
- 下载镜像
- 镜像
- 运行镜像
- 查看容器
- 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命令。本文就是关于这一点的。
如果你不知道Docker是什么, 那么你可以参加Udemy入门课程。
查看版本
你要了解的第一件事是如何找到已安装的Docker版本。
[email protected]:/home/geekflare$ docker --version
Docker version 18.09.6, build 481bc77
下载镜像
假设你需要从dockerhub(docker存储库)中提取docker映像。下面的示例提取Apache HTTP服务器映像。
[email protected]:/home/geekflare$ docker pull httpd
Using default tag: latest
latest: Pulling from library/httpd
f5d23c7fed46: Pull complete
b083c5fd185b: Pull complete
bf5100a89e78: Pull complete
98f47fcaa52f: Pull complete
622a9dd8cfed: Pull complete
Digest: sha256:8bd76c050761610773b484e411612a31f299dbf7273763103edbda82acd73642
Status: Downloaded newer image for httpd:latest
[email protected]:/home/geekflare$
镜像
列出所有拉到系统上的docker映像, 以及映像详细信息, 例如TAG / IMAGE ID / SIZE等。
[email protected]:/home/geekflare$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
httpd latest ee39f68eb241 2 days ago 154MB
hello-world latest fce289e99eb9 6 months ago 1.84kB
sequenceiq/hadoop-docker 2.7.0 789fa0a3b911 4 years ago 1.76GB
运行镜像
运行命令中提到的docker镜像。此命令将创建一个将在其中运行Apache HTTP服务器的docker容器。
[email protected]:/home/geekflare$ docker run -it -d httpd
09ca6feb6efc0578951a3e2557ed5855b2edda39a795d9703eb54d975930fe6e
查看容器
ps列出了所有正在运行的docker容器以及容器详细信息。
[email protected]:/home/geekflare$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
09ca6feb6efc httpd "httpd-foreground" 36 seconds ago Up 33 seconds 80/tcp suspicious_bell
如你所见, Apache服务器正在此docker容器中运行。
ps -a
列出所有正在运行/已退出/已停止的Docker容器以及容器详细信息。
[email protected]:/home/geekflare$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
09ca6feb6efc httpd "httpd-foreground" 51 seconds ago Up 49 seconds 80/tcp suspicious_bell
2f6fb3381078 sequenceiq/hadoop-docker:2.7.0 "/etc/bootstrap.sh -d" 2 weeks ago Exited (137) 9 days ago quizzical_raman
9f397feb3a46 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
9b6343d3b5a0 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# ls
bin 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 9b6343d3b5a0
9b6343d3b5a0
运行以下命令以检查是否已卸下容器。
[email protected]:/home/geekflare$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
09ca6feb6efc httpd "httpd-foreground" About a minute ago Up About a minute 80/tcp suspicious_bell
2f6fb3381078 sequenceiq/hadoop-docker:2.7.0 "/etc/bootstrap.sh -d" 2 weeks ago Exited (137) 9 days ago quizzical_raman
9f397feb3a46 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 fce289e99eb9
Untagged: hello-world:latest
Untagged: [email protected]:41a65640635299bab090f783209c1e3a3f11934cf7756b09cb2f1e02147c6ed8
Deleted: sha256:fce289e99eb9bca977dae136fbe2a82b6b7d4c372474c9235adc1741675f587e
Deleted: sha256:af0b15c8625bb1938f1d7b17081031f649fd14e6b233688eea3c5483994a66a3
[email protected]:/home/geekflare$
重新启动Docker
使用命令中提到的容器ID重新启动Docker容器。
[email protected]:/home/geekflare$ docker restart 09ca6feb6efc
09ca6feb6efc
运行以下命令, 并检查STATUS参数以验证容器是否最近启动。
[email protected]:/home/geekflare$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
09ca6feb6efc httpd "httpd-foreground" 6 minutes ago Up 9 seconds 80/tcp suspicious_bell
停止Docker
停止具有命令中提到的容器ID的容器。
[email protected]:/home/geekflare$ docker stop 09ca6feb6efc
09ca6feb6efc
运行以下命令以检查容器是否仍在运行或已停止。
[email protected]:/home/geekflare$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
启动Docker
docker中的此命令使用命令中提到的容器ID启动docker容器。
[email protected]:/home/geekflare$ docker start 09ca6feb6efc
09ca6feb6efc
运行以下命令以检查容器是否已启动。
[email protected]:/home/geekflare$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
09ca6feb6efc 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 09ca6feb6efc
09ca6feb6efc
运行以下命令以查看容器是否被杀死。
[email protected]:/home/geekflare$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
docker commit
使用本地系统中命令中提到的容器ID保存新的docker镜像。在下面的示例中, geekflare是用户名, 而httpd_image是镜像名称。
[email protected]:/home/geekflare$ docker commit 09ca6feb6efc geekflare/httpd_image
sha256:d1933506f4c1686ab1a1ec601b1a03a17b41decbc21d8acd893db090a09bb31c
docker login
登录到Docker Hub。系统将要求你输入Docker Hub凭据进行登录。
[email protected]:/home/geekflare$ 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: geekflare
Password:
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store
Login Succeeded
docker push
在dockerhub上上传命令中提到的映像名称的docker映像。
[email protected]:/home/geekflare$ docker push geekflare/httpd_image
The push refers to repository [docker.io/geekflare/httpd_image]
734d9104a6a2: Pushed
635721fc6973: Mounted from library/httpd
bea448567d6c: Mounted from library/httpd
bfaa5f9c3b51: Mounted from library/httpd
9d542ac296cc: Mounted from library/httpd
d8a33133e477: Mounted from library/httpd
latest: digest: sha256:3904662761df9d76ef04ddfa5cfab764b85e3eedaf10071cfbe2bf77254679ac size: 1574
docker network
docker中的以下命令列出了群集中所有网络的详细信息。
[email protected]:/home/geekflare$ docker network ls
NETWORK ID NAME DRIVER SCOPE
85083e766f04 bridge bridge local
f51d1f3379e0 host host local
5e5d9a192c00 none null local
还有其他几个docker network命令。
[email protected]:/home/geekflare$ docker network
Usage: docker network COMMAND
Manage networks
Commands:
connect Connect a container to a network
create Create a network
disconnect Disconnect a container from a network
inspect Display detailed information on one or more networks
ls List networks
prune Remove all unused networks
rm Remove one or more networks
Run 'docker network COMMAND --help' for more information on a command.
docker info
获取有关系统上安装的docker的详细信息, 包括内核版本, 容器和映像的数量等。
[email protected]:/home/geekflare$ docker info
Containers: 3
Running: 1
Paused: 0
Stopped: 2
Images: 3
Server Version: 18.09.6
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 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: bb71b10fd8f58240ca47fbb579b9d1028eea7c84
runc version: 2b18fe1d885ee5083ef9f0838fee39b62d653e30
init version: fec3683
Security Options:
apparmor
seccomp
Profile: default
Kernel Version: 4.18.0-25-generic
Operating System: Ubuntu 18.10
OSType: linux
Architecture: x86_64
CPUs: 1
Total Memory: 4.982GiB
Name: geekflare
ID: RBCP:YGAP:QG6H:B6XH:JCT2:DTI5:AYJA:M44Z:ETRP:6TO6:OPAY:KLNJ
Docker Root Dir: /var/lib/docker
Debug Mode (client): false
Debug Mode (server): false
Username: geekflare
Registry: https://index.docker.io/v1/
Labels:
Experimental: false
Insecure Registries:
127.0.0.0/8
Live Restore Enabled: false
Product 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$ ls
Desktop Documents example examples.desktop httpd.pid nginx_new.yml nginx.yml
查看历史
使用命令中提到的映像名称显示docker映像的历史记录。
[email protected]:/home/geekflare$ docker history httpd
IMAGE CREATED CREATED BY SIZE COMMENT
ee39f68eb241 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 09ca6feb6efc
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 message
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 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 down
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 message
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 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 down
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 message
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 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 message
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 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 hadoop
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
sequenceiq/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 19
izone/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 --help
Usage: docker update [OPTIONS] CONTAINER [CONTAINER...]
Update configuration of one or more containers
Options:
--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 2f6fb3381078
2f6fb3381078
创建卷
创建一个卷, Docker容器将使用该卷来存储数据。
[email protected]:/home/geekflare$ docker volume create
7e7bc886f69bb24dbdbf19402e31102a25db91bb29c56cca3ea8b0c611fd9ad0
如果未创建该卷, 请运行以下命令。
[email protected]:/home/geekflare$ docker volume ls
DRIVER VOLUME NAME
local 7e7bc886f69bb24dbdbf19402e31102a25db91bb29c56cca3ea8b0c611fd9ad0
安装插件
安装调试环境设置为1.的docker插件vieux / sshfs。
[email protected]:/home/geekflare$ docker plugin install vieux/sshfs DEBUG=1
Plugin "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] y
latest: Pulling from vieux/sshfs
52d435ada6a4: Download complete
Digest: sha256:1d3c3e42c12138da5ef7873b97f7f32cf99fb6edde75fa4f0bcf9ed277855811
Status: Downloaded newer image for vieux/sshfs:latest
Installed plugin vieux/sshfs
Run the below command to list the docker plugins.
[email protected]:/home/geekflare$ docker plugin ls
ID NAME DESCRIPTION ENABLED
2a32d1fb95af vieux/sshfs:latest sshFS plugin for Docker true
docker logout
从dockerhub注销。
[email protected]:/home/geekflare$ docker logout
Removing login credentials for https://index.docker.io/v1/
总结
希望你现在对docker命令有一个不错的了解。在你的开发或实验室环境中尝试这些命令以进行练习和学习。
如果你有兴趣学习Docker和Kubernetes, 请查看此在线课程。
评论前必须登录!
注册