安装要求
- 必须在64位机器上运行
- Linux 系统内核为3.8或更新
在centos上安装docker
image.png
安装wget
yum -y install wget
配置epel源
yum install epel*
docker-ce 的yum源配置
cd /etc/yum.repos.d/
清华源
wget https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/centos/docker-ce.repo
阿里源
wget https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
image.png
安装docker
linux:
yum -y install docker-ce
mac:
https://store.docker.com/editions/community/docker-ce-desktop-mac
查看docker信息
查看版本docker -v
docker -v
Docker version 17.09.0-ce, build afdb6d4
查看info
docker info
Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?
Docker 使用
Docker的三个基本概念
- 镜像 (Image)
- 仓库 (Repository)
- 容器 (Container)
理解了这三个概念,就理解了Docker的整个生命周期
Docker镜像
主要内容
- 镜像概念
- 镜像原理
- 镜像获取
- 镜像管理
Docker镜像
镜像概念
Docker 镜像就是一个只读的模板
镜像可以用来创建Docker容器
镜像原理
镜像原理分层图
镜像原理图
下载镜像
启动docker服务
# systemctl start docker
下载 busybox
docker pull busybox
[root@bogon yum.repos.d]# docker pull busybox
Using default tag: latest
latest: Pulling from library/busybox
8e674ad76dce: Pull complete
Digest: sha256:c94cf1b87ccb80f2e6414ef913c748b105060debda482058d2b8d0fce39f11b9
Status: Downloaded newer image for busybox:latest
下载 centos:7
docker pull centos:7.4.1708
[root@bogon yum.repos.d]# docker pull centos:7.4.1708
7.4.1708: Pulling from library/centos
840caab23da4: Pull complete
Digest: sha256:8906d699cbd9406b07a105bedebc14a5945c200971b0a3a067aa245badc545b2
Status: Downloaded newer image for centos:7.4.1708
搜索
docker search centos
命令:
查看镜像文件
docker images
导出用 docker save
[root@centos7 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
jon/wordpress 4.2 5dfbd68b855c 4 days ago 532MB
jon/mysql 5.5 3a1c55b2cb13 4 days ago 543MB
jon/php-fpm 5.4 5497fe779c17 4 days ago 495MB
jon/cent-tomcat 7.0.82 c8ad1fe4cf33 4 days ago 664MB
jon/centos 7.4.1708 960ef292109b 4 days ago 403MB
jenkins 2.60.3 3f08dc4f3f5d 4 weeks ago 809MB
centos 7.4.1708 3afd47092a0e 4 weeks ago 197MB
[root@centos7 ~]# docker save -o centos_7_4_1708.tar centos:7.4.1708
导入
$ docker load --input centos_7_4_1708.tar
或
$ docker load < centos_7_4_1708.tar
解决docker pull 镜像速度慢的问题
直接下载Docker镜像时,由于种种原因,经常下载失败,即使连接成功也是很慢,怎么办呢
目前我知道可以提升速度的办法:DaoCloud 提供Docker Hub Mirror服务
用户可以进入Docker Hub Mirror注册入口注册(https://account.daocloud.io/signin)。在进入控制台页面后直接点击 启动你的加速器后,您即可得到一个Mirror的地址,将该地址配置在Docker Daemon的启动脚本中,重启Docker Daemon即可使得Docker Hub Mirror配置生效。
注册登陆后,点击加速器
命令
然后会得到一个Mirror的地址,将该地址配置在Docker Daemon的启动脚本中,重启Docker Daemon即可使得Docker Hub Mirror配置生效,换句话说,把这个获取的地址复制到要加速的服务器上执行一次,然后重启docker,就ok
加速命令
# curl -sSL https://get.daocloud.io/daotools/set_mirror.sh | sh -s http://4e70ba5d.m.daocloud.io
重启docker服务
systemctl restart docker
阿里加速
网站: https://cr.console.aliyun.com/#/accelerator
一. 从服务器下载
获取镜像命令:
docker pull <image>
搜索镜像命令:
docker search <image>
该命令会列出docker hub 服务器上所有符合条件的镜像
二. 使用dockerfile 文件定制镜像
dockerfile参数详解
vim Dockerfile
From centos:7.4.1708
MAINTAINER wangjj
ENV TZ "Asia/Shanghai"
ENV TERM xterm
RUN yum install -y epel-release
RUN yum install -y nginx
EXPOSE 80
CMD ["/bin/bash"]
创建镜像
docker build -t wang/centos-nginx:7.4 .
[root@bogon wjj]# docker build -t wang/centos-nginx:7.4 .
..........
Dependency Updated:
openssl-libs.x86_64 1:1.0.2k-16.el7_6.1
Complete!
Removing intermediate container 3697f5f8c378
---> fadca83962a0
Step 7/8 : EXPOSE 80
---> Running in cd6a54dc3171
Removing intermediate container cd6a54dc3171
---> 9cd7c92d8d0b
Step 8/8 : CMD ["/bin/bash && systemctl start nginx"]
---> Running in 40a8c1988926
Removing intermediate container 40a8c1988926
---> 0c5eec1121f6
Successfully built 0c5eec1121f6
Successfully tagged wang/centos-nginx:7.4
运行镜像
docker run -itd wang/centos-nginx:7.4
[root@bogon wjj]# docker run -itd wang/centos-nginx:7.4
efb1acf5577b3baf79dfb12cb7e29647366679c4fa33969b66d8ea6f4392e9f0
查看运行镜像
docker ps
[root@bogon wjj]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
efb1acf5577b wang/centos-nginx:7.4 "/bin/bash" 4 minutes ago Up 4 minutes 80/tcp