核心概念
Docker 有三大核心概念,分别是镜像,容器,仓库。
Docker 镜像
Docker 镜像类似于虚拟机的镜像,可以把它理解为一个只读的模板,例如一个镜像可以包含一个基本的操作系统环境,里面仅仅安装了Apache应用程序,可以把它称之为一个Apache镜像。
镜像是创建Docker容器的基础。
通过版本管理和增量文件系统,Docker提供了一套十分简单的机制来更新现有的镜像,用户甚至可以从网上下载一个已经做好的应用镜像,并进行使用。
Docker 容器
Docker容器类似于一个轻量级的沙箱,Docker利用容器来运行和隔离应用。
容器是从镜像创建的应用运行实例,它可以启动,开始停止删除,这些容器彼此相互隔离,互相不可见。
可以把容器看做一个简易版的Linux系统环境,以及运行在其中的应用程序打包而成的盒子。
Docker 仓库
Docker仓库类似于代码仓库,是Docker集中保存镜像文件的场所。
根据保存的镜像是否公开与否,Docker仓库可以分为公开仓库和私有仓库两种形式。
安装Docker引擎
下面的实例,是在Windows下安装docker
- 访问官网的首页。
https://www.docker.com/
- 检验是否安装成功
打开powershell 输入docker
这样,windows 版本的docker已经安装完毕
使用Docker镜像
对于docker镜像的使用如下
获取镜像
获取docker镜像,使用如下的命令。
输入命令获取docker镜像
docker pull ubuntu
从下载的过程可以看到,对于docker容器来说,镜像文件一般由若干层组成,
每一个字符串都是唯一的id,使用docker的pull命令的时候,会一层层的下载相关的镜像,并由上一层镜像指向上上一层的镜像。
查看镜像的信息
使用docke的images命令,查看docker的镜像信息。
上方使用images列出docker的镜像信息。
使用tag命令添加镜像的标签
为了方便于docker镜像的使用,使用docker的tag命令,添加docker的镜像标签。
docker tag ubuntu:latest myubuntu:laster
使用的效果如下
使用inspect命令查看docker的详细信息
docker image inspect ubuntu
查看信息如下
PS C:\Users\Administrator\Desktop> docker image inspect ubuntu
[
{
"Id": "sha256:74435f89ab7825e19cf8c92c7b5c5ebd73ae2d0a2be16f49b3fb81c9062ab303",
"RepoTags": [
"myubuntu:laster",
"ubuntu:latest"
],
"RepoDigests": [
"ubuntu@sha256:35c4a2c15539c6c1e4e5fa4e554dac323ad0107d8eb5c582d6ff386b383b7dce"
],
"Parent": "",
"Comment": "",
"Created": "2020-06-17T01:20:56.323568224Z",
"Container": "9477bb1d88721b63e0c6a359eb48a081741a0992480bf34bbcebf4e5734fcf67",
"ContainerConfig": {
"Hostname": "9477bb1d8872",
"Domainname": "",
"User": "",
"AttachStdin": false,
"AttachStdout": false,
"AttachStderr": false,
"Tty": false,
"OpenStdin": false,
"StdinOnce": false,
"Env": [
"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
],
"Cmd": [
"/bin/sh",
"-c",
"#(nop) ",
"CMD [\"/bin/bash\"]"
],
"ArgsEscaped": true,
"Image": "sha256:5412cfe47ff528df823761788f99a11568e2d25f48a4245a3d67cdc3e564c839",
"Volumes": null,
"WorkingDir": "",
"Entrypoint": null,
"OnBuild": null,
"Labels": {}
},
"DockerVersion": "18.09.7",
"Author": "",
"Config": {
"Hostname": "",
"Domainname": "",
"User": "",
"AttachStdin": false,
"AttachStdout": false,
"AttachStderr": false,
"Tty": false,
"OpenStdin": false,
"StdinOnce": false,
"Env": [
"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
],
"Cmd": [
"/bin/bash"
],
"ArgsEscaped": true,
"Image": "sha256:5412cfe47ff528df823761788f99a11568e2d25f48a4245a3d67cdc3e564c839",
"Volumes": null,
"WorkingDir": "",
"Entrypoint": null,
"OnBuild": null,
"Labels": null
},
"Architecture": "amd64",
"Os": "linux",
"Size": 73856440,
"VirtualSize": 73856440,
"GraphDriver": {
"Data": {
"LowerDir": "/var/lib/docker/overlay2/63e1c7f313e414bd5465953da0ceb615cc9c5906c9b539ed164560b18020bc85/diff:/var/lib/docker/overlay2/09f0332099bc97d0052e07879f18271f2c7ea879501498a1eca00711065c9e9a/diff:/var/lib/docker/overlay2/8fef100fbab32d7aa7a3cc70740ef97b45cc9dcfce91453dd10cc502a3fa10f7/diff",
"MergedDir": "/var/lib/docker/overlay2/3ec5eeae7aa3781aa605fa1a1dc598c39d7774796600b3c1f86e8190c24d5a47/merged",
"UpperDir": "/var/lib/docker/overlay2/3ec5eeae7aa3781aa605fa1a1dc598c39d7774796600b3c1f86e8190c24d5a47/diff",
"WorkDir": "/var/lib/docker/overlay2/3ec5eeae7aa3781aa605fa1a1dc598c39d7774796600b3c1f86e8190c24d5a47/work"
},
"Name": "overlay2"
},
"RootFS": {
"Type": "layers",
"Layers": [
"sha256:e1c75a5e0bfa094c407e411eb6cc8a159ee8b060cbd0398f1693978b4af9af10",
"sha256:9e97312b63ff63ad98bb1f3f688fdff0721ce5111e7475b02ab652f10a4ff97d",
"sha256:ec1817c93e7c08d27bfee063f0f1349185a558b87b2d806768af0a8fbbf5bc11",
"sha256:05f3b67ed530c5b55f6140dfcdfb9746cdae7b76600de13275197d009086bb3d"
]
},
"Metadata": {
"LastTagTime": "2020-07-06T13:37:35.064486339Z"
}
}
]
使用history命令查看镜像历史
docker history ubuntu
PS C:\Users\Administrator\Desktop> docker history ubuntu
IMAGE CREATED CREATED BY SIZE COMMENT
74435f89ab78 2 weeks ago /bin/sh -c #(nop) CMD ["/bin/bash"] 0B
<missing> 2 weeks ago /bin/sh -c mkdir -p /run/systemd && echo 'do… 7B
<missing> 2 weeks ago /bin/sh -c set -xe && echo '#!/bin/sh' > /… 811B
<missing> 2 weeks ago /bin/sh -c [ -z "$(apt-get indextargets)" ] 1.01MB
<missing> 2 weeks ago /bin/sh -c #(nop) ADD file:b2342c7e6665d5ff3… 72.8MB
PS C:\Users\Administrator\Desktop>
搜索相关的镜像
搜索官方带nginx的关键字的镜像
PS C:\Users\Administrator\Desktop> docker search --filter=is-official=true nginx
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
nginx Official build of Nginx. 13426 [OK]
PS C:\Users\Administrator\Desktop>
删除和清理镜像
对镜像镜像相关的删除
使用标签删除镜像
docker rmi ubuntu
PS C:\Users\Administrator\Desktop> docker rmi ubuntu
Untagged: ubuntu:latest
Untagged: ubuntu@sha256:35c4a2c15539c6c1e4e5fa4e554dac323ad0107d8eb5c582d6ff386b383b7dce
PS C:\Users\Administrator\Desktop>
使用镜像ID删除镜像
PS C:\Users\Administrator\Desktop> docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
myubuntu laster 74435f89ab78 2 weeks ago 73.9MB
ubuntu latest 74435f89ab78 2 weeks ago 73.9MB
PS C:\Users\Administrator\Desktop> docker rmi ubuntu
Untagged: ubuntu:latest
Untagged: ubuntu@sha256:35c4a2c15539c6c1e4e5fa4e554dac323ad0107d8eb5c582d6ff386b383b7dce
PS C:\Users\Administrator\Desktop> docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
myubuntu laster 74435f89ab78 2 weeks ago 73.9MB
PS C:\Users\Administrator\Desktop> docker rmi 74435f89ab78
Untagged: myubuntu:laster
Deleted: sha256:74435f89ab7825e19cf8c92c7b5c5ebd73ae2d0a2be16f49b3fb81c9062ab303
Deleted: sha256:8a8d1f0b34041a66f09e49bdc03e75c2190f606b0db7e08b75eb6747f7b49e11
Deleted: sha256:f1b8f74eff975ae600be0345aaac8f0a3d16680c2531ffc72f77c5e17cbfeeee
Deleted: sha256:27d46ebb54384edbc8c807984f9eb065321912422b0e6c49d6a9cd8c8b7d8ffc
Deleted: sha256:e1c75a5e0bfa094c407e411eb6cc8a159ee8b060cbd0398f1693978b4af9af10
PS C:\Users\Administrator\Desktop> docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
PS C:\Users\Administrator\Desktop>
清理镜像
使用docker镜像来进行清理
PS C:\Users\Administrator\Desktop> docker image prune
WARNING! This will remove all dangling images.
Are you sure you want to continue? [y/N] y
Total reclaimed space: 0B
PS C:\Users\Administrator\Desktop> docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
PS C:\Users\Administrator\Desktop>
创建镜像
基于已有的容器创建镜像
查看运行的容器列表
PS C:\Users\Administrator\Desktop> docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
ff7a974b9771 nginx "/docker-entrypoint.…" About a minute ago Up About a minute 80/tcp fervent_curie
创建相关的容器
docker container commit -m "ming" -a "mingming" ff7a974b9771 test:0.1
再次查看
PS C:\Users\Administrator\Desktop> docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
test 0.1 54a6eb8d4483 5 seconds ago 132MB
ubuntu latest 74435f89ab78 2 weeks ago 73.9MB
nginx latest 2622e6cca7eb 3 weeks ago 132MB
PS C:\Users\Administrator\Desktop>
使用模板方式创建
https://download.openvz.org/template/precreated/
下载相关的模板
导入相关的镜像
cat centos-6-x86-minimal.tar.gz|docker import - centos6
使用dockerFile创建
# 构建阶段
FROM node
# 指定工作目录
WORKDIR /
# 指定工作端口
EXPOSE 4000
# 安装gitbook
RUN npm install gitbook -g;
RUN npm install gitbook-cli -g;
# 拉取文件
RUN git clone https://github.com/mySoul8012/SE-Basic-knowledge.git
# 进入目录
WORKDIR /SE-Basic-knowledge
# 初始化镜像
RUN gitbook init
CMD ["gitbook", "serve"]
输入构建的镜像
docker image build -t ming:0.1
保存和载入镜像
存出镜像
PS C:\Users\Administrator\Desktop> docker save -o ubuntu.tar ubuntu
PS C:\Users\Administrator\Desktop>
载入镜像
PS C:\Users\Administrator\Desktop> docker load -i ubuntu.tar
Loaded image: ubuntu:latest
PS C:\Users\Administrator\Desktop> docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
ff7a974b9771 nginx "/docker-entrypoint.…" 12 minutes ago Up 12 minutes 80/tcp fervent_curie
PS C:\Users\Administrator\Desktop> docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
test 0.1 54a6eb8d4483 10 minutes ago 132MB
ubuntu latest 74435f89ab78 2 weeks ago 73.9MB
nginx latest 2622e6cca7eb 3 weeks ago 132MB
PS C:\Users\Administrator\Desktop>
上传镜像
上传镜像到docker
PS C:\Users\Administrator\Desktop> docker push test
The push refers to repository [docker.io/library/test]
53d99160caf2: Preparing
f978b9ed3f26: Preparing
9040af41bb66: Preparing
7c7d7f446182: Preparing
d4cf327d8ef5: Preparing
13cb14c2acd3: Waiting
denied: requested access to the resource is denied
PS C:\Users\Administrator\Desktop>
可以看到拒绝,这里先登录账号
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: melovemingming
Password:
Login Succeeded
再进行文件的上传
docker push melovemingming/nginx