Docker
本文linux使用的是阿里云上的
1.登录阿里云查看加速器(有安装方式)
https://cr.console.aliyun.com/cn-hangzhou/instances/mirrors
使用官方安装脚本自动安装 (仅适用于公网环境):
curl -fsSL https://get.docker.com | bash -s docker --mirror Aliyun
2.安装校验
docker version
result:
Client: Docker Engine - Community
Version: 19.03.5
API version: 1.40
Go version: go1.12.12
Git commit: 633a0ea
Built: Wed Nov 13 07:25:41 2019
OS/Arch: linux/amd64
Experimental: false
//能看到这些代表安装ok
Docker 下载镜像
1.命令
docker pull [IMAGE_NAME]:[TAG]命令来下载镜像
其中 IMAGE_NAME 表示的是镜像的名称,而 TAG 是镜像的标签,也就是说我们需要通过 “镜像 + 标签” 的方式来下载镜像。
举个例子:
docker pull mysql:5.7
[root@iZ2ze0a8w2ct7o2xe4d2tqZ /]# docker pull mysql:5.7
5.7: Pulling from library/mysql
804555ee0376: Pull complete
c53bab458734: Pull complete
ca9d72777f90: Pull complete
2d7aad6cb96e: Pull complete
8d6ca35c7908: Pull complete
6ddae009e760: Pull complete
327ae67bbe7b: Pull complete
31f1f8385b27: Pull complete
a5a3ad97e819: Pull complete
48bede7828ac: Pull complete
380afa2e6973: Pull complete
Digest: sha256:b38555e593300df225daea22aeb104eed79fc80d2f064fde1e16e1804d00d0fc
Status: Downloaded newer image for mysql:5.7
docker.io/library/mysql:5.7
将仓库的地址更改成了国内的 docker.io 的镜像仓库,所以在上图中,镜像前面会有 docker.io 出现。
- PULL 子命令
docker pull --help
result:
[root@iZ2ze0a8w2ct7o2xe4d2tqZ /]# docker pull --help
Usage: docker pull [OPTIONS] NAME[:TAG|@DIGEST]
Pull an image or a repository from a registry
Options:
-a, --all-tags Download all tagged images in the repository
--disable-content-trust Skip image verification (default true)
-q, --quiet Suppress verbose output
-a, --all-tags=true|false: 是否获取仓库中所有镜像,默认为否;
--disable-content-trust: 跳过镜像内容的校验,默认为 true;
Docker 查看镜像信息
一、images 命令列出镜像
docker images || docker image ls
[root@iZ2ze0a8w2ct7o2xe4d2tqZ /]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mysql 5.7 db39680b63ac 2 weeks ago 437MB
mongo latest a0e2e64ac939 3 weeks ago 364MB
[root@iZ2ze0a8w2ct7o2xe4d2tqZ /]# docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
mysql 5.7 db39680b63ac 2 weeks ago 437MB
mongo latest a0e2e64ac939 3 weeks ago 364MB
REPOSITORY: 来自于哪个仓库;
TAG: 镜像的标签信息,比如 5.7、latest 表示不同的版本信息;
IMAGE ID: 镜像的 ID, 如果您看到两个 ID 完全相同,那么实际上,它们指向的是同一个镜像,只是标签名称不同罢了;
CREATED: 镜像最后的更新时间;
SIZE: 镜像的大小,优秀的镜像一般体积都比较小,这也是我更倾向于使用轻量级的 alpine 版本的原因;
二、使用 tag 命令为镜像添加标签
我们可以使用 docker tag
命令,为本地镜像添加一个新的标签
docker tag docker.io/mysql:5.7 allen_mysql:5.7
为 docker.io/mysql 镜像,添加新的镜像标签 allen_mysql:5.7。然后使用 docker images 命令,查看本地镜像:
REPOSITORY TAG IMAGE ID CREATED SIZE
allen_mysql 5.7 db39680b63ac 2 weeks ago 437MB
mysql 5.7 db39680b63ac 2 weeks ago 437MB
mongo latest a0e2e64ac939 3 weeks ago 364MB
本地多了一个 allen_mysql:5.7
的镜像,allen_mysql:5.7
和 docker.io/mysql:5.7
的镜像 ID 是一模一样的,说明它们是同一个镜像,只是别名不同而已。
三、使用 inspect 命令查看镜像详细信息
docker inspect --可以获取镜像的详细信息,其中,包括创建者,各层的数字摘要===
举例:
docker inspect docker.io/mysql:5.7
docker inspect
返回的是 JSON
格式的信息,如果您想获取其中指定的一项内容,可以通过 -f
来指定,如获取镜像大小:
docker inspect -f {{".Size"}} docker.io/mysql:5.7
四、使用 history 命令查看镜像历史
docker history 以列出各个层(layer)的创建信息,如我们查看 docker.io/mysql:5.7 的各层信息:
docker history docker.io/mysql:5.7
想要看具体信息,可以通过添加 --no-trunc
选项,如下面命令:
docker history --no-trunc docker.io/mysql:5.7
Docker 搜索镜像
docker search [option] keyword
搜索仓库中 mysql
相关的镜像,可以输入如下命令:
docker search mysql
result:
mysql MySQL is a widely used, open-source relation… 9021 [OK]
mariadb MariaDB is a community-developed fork of MyS… 3186 [OK]
mysql/mysql-server Optimized MySQL Server Docker images. Create… 672 [OK]
percona Percona Server is a fork of the MySQL relati… 465 [OK]
centos/mysql-57-centos7 MySQL 5.7 SQL database server 67
centurylink/mysql Image containing mysql. Optimized to be link… 61 [OK]
mysql/mysql-cluster Experimental MySQL Cluster Docker images. Cr… 59
deitch/mysql-backup REPLACED! Please use http://hub.docker.com/r… 41 [OK]
bitnami/mysql Bitnami MySQL Docker Image 35 [OK]
tutum/mysql Base docker image to run a MySQL database se… 34
schickling/mysql-backup-s3 Backup MySQL to S3 (supports periodic backup… 28 [OK]
prom/mysqld-exporter 24 [OK]
linuxserver/mysql A Mysql container, brought to you by LinuxSe… 24
centos/mysql-56-centos7 MySQL 5.6 SQL database server 17
circleci/mysql MySQL is a widely used, open-source relation… 16
mysql/mysql-router MySQL Router provides transparent routing be… 14
arey/mysql-client Run a MySQL client from a docker container 13 [OK]
openshift/mysql-55-centos7 DEPRECATED: A Centos7 based MySQL v5.5 image… 6
genschsa/mysql-employees MySQL Employee Sample Database 4 [OK]
fradelg/mysql-cron-backup MySQL/MariaDB database backup using cron tas… 4 [OK]
devilbox/mysql Retagged MySQL, MariaDB and PerconaDB offici… 2
ansibleplaybookbundle/mysql-apb An APB which deploys RHSCL MySQL 2 [OK]
jelastic/mysql An image of the MySQL database server mainta… 1
monasca/mysql-init A minimal decoupled init container for mysql 0
widdpim/mysql-client Dockerized MySQL Client (5.7) including Curl… 0 [OK]
二、search 子命令
docker search --help
result:
[root@iZ2ze0a8w2ct7o2xe4d2tqZ /]# docker search --help
Usage: docker search [OPTIONS] TERM
Search the Docker Hub for images
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
可以看到 search 支持的子命令有:
-f, --filter filter: 过滤输出的内容;
--limit int:指定搜索内容展示个数;
--no-index: 不截断输出内容;
--no-trunc:不截断输出内容;
Docker 删除&清理镜像
一、通过标签删除镜像
docker rmi [image] || docker image rm [image]
支持的子命令如下:
-f, -force
: 强制删除镜像,即便有容器引用该镜像;-no-prune
: 不要删除未带标签的父镜像;
[root@iZ2ze0a8w2ct7o2xe4d2tqZ /]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
allen_mysql 5.7 db39680b63ac 2 weeks ago 437MB
mysql 5.7 db39680b63ac 2 weeks ago 437MB
mongo latest a0e2e64ac939 3 weeks ago 364MB
[root@iZ2ze0a8w2ct7o2xe4d2tqZ /]# docker rmi allen_mysql:5.7
Untagged: allen_mysql:5.7
再次查看:
[root@iZ2ze0a8w2ct7o2xe4d2tqZ /]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mysql 5.7 db39680b63ac 2 weeks ago 437MB
mongo latest a0e2e64ac939 3 weeks ago 364MB
如果某个镜像不存在多个标签,当且仅当只有一个标签时,执行删除命令时,您就要小心了,这会彻底删除镜像。
这个时候,我们再执行 docker rmi docker.io/mysql:5.7
命令:
[root@iZ2ze0a8w2ct7o2xe4d2tqZ /]# docker rmi docker.io/mysql:5.7
Untagged: mysql:5.7
Untagged: mysql@sha256:b38555e593300df225daea22aeb104eed79fc80d2f064fde1e16e1804d00d0fc
Deleted: sha256:db39680b63ac47a1d989da7b742f7b382af34d85a68214f8977bad59c05901a6
Deleted: sha256:7b279f5f598157a5bb91d47987b34cd7a35ef7a83bb327f573f1dc59c7415a24
Deleted: sha256:6626a405b6cdbab66034066924cfe9ab625aa3973484d9ec916a0c678c7963f2
Deleted: sha256:f133ff996684b51815bae60c1a843af779c22a8abd4b828b2d4ba492281c08ce
Deleted: sha256:75bf412bd438b6685a37fbe400802f12f2e3f491233d25794fc6e32d35977180
Deleted: sha256:5f7c68324b959d2c806db18d02f153bc810f9842722415e077351bc834cc8578
Deleted: sha256:338fc0cd3fb4b87a2b83d274e8fbf475fbde19947c4ac5c5eb6e981a6fb0e8f0
Deleted: sha256:f7a4ccab931f1d1e861961eb951a7806d91ccb375e737fe1f84282f6bbafd2be
Deleted: sha256:f388e1092f8fb931a3cd07a7381bd9707d19526ff81f8b624e932f4919c27a3e
Deleted: sha256:e209b7a884b4d2e9d56bbac40ced48f2caa6a19e7ad6eb6dd20ff754f3af2c5d
Deleted: sha256:2401cf11c5455d505ef49657afcc709197ffcdfc9bd732508e9b62578a30b3a5
Deleted: sha256:814c70fdae62bc26c603bfae861f00fb1c77fc0b1ee8d565717846f4df24ae5d
二、通过 ID 删除镜像
docker rmi ee7cbd482336
一旦制定了通过 ID 来删除镜像,它会先尝试删除所有指向该镜像的标签,然后在删除镜像本身。
三、删除镜像的限制
删除镜像很简单,但也不是我们何时何地都能删除的,它存在一些限制条件。
当通过该镜像创建的容器未被销毁时,镜像是无法被删除的。为了验证这一点,我们来做个试验。首先,我们通过 docker pull alpine
命令,拉取一个最新的 alpine
镜像, 然后启动镜像,让其输出 hello, docker!
:
[root@iZ2ze0a8w2ct7o2xe4d2tqZ /]# docker pull alpine
[root@iZ2ze0a8w2ct7o2xe4d2tqZ /]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
alpine latest cc0abc535e36 2 weeks ago 5.59MB
mongo latest a0e2e64ac939 3 weeks ago 364MB
[root@iZ2ze0a8w2ct7o2xe4d2tqZ /]# docker run docker.io/alpine echo 'hello,docker!'
hello,docker!
[root@iZ2ze0a8w2ct7o2xe4d2tqZ /]# docker rmi alpine
Error response from daemon: conflict: unable to remove repository reference "alpine" (must force) - container 5d8d23f9247b is using its referenced image cc0abc535e36
可以看到提示信息,无法删除该镜像,因为有容器正在引用他!同时,这段信息还告诉我们,除非通过添加 -f
子命令,也就是强制删除,才能移除掉该镜像
docker rmi -f docker.io/alpine
不推荐这样暴力的做法,正确的做法应该是:
-
先删除引用这个镜像的容器;
-
再删除这个镜像;
根据上图中提示的,引用该镜像的容器 ID (
9d59e2278553
), 执行删除命令:
[root@iZ2ze0a8w2ct7o2xe4d2tqZ /]# docker rm 5d8d23f9247b
5d8d23f9247b
然后,再执行删除镜像的命令:
docker rmi 5cb3aa00f899
这个时候,就能正常删除了!
四、清理镜像
使用 Docker 一段时间后,系统一般都会残存一些临时的、没有被使用的镜像文件,可以通过以下命令进行清理:
docker image prune
它支持的子命令有:
-a, --all
: 删除所有没有用的镜像,而不仅仅是临时文件;-f, --force
:强制删除镜像文件,无需弹出提示确认;
另外,执行完 docker image prune
命令后,还是告诉我们释放了多少存储空间!
[root@iZ2ze0a8w2ct7o2xe4d2tqZ /]# docker image prune
WARNING! This will remove all dangling images.
Are you sure you want to continue? [y/N] y
Total reclaimed space: 0B
Docker 创建镜像
Docker 创建镜像主要有三种:
-
基于已有的镜像创建;
-
基于 Dockerfile 来创建;
-
基于本地模板来导入;
一、基于已有的镜像创建
通过如下命令来创建:
docker container commit
支持的子命令如下:
-a, --author
="": 作者信息;-c, --change
=[]: 可以在提交的时候执行 Dockerfile 指令,如 CMD、ENTRYPOINT、ENV、EXPOSE、LABEL、ONBUILD、USER、VOLUME、WORIR 等;-m, --message
="": 提交信息;-p, --pause
=true: 提交时,暂停容器运行。
接下来,基于本地已有的 Ubuntu 镜像,创建一个新的镜像:
[root@iZ2ze0a8w2ct7o2xe4d2tqZ /]# docker images
首先,让我将它运行起来,并在其中创建一个 test.txt 文件:
docker run -it docker.io/ubuntu:latest /bin/bash
root@a0a0c8cfec3a:/# touch test.txt
root@a0a0c8cfec3a:/# exit
创建完 test.txt 文件后,需要记住标注的容器 ID: a0a0c8cfec3a
, 用它来提交一个新的镜像(PS: 你也可以通过名称来提交镜像,这里只演示通过 ID 的方式)。
执行命令:
docker container commit -m "Added test.txt file" -a "Allen" a0a0c8cfec3a test:0.1
提交成功后,会返回新创建的镜像 ID 信息,如下图所示:
再次查看本地镜像信息,可以看到新创建的 test:0.1
镜像了:
二、基于 Dockerfile 来创建镜像
通过 Dockerfile 的方式来创建镜像,是最常见的一种方式了,也是比较推荐的方式。Dockerfile 是一个文本指令文件,它描述了是如何基于一个父镜像,来创建一个新镜像的过程。
下面让我们来编写一个简单的 Dockerfile 文件,它描述了基于 Ubuntu 父镜像,安装 Python3 环境的镜像:
FROM docker.io/ubuntu:latest
LABEL version="1.0" maintainer="Allen <weiwosuo@github>"
RUN apt-get update && \
apt-get install -y python3 && \
apt-get clean && \
rm -rf /var/lib/apt/lists/*
创建完成后,通过这个 Dockerfile 文件,来构建新的镜像,执行命令:
docker image build -t python:3 .
注意:命令的最后有个点,如果不加的话,会构建不成功 !
编译成功后,再次查看本地镜像信息,就可以看到新构建的 python:3 镜像了。
Docker 导出&加载镜像
学习 Docker 如何导出&加载镜像。当我们需要将镜像分享给别人时,可以将镜像导出成 tar 包,别人直接通过加载这个 tar 包,快速地将镜像引入到本地镜像库。
主要是通过如下两个命令:
docker save
docker load
一、导出镜像
查看本地镜像如下:
[root@iZ2ze0a8w2ct7o2xe4d2tqZ /]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mongo latest a0e2e64ac939 3 weeks ago 364MB
例如,我们想要将 mongo :latest 镜像导出来,执行命令:
docker save -o mongo.tar mongo:latest
执行成功后,查看当前目录:
[root@iZ2ze0a8w2ct7o2xe4d2tqZ /]# ll
-rw------- 1 root root 368132096 Jan 14 17:02 mongo.tar
镜像文件已经生成。接下来,你可以将它通过复制的方式,分享给别人了!
二、加载镜像
别人拿到了这个 tar
包后,要如何导入到本地的镜像库呢?
通过执行如下命令:
docker load -i python_3.tar
或者:
docker load < python_3.tar
导入成功后,查看本地镜像信息,你就可以获得别人分享的镜像了!怎么样,是不是很方便呢!
Docker 上传镜像
一、获取 Docker ID
学习如何上传 Docker 镜像至 Docker Hub 上。
想要上传镜像到 Docker Hub 上,首先,我们需要注册 Docker Hub 账号。打开 Docker Hub 网址 https://hub.docker.com,开始注册:
1.填写您的 Docker ID (也就是账号),以及密码,Email, 点击继续。
2.接下来,Docker Hub 会发送验证邮件,到您填写的邮箱当中:
点击验证即可,接下来,再次返回 Docker Hub 官网,用您刚刚注册的 Docker ID 和密码来登录账号!
二、创建镜像仓库
登录成功后->选择创建一个镜像仓库->填写仓库名称、描述信息、是否公开后,点击创建。
三、上传镜像
进入命令行,用我们刚刚获取的 Docker ID 以及密码登录,执行命令:
docker login
[root@iZ2ze0a8w2ct7o2xe4d2tqZ /]# docker login
Login with your Docker ID to push and pull images from Docker Hub. If you don't have a Docker ID, head over to https://hub.docker.com to create one.
Username: 18569654550
Password:
WARNING! Your password will be stored unencrypted in /root/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store
Login Succeeded
首先,我们对其打一个新的标签,前缀与我们新创建的 Docker ID 、仓库名保持一致:
[root@iZ2ze0a8w2ct7o2xe4d2tqZ /]# docker tag mongo:latest weiwosuoai1991/mongo:latest
[root@iZ2ze0a8w2ct7o2xe4d2tqZ /]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
weiwosuoai1991/mongo latest a0e2e64ac939 3 weeks ago 364MB
mongo latest a0e2e64ac939 3 weeks ago 364MB
查看本地信息,可以看到,标签打成功了。接下开,开始上传!执行命令:
docker push weiwosuoai1991/mongo
[root@iZ2ze0a8w2ct7o2xe4d2tqZ /]# docker push weiwosuoai1991/mongo:latest
The push refers to repository [docker.io/weiwosuoai1991/mongo]
09291073f74c: Preparing
b73387584fd0: Preparing
93054343d81f: Preparing
ac0ccde3f7d4: Preparing
446e0a1ba1ee: Preparing
88eb105e290d: Waiting
683658141bb2: Waiting
af20b2951a75: Waiting
ac598a59a7e7: Waiting
918efb8f161b: Waiting
27dd43ea46a8: Waiting
9f3bfcc4a1a8: Waiting
2dc9f76fb25b: Waiting
denied: requested access to the resource is denied
上传成功!去 Docker Hub 官网,新创建的仓库的信息页面验证一下,是否真的成功了:
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-4EjUMzAy-1578995467627)(C:\Users\Administrator\AppData\Roaming\Typora\typora-user-images\1578993508494.png)]
常用环境安装:
Docker 快速安装&搭建 Redis 环境
一、下载 Redis 镜像
首先拉取 Redis 镜像, 这里我选择的是 redis:alpine
轻量级镜像版本:
docker pull redis:alpine
下载完成后,通过 docker images
确认镜像是否已经下载到本地:
[root@iZ2ze0a8w2ct7o2xe4d2tqZ /]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
redis alpine 8af6a13284bf 11 days ago 33MB
mongo latest a0e2e64ac939 3 weeks ago 364MB
weiwosuoai1991/mongo latest a0e2e64ac939 3 weeks ago 364MB
二、运行 Redis 容器
docker run \
-p 6379:6379 \
--name redis \
-v /usr/local/docker/redis/redis.conf:/etc/redis/redis.conf \
-v /usr/local/docker/redis/data:/data \
-d redis:alpine \
redis-server /etc/redis/redis.conf --appendonly yes --requirepass "mypassword"
命令说明:
-p 6379:6379
: 将容器的 6379 端口映射到宿主机的 6379 端口;-v /usr/local/docker/redis/data:/data
: 将容器中的 /data 数据存储目录, 挂载到宿主机中 /usr/local/docker/redis/data 目录下;-v /usr/local/docker/redis/redis.conf:/etc/redis/redis.conf
: 将容器中 /etc/redis/redis.conf 配置文件,挂载到宿主机的 /usr/local/docker/redis/redis.conf 文件上;redis-server --appendonly yes --requirepass "mypassword"
: 在容器执行 redis-server 启动命令,并打开 redis 持久化配置, 以及设置密码;
命令运行完成后,查看容器是否启动成功:
[root@iZ2ze0a8w2ct7o2xe4d2tqZ /]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a00fcf1273fd redis:alpine "docker-entrypoint.s…" 43 seconds ago Up 42 seconds 0.0.0.0:6379->6379/tcp redis
redis 容器已经启动成功了!
三、连接刚刚创建好的容器
执行如下命令,连接 redis:
docker run -it redis:alpine redis-cli -h 172.18.0.1
172.18.0.1 是linux 内网
Docker 快速安装&搭建 MongDB 环境
一、下载 MongoDB 镜像
这里以 mongo 4 版本为例,下载镜像:
docker pull mongo:4
docker images 查看
二、运行 MongoDB 镜像
下载成功后,运行 mongoDB 镜像:
docker run -d \
--name mongo \
-v /usr/local/docker/mongo/configdb:/data/configdb \
-v /usr/local/docker/mongo/data:/data/db \
-p 27017:27017 \
mongo:4 \
--auth
-d
: 以后台的方式运行;--name mongo
: 指定容器名称为 mongo;-v /usr/local/docker/mongo/configdb:/data/configdb
: 将容器中 /data/configdb 目录挂载到宿主机的 /usr/local/docker/mongo/configdb 目录下;-v /usr/local/docker/mongo/data:/data/db
: 将容器中 /data/db 数据目录挂载到宿主机的 /usr/local/docker/mongo/data 目录下;-p 27017:27017
: 将容器的 27017 端口映射到宿主机的 27017 端口;
执行命令完成后,查看下容器是否启动:
[root@iZ2ze0a8w2ct7o2xe4d2tqZ /]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
fbb02e859bb5 mongo:4 "docker-entrypoint.s…" 38 seconds ago Up 37 seconds 0.0.0.0:27017->27017/tcp mongo
a00fcf1273fd redis:alpine "docker-entrypoint.s…" 9 minutes ago Up 9 minutes 0.0.0.0:6379->6379/tcp redis
三、添加管理员账号
执行命令:
docker exec -it mongo mongo admin
然后,创建一个拥有最高权限 root 账号:
db.createUser({ user: 'admin', pwd: '123456', roles: [ { role: "root", db: "admin" } ] });
创建成功后,你会看到 Successfully added user
:
> db.createUser({ user: 'admin', pwd: '123456', roles: [ { role: "root", db: "admin" } ] });
Successfully added user: {
"user" : "admin",
"roles" : [
{
"role" : "root",
"db" : "admin"
}
]
}
创建成功后,会看到 Successfully added user
四、用新创建的 root 账户连接,测试一下
docker run -it --rm --link mongo:mongo mongo mongo -u admin -p 123456 --authenticationDatabase admin mongo/admin
显示所有的数据库:
show dbs
使用某个数据库:
use admin
输入命令 exit
,退出连接!
参考地址:https://www.exception.site/docker/