Docker 使用笔记


Docker 使用笔记

  • 1 Ubuntu 安装Docker
    • 1.1 配置 apt 支持 SSL 传输
    • 1.2 添加 Docker GPG 秘钥
    • 1.3 添加 Docker 源
    • 1.4 更新 apt 源
    • 1.5 安装 Docker
    • 1.6 查看 Docker 服务状态
    • 1.7 启动 Docker 服务
    • 1.8 查看 Docker 版本信息
    • 1.9 配置 Docker 开机自启
  • 2 修改 Docker 镜像
    • 2.1 创建/修改 Docker 配置文件
    • 2.2 重启 Docker 服务
  • 3 修改 Docker cgroups
  • 4 Docker 操作
    • 4.1 查找镜像
    • 4.2 下载镜像
    • 4.3 查看下载
    • 4.4 删除镜像
    • 4.5 创建容器
    • 4.6 启动容器
    • 4.7 停止容器
    • 4.8 重启容器
    • 4.9 删除容器
    • 4.10 查看所有容器(运行/未运行)
  • 5 Docker 容器迁移
    • 5.1 export&import 还是 save & load
    • 5.2 save & load
    • 5.3 容器转镜像
    • 5.4 镜像转静态文件
    • 5.5 镜像还原
    • 5.5 容器还原

1 Ubuntu 安装Docker 1.1 配置 apt 支持 SSL 传输
sudo apt install -y apt-transport-https ca-certificates curl software-properties-common

1.2 添加 Docker GPG 秘钥
# Docker 官方 GPG 秘钥 #curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add - # Docker 阿里 GPG 秘钥 curl -fsSL https://mirrors.aliyun.com/docker-ce/linux/ubuntu/gpg | sudo apt-key add -

1.3 添加 Docker 源
# Docker 官方源 #sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" # Docker 阿里源 sudo add-apt-repository "deb [arch=amd64] https://mirrors.aliyun.com/docker-ce/linux/ubuntu $(lsb_release -cs) stable"

1.4 更新 apt 源
sudo apt update

1.5 安装 Docker
sudo apt install -y docker-ce

1.6 查看 Docker 服务状态
root@ubuntu:~# systemctl status docker ● docker.service - Docker Application Container Engine Loaded: loaded (/lib/systemd/system/docker.service; enabled; vendor preset: enabled) Active: active (running) since Wed 2020-04-15 09:52:38 CST; 1h 22min ago Docs: https://docs.docker.com Main PID: 19134 (dockerd) Tasks: 18 Memory: 75.6M CGroup: /system.slice/docker.service └─19134 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock

1.7 启动 Docker 服务
sudo systemctl start docker

1.8 查看 Docker 版本信息
root@ubuntu:~# docker version Client: Version:19.03.6 API version:1.40 Go version:go1.12.10 Git commit:369ce74a3c Built:Fri Feb 28 23:26:00 2020 OS/Arch:linux/amd64 Experimental:falseServer: Engine: Version:19.03.6 API version:1.40 (minimum version 1.12) Go version:go1.12.10 Git commit:369ce74a3c Built:Wed Feb 19 01:04:38 2020 OS/Arch:linux/amd64 Experimental:false containerd: Version:1.3.3-0ubuntu1~19.10.2 GitCommit: runc: Version:spec: 1.0.1-dev GitCommit: docker-init: Version:0.18.0 GitCommit: root@ubuntu:~# docker --version Docker version 19.03.6, build 369ce74a3c root@ubuntu:~# docker ps CONTAINER IDIMAGECOMMANDCREATEDSTATUSPORTSNAMES root@ubuntu:~#

1.9 配置 Docker 开机自启
sudo systemctl enable docker

2 修改 Docker 镜像 2.1 创建/修改 Docker 配置文件 创建或者修改 Docker 的 /etc/docker/daemon.json
sudo gedit /etc/docker/daemon.json

{ "registry-mirrors": [ "https://dockerhub.azk8s.cn", "https://reg-mirror.qiniu.com", "https://quay-mirror.qiniu.com" ], "exec-opts": [ "native.cgroupdriver=systemd" ] }

2.2 重启 Docker 服务
service docker restart

3 修改 Docker cgroups
sudo systemctl daemon-reload sudo systemctl restart docker

root@ubuntu:/etc/docker# docker info | grep Cgroup Cgroup Driver: systemd WARNING: No swap limit support root@ubuntu:/etc/docker#

4 Docker 操作 4.1 查找镜像 docker search 镜像名称
root@ubuntu:~# docker search redis NAMEDESCRIPTIONSTARSOFFICIALAUTOMATED redisRedis is an open source key-value store that…8028[OK] bitnami/redisBitnami Redis Docker Image141[OK] sameersbn/redis79[OK] grokzen/redis-clusterRedis cluster 3.0, 3.2, 4.0 & 5.065 rediscommander/redis-commanderAlpine image for redis-commander - Redis man…37[OK] kubeguide/redis-masterredis-master with "Hello World!"31 redislabs/redisClustered in-memory database engine compatib…24 oliver006/redis_exporterPrometheus Exporter for Redis Metrics. Supp…21 redislabs/redisearchRedis With the RedisSearch module pre-loaded…21 arm32v7/redisRedis is an open source key-value store that…21 bitnami/redis-sentinelBitnami Docker Image for Redis Sentinel12[OK] webhippie/redisDocker images for Redis11[OK] redislabs/redisgraphA graph database module for Redis10[OK] s7anley/redis-sentinel-dockerRedis Sentinel9[OK] arm64v8/redisRedis is an open source key-value store that…9 insready/redis-statDocker image for the real-time Redis monitor…9[OK] redislabs/redismodAn automated build of redismod - latest Redi…7[OK] centos/redis-32-centos7Redis in-memory data structure store, used a…5 redislabs/redisinsightRedisInsight - The GUI for Redis5 circleci/redisCircleCI images for Redis4[OK] clearlinux/redisRedis key-value data structure server with t…2 tiredofit/redisRedis Server w/ Zabbix monitoring and S6 Ove…1[OK] wodby/redisRedis container image with orchestration1[OK] runnable/redis-stunnelstunnel to redis provided by linking contain…1[OK] xetamus/redis-resourceforked redis-resource0[OK]

4.2 下载镜像 【Docker 使用笔记】docker pull 镜像名称:版本
# 不指定版本会下载最新版本 docker pull 镜像名称:版本

root@ubuntu:~# docker pull redis Using default tag: latest latest: Pulling from library/redis 8559a31e96f4: Pull complete 85a6a5c53ff0: Pull complete b69876b7abed: Pull complete a72d84b9df6a: Pull complete 5ce7b314b19c: Pull complete 04c4bfb0b023: Pull complete Digest: sha256:800f2587bf3376cb01e6307afe599ddce9439deafbd4fb8562829da96085c9c5 Status: Downloaded newer image for redis:latest docker.io/library/redis:latest

4.3 查看下载
root@ubuntu:~# docker images REPOSITORYTAGIMAGE IDCREATEDSIZE redislatest4cdbec704e472 weeks ago98.2MB mysqllatest9228ee8bac7a2 weeks ago547MB

4.4 删除镜像 docker rmi 镜像ID
如果该镜像有运行的镜像则不能删除,可以先停止容器,再删除
root@ubuntu:~# docker images REPOSITORYTAGIMAGE IDCREATEDSIZE redislatest4cdbec704e472 weeks ago98.2MB mysqllatest9228ee8bac7a2 weeks ago547MB a273218159/zentaolatestc5b4be595f8f21 months ago1.15GB root@ubuntu:~# docker rmi c5b4be595f8f Untagged: a273218159/zentao:latest Untagged: a273218159/zentao@sha256:590fca6580fb1b7e736db35cc12f9c109b5cf85f1142368a7bcc8cab0e74bf81 Deleted: sha256:c5b4be595f8f24ce5a2b78f0aae36893d2fc8884fe12f8d1b20a7c0198c29cdc Deleted: sha256:546a12bcbdf971a1af11d42c6b46bbdc5659d54749cfe8e35b50c413f9a59e63 Deleted: sha256:100abf6963bf685778862c64c89d0aa535a958c34457b49aacf3b5c40bbe777f Deleted: sha256:bcc97fbfc9e1a709f0eb78c1da59caeb65f43dc32cd5deeb12b8c1784e5b8237 root@ubuntu:~# docker images REPOSITORYTAGIMAGE IDCREATEDSIZE redislatest4cdbec704e472 weeks ago98.2MB mysqllatest9228ee8bac7a2 weeks ago547MB

4.5 创建容器 docker run [OPTIONS] IMAGE [COMMAND] [ARG…]
Docker 使用笔记
文章图片

4.6 启动容器 docker start 容器名
docker start 容器名

root@ubuntu:~# docker start mysql8 mysql8 root@ubuntu:~# docker ps CONTAINER IDIMAGECOMMANDCREATEDSTATUSPORTSNAMES a995fe3a5c12mysql:latest"docker-entrypoint.s…"2 months agoUp 6 seconds0.0.0.0:3306->3306/tcp, 33060/tcpmysql8 root@ubuntu:~#

4.7 停止容器 docker stop 容器名
docker stop 容器名

root@ubuntu:~# docker ps CONTAINER IDIMAGECOMMANDCREATEDSTATUSPORTSNAMES a995fe3a5c12mysql:latest"docker-entrypoint.s…"2 months agoUp 4 seconds0.0.0.0:3306->3306/tcp, 33060/tcpmysql8 root@ubuntu:~# docker stop mysql8 mysql8 root@ubuntu:~# docker ps CONTAINER IDIMAGECOMMANDCREATEDSTATUSPORTSNAMES root@ubuntu:~#

4.8 重启容器 docker restart 容器名
docker restart 容器名

root@ubuntu:~# docker ps CONTAINER IDIMAGECOMMANDCREATEDSTATUSPORTSNAMES root@ubuntu:~# docker restart mysql8 mysql8 root@ubuntu:~# docker ps CONTAINER IDIMAGECOMMANDCREATEDSTATUSPORTSNAMES a995fe3a5c12mysql:latest"docker-entrypoint.s…"2 months agoUp 4 seconds0.0.0.0:3306->3306/tcp, 33060/tcpmysql8 root@ubuntu:~#

4.9 删除容器 docker rm 容器名
docker rm 容器名

4.10 查看所有容器(运行/未运行)
root@ubuntu:~# docker container ls -a CONTAINER IDIMAGECOMMANDCREATEDSTATUSPORTSNAMES 57dcd97fd4e54cdbec704e47"docker-entrypoint.s…"2 months agoExited (0) 2 months agoredis a995fe3a5c12mysql:latest"docker-entrypoint.s…"2 months agoUp About a minute0.0.0.0:3306->3306/tcp, 33060/tcpmysql8 root@ubuntu:~#

Docker 使用笔记
文章图片

5 Docker 容器迁移 5.1 export&import 还是 save & load
方法 作用 作用对象 能否保留数据 导出的内容 主要的应用场景
export & import 用来将container的文件系统进行打包的 container 不可以 一个Linux系统的文件目录 制作基础镜像
save & load 用来将一个或者多个image打包 image(container实际上也可以,但其实际上作用的是container下的image) 可以 一个分层的文件系统(相对于前者比较大,因为多层文件系统中可能有东西是重合的) 打包多个镜像
5.2 save & load
docker commit -a "hyacinth" -m "MySQL数据库容器迁移" mysql8 mysql:V8.0 docker commit --author "hyacinth" --message "MySQL数据库容器迁移" mysql8 mysql:V8.0

参数 说明
-a/–author 作者信息
-m/–message 备注信息
mysql8 需要迁移的容器名称(正在运行的容器)
mysql:V8.0 镜像名称和镜像标签
注:如果容器有挂载外部文件,如果想保留挂载文件的数据则需要把所有的挂载文件复制到新的环境,在恢复容器的时候要 -v 指定挂载文件
root@ubuntu:~# docker inspect --format='{{.Mounts}}' mysql8 [{volume 55485a1d2d9cab81f130a7b64755fc593e28fadf4989f27abb74a4cccb4c73c4 /var/lib/docker/volumes/55485a1d2d9cab81f130a7b64755fc593e28fadf4989f27abb74a4cccb4c73c4/_data /var/lib/mysql localtrue }] root@ubuntu:~#

5.3 容器转镜像
root@ubuntu:/home/jack/docker# docker ps CONTAINER IDIMAGECOMMANDCREATEDSTATUSPORTSNAMES a995fe3a5c12mysql:latest"docker-entrypoint.s…"2 months agoUp 16 seconds0.0.0.0:3306->3306/tcp, 33060/tcpmysql8 root@ubuntu:/home/jack/docker# docker images REPOSITORYTAGIMAGE IDCREATEDSIZE redislatest4cdbec704e472 months ago98.2MB mysqllatest9228ee8bac7a2 months ago547MB root@ubuntu:/home/jack/docker#

root@ubuntu:/home/jack/docker# docker commit -a "hyacinth" -m "MySQL数据库容器迁移" mysql8 mysql:V8.0 sha256:10a78039733080095eed5458bcbef77e4ec5e0304c0771fedd5ddebb98c63b13 root@ubuntu:/home/jack/docker# docker images REPOSITORYTAGIMAGE IDCREATEDSIZE mysqlV8.010a7803973306 seconds ago547MB redislatest4cdbec704e472 months ago98.2MB mysqllatest9228ee8bac7a2 months ago547MB root@ubuntu:/home/jack/docker#

5.4 镜像转静态文件
docker save -o mysql:V8.0 > mysql.tar

参数 说明
mysql:V8.0 镜像名称和镜像标签
mysql.tar 外部静态文件文件名称(当面Shell控制台所在目录)
root@ubuntu:/home/jack/docker# docker save mysql:V8.0 > mysql.tar root@ubuntu:/home/jack/docker# ll 总用量 1078040 drwxr-xr-x4 rootroot4096 6月23 22:58 ./ drwxr-xr-x 22 jackjack4096 6月22 22:18 ../ drwxr-xr-x2 systemd-coredump root4096 4月10 18:14 data/ -rw-------1 rootroot 551940608 6月22 22:37 mysql -rw-r--r--1 rootroot 551940608 6月23 22:58 mysql.tar drwxr-xr-x2 rootroot4096 4月10 18:14 redis.conf/ root@ubuntu:/home/jack/docker#

5.5 镜像还原
docker load -i mysql.tar

root@ubuntu:/home/hyacinth/SoftWare# docker images REPOSITORYTAGIMAGE IDCREATEDSIZE

root@ubuntu:/home/hyacinth/SoftWare# docker load -i mysql.tar c3a984abe8a8: Loading layer [==================================================>]72.48MB/72.48MB 3aa6919958ae: Loading layer [==================================================>]338.4kB/338.4kB f36486186489: Loading layer [==================================================>]9.539MB/9.539MB c08e063406bc: Loading layer [==================================================>]4.495MB/4.495MB b63d6049d8d7: Loading layer [==================================================>]1.536kB/1.536kB a1452fb52f96: Loading layer [==================================================>]53.75MB/53.75MB 1a945d224d74: Loading layer [==================================================>]6.656kB/6.656kB 7bb6a75aad66: Loading layer [==================================================>]3.584kB/3.584kB b10e17c4e958: Loading layer [==================================================>]411.2MB/411.2MB e61fc95905e3: Loading layer [==================================================>]5.632kB/5.632kB 38d33c0c39bf: Loading layer [==================================================>]16.38kB/16.38kB e1d0e0aeeac6: Loading layer [==================================================>]1.536kB/1.536kB ca98c7859dd8: Loading layer [==================================================>]8.192kB/8.192kB Loaded image: mysql:V8.0 root@ubuntu:/home/hyacinth/SoftWare#

root@ubuntu:/home/hyacinth/SoftWare# docker images REPOSITORYTAGIMAGE IDCREATEDSIZE mysqlV8.010a78039733010 minutes ago547MB root@ubuntu:/home/hyacinth/SoftWare#

5.5 容器还原
docker run -d --name=mysql8 -p 3306:3306 --restart=always mysql:V8.0

    推荐阅读