Docker基础命令

Posted by Li Hui on Wednesday, January 13, 2021

image-20210113235714155

前言

随着容器化技术的推广,在不久的将来,大多数的公司都将采用容器化技术对现有的业务进行改造,因此容器化是一个大趋势, 在当下, 正值云原生的发展的前期, 掌握容器化技术, 对于云原生的业务的了解也是一种帮助, 本文主要还是整理一下Docker的基本命令, Docker基本命令其实很好找到, 本文只是对自己的掌握的命令的一个总结和对Docker命令的整理, 以防自己以后会忘记.

本文采用的是https://www.docker.com/play-with-docker 提供的 每次提供四个小时的试用期, 完全可以满足使用.

内容

获取镜像(docker pull)

命令

image_name是镜像的名称 后面如果没有tag 则默认拉去最新的镜像,如果有则拉去对应Tag号的镜像

$  docker pull {{image_name}}:{{tag}}

demo

带Tag

$ docker pull centos:7

image-20210113232024768

不带Tag

$ docker pull centos

image-20210113232044202

查看镜像信息

命令&&demo

$ docker images

image-20210113232218482

搜寻镜像

命令

$ docker search [OPTIONS] TERM

参数解释

Options:
  -f, --filter filter   Filter output based on conditions provided
      --format string   Pretty-print search using a Go template
      --limit int       Max number of search results (default 25)
      --no-trunc        Don't truncate output

demo

$ docker search nginx

image-20210113233133843

删除镜像

删除一个或者多个镜像 注意删除容器时 docker rm CONTAINER

命令

$ docker rmi [OPTIONS] IMAGE [IMAGE...]

参数

Options:
  -f, --force      Force removal of the image
      --no-prune   Do not delete untagged parents

demo

$ docker rmi centos:latest

image-20210113233530266

创建镜像

从一个变化的容器种创建一个镜像,为何这样说,因为容器是可写的,而镜像也就是image是只读的.

命令

$ docker commit [OPTIONS] CONTAINER [REPOSITORY[:TAG]]

参数

Options:
  -a, --author string    Author (e.g., "John Hannibal Smith <hannibal@a-team.com>")
  -c, --change list      Apply Dockerfile instruction to the created image
  -m, --message string   Commit message
  -p, --pause            Pause container during commit (default true)

demo

$ docker commit -m "Added a demo file" -a "dockerUser" d4b6b8a8db0d demoImg:0.0.1

image-20210113234321827

image-20210113234332658

存入和载入镜像

存入镜像

导出镜像为本地tar文档

命令

$ docker save [OPTIONS] IMAGE [IMAGE...]

参数

Options:
  -o, --output string   Write to a file, instead of STDOUT

demo

$ docker save -o centos.tar centos

image-20210113234736875

载入镜像

将导出的tar文件导入到本地镜像仓库中,会导入镜像和其元数据信息

命令

$ docker load [OPTIONS]

参数

Options:
  -i, --input string   Read from tar archive file, instead of STDIN
  -q, --quiet          Suppress the load output

demo

docker load < centos.jar

image-20210113235129588