# 官方安装教程
[root@docker-server~]# yum install -y yum-utils
[root@docker-server~]# yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
# 安装docker-ce
yum install -y docker-ce
# 查看docker 版本
docker version
docker info
2.3 Docker 基础用法
-
基本使用
# 查看docker 信息
root@docker-server~]#docker version
root@docker-server~]#docker info
# 查看本地镜像
[root@docker-server~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
tomcat latest c662ee449a7e 4 days ago 680MB
nginx latest f8f4ffc8092c 5 days ago 133MB
rivers/nginx v1 f8f4ffc8092c 5 days ago 133MB
# 搜索镜像
root@docker-server~]# docker search nginx
# 下载镜像
[root@docker-server~]# docker pull nginx
# 运行docker 中的nginx 容器
[root@web02_server~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
redis latest 5d89766432d0 5 days ago 105MB
nginx latest f8f4ffc8092c 5 days ago 133MB
consol/tomcat-7.0 latest 7c34bafd1150 6 years ago 601MB
[root@docker-server~]# docker run -itd nginx
121962a620a5f18e56fb63e8cf19668f6d1be067eeccfcceaa27437355f0d499
[root@web02_server~]# docker run -itd -p 8088:8080 consol/tomcat-7.0:latest
3088f97bbd9af1d080b117b2b94fdea35f0461f5d9b0bba0e0c50eaabed8370e
# 查看容器进程
# docker ps 查看正在运行的 进程
[root@docker-server~]# docker ps -a # 显示所有的容器,包括未运行的
# 停止|重启|杀死 一个容器
#1.通过CONTAINER ID 关闭 (ID通过docker ps查看)
[root@web02_server~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
51399f33ff35 nginx "/docker-entrypoint.…" 36 minutes ago Up 36 minutes 0.0.0.0:8080->80/tcp, :::8080->80/tcp adoring_leavitt
[root@web02_server~]# docker kill 51399f33ff35
# 2.通过names stop|start 启动关闭
[root@web02_server~]# docker run -d -p 8099:8080 --name nginx-test nginx
bede0dff127c16c61c4221d217d33ec84c6d317e6cfdc6d8c6afe54a9428145b
[root@web02_server~]# docker stop nginx-test
nginx-test
[root@web02_server~]# docker start nginx-test
nginx-test
3.通过Dashboard页面关闭
# 暴露一个端口,宿主机进行访问
[root@docker-server~]# docker run -d -p 8080:80 nginx
51399f33ff3563d9444796d926409ce51e509f7b9e652fce75c415082dc72b27
浏览器输入:10.0.0.8:8080 即可访问容器里面的 nginx
浏览器输入:http://10.0.0.8:8088/,即可访问tomcat
三、Docker 镜像管理
3.1 镜像是什么?
- 一个分层存储的文件,不是一个单一的文件
- 一个软件的环境
- 一个镜像可以创建N个容器
- 一种标准化的交付
- 一个不包含Linux内核而又精简的Linux操作系统
3.2 配置加速器
Docker Hub是由Docker公司负责维护的公共镜像仓库,包含大量的容器镜像,Docker工具默认从这个公共镜像库下载镜像。
# Docker镜像修改方法,vim /etc/docker/daemon.json,执行如下命令即可:
vi /etc/docker/daemon.json
{
"registry-mirrors": ["https://b9pmyelo.mirror.aliyuncs.com"]
}
# 如果没有daemon.json文件,可以手动创建一个
# 重启docker
systemctl restart docker
3.3 镜像常用管理命令
-
docker 命令参数
[root@docker-server~]# docker --help
# docker 语法
Usage: docker [OPTIONS] COMMAND
A self-sufficient runtime for containers
# 常用选项参数,基本用不到
Options:
--config string Location of client config files
(default "/root/.docker")
-c, --context string Name of the context to use to connect
to the daemon (overrides DOCKER_HOST
env var and default context set with
"docker context use")
-D, --debug Enable debug mode
-H, --host list Daemon socket(s) to connect to
-l, --log-level string Set the logging level
("debug"|"info"|"warn"|"error"|"fatal")
(default "info")
--tls Use TLS; implied by --tlsverify
--tlscacert string Trust certs signed only by this CA
(default "/root/.docker/ca.pem")
--tlscert string Path to TLS certificate file (default
"/root/.docker/cert.pem")
--tlskey string Path to TLS key file (default
"/root/.docker/key.pem")
--tlsverify Use TLS and verify the remote
-v, --version Print version information and quit
# docker 管理命令,后来分类细化后才有的
Management Commands:
app* Docker App (Docker Inc., v0.9.1-beta3)
builder Manage builds
buildx* Build with BuildKit (Docker Inc., v0.6.1-docker)
config Manage Docker configs
container Manage containers
context Manage contexts
image Manage images
manifest Manage Docker image manifests and manifest lists
network Manage networks
node Manage Swarm nodes
plugin Manage plugins
scan* Docker Scan (Docker Inc., v0.8.0)
secret Manage Docker secrets
service Manage services
stack Manage Docker stacks
swarm Manage Swarm
system Manage Docker
trust Manage trust on Docker images
volume Manage volumes
# docker 命令,开始只有command命令,没有管理命令
Commands:
attach Attach local standard input, output, and error streams to a running container
build Build an image from a Dockerfile
commit Create a new image from a container's changes
cp Copy files/folders between a container and the local filesystem
create Create a new container
diff Inspect changes to files or directories on a container's filesystem
events Get real time events from the server
exec Run a command in a running container
export Export a container's filesystem as a tar archive
history Show the history of an image
images List images
import Import the contents from a tarball to create a filesystem image
info Display system-wide information
inspect Return low-level information on Docker objects
kill Kill one or more running containers
load Load an image from a tar archive or STDIN
login Log in to a Docker registry
logout Log out from a Docker registry
logs Fetch the logs of a container
pause Pause all processes within one or more containers
port List port mappings or a specific mapping for the container
ps List containers
pull Pull an image or a repository from a registry
push Push an image or a repository to a registry
rename Rename a container
restart Restart one or more containers
rm Remove one or more containers
rmi Remove one or more images
run Run a command in a new container
save Save one or more images to a tar archive (streamed to STDOUT by default)
search Search the Docker Hub for images
start Start one or more stopped containers
stats Display a live stream of container(s) resource usage statistics
stop Stop one or more running containers
tag Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE
top Display the running processes of a container
unpause Unpause all processes within one or more containers
update Update configuration of one or more containers
version Show the Docker version information
wait Block until one or more containers stop, then print their exit codes
# docker命令主要分2部分,早期 只有command 这一部分,当发展到CE 版本,对docker 命令做了一个分类,形成了Management Commands 这一部分
- 常用管理命令
| 指令 | 描述 | eg: |
|---|---|---|
| ls | 列出本地镜像 | docker images ls |
| build | 构建镜像来自Dockerfile | 后面详细介绍 |
| history | 查看镜像历史 | docker history nginx |
| inspect | 现实一个或多个镜像详细信息 | docker inspect tomcat |
| pull | 从镜像仓库拉取镜像 | docker pull nginx |
| push | 推送一个镜像到镜像仓库 | docker push rivers/nginx:v1 |
| rm | 移除一个或多个镜像 | docker rm redis |
| prune | 移除没有被标记或者没有任何容器引用的镜像,-a 就会删除所有本地镜像(docker image prune -a) | docker image prune |
| tag | 创建一个引用源镜像标记目标镜像 | docker tag nginx:latest rivers/nginx:v1 |
| save | 保存一个或多个镜像到一个tar 归档文件(假如某台不能上网,就在可以上网的机器上拉取,归档,scp传过去,加载) | docker save nginx -o nginx.tar |
| load | 加载镜像来自tar归档或标准输入 | docker load -i nginx.tar |
三、Docker 容器管理
3.1 创建容器常用选项
- 创建镜像常用选项
| 选项 | 描述 |
|---|---|
| -i, –interactive | 交互式 |
| -t, –tty | 分配一个伪终端 |
| -d, –detach | 运行容器到后台 |
| -e, –env | 设置环境变量 |
| -p, –publish list | 发布容器端口到主机 |
| -P, –publish-all | 发布容器所有EXPOSE的端口到宿主机随机端口 |
| –name string | 指定容器名称 |
| -h, –hostname | 设置容器主机名 |
| –ip string | 指定容器IP,只能用于自定义网络 |
| –network | 连接容器到一个网络 |
| -v, –volume list–mount mount(新方式) | 将文件系统附加到容器 |
| –restart string | 容器退出时重启策略,默认no,可选值:[always] |
| -m,–memory | 容器可以使用的最大内存量 |
| –memory-swap | 允许交换到磁盘的内存量 |
| –memory-swappiness=<0-100> | 容器使用SWAP分区交换的百分比(0-100,默认为-1) |
| –oom-kill-disable | 禁用OOM Killer |
| –cpus | 可以使用的CPU数量 |
| –cpuset -cpus | 限制容器使用特定的CPU核心,如(0-3, 0,1) |
| –cpu-shares | CPU共享(相对权重) |
3.2 常用管理命令
- 常用管理命令
| 选项 | 描述 |
|---|---|
| ls | 列出容器 |
| inspect | 查看一个或多个容器详细信息 |
| exec | 在运行容器中执行命令 |
| commit | 创建一个新镜像来自一个容器(用的少) |
| cp | 拷贝文件/文件夹到一个容器 |
| logs | 获取一个容器日志 |
| port | 列出或指定容器端口映射 |
| top | 显示一个容器运行的进程 |
| stats | 显示容器资源使用统计 |
| stop/start/restart | 停止/启动一个或多个容器 |
| rm | 删除一个或多个容器 |
| prune | 移除已停止的容器 |
3.3 容器和镜像命令实战
3.3.1 镜像命令实战总结
-
镜像命令
#查找redis镜像
[root@web02_server~]# docker search redis
#下载镜像
[root@web02_server~]# docker pull redis
#查看本机镜像
[root@web02_server~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
redis latest 5d89766432d0 5 days ago 105MB
nginx latest f8f4ffc8092c 5 days ago 133MB
consol/tomcat-7.0 latest 7c34bafd1150 6 years ago 601MB
[root@web02_server~]#
#上传镜像
[root@web02_server~]# docker push redisnew:latest
#备份镜像为tar包
[root@web02_server~]# docker save nginx -o nginx.tar
#导入备份的镜像文件
[root@web02_server~]# docker load -i nginx.tar
#删除镜像(必须先删除该镜像启动的所有容器)
[root@web02_server~]# docker rmi nginx:latest
[root@web02_server~]# docker ps -l
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f49824647c75 centos:latest "/bin/bash" 11 minutes ago Exited (1) 7 seconds ago apache
[root@web02_server~]# docker rm apache # 删除自定义的apache 容器
apache
# 查看镜像的详细信息
[root@web02_server~]# docker inspect redis:latest
# 查看镜像的历史信息
[root@web02_server~]# docker history redis:latest
# 给镜像添加新的名词和标签
[root@web02_server~]# docker tag redis:latest newname:newtag
[root@web02_server~]# docker tag nginx:latest rivers/nginx:v1
3.3.2 容器命令实战总结
-
容器命令实战
# 在后台启动容器
[root@web02_server~]# docker run -itd nginx
cb4c606fd62558321677a9b15b3be40390bdd0077da6a5ee3d9f884bb31d03a5
# 在前台启动容器
[root@web02_server~]# docker run -it --name Centos centos:latest /bin/bash
[root@web02_server~]# ctrl+p, ctrl+q # 使用快捷键退出,保证容器不关闭
[root@web02_server~]# docker ps -l #查看最近启动的容器进程
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
fef50558fd7f centos:latest "/bin/bash" About a minute ago Up About a minute Centos
[root@web02_server~]#
# 查看正在运行的容器
[root@web02_server~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
fef50558fd7f centos:latest "/bin/bash" 2 minutes ago Up 2 minutes Centos
cb4c606fd625 nginx "/docker-entrypoint.…" 4 minutes ago Up 4 minutes 80/tcp silly_curie
3088f97bbd9a consol/tomcat-7.0:latest "/bin/sh -c /opt/tom…" 2 hours ago Up 2 hours 8778/tcp, 0.0.0.0:8088->8080/tcp, :::8088->8080/tcp magical_lumiere
# 只查看id
[root@web02_server~]# docker ps -q
fef50558fd7f
cb4c606fd625
3088f97bbd9a
# 查看所有容器,包含未启动的
[root@web02_server~]# docker ps -a
# 启动、停止、重启容器
[root@web02_server~]# docker stop Centos #前提是启动时 加参数 --name 选项才可以
Centos
[root@web02_server~]# docker start Centos
Centos
[root@web02_server~]# docker ps -q # 只查看ID
fef50558fd7f
cb4c606fd625
3088f97bbd9a
[root@web02_server~]# docker stop fef50558fd7f
fef50558fd7f
[root@web02_server~]# docker restart fef50558fd7f
fef50558fd7f
[root@web02_server~]#
# 删除容器 -f 参数,强制删除
[root@web02_server~]# docker rm -f fef50558fd7f
fef50558fd7f
[root@web02_server~]#
# 删除所有容器
[root@web02_server~]# docker rm -f $(docker ps -aq)
# 进入容器的默认进程
[root@web02_server~]# docker attach cb4c606fd625
2021/10/04 07:51:40 [notice] 1#1: signal 28 (SIGWINCH) received
2021/10/04 07:51:40 [notice] 1#1: signal 28 (SIGWINCH) received
2021/10/04 07:51:40 [notice] 31#31: signal 28 (SIGWINCH) received
2021/10/04 07:51:40 [notice] 31#31: signal 28 (SIGWINCH) received
[root@web02_server~]# exit # 退出后容器会关闭
# 查看容器详细信息
[root@web02_server~]# docker inspect cb4c606fd625
[root@web02_server~]# docker inspect 7f39d656b934|grep -i ipaddr # 查看容器IP地址
"SecondaryIPAddresses": null,
"IPAddress": "172.17.0.3",
"IPAddress": "172.17.0.3",
# 进入nginx容器,查看nginx路径,设置html文件,访问测试
[root@web02_server~]# docker ps -l
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
7f39d656b934 nginx "/docker-entrypoint.…" 3 minutes ago Up 2 minutes 0.0.0.0:8000->80/tcp, :::8000->80/tcp wizardly_tesla
[root@web02_server~]# docker exec -it 7f39d656b934 /bin/bash
root@7f39d656b934:/# cd /usr/share/nginx/html/ #进入nginx网站目录
root@7f39d656b934:/usr/share/nginx/html# ls
50x.html index.html
root@7f39d656b934:/usr/share/nginx/html#
root@7f39d656b934:/usr/share/nginx/html# echo "<h1>this nginx Container</h1>" > index.html # 准备测试文件内容
root@7f39d656b934:/usr/share/nginx/html# exit
# 测试访问
[root@web02_server~]# curl 0 10.0.0.8:8000/index.html
curl: (7) Failed connect to 0:80; Connection refused
<h1>this nginx Container</h1>
[root@web02_server~]#
# 从容器内拷贝首页文件到宿主机,修改后拷贝回容器内
[root@web02_server~]# docker cp 7f39d656b934:/usr/share/nginx/html/index.html .
[root@web02_server~]# ls
anaconda-ks.cfg backup index.html nohup.out szz-job-v1.jar
[root@web02_server~]# vim index.html
<h1>this nginx Container</h1>
<h2>在宿主机上修改的index.html</h2>
# 将本地index.html 拷贝到docker容器里面
[root@web02_server~]# docker cp ./index.html 7f39d656b934:/usr/share/nginx/html/index.html
[root@web02_server~]#
# 测试
[root@web02_server~]# docker inspect 7f39d656b934|grep -i ipaddr
"SecondaryIPAddresses": null,
"IPAddress": "172.17.0.3",
"IPAddress": "172.17.0.3",
[root@web02_server~]# curl http://172.17.0.3
<h1>this nginx Container</h1>
<h2>在宿主机上修改的index.html</h2>
[root@web02_server~]#
# 启动一个nginx 容器
[root@web02_server~]# docker run -d -e evn=prod -p 88:80 --name web01 -h nginx-server --restart=always nginx
3d175068754b1e1d5d14970e5f41b35c6cbcb37427b9cf83e4a284ec377aeed0
-e 环境变量 evn
--name 设置名称 web01
-h 设置 主机名 nginx-server
-- restart=always 设置开机启动,docker 重启了,设置了restart=always,docker 也会自动拉起nginx服务
echo $evn
prod
[root@web02_server~]# docker exec -it web01 /bin/bash
root@nginx-server:/# echo $evn
prod
root@nginx-server:/# hostname
nginx-server
root@nginx-server:/#
# 只查看web01 容器运行的端口
[root@web02_server~]# docker port web01
80/tcp -> 0.0.0.0:88
80/tcp -> :::88
[root@web02_server~]#
# 不进入容器,查看容器利用率
[root@web02_server~]# docker stats web01
CONTAINER ID NAME CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O PIDS
3d175068754b web01 0.00% 1.719MiB / 904.6MiB 0.19% 65 / 0B 3.2MB / 0B 2
^C
[root@web02_server~]# docker stats web01 --no-stream|awk '{print $3}'
NAME
0.00%
[root@web02_server~]# docker stats web01 --no-stream|awk 'NR==2{print $3}'
0.00%
3.3.3 资源控制命令总结
-
资源限制
# 限制redis 容器使用资源,非常有必要,最多使用500M,最多使用1个cpu
[root@web02_server~]# docker run -m="500m" --cpus="1" -d redis
ce392030c634d7206b51a11af880492232ae48ba8d7d36b4e6e30e07af17238c
[root@web02_server~]#
# 限制CPU的使用速率:
[root@web02_server~]# docker run --cpu-quota 20000 redis
# 据多个容器设置其占用CPU的比例,如下为1:1:2:4
[root@web02_server~]# docker run --cpu-shares 1024 容器1^C
[root@web02_server~]# docker run --cpu-shares 1024 容器2^C
[root@web02_server~]# docker run --cpu-shares 2048 容器3^C
[root@web02_server~]# docker run --cpu-shares 4096 容器4^C
# 根据多个容器进行绑定所使用的CPU的核心,如下为八颗CPU,分配给两个容器:
[root@web02_server~]# docker run --cpuset-cpus 0,1,2,3 容器1^C
[root@web02_server~]# docker run --cpuset-cpus 4,5,6,7 容器2^C
#内存控制
#限制容器1使用的内存为512M;
[root@web02_server~]# docker run -m 512m 容器1^C
#blkio的控制:
如果一台服务器上多个容器的部署,会出现同时又多个程序进程读写磁盘数据的情况;
[root@web02_server~]# docker run --device-write-bps /dev/sda1:1mb 容器1^C
[root@web02_server~]# docker run --device-read-bps /dev/sda1:1mb 容器2^C
3.4 容器数据持久化
3.4.1 为什么要容器数据持久化
-
为什么要数据持久化
# 当我们在虚拟机中创建了容器后,如果产生了数据,我们删除了容器,数据就会丢失,下次从新创建容器,就是默认的,之前配置的就会丢失,所以需要数据持久化。创建容器后,数据也写到宿主机上,这样就不会数据丢失。
# Docker提供2种方式将数据从宿主机挂载到容器中:
• volumes:Docker管理宿主机文件系统的一部分(/var/lib/docker/volumes)。
• bind mounts:将宿主机上的任意位置的文件或者目录挂载到容器中
volumes示例:
1、创建数据卷
# docker volume create nginx-vol
# docker volume ls
# docker volume inspect nginx-vol
2、使用数据卷
# docker run -d --name=nginx-test --mount src=nginxvol,dst=/usr/share/nginx/html nginx
# docker run -d --name=nginx-test -v nginxvol:/usr/share/nginx/html nginx
bind mounts示例:
1、挂载宿主机目录到容器
# docker run -d --name=nginx-test --mount
type=bind,src=/app/wwwroot,dst=/usr/share/nginx/html nginx
# docker run -d --name=nginx-test -v
/app/wwwroot:/usr/share/nginx/html nginx
3.4.2 数据持久化操作案例
-
将本地nginx 容器数据持久化,采用bind mounts 实现
# 1. 清除原本的nginx 镜像,确保环境无误
[root@web02_server~]# docker stop 3d175068754b
3d175068754b
[root@web02_server~]# docker rm 3d175068754b
3d175068754b
[root@web02_server~]#
[root@web02_server~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
# 2. 创建宿主机 挂在目录 /opt/wwwroot
[root@web02_server~]# mkdir /opt/wwwroot
# 3. 创建容器
[root@web02_server~]# docker run -itd -p 88:80 --name web -v /opt/wwwroot/:/usr/share/nginx/html nginx
cf6e36c41091731b13bb47aaf6515a9d864bce1bec0f766b2e16de4eee6b407b
# 查看最新创建的容器进程
[root@web02_server~]# docker ps -l
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
cf6e36c41091 nginx "/docker-entrypoint.…" 26 seconds ago Up 25 seconds 0.0.0.0:88->80/tcp, :::88->80/tcp web
# 过滤出容器IP 地址,方便本机测试
[root@web02_server~]# docker inspect cf6e36c41091|grep -i ipaddr
"SecondaryIPAddresses": null,
"IPAddress": "172.17.0.2",
"IPAddress": "172.17.0.2",
# 3.测试 报错,发现403.这是正常的,原因看第4步
[root@web02_server~]# curl http://172.17.0.2
<html>
<head><title>403 Forbidden</title></head>
<body>
<center><h1>403 Forbidden</h1></center>
<hr><center>nginx/1.21.3</center>
</body>
</html>
[root@web02_server~]#
#4.进入容器,进入nginx 网站目录,发现没有html文件,我们新建一个
[root@web02_server~]# docker exec -it web /bin/bash
root@cf6e36c41091:/# cd /usr/share/nginx/html
root@cf6e36c41091:/# ls
root@cf6e36c41091:/usr/share/nginx/html# echo "<h1>hello</h1>" > index.html
root@cf6e36c41091:/usr/share/nginx/html# exit
exit
# 5.再次测试,访问正常
[root@web02_server~]# curl http://172.17.0.2
<h1>hello</h1>
[root@web02_server~]#
# 6.此时stop nginx 容器,并且删除,再次访问
[root@web02_server~]# docker stop web
web
[root@web02_server~]# docker rm web
web
[root@web02_server~]# curl http://172.17.0.2^C # 无法访问
# 7. 再次创建容器,测试:发现数据还在
[root@web02_server~]# docker run -itd -p 88:80 --name web -v /opt/wwwroot/:/usr/share/nginx/html nginx
d350f8f8fc20b4c71c40d694964792aaecdbf0402c080591d821008a7201b828
[root@web02_server~]# curl http://172.17.0.2
<h1>hello</h1>
[root@web02_server~]#
3.5 Docker 容器网络
3.5.1 docker 容器网络通信原理及配置
- Docker使用iptables实现网络通信。
- veth pair:成对出现的一种虚拟网络设备,数据从一端进,从另
一端出。 用于解决网络命名空间之间隔离。 - docker0:网桥是一个二层网络设备,通过网桥可以将Linux支持
的不同的端口连接起来,并实现类似交换机那样的多对多的通信。
3.5.1.1 外部访问容器 iptables -t nat -vnL DOCKER
-
外部访问 容器
[root@web02_server~]# iptables -t nat -vnL DOCKER
Chain DOCKER (2 references)
pkts bytes target prot opt in out source destination
0 0 RETURN all -- docker0 * 0.0.0.0/0 0.0.0.0/0
0 0 DNAT tcp -- !docker0 * 0.0.0.0/0 0.0.0.0/0 tcp dpt:88 to:172.17.0.2:80
[root@web02_server~]#
3.5.1.2 容器访问外部 iptables -t nat -vnL POSTROUTING
-
容器访问外部
[root@web02_server~]# iptables -t nat -vnL POSTROUTING
Chain POSTROUTING (policy ACCEPT 5331 packets, 344K bytes)
pkts bytes target prot opt in out source destination
0 0 MASQUERADE all -- * !docker0 172.17.0.0/16 0.0.0.0/0
0 0 MASQUERADE tcp -- * * 172.17.0.2 172.17.0.2 tcp dpt:80
3.5.1.3 通讯方式一:端口映射|暴露
-
端口映射
# 在run 容器时,-P选项可以将docker容器内的端口随机映射到宿主机的49000-49900范围;
1.创建基于centos镜像的容器并且实现端口映射,将容器的httpd的80端口映射到宿主机的8081端口;
[root@web02\_server~]# docker run -itd --name docker\_httpd -e"container=container" --privileged -p 8081:80 centos /usr/sbin/init
219f3636adb4c17920a5768425d48eb1b50275ba89bf3b4bd1715ff846c14afb
[root@web02\_server~]# docker ps|grep init
219f3636adb4 centos "/usr/sbin/init" 19 seconds ago Up 17 seconds 0.0.0.0:8081->80/tcp, :::8081->80/tcp docker_httpd
[root@web02\_server~]#
2.为容器安装httpd服务并且启动服务;
[root@web02\_server~]# docker exec -it docker\_httpd /bin/bash
[root@219f3636adb4 /]# yum -y install net-tools httpd vim
[root@219f3636adb4 /]# systemctl start httpd
[root@219f3636adb4 /]# ip a|grep 172|awk '{print $2}'
172.17.0.4/16
[root@219f3636adb4 /]# netstat -lnutp|grep 80
tcp 0 0 0.0.0.0:80 0.0.0.0:\* LISTEN 331/httpd
[root@219f3636adb4 /]# exit
exit
3. docker 宿主机查看,检查
[root@web02\_server~]# docker ps|grep init
219f3636adb4 centos "/usr/sbin/init" 7 minutes ago Up 7 minutes 0.0.0.0:8081->80/tcp, :::8081->80/tcp docker_httpd
[root@web02\_server~]#
[root@web02\_server~]# netstat -lnutp|grep 8081
tcp 0 0 0.0.0.0:8081 0.0.0.0:\* LISTEN 36063/docker-proxy
tcp6 0 0 :::8081 :::\* LISTEN 36067/docker-proxy
[root@web02\_server~]#
4.外部节点测试:
浏览器输入:http://10.0.0.8:8081
或者 linux 命令行输入:curl http://10.0.0.8:8081
3.5.1.4 通讯方式二:容器互联
-
容器互联
1.创建源容器:
[root@web02\_server~]# docker run -itd --name centos7-1 centos /bin/bash
925baaf21047ce727c65d527394b5658efc463a200d8a485ad900ac348ac0248
[root@web02\_server~]# docker ps -l
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
925baaf21047 centos "/bin/bash" 10 seconds ago Up 9 seconds centos7-1
2.创建接收容器,指定centos容器为源容器:
[root@web02\_server~]# docker run -itd --name centos7-2 --link centos7-1:centos7-1 centos /bin/bash
dc2af328a61c9c2a3ba165da5e9d0d9db6b98add5331bde1a10ea4a46a611cdb
[root@web02\_server~]#
[root@web02\_server~]# docker ps |grep centos7-2
dc2af328a61c centos "/bin/bash" About a minute ago Up About a minute centos7-2
[root@web02\_server~]#
3.连接到接收容器进行测试容器之间的连通性:
[root@web02\_server~]# docker exec -it centos7-2 /bin/bash
[root@dc2af328a61c /]# ping centos7-1
PING centos7-1 (172.17.0.4) 56(84) bytes of data.
64 bytes from centos7-1 (172.17.0.4): icmp_seq=1 ttl=64 time=0.075 ms
64 bytes from centos7-1 (172.17.0.4): icmp_seq=2 ttl=64 time=0.045 ms
^C
--- centos7-1 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1002ms
rtt min/avg/max/mdev = 0.045/0.060/0.075/0.015 ms
[root@dc2af328a61c /]#
3.5.2 Docker 4种 网络模式
- docker 容器网络
Docker在安装后自动提供3种网络,可以使用
docker network ls命令查看[root@web02_server~]# docker network ls NETWORK ID NAME DRIVER SCOPE f2671c948ee2 bridge bridge local 56ca8e9e001c host host local fe6f1a90a81f none null local [root@web02_server~]#Docker使用Linux桥接,在宿主机虚拟一个Docker容器网桥(docker0),Docker启动一个容器时会根据Docker网桥的网段分配给容器一个IP地址,称为Container-IP,同时Docker网桥是每个容器的默认网关。因为在同一宿主机内的容器都接入同一个网桥,这样容器之间就能够通过容器的Container-IP直接通信
3.5.2.1 bridge 模式
-
bridge 桥接模式
1.Bridge模式:默认的网络模式,在启动docker进程后,宿主机会创建名为docker0的虚拟网桥,ip地址为172.17.0.1,所有容器的默认网络会自动获取一个172.17.0.0网络的地址,实际上宿主机在此模式默认自动做了一条NAT转换规则并且开启路由转发;
3.5.2.2 host 模式
-
host 模式—共享宿主机的IP和端口,但不能冲突
2.Host模式:如果启动容器的时候使用host模式,那么这个容器将不会获得一个独立的Network Namespace,而是和宿主机共用一个Network Namespace。容器将不会虚拟出自己的网卡,配置自己的IP等,而是使用宿主机的IP和端口。但是,容器的其他方面,如文件系统、进程列表等还是和宿主机隔离的;
3.5.2.3 none 模式
-
none 模式
3.None模式: Docker容器拥有自己的Network Namespace,但是,并不为Docker容器进行任何网络配置。也就是说,这个Docker容器没有网卡、IP、路由等信息。需要我们自己为Docker容器添加网卡、配置IP等;
3.5.2.4 Container模式
-
Container模式
指定新创建的容器和已经存在的一个容器共享一个 Network Namespace,而不是和宿主机共享。新创建的容器不会创建自己的网卡,配置自己的 IP,而是和一个指定的容器共享 IP、端口范围等。同样,两个容器除了网络方面,其他的如文件系统、进程列表等还是隔离的。两个容器的进程可以通过 lo 网卡设备通信;
3.5.3 配置网络模式
3.5.3.1 配置默认桥接bridge模式:默认桥接到 docker 0
-
默认bridge 模式,可以不指定
# 默认桥接到docker 0 通信
[root@web02_server~]# ip address show docker0
4: docker0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default
link/ether 02:42:8b:50:e3:1e brd ff:ff:ff:ff:ff:ff
inet 172.17.0.1/16 brd 172.17.255.255 scope global docker0
valid_lft forever preferred_lft forever
inet6 fe80::42:8bff:fe50:e31e/64 scope link
valid_lft forever preferred_lft forever
[root@web02_server~]#
[root@web02_server~]# docker run -itd --net=bridge --name centos7-3 centos
d4150878301744871040d8e2043a98ab831fc2473c09a033485da3a3ae1c1764
[root@web02_server~]# docker ps -l
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
d41508783017 centos "/bin/bash" 9 seconds ago Up 6 seconds centos7-3
3.5.3.2 配置host模式:使用 --net=host指定
-
配置host 模式
[root@web02_server~]# docker run -itd --net=host --name centos7-3 centos
bde69a0881b49411be31f9aa25d9729953a8762217c179e3f6c78a38225e0661
[root@web02_server~]# docker ps -l
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
bde69a0881b4 centos "/bin/bash" 9 seconds ago Up 8 seconds centos7-3
[root@web02_server~]
# 进入创建的 centos 7-3容器
[root@web02_server~]# docker exec -it centos7-3 /bin/bash
[root@web02_server /]#
# 创建一个httpd 服务
[root@web02_server /]# yum -y install iproute net-tools httpd
# 查看centos 7-3容器的路由
[root@web02_server /]# ip route
default via 10.0.0.2 dev eth0 proto static metric 102
10.0.0.0/24 dev eth0 proto kernel scope link src 10.0.0.8 metric 102
172.16.1.0/24 dev eth1 proto kernel scope link src 172.16.1.8 metric 101
172.17.0.0/16 dev docker0 proto kernel scope link src 172.17.0.1
[root@web02_server /]#
[root@web02_server /]# /usr/sbin/httpd
httpd (pid 105) already running
[root@web02_server /]#
[root@web02_server~]# netstat -utpln |grep 80
tcp6 0 0 :::80 :::* LISTEN 31347/httpd
tcp6 0 0 :::8081 :::* LISTEN 7387/java
[root@web02_server~]#
3.5.3.3 配置none模式
-
配置 none模式
[root@web02_server~]# docker run -itd --net=none --name centos7 centos
28da7577477176c4d9b95df5f5e6553714d6a87539323c4b68fa3895bb50c064
[root@web02_server~]# docker ps -l
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
28da75774771 centos "/bin/bash" 7 seconds ago Up 6 seconds centos7
[root@web02_server~]# docker inspect centos7 |grep -i ipaddr
"SecondaryIPAddresses": null,
"IPAddress": "",
"IPAddress": "",
[root@web02_server~]#
3.5.3.4 配置container模式:使用 --net=container:container_id/container_name
-
配置container 模式
# 先启动一个容器
[root@web02_server~]# docker run -itd --name tomcat consol/tomcat-7.0
6b2544e960634da6389d10a703d5591fe2bcef57d128328929eb0720de3c1a49
[root@web02_server~]# docker ps -l
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
6b2544e96063 consol/tomcat-7.0 "/bin/sh -c /opt/tom…" 10 seconds ago Up 7 seconds 8080/tcp, 8778/tcp tomcat
[root@web02_server~]# docker inspect tomcat |grep -i ipaddr
"SecondaryIPAddresses": null,
"IPAddress": "172.17.0.3",
"IPAddress": "172.17.0.3",
[root@web02_server~]#
# 新创建一个容器
[root@web02_server~]# docker run -itd --name=container\_test --net=container:6b2544e96063 redis
291a19591a0fccc79ce8eddf9399a5da831cfb82261be60e91e89858f1af9410
[root@web02_server~]#
# 查看最新启动的 容器
[root@web02_server~]# docker ps -l
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
291a19591a0f redis "docker-entrypoint.s…" 38 seconds ago Up 37 seconds container_test
[root@web02_server~]#
3.5.3.5 自定义配置桥接模式、修改网段— 实战案例
-
自定义桥接模式
# 注意说明:
本机 宿主机 eth0 ip 地址:10.0.0.8
网关:10.0.0.2
DNS:223.5.5.5
# CentOS7需安装网络和桥接工具
[root@web02_server~]# yum -y install bridge-utils net-tools
[root@web02_server~]#cd /etc/sysconfig/network-scripts/
# 复制一份网卡,重命名为 ifcfg-br0
[root@web02_server/etc/sysconfig/network-scripts]# \cp ifcfg-eth0 ./ifcfg-br0\cp ifcfg-eth0 ./ifcfg-br0
# 备份一个eth0 网卡,方便恢复
[root@web02_server/etc/sysconfig/network-scripts]# \cp ifcfg-eth0 ifcfg-eth0.bak
# 编写 ifcfg-br0 桥接网卡
[root@web02_server/etc/sysconfig/network-scripts]# vim ifcfg-br0
TYPE=Bridge
BOOTPROTO=static
DEFROUTE=yes
PEERDNS=yes
PEERROUTES=yes
IPV4\_FAILURE\_FATAL=yes
NAME=br0
DEVICE=br0
ONBOOT=yes
NM\_CONTROLLED=no
IPADDR=10.0.0.8
PREFIX=24
GATEWAY=10.0.0.2
DNS1=10.0.0.2
# 修改 ifcfg-eth0 网卡
[root@web02_server/etc/sysconfig/network-scripts]# vim ifcfg-eth0
TYPE=Ethernet
BOOTPROTO=none
DEFROUTE=yes
PEERDNS=yes
PEERROUTES=yes
IPV4\_FAILURE\_FATAL=yes
NAME=eth0
DEVICE=eth0
ONBOOT=yes
NM\_CONTROLLED=no
BRIDGE=br0
# 重启网卡
[root@web02_server/etc/sysconfig/network-scripts]# cd --
/root
[root@web02_server~]# systemctl restart network
# 此时,eth0 网卡桥接到了 br0 网卡,eth0 没有了IP地址
[root@web02_server~]# ifconfig
br0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 10.0.0.8 netmask 255.255.255.0 broadcast 10.0.0.255
inet6 fe80::20c:29ff:fe32:8b73 prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:32:8b:73 txqueuelen 1000 (Ethernet)
RX packets 67295 bytes 230884723 (220.1 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 56721 bytes 6322538 (6.0 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet6 fe80::20c:29ff:fe32:8b73 prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:32:8b:73 txqueuelen 1000 (Ethernet)
RX packets 10371209 bytes 13133670349 (12.2 GiB)
RX errors 0 dropped 6 overruns 0 frame 0
TX packets 2643465 bytes 285133908 (271.9 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
[root@web02_server~]# brctl show
bridge name bridge id STP enabled interfaces
br0 8000.000c29328b73 no eth0
docker0 8000.02428b50e31e no
[root@web02_server~]#
# 安装pipework开源网络配置工具
[root@web02_server~]# git clone https://github.com/jpetazzo/pipework
Cloning into 'pipework'...
remote: Enumerating objects: 518, done.
remote: Counting objects: 100% (8/8), done.
remote: Compressing objects: 100% (6/6), done.
remote: Total 518 (delta 2), reused 5 (delta 2), pack-reused 510
Receiving objects: 100% (518/518), 182.72 KiB | 0 bytes/s, done.
Resolving deltas: 100% (272/272), done.
[root@web02_server~]# cp pipework/pipework /usr/local/bin/
# 指定一个网段,容器划分的网段,修改网段 /etc/sysconfig/docker
[root@web02_server~]# vim /etc/sysconfig/docker
OPTIONS=' --selinux-enabled -b=br0 --fixed-cidr=10.0.0.0/24 --log-driv
er=journald --signature-verification=false'
[root@web02_server~]# systemctl restart docker
-b=br0 ---docker 引擎使用br0 桥接
--fixed-cidr=10.0.0.0/24 指定某一个网段
# 创建容器时指定--net=none不采用默认桥接方式自动产生ip,使用pipeword工具设置ip地址
[root@web02_server~]# docker run -itd --net=none --name centos7-101 centos /bin/bash
165af60e0748a97c17371967e4193402f04df2f074877fc39ea1d4ca1f600b46
# 使用pipework命令为容器配置ip地址。指定桥接网卡、容器ID、ip地址、子网掩码、网关,pipeword工具功能强大,划分vlan等,划分 10.0.0.100/24 这个ip地址
[root@web02_server~]# pipework br0 centos7-101 10.0.0.100/24@10.0.0.2
[root@docker ~]# docker exec -it centos7-101 /bin/bash
[root@ade2fb172937 /]# yum -y install net-tools
[root@2c1bb62ea9a9 /]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
68: eth1@if69: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default qlen 1000
link/ether b2:00:a3:2e:65:4c brd ff:ff:ff:ff:ff:ff link-netnsid 0
inet 10.0.0.100/24 brd 10.0.0.255 scope global eth1
valid_lft forever preferred_lft forever
[root@2c1bb62ea9a9 /]#
[root@2c1bb62ea9a9 /]# ping -c 2 www.baidu.com
PING www.a.shifen.com (36.152.44.96) 56(84) bytes of data.
64 bytes from 36.152.44.96 (36.152.44.96): icmp\_seq=1 ttl=128 time=16.6 ms
64 bytes from 36.152.44.96 (36.152.44.96): icmp\_seq=2 ttl=128 time=16.1 ms
--- www.a.shifen.com ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1003ms
rtt min/avg/max/mdev = 16.094/16.343/16.592/0.249 ms
[root@2c1bb62ea9a9 /]#
四、Dockerfile构建镜像
4.1 Dockerfile 概述
- 概述
**概述:**除了手动生成docker镜像之外,还可以通过dockerfile自动生成镜像,dockerfile是由一组指定组成的文件,其中每条指令对应linux中的一条命令,docker程序将读取dockerfile中的指令生成指定镜像;
Docker通过Dockerfile自动构建镜像,Dockerfile是一个包含用于组建镜像的文本文件,由一条一条
的指令组成。逐行执行结构:
1.基础镜像信息;
2.维护者信息;
3.镜像操作指令;
4.容器启动时执行指令;
**规则:**dockerfile每行支持一条指令,每条指令可携带多个参数,支持使用以#号开头的注释;
4.2 Docker 常用指令
- 常用指令
| 命令 | 描述 |
|---|---|
| FROM | 构建新镜像是基于哪个镜像 |
| LABEL | 标签 |
| RUN | 构建镜像时运行的Shell命令 |
| COPY | 拷贝文件或目录到镜像中 |
| ADD | 解压压缩包并拷贝 |
| ENV | 设置环境变量 |
| USER | 为RUN、CMD和ENTRYPOINT执行命令指定运行用户 |
| EXPOSE | 声明容器运行的服务端口 |
| WORKDIR | 为RUN、CMD、ENTRYPOINT、COPY和ADD设置工作目录 |
| CMD | 运行容器时默认执行,如果有多个CMD指令,最后一个生效 |
4.3 构建镜像命令
-
构建镜像常用命令
Usage: docker build [OPTIONS] PATH | URL | - [flags]
Options:
-t, --tag list # 镜像名称
-f, --file string # 指定Dockerfile文件位置
# docker build -t shykes/myapp .
# docker build -t shykes/myapp -f /path/Dockerfile /path
# docker build -t shykes/myapp http://www.example.com/Dockerfile
-
简单的构建镜像案例
[root@web02\_server~/dockerfile]# cat dockerfile
FROM centos:7
RUN yum install epel-release -y && \
yum install nginx -y
CMD ["nginx","-g","daemon off;"]
[root@web02\_server~/dockerfile]# docker build -t nginx:v1 .
Sending build context to Docker daemon 2.048kB
Step 1/3 : FROM centos:7
7: Pulling from library/centos
2d473b07cdd5: Pull complete
Digest: sha256:9d4bcbbb213dfd745b58be38b13b996ebb5ac315fe75711bd618426a630e0987
Status: Downloaded newer image for centos:7
---> eeb6ee3f44bd
Step 2/3 : RUN yum install epel-release -y && yum install nginx -y
---> Running in 1ffcbdc36107
…………(省略)
Dependency Updated:
openssl-libs.x86_64 1:1.0.2k-21.el7_9
Complete!
Removing intermediate container 1ffcbdc36107
---> 32e58021c0b5
Step 3/3 : CMD ["nginx","-g","daemon off;"]
---> Running in 1de98bc362df
Removing intermediate container 1de98bc362df
---> 5662cbdf65e0
Successfully built 5662cbdf65e0
Successfully tagged nginx:v1
# 查看本地构建的镜像(tag 标签为1是自己构建的)
[root@web02\_server~/dockerfile]# docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx v1 5662cbdf65e0 3 minutes ago 415MB
#启用一个本地构建的nginx v1版本镜像(这里借用数据持久化环境,创建了/opt/wwwroot/)
[root@web02\_server~]# docker run -itd --name web01 -p 90:80 -v /opt/wwwroot/:/usr/share/nginx/html nginx:v1
24528b991b7c26317ae9070f4a6395b68b3c2bb329f9598ac4307bbd773a6192
[root@web02\_server~]# docker ps -l
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
24528b991b7c nginx:v1 "nginx -g 'daemon of…" 21 seconds ago Up 20 seconds 0.0.0.0:90->80/tcp, :::90->80/tcp web01
[root@web02\_server~]# docker inspect web01|grep -i ipaddr
"SecondaryIPAddresses": null,
"IPAddress": "172.17.0.2",
"IPAddress": "172.17.0.2",
[root@web02\_server~]#
[root@web02\_server~]# curl http://172.17.0.2:80
<h1>hello</h1>
[root@web02\_server~]#
4.4 构建nginx 镜像— 编译安装nginx
-
构建nginx 镜像
# 准备一个压缩包,里面放nginx
[root@web02_server~/dockerfile/nginx]# ls
Dockerfile nginx-1.15.5.tar.gz nginx.conf php.conf
# 构建nginx dockerfile
[root@web02_server~/dockerfile/nginx]# cat Dockerfile
FROM centos:7
LABEL maintainer www.ctnrs.com
RUN yum install -y gcc gcc-c++ make \
openssl-devel pcre-devel gd-devel \
iproute net-tools telnet wget curl && \
yum clean all && \
rm -rf /var/cache/yum/*
ADD nginx-1.15.5.tar.gz /
RUN cd nginx-1.15.5 && \
./configure --prefix=/usr/local/nginx \
--with-http_ssl_module \
--with-http_stub_status_module && \
make -j 4 && make install && \
mkdir /usr/local/nginx/conf/vhost && \
cd / && rm -rf nginx* && \
ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
ENV PATH $PATH:/usr/local/nginx/sbin
COPY nginx.conf /usr/local/nginx/conf/nginx.conf
WORKDIR /usr/local/nginx
EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]
# 构建一个 nginx v2版本的----编译安装
[root@web02_server~/dockerfile/nginx]# docker build -t nginx:v2 .
Sending build context to Docker daemon 1.031MB
Step 1/10 : FROM centos:7
…… (省略)
……
---> 6cc9110c56c4
Step 10/10 : CMD ["nginx", "-g", "daemon off;"]
---> Running in a3ab71b03867
Removing intermediate container a3ab71b03867
---> e9548d7e4691
Successfully built e9548d7e4691
Successfully tagged nginx:v2
# 启动一个 nginx 经常,名字 web02
[root@web02_server~]# docker run -itd --name web02 -p 99:80 -v /opt/wwwroot/:/usr/share/nginx/html nginx:v2
456c3451ade36178a5c54e1ff4a318305d4cce2c8a7dec2ffa29086de4187d5e
[root@web02_server~]# docker ps -l
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
456c3451ade3 nginx:v2 "nginx -g 'daemon of…" 12 seconds ago Up 11 seconds 0.0.0.0:99->80/tcp, :::99->80/tcp web02
[root@web02_server~]# docker inspect web02 |grep -i ipaddr
"SecondaryIPAddresses": null,
"IPAddress": "172.17.0.3",
"IPAddress": "172.17.0.3",
# 测试访问,发现访问的是nginx 默认页面,为啥呢,因为我们编译安装的网站目录不是 /usr/share/nginx/html,而是自定义的 安装目录:
[root@web02_server~]# curl http://172.17.0.3
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
body {
width: 35em;
margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif;
}
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
[root@web02_server~]#
#在运行一个nginx 容器 web03 。
[root@web02_server~]# docker run -itd --name web03 -p 100:80 -v /opt/wwwroot/:/usr/local/nginx/html nginx:v2
d0955979f38020832f7ddd55588b2e1402b78ede45e7a6602cb18c8148c26995
[root@web02_server~]#
# 这里访问的才是我们之前 数据持久化写的 网页
[root@web02_server~]# curl http://172.17.0.4
<h1>hello</h1>
[root@web02_server~]#
4.5 Harbor镜像仓库搭建与使用
4.5.1 什么是Harbor
- Harbor是由VMWare公司开源的容器镜像仓库。
- 事实上,Harbor是在Docker Registry上进行了相应的企业级扩展,从而获得了更加广泛的应用,这些新的企业级特性包括:
管理用户界面,基于角色的访问控制,AD/LDAP集成以及审计日志等,足以满足基本企业需求。 - Harbor 官网:
https://goharbor.io/ - Harbor 官方文档:
https://github.com/goharbor/harbor
4.5.2 部署Harbor 环境说明
- 宿主机环境
| 主机名 | IP地址 | 系统内核 |
|---|---|---|
| HB-XYJF-nginx-node1 | 10.0.0.91 | CentOS Linux release 7.6.1810 |
| docker01 | 10.0.0.8 | CentOS Linux release 7.6.1810 |
-
-
实验部署先决条件
# 服务器硬件配置:
• 最低要求:CPU2核/内存4G/硬盘40GB
• 推荐:CPU4核/内存8G/硬盘160GB
# 软件:
• Docker CE 17.06版本+
• Docker Compose 1.18版本+
# Harbor安装有2种方式:
• 在线安装:从Docker Hub下载Harbor相关镜像,因此安装软件包非常小
• 离线安装:安装包包含部署的相关镜像,因此安装包比较大
本次测试,采用离线部署方式
4.5.3 部署Harbor 私有仓库步骤
4.5.3.1 下载安装 Docker 和Docker Compose
-
下载安装Docker
# 官方安装教程
[root@docker-server~]# yum install -y yum-utils
[root@docker-server~]# yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
# 安装docker-ce
yum install -y docker-ce
# 配置加速器
参照前面加速器配置
-
下载安装Docker Compose
# 下载地址:https://github.com/docker/compose/releases
# 上传 compose 和 harbor
[root@docker01~]# ls
anaconda-ks.cfg harbor-offline-installer-v2.0.0.tgz
docker-compose-Linux-x86_64 nohup.out
pipework szz-job-v1.jar
# 下载完成后,通过工具上传到服务器,这步忽略。
。
#对docker-compose文件增加执行权限,移动到sbin目录下面
[root@docker01~]# chmod o+x docker-compose-Linux-x86\_64
[root@docker01~]# mv docker-compose-Linux-x86\_64 /usr/bin/docker-compose