# docker 命令

https://docs.docker.com/engine/reference/commandline/docker/

# 镜像仓库

# login

​ 登录到Docker镜像仓库,未指定镜像仓库地址,默认为官方仓库 Docker Hub。

语法:

docker login [OPTIONS] [SERVER]

OPTIONS:

  • -u : 登录用户名
  • -p : 登录密码

实例:

docker login #交互式登录,会提示输入用户名和密码
docker login -u username #提示输入密码
docker login -u username -p passwd #直接用户密码登录,一般不使用,会涉密

退出登录

docker logout

查找镜像

语法:

docker search [OPTIONS] term

OPTIONS:

  • --filter , -f : filter output based on conditions provided
  • --limit : 最大搜索的结果数,默认25
  • --no-trunc : 显示完整的镜像描述。
  • --star , -s : 列出收藏数不小于指定值的镜像

例子:

docker search busybox
# pull

​ 从镜像仓库拉取或更新镜像

语法

docker pull [OPTIONS] NAME[TAG|@DIGEST]

OPTIONS:

  • -a : 拉取所有tagged镜像
  • --disable-content-trust : 忽略镜像的检验,默认开启

实例

docker pull ubuntu #下载最新的镜像(:latest)
docker pull -a ubuntu # 下载java的所有镜像
# push

​ 上传本地镜像到镜像仓库(要先登录到镜像仓库)

语法:

docker push [OPTIONS] NAME[:TAG]

OPTIONS:

  • --disable-content-trust :忽略镜像的校验,默认开启

例子:

docker push myapp #上传myapp到镜像仓库
docker push myapp:v1.0.1 #上传myapp到镜像仓库,并打上v1.0.1的tag。

# 本地镜像管理

# images

​ 列出本地镜像

语法:

docker images [OPTIONS] [REPOSITORY[:TAG]]

OPTIONS:

  • -a : 列出本地所有镜像(含中间映像层,默认过滤)

  • -digests : 显示镜像的摘要信息。

  • -f : 显示满足条件的镜像

  • --format :指定返回值的模板文件。

  • --no-trunc : 显示完整的镜像信息。

  • -q : 只显示镜像ID

例子:

docker images #显示本地镜像列表,等同docker image ls
docker images -q #显示本地镜像的ID,等同docker image ls -q
docker rmi $(docker images -q) #删除所有本地镜像

docker images busybox #列出所有本地仓库的busybox的镜像
# rmi

​ 删除一个或多个镜像

语法

docker rmi [OPTIONS] <IMAGE> [IMAGE...]

OPTIONS:

  • -f : 强制删除。
  • -no-prune : 不移除该镜像的过程镜像,默认移除。

例子:

docker rmi busybox #删除本地busybox
docker rmi -f busybox #强制删除本地busybox
docker rmi $(docker images -q) #删除本地所有镜像
# tag

​ 标记本地镜像,将其归入某一仓库。

语法:

docker tag SOURCE_IMAGE[:TAG] [REGISTRYHOST/][USERNAME/]NAME[:TAG]

例子:

docker tag 0e5574283393 mofar/httpd:v1.0 #tag an Image ref by ID
docker tag httpd mofar/httpd:v1.0 #tag an Image ref by Name
docker tag httpd:test mofar/httpd:v1.0.test #tag an Image ref by Name and Tag.
docker tag httpd myregistryhost:5000/mofar/httpd:v1.0 #tag an Image for a private repository
# build

​ 用Dockerfile创建镜像

语法

docker build [OPTIONS] PATH | URL | -

OPTIONS:

# history
# save
# load
# import