Docker简介
Docker 是一个
开源的应用容器引擎,让开发者可以打包他们的应用以及依赖包到一个可移植的镜像中,然后发布到任何流行的
Linux或
Windows 机器上,也可以实现
虚拟化。容器是完全使用
沙箱机制,相互之间不会有任何接口。
沙箱机制:在docker里启动了一个防火墙并不会影响本机的使用
一个完整的Docker有以下几个部分组成:
DockerClient客户端--命令
Docker Daemon守护进程--docker是一个进程,需要启动才可以使用
Docker Image镜像
DockerContainer容器
容器:模拟出一个相对隔离的空间(容器端口对物理机端口的映射)
镜像:用来创建容器的模板,放在镜像仓库中
docker与KVM的对比
docker的部署与安装
centos7自带的docker版本:
第一步,需要下载阿里云较新版本的docker源文件
命令如下:
可以查看下下载的地方,下载到这里了:
第二步,移动文件到yum.repos.d仓库下,命令如下:
mv docker-ce.repo /etc/yum.repos.d/
第三步,下载docker-ce
docker-ce社区免费版
docker-ee企业收费版
命令如下:yum install docker-ce -y
第四步,直接启动docker
报错是因为没启动docker,docker是一个进程需要启动才可以用
启动命令:
systemctl restart docker
systemctl enable docker#设置开机自启命令
第五步,验证docker服务是否可以正常启动
docker image ls
镜像加速
为什么要镜像加速?
因为docker在拉取镜像的时候是默认使用dockerhub上面的镜像即国外镜像速度比较慢,所以要用阿里云镜像加速
第一步,进入阿里云官网
第二步,进入控制台---》产品与服务---》容器镜像服务
第三步,创建一个脚本文件,加入脚本
1 创建脚本文件.sh结尾的
[root@localhost ~]# vi docker-speend.sh
2 把阿里云提供的脚本直接复制进来
3 启动加速,命令:bash docker-speend.sh
[root@localhost ~]# bash docker-speend.sh { "registry-mirrors": ["https://3kbsu061.mirror.aliyuncs.com"] } [root@localhost ~]#
成功后多了一个daemon.json文件
以上,加速就配置好了
可以做成一键下载及镜像加速配置,执行这个文件即可,命令bash xxx.sh
#!/bin/bash wget https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo mv docker-ce.repo /etc/yum.repos.d/ sudo mkdir -p /etc/docker sudo tee /etc/docker/daemon.json <<-'EOF' { "registry-mirrors": ["https://3kbsu061.mirror.aliyuncs.com"] } EOF sudo systemctl daemon-reload sudo systemctl restart docker
镜像操作指令
万能的帮助命令
docker --help#可以查看docker所有使用方法
docker image --help#可以查看docker镜像操作的所有方法
docker image ls --help#可以查看docker镜像操作中ls指令的用户
[root@localhost ~]# docker image --help Usage: docker image COMMAND Manage images Commands: build Build an image from a Dockerfile history Show the history of an image import Import the contents from a tarball to create a filesystem image inspect Display detailed information on one or more images load Load an image from a tar archive or STDIN ls List images prune Remove unused images pull Pull an image or a repository from a registry push Push an image or a repository to a registry rm Remove one or more images save Save one or more images to a tar archive (streamed to STDOUT by default) tag Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE Run 'docker image COMMAND --help' for more information on a command.
镜像操作指令:
第二步:docker镜像的使用命令详解
build 从dcoker文件里创建镜像 history 显示以个镜像的历史 import 从归档文件中创建镜像 inspect 显示一个或多个镜像的详细信息 load 加载一个镜像从tar压缩包或者标准输入 ls 列出镜像 prune 清除没有被使用的镜像 pull 从注册处拉取一个镜像或者镜像库 push 把一个镜像或者镜像库推送至注册处 rm 移除一个或多个镜像 save 保存一个或者多个镜像到tar压缩包 tag 创建一个SOURCE_IMAGE的标记TARGET_IMAGE
#拉取镜像
docker image pull REPOSITORY:TAG
REPOSITORY镜像名
如果在拉取镜像或其他操作的时候只指定了REPOSITORY的话,默认拉取的是tag为latest的版本
[root@localhost ~]# docker image pull busybox Using default tag: latest latest: Pulling from library/busybox 0669b0daf1fb: Pull complete Digest: sha256:b26cd013274a657b86e706210ddd5cc1f82f50155791199d29b9e86e935ce135 Status: Downloaded newer image for busybox:latest docker.io/library/busybox:latest [root@localhost ~]# docker image ls REPOSITORY TAG IMAGE ID CREATED SIZE busybox latest 83aa35aa1c79 4 weeks ago 1.22MB
#查看当前有哪些镜像
[root@localhost ~]# docker image ls REPOSITORY TAG IMAGE ID CREATED SIZE busybox latest 83aa35aa1c79 4 weeks ago 1.22MB
#查看镜像构建历史
[root@localhost ~]# docker image history busybox IMAGE CREATED CREATED BY SIZE COMMENT 83aa35aa1c79 4 weeks ago /bin/sh -c #(nop) CMD ["sh"] 0B <missing> 4 weeks ago /bin/sh -c #(nop) ADD file:450bea8cddb743ed2… 1.22MB
扫描二维码关注公众号,回复:
10702613 查看本文章
#制作镜像压缩包
[root@localhost ~]# docker image save --help Usage: docker image save [OPTIONS] IMAGE [IMAGE...] Save one or more images to a tar archive (streamed to STDOUT by default) Options: -o, --output string Write to a file, instead of STDOUT
方法一:
使用-o选项
docker image save busybox:latest -o busyboxlatest.tar
方法二:使用标准输出
docker image save busybox:1.31.0 > busybox1.31.-v2.tar
#使用镜像压缩包
[root@localhost ~]# docker image load --help Usage: docker image load [OPTIONS] Load an image from a tar archive or STDIN Options: -i, --input string Read from tar archive file, instead of STDIN -q, --quiet Suppress the load output
方法一:使用-i,input选项
1 先删除拉取的镜像 [root@localhost ~]# docker image rm busybox:latest Untagged: busybox:latest Untagged: busybox@sha256:b26cd013274a657b86e706210ddd5cc1f82f50155791199d29b9e86e935ce135 Deleted: sha256:83aa35aa1c79e4b6957e018da6e322bfca92bf3b4696a211b42502543c242d6f Deleted: sha256:a6d503001157aedc826853f9b67f26d35966221b158bff03849868ae4a821116 2 查看 [root@localhost ~]# docker image ls REPOSITORY TAG IMAGE ID CREATED SIZE busybox 1.31.0 19485c79a9bb 7 months ago 1.22MB 3 load压缩包 [root@localhost ~]# docker image load -i busyboxlatest.tar a6d503001157: Loading layer [==================================================>] 1.437MB/1.437MB Loaded image: busybox:latest 4 查看 [root@localhost ~]# docker image ls REPOSITORY TAG IMAGE ID CREATED SIZE busybox latest 83aa35aa1c79 4 weeks ago 1.22MB busybox 1.31.0 19485c79a9bb 7 months ago 1.22MB
方法二:标准输出
[root@localhost ~]# docker image rm busybox:1.31.0 Untagged: busybox:1.31.0 Untagged: busybox@sha256:fe301db49df08c384001ed752dff6d52b4305a73a7f608f21528048e8a08b51e Deleted: sha256:19485c79a9bbdca205fce4f791efeaa2a103e23431434696cc54fdd939e9198d Deleted: sha256:6c0ea40aef9d2795f922f4e8642f0cd9ffb9404e6f3214693a1fd45489f38b44 [root@localhost ~]# docker image ls REPOSITORY TAG IMAGE ID CREATED SIZE busybox latest 83aa35aa1c79 4 weeks ago 1.22MB [root@localhost ~]# docker image load < busybox1.31.-v2.tar 6c0ea40aef9d: Loading layer [==================================================>] 1.437MB/1.437MB Loaded image: busybox:1.31.0 [root@localhost ~]# docker image ls REPOSITORY TAG IMAGE ID CREATED SIZE busybox latest 83aa35aa1c79 4 weeks ago 1.22MB busybox 1.31.0 19485c79a9bb 7 months ago 1.22MB
#方法三:把压缩包发送到其他的服务器上
[root@localhost ~]# scp busyboxlatest.tar 39.106.41.11:/home
#删除镜像
方法一:
[root@localhost ~]# docker image rm busybox:1.31.0 Untagged: busybox:1.31.0 Untagged: busybox@sha256:fe301db49df08c384001ed752dff6d52b4305a73a7f608f21528048e8a08b51e Deleted: sha256:19485c79a9bbdca205fce4f791efeaa2a103e23431434696cc54fdd939e9198d Deleted: sha256:6c0ea40aef9d2795f922f4e8642f0cd9ffb9404e6f3214693a1fd45489f38b44
方法二:
rmi=image+rm
[root@localhost ~]# docker rmi busybox
#镜像改名
[root@localhost ~]# docker image tag --help Usage: docker image tag SOURCE_IMAGE[:TAG] TARGET_IMAGE[:TAG] Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE [root@localhost ~]# docker image tag busybox:1.31.0 busybox:1.32.0 [root@localhost ~]# docker image ls REPOSITORY TAG IMAGE ID CREATED SIZE busybox latest 83aa35aa1c79 4 weeks ago 1.22MB busybox 1.31.0 19485c79a9bb 7 months ago 1.22MB busybox 1.32.0 19485c79a9bb 7 months ago 1.22MB #改名相当于复制了一份,IMAGE ID是一模一样的
#清除不使用的镜像
[root@localhost ~]# docker image prune --help Usage: docker image prune [OPTIONS] Remove unused images Options: -a, --all Remove all unused images, not just dangling ones --filter filter Provide filter values (e.g. 'until=<timestamp>') -f, --force Do not prompt for confirmation [root@localhost ~]# docker image prune -f