1. Docker
Docker 对其系统又要求 必须是 CentOS 7, 最好是 CentOS 7.6\7.7 版本
- docker 官网:https://www.docker.com/
- EE 版本: 企业版本,收费
- CE: 社区版本,免费
2. Docker的安装
-
Docker的镜像在国外,我们下载的话,需要耗费很久的时间
-
这个时候我们采用 阿里云的镜像进行下载安装
-
在 阿里云镜像网: https://mirrors.aliyun.com
- 点击容器,找到 docker-ce,点击进去
- 下拉到末尾,找到下载地址
- 选择对应的系统,将其下载地址,与秘钥(gpg)地址写入 repo 文件当中
-
编辑 yum 源,指定 阿里云的镜像
[root@localhost ~]# vim /etc/yum.repos.d/docker.repo
[mirrors.aliyun.docker]
name=Ali_docker
# rpm源路径
baseurl=https://mirrors.aliyun.com/docker-ce/linux/centos/7/x86_64/stable/
enabled=1
gpgcheck=1
# 启动密钥校验
gpgkey=https://mirrors.aliyun.com/docker-ce/linux/centos/gpg
# 密钥路径.
- 安装 docker-ce
- docker程序
yum -y install docker-ce
- 启动docker-ce
systemctl start docker
- 查看版本号
docker -v
Docker version 19.03.8, build afacb8b
- 查看详细的docker 版本信息
[root@localhost ~] # docker version
Client: Docker Engine - Community
Version: 19.03.8
API version: 1.40
Go version: go1.12.17
Git commit: afacb8b
Built: Wed Mar 11 01:27:04 2020
OS/Arch: linux/amd64
Experimental: false
Server: Docker Engine - Community
Engine:
Version: 19.03.8
API version: 1.40 (minimum version 1.12)
Go version: go1.12.17
Git commit: afacb8b
Built: Wed Mar 11 01:25:42 2020
OS/Arch: linux/amd64
Experimental: false
containerd:
Version: 1.2.13
GitCommit: 7ad184331fa3e55e52b890ea95e65ba581ae3429
runc:
Version: 1.0.0-rc10
GitCommit: dc9208a3303feef5b3839f4323d9beb36df0a9dd
docker-init:
Version: 0.18.0
GitCommit: fec3683
- 查看docker信息
[root@localhost ~]# docker info
Client:
Debug Mode: false
Server:
Containers: 14
Running: 6
Paused: 0
Stopped: 8
Images: 6
……
……
Docker Root Dir: /var/lib/docker
Debug Mode: false
Registry: https://index.docker.io/v1/
Labels:
Experimental: false
Insecure Registries:
127.0.0.0/8
Registry Mirrors:
https://xjapiptd.mirror.aliyuncs.com/
Live Restore Enabled: false
3. Docker 环境准备
- Docker pull 命令是从镜像仓库中拉取或更新指定的镜像
- 网站连接地址为外网地址,拉取特别慢
- 所以先更换地址
1) 更换为 daocloud.io 镜像(不是很推荐)
- 官网:daocloud.io
- 注册,加速器,对应的操作系统又不同的方式
curl -sSL https://get.daocloud.io/daotools/set_mirror.sh | sh -s http://f1361db2.m.daocloud.io
- 重新启动
- 并查看信息
systemctl restart docker
docker info
2) 更换为阿里云镜像(推荐)
-
在阿里云官网,登录,找到 docker 镜像服务–》 控制台 --》 镜像加速 --》 复制地址
-
将地址更改
[root@localhost ~]# vim /etc/docker/daemon.json
{"registry-mirrors": ["https://xjapiptd.mirror.aliyuncs.com"]}
- 重载并重启docker
[root@localhost ~]# systemctl daemon-reload
[root@localhost ~]# systemctl restart docker
- 查看信息
[root@localhost ~]# docker info
3) 安装一些需要的镜像
- 下载 busybox镜像
- 演示生产环境迁移或者安装软件时使用
docker pull busybox
- 下载 CentOS
- 默认下载的 最新版本(8)版本,需要指定为 7 版本
docker pull centos:7
- 将 CentOS 7版本 重新一个标签为 latest
docker tag centos:7 centos:latest
- 查看当前镜像
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
busybox latest 83aa35aa1c79 13 days ago 1.22MB
centos 7 5e35e350aded 4 months ago 203MB
centos latest 5e35e350aded 4 months ago 203MB
- 删除CentOS 7 标签
docker rmi centos:7
- 查看 当前的镜像
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
busybox latest 83aa35aa1c79 13 days ago 1.22MB
httpd latest c5a012f9cf45 3 weeks ago 165MB
centos latest 5e35e350aded 4 months ago 203MB
在容器中安装Nginx
[root@localhost ~]# docker run -itd --name web centos /bin/bash
5df3e990873ac9a817964f1e5566af0d87373ea2fbc0016c527d3b6ba143da60
[root@localhost ~]#
[root@localhost ~]# docker-enter web
[root@5df3e990873a ~]#
[root@5df3e990873a ~]# yum provides ip
# 通过 yum provides 查看 ip 由那个软件包提供
[root@5df3e990873a ~]# yum -y install iproute
# 安装 ip 命令
[root@5df3e990873a ~]# yum -y install net-tools
[root@5df3e990873a ~]# yum -y install openssh-clients
[root@5df3e990873a ~]# ip a
# 查看本机地址
[root@5df3e990873a ~]# scp root@192.168.116.102:/root/nginx-* ./
# 拉取 Nginx源码包
[root@5df3e990873a ~]# yum -y install pcre* gcc openssl zlib-devel
[root@5df3e990873a nginx-1.12.2]# useradd nginx
[root@5df3e990873a ~]# tar -zxf nginx-1.12.2.tar.gz -C /usr/src/
[root@5df3e990873a ~]# cd /usr/src/nginx-1.12.2/
[root@5df3e990873a nginx-1.12.2]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx && make && make install
[root@5df3e990873a nginx-1.12.2]# ln -s /usr/local/nginx/sbin/nginx /usr/sbin/
[root@5df3e990873a nginx-1.12.2]# echo 'This is a web1' > /usr/local/nginx/html/index.html
[root@5df3e990873a nginx-1.12.2]# nginx
[root@5df3e990873a nginx-1.12.2]# curl 127.0.0.3
This is a web1
保存为一个新的镜像
docker commit web tianci/centos-ssh-nginx:latest
[root@localhost ~]# docker commit web tianci/centos-ssh-nginx:latest
sha256:7eabf3b5ebfff6b095a498683ac6c90dbaf8e8ff742f1c556a64330e15560ecb
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
tianci/centos-ssh-nginx latest 7eabf3b5ebff 28 seconds ago 507MB
busybox latest 83aa35aa1c79 13 days ago 1.22MB
httpd latest c5a012f9cf45 3 weeks ago 165MB
centos latest 5e35e350aded 4 months ago 203MB
运行镜像
[root@localhost ~]# docker run -itd --name web1 tianci/centos-ssh-nginx /bin/bash
ce853617590debf8cb5b477e1a88b2f9cb320a81ee02ebe6a7610e2738cc4e92
- 进入容器中
- IP地址不需要更改,会自动变更
[root@localhost ~]# docker exec -it web1 /bin/bash
[root@ce853617590d /]#