1、Docker版本

Docker在17.03版本前叫Docker Engine,版本号范围为0.1.0~1.13.1

17.03之后分为Docker CE(社区版)和Docker EE(企业版),并采用基于时间的版本号方案。

Docker CE/EE每个季度发布一个季度版本,也就是说每年会发布 4 个季度版本, 17.03, 17.06, 17.09, 17.12 就是 2017 年的 4 个季度版本的版本号, 同时 Docker CE 每个月还会发布一个 EDGE 版本,比如 17.04, 17.05, 17.07, 17.08, 17.10, 17.11.

2-Docker安装 - 图1

2、Docker安装

2.1、设置镜像源

  1. [root@localhost yum.repos.d]# /etc/yum.repos.d/
  2. [root@localhost yum.repos.d]# wget https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/centos/docker-ce.repo
  3. # 默认docker镜像源为docker.com,更改为国内镜像源
  4. [root@localhost yum.repos.d]# sed -i -e 's#https://download.docker.com#https://mirrors.tuna.tsinghua.edu.cn/docker-ce#g' /etc/yum.repos.d/docker-ce.repo

2.2、安装

  1. [root@localhost yum.repos.d]# yum install docker-ce

2.3、配置文件说明

docker-ce默认配置文件为/etc/docker/daemon.json,此文件默认不存在需创建。

2.3.1、修改Docker镜像源

docker默认镜像源为dockerhub,修改为国内镜像源,主要有以下三家

  • docker cn

  • 阿里云镜像站

  • 中国科技大学镜像站

  1. # vim /etc/docker/daemon.json
  2. {
  3. "registry-mirrors": ["http://hub-mirror.c.163.com"]
  4. }

2.4、启动docker服务

  1. [root@localhost yum.repos.d]# systemctl start docker.service

3、docker 命令介绍

  • docker命令支持两种模式,一种为新的按照分类管理方式,一种为兼容之前的独立命令方式

3.1、分类管理方式

  1. [root@localhost yum.repos.d]# docker --help
  2. Usage: docker [OPTIONS] COMMAND
  3. Management Commands: # 新的分类管理方式
  4. builder Manage builds
  5. config Manage Docker configs
  6. container Manage containers
  7. context Manage contexts
  8. engine Manage the docker engine
  9. image Manage images
  10. network Manage networks
  11. node Manage Swarm nodes
  12. plugin Manage plugins
  13. secret Manage Docker secrets
  14. service Manage services
  15. stack Manage Docker stacks
  16. swarm Manage Swarm
  17. system Manage Docker
  18. trust Manage trust on Docker images
  19. volume Manage volumes

3.2、独立命令模式

  1. Commands: # 老的支持方式
  2. attach Attach local standard input, output, and error streams to a running container
  3. build Build an image from a Dockerfile
  4. commit Create a new image from a container's changes
  5. cp Copy files/folders between a container and the local filesystem
  6. create Create a new container
  7. deploy Deploy a new stack or update an existing stack

3.3、查看docker版本

  1. [root@localhost yum.repos.d]# docker version
  2. Client: Docker Engine - Community
  3. Version: 19.03.1
  4. API version: 1.40
  5. Go version: go1.12.5
  6. Git commit: 74b1e89
  7. Built: Thu Jul 25 21:21:07 2019
  8. OS/Arch: linux/amd64
  9. Experimental: false

3.4、常用命令

  1. docker search: docker hub搜索镜像
  2. docker pull:从镜像仓库下载image镜像
  3. docker images 列出image镜像
  4. docker create 创建一个新的容器
  5. docker satrt 启动一个停止状态的容器
  6. docker run:运行一个新的容器
  7. docker attach
  8. docker ps 显示容器列表
  9. docker logs:查看容器日志
  10. docker restart:重启容器
  11. dockerr stop:停止一个容器
  12. docker killkill容器
  13. docker rm:删除容器

4、示例

4.1、搜索容器

  1. [root@localhost yum.repos.d]# docker search nginx
  2. NAME DESCRIPTION STARS OFFICIAL AUTOMATED
  3. nginx Official build of Nginx. 11931 [OK]
  4. jwilder/nginx-proxy Automated Nginx reverse proxy for docker con 1651 [OK]

4.2、下载镜像

  1. [root@localhost yum.repos.d]# docker pull redis:latest
  2. latest: Pulling from library/redis
  3. 1ab2bdfe9778: Downloading
  4. 966bc436cc8b: Download complete
  5. c1b01f4f76d9: Download complete
  6. 8a9a85c968a2: Download complete
  7. 8e4f9890211f: Download complete
  8. 93e8c2071125: Download complete
  9. latest: Pulling from library/redis
  10. 1ab2bdfe9778: Pull complete
  11. 966bc436cc8b: Pull complete
  12. c1b01f4f76d9: Pull complete
  13. 8a9a85c968a2: Pull complete
  14. 8e4f9890211f: Pull complete
  15. 93e8c2071125: Pull complete
  16. Digest: sha256:9755880356c4ced4ff7745bafe620f0b63dd17747caedba72504ef7bac882089
  17. Status: Downloaded newer image for redis:latest
  18. docker.io/library/redis:latest

4.3、查看本地docker镜像

  1. [root@localhost yum.repos.d]# docker images
  2. REPOSITORY TAG IMAGE ID CREATED SIZE
  3. nginx stable-alpine 8587e8f26fc1 2 weeks ago 21.2MB
  4. redis latest f7302e4ab3a8 3 weeks ago 98.2MB
  5. [root@localhost yum.repos.d]#
  6. [root@localhost yum.repos.d]#
  7. [root@localhost yum.repos.d]#
  8. [root@localhost yum.repos.d]# docker image ls
  9. REPOSITORY TAG IMAGE ID CREATED SIZE
  10. nginx stable-alpine 8587e8f26fc1 2 weeks ago 21.2MB
  11. redis latest f7302e4ab3a8 3 weeks ago 98.2MB

4.4、创建容器

  1. # 通过-it创建busybox容器
  2. [root@localhost yum.repos.d]# docker run --name b1 -it busybox:latest
  3. Unable to find image 'busybox:latest' locally
  4. latest: Pulling from library/busybox
  5. 7c9d20b9b6cd: Pull complete
  6. Digest: sha256:fe301db49df08c384001ed752dff6d52b4305a73a7f608f21528048e8a08b51e
  7. Status: Downloaded newer image for busybox:latest
  8. # 通过-d创建nginx容器并后台启动
  9. [root@localhost ~]# docker run --name nginxserver1 -d nginx:stable-alpine
  10. b588101f59095e9c57e36e367ac85a67f1597a22321851c4a860a7d083ccc28f

4.4.1、-it和-d的区别
  • -it 表示启动容器时启动一个伪终端进程,此进程PID为1,启动后容器不会退出。
  • -d 表示后台启动服务,如果容器中没有PID为1的进程启动,则使用-d运行后容器会自动退出。

4.5、查看本地容器列表

  1. # docker ps查看运行状态的容器
  2. [root@localhost ~]# docker ps
  3. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  4. 1669d617b6b2 busybox:latest "sh" 4 minutes ago Up 4 minutes b1
  5. # docker ps -a查看所有状态的容器
  6. [root@localhost ~]# docker ps -a
  7. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  8. 1669d617b6b2 busybox:latest "sh" 5 minutes ago Exited (130) 2 seconds ago b1
  9. 8e0295137493 nginx:stable-alpine "nginx -g 'daemon of…" 8 minutes ago Exited (0) 6 minutes ago nginxser1