Docker有很多命令,这些命令有助于控制Docker的行为。本文将详细探讨Docker常用命令。
一、Docker镜像常用命令
1、搜索镜像
可使用docker search 命令搜索存放在Docker Hub中的镜像。例如:
docker search java
执行该命令后,Docker就会在Docker Hub中搜索含有java这个关键词的镜像仓库。执行该命令后,可看到类似于如下的表格:
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
node Node.js is a JavaScript-based platform for s… 5363 [OK]
tomcat Apache Tomcat is an open source implementati… 1793 [OK]
java Java is a concurrent, class-based, and objec… 1676 [OK]
openjdk OpenJDK is an open-source implementation of … 903 [OK]
ghost Ghost is a free and open source blogging pla… 740 [OK]
anapsix/alpine-java Oracle Java 8 (and 7) with GLIBC 2.23 over A… 296 [OK]
该表格包含五列,含义如下。
- NAME:镜像仓库名称。
- DESCRIPTION:镜像仓库描述
- STARS:镜像仓库收藏数,表示该镜像仓库的受欢迎程度,类似于GitHub的stars。
- OFFICIAL:表示是否为官方仓库,该标记为OK的镜像均由各软件的官方项目组创建和维护。由结果可知,Java这个镜像仓库是官方仓库。
- AUTOMATED:表示是否是自动构建的镜像仓库。
2、下载镜像
docker pull java
执行该命令后,Docker默认从Docker Hub中的java仓库下载最新版本的java镜像。若镜像下载缓慢,可配置镜像加速器。该命令还可指定想要下载的镜像标签及Docker Registry地址,例如:
docker pull reg.itmuch.com/java:7
3、列出镜像
docker images
上述命令可列出已下载到本地的镜像,执行命令后,将会看到如下表格:
REPOSITORY TAG IMAGE ID CREATED SIZE
java latest d23bdf5b1b1b 14 months ago 643MB
该表格包含了5列,含义如下:
- REPOSITORY:镜像所属仓库名称。
- TAG:镜像标签。默认是latest,表示最新。
- IMAGE ID:镜像ID,表示镜像唯一标识。
- CREATED:镜像创建时间。
- SIZE:镜像大小。
4、删除本地镜像
docker rmi java
表示删除java这个镜像。
docker rmi -f $(docker images)
表示删除所有镜像
-f参数表示强制删除。
docker rmi $(docker images -f "dangling=true" -q)
删除本地仓库里< none >的镜像,需先将现在运行的容器先删除docker rm -f $(docker ps -a -q)
二、Docker容器常用命令
1、新建并启动容器
使用以下docker run命令可新建并启动一个容器。
docker run
该命令是最常用的命令,它有很多选项,可使用docker run –help 查看选项。
$ 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
--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