docker镜像、Dockerfile
- 一、docker镜像
- 二、Dockerfile定制镜像
-
- 1、Dockerfile使用流程
-
- [1.1 编写Dockerfile](#1.1 编写Dockerfile)
- 1.2、构建镜像
- [1.3 创建容器测试镜像定制操作](#1.3 创建容器测试镜像定制操作)
- 2、Dockerfile常用指令
- 三、部署springcloud微服务架构的商品秒杀项目
-
- 1、部署项目需要的基础服务
-
- [1.1 部署redis缓存](#1.1 部署redis缓存)
- [1.2 部署MySQL数据库](#1.2 部署MySQL数据库)
- [1.3 部署rabbitmq消息队列](#1.3 部署rabbitmq消息队列)
- 2、部署微服务框架
-
- [2.1 部署erueka 注册中心](#2.1 部署erueka 注册中心)
- [2.2 部署config-server 配置中心](#2.2 部署config-server 配置中心)
- [2.3 部署zuul-server 网关](#2.3 部署zuul-server 网关)
- 3、部署业务
-
- [3.1 部署会员服务](#3.1 部署会员服务)
- [3.2 部署商品展示业务](#3.2 部署商品展示业务)
- [3.3 部署秒杀业务](#3.3 部署秒杀业务)
- [3.4 部署web前端](#3.4 部署web前端)
- [3.4 部署websocket, 实现http长连接](#3.4 部署websocket, 实现http长连接)
一、docker镜像
1、镜像介绍
-
分层的文件系统
优势:节省空间、速度快、重用
-
只读
创建容器时,会在镜像上面添加一个可写层
2、镜像核心技术
- COW copy on write 写时复制
- Union fs 联合文件系统
overlay2
device mapper
bash
[root@martin-host ~]# docker info
Client: Docker Engine - Community
Version: 26.1.4
Context: default
Debug Mode: false
Plugins:
buildx: Docker Buildx (Docker Inc.)
Version: v0.14.1
Path: /usr/libexec/docker/cli-plugins/docker-buildx
compose: Docker Compose (Docker Inc.)
Version: v2.27.1
Path: /usr/libexec/docker/cli-plugins/docker-compose
Server:
Containers: 1
Running: 0
Paused: 0
Stopped: 1
Images: 4
Server Version: 26.1.4
Storage Driver: overlay2
二、Dockerfile定制镜像
1、Dockerfile使用流程
1.1 编写Dockerfile
bash
[root@martin-host testDockerfile]# ls
CentOS-Base.repo Dockerfile
[root@martin-host testDockerfile]# cat Dockerfile
FROM centos:7
COPY CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo
RUN yum install -y net-tools
1.2、构建镜像
bash
[root@martin-host testDockerfile]# docker build -t centos:v1 ./
1.3 创建容器测试镜像定制操作
bash
[root@martin-host testDockerfile]# docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
centos v1 3e88e2fdb6df 56 seconds ago 461MB
redis latest 7614ae9453d1 2 years ago 113MB
centos 7 eeb6ee3f44bd 2 years ago 204MB
nginx 1.18 c2c45d506085 3 years ago 133MB
[root@martin-host testDockerfile]# docker run -tid --name=test1 centos:v1
949b757d2b35b4d115a88f69cc8f1d704b6e6fcd34a64d4ecbfc28422e7530aa
[root@martin-host testDockerfile]# docker exec -ti test1 bash
[root@949b757d2b35 /]# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1472
inet 10.88.7.2 netmask 255.255.255.0 broadcast 10.88.7.255
ether 02:42:0a:58:07:02 txqueuelen 0 (Ethernet)
RX packets 8 bytes 656 (656.0 B)
2、Dockerfile常用指令
- FROM
指定基础镜像;
镜像不存在,构建镜像时自动下载镜像
建议尽量选择小容量的镜像 / debian/ ubuntu
bash
FROM 镜像名称
- RUN
指定定制命令
bash
RUN 命令 && 命令 && 命令
- CMD
定义容器创建时,自动执行的命令
注意事项:
1、前台启动服务的指令
2、创建容器时,不要自己指定命令,会覆盖CMD
3、一个Dockerfile中只能有一条CMD指令
bash
CMD httpd -D FOREGROUND
CMD ["httpd", "-D", "FOREGROUND"] // 推荐
- ENTRYPOINT
定义容器创建时,自动执行的命令
不会被覆盖
bash
ENTRYPOINT ["httpd", "-D", "FOREGROUND"]
- COPY
复制文件
注意:只能复制本地文件
bash
COPY 源文件 目的文件
- ADD
复制文件
注意:
1、支持本地文件、URL、压缩包会自动解压
bash
ADD 源文件 目的文件
bash
ADD file01 /tmp/file01
ADD http://nginx.org/download/nginx-1.27.0.tar.gz /tmp
ADD jdk-8u91-linux-x64.tar.gz /tmp
- EXPOSE
说明容器服务端口
注意:
1、-P随机发布端口时,Dockerfile中必须有EXPOSE指令
bash
EXPOSE 端口 端口
- VOLUME
定义持久化存储的目录
创建容器时不使用-v明确指定目录,会自动生成匿名卷
bash
VOLUME 目录
- ENV
定义环境变量
bash
ENV 变量名称 值
- WORKDIR
定义当前目录
bash
WORKDIR 目录
- USER
指定容器运行的用户
bash
USER 用户名
三、部署springcloud微服务架构的商品秒杀项目
1、部署项目需要的基础服务
1.1 部署redis缓存
bash
[root@martin-host web]# docker run -tid --name=redis --hostname=redis --restart=always --net=host redis:3.2.10
33ff624e19c309793fdfa23dbd5c0a88490637cd5e0090bfee7dc98c63721732
[root@martin-host web]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
33ff624e19c3 redis:3.2.10 "docker-entrypoint.s..." 5 seconds ago Up 4 seconds redis
[root@martin-host web]# netstat -tunlp | grep 6379
tcp 0 0 0.0.0.0:6379 0.0.0.0:* LISTEN 2570/redis-server *
tcp6 0 0 :::6379 :::* LISTEN 2570/redis-server *
1.2 部署MySQL数据库
bash
[root@martin-host web]# docker run -tid --name=web_db --hostname=web_db --restart=always --net=host -e MYSQL_ROOT_PASSWORD=admin mysql:5.7
4ba7f19a4e15ef11facd18e329796dc391c0d0ce1a144fe7f67380fad65ab043
[root@martin-host web]#
[root@martin-host web]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
4ba7f19a4e15 mysql:5.7 "docker-entrypoint.s..." 4 seconds ago Up 3 seconds web_db
33ff624e19c3 redis:3.2.10 "docker-entrypoint.s..." About a minute ago Up About a minute redis
[root@martin-host web]# docker cp shop_goods.sql web_db:/
Successfully copied 3.58kB to web_db:/
[root@martin-host web]# docker cp shop_member.sql web_db:/
Successfully copied 3.58kB to web_db:/
[root@martin-host web]# docker cp shop_seckill.sql web_db:/
Successfully copied 7.68kB to web_db:/
[root@martin-host web]#
[root@martin-host web]# docker exec -ti web_db bash
root@web_db:/#
root@web_db:/# mysql -uroot -padmin < /shop_goods.sql
mysql: [Warning] Using a password on the command line interface can be insecure.
root@web_db:/# mysql -uroot -padmin < /shop_member.sql
mysql: [Warning] Using a password on the command line interface can be insecure.
root@web_db:/# mysql -uroot -padmin < /shop_seckill.sql
mysql: [Warning] Using a password on the command line interface can be insecure.
root@web_db:/#
root@web_db:/# mysql -uroot -padmin
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 5.7.35 MySQL Community Server (GPL)
Copyright (c) 2000, 2021, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| shop_goods |
| shop_member |
| shop_seckill |
| sys |
+--------------------+
7 rows in set (0.00 sec)
mysql>
mysql> grant all on *.* to 'root'@'%' identified by 'admin';
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
mysql> exit
Bye
root@web_db:/# exit
exit
1.3 部署rabbitmq消息队列
bash
[root@martin-host web]# docker run -tid --name=rabbitmq --hostname=rabbitmq --net=host rabbitmq:3-management
d9e860cf2773cbf4c6462483fb5dabea600b00295f44f55d088f5797de0c8efa
[root@martin-host web]#
[root@martin-host web]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
d9e860cf2773 rabbitmq:3-management "docker-entrypoint.s..." 3 seconds ago Up 2 seconds rabbitmq
4ba7f19a4e15 mysql:5.7 "docker-entrypoint.s..." 4 minutes ago Up 4 minutes web_db
33ff624e19c3 redis:3.2.10 "docker-entrypoint.s..." 6 minutes ago Up 6 minutes redis
[root@martin-host ~]# docker exec -ti rabbitmq bash
root@rabbitmq:/# rabbitmqctl add_user liushao admin
Adding user "liushao" ...
Done. Don't forget to grant the user permissions to some virtual hosts! See 'rabbitmqctl help set_permissions' to learn more.
root@rabbitmq:/# rabbitmqctl set_permissions liushao ".*" ".*" ".*"
Setting permissions for user "liushao" in vhost "/" ...
root@rabbitmq:/#
2、部署微服务框架
2.1 部署erueka 注册中心
负责注册所有服务的通信地址
bash
[root@martin-host web]# cat eurekaDockerfile
FROM adoptopenjdk/openjdk8:latest
COPY eureka-server-1.0-SNAPSHOT.jar /eureka-server.jar
CMD ["java", "-jar", "/eureka-server.jar"]
[root@martin-host web]#
[root@martin-host web]# docker build -t eureka-server:1.0 -f eurekaDockerfile ./
root@martin-host web]# docker run -tid --name=eureka-server --hostname=eureka-server --restart=always --net=host eureka-server:1.0
c682585f005c0be0ffae1dba1232410f92045c0763de5393d1efaedd79a0bdb3
[root@martin-host web]# docker logs eureka-server
2024-07-19 02:40:53.881 INFO 1 --- [ main] .s.c.n.e.s.EurekaAutoServiceRegistration : Updating port to 8761
2.2 部署config-server 配置中心
负责集中管理所有业务组件的配置文件、从远程的git服务器拉取配置文件
bash
[root@martin-host web]# cat configDockerfile
FROM adoptopenjdk/openjdk8:latest
COPY config-server-1.0-SNAPSHOT.jar /config-server.jar
CMD ["java", "-jar", "/config-server.jar"]
[root@martin-host web]#
[root@martin-host web]# docker build -t config-server:1.0 -f configDockerfile ./
[root@martin-host web]# docker run -tid --name=config-server --hostname=config-server --net=host --restart=always config-server:1.0
[root@martin-host web]# docker logs config-server
2024-07-19 02:49:37.965 INFO 1 --- [ main] .s.c.n.e.s.EurekaAutoServiceRegistration : Updating port to 9100
2.3 部署zuul-server 网关
负责集中接收处理请求、反向代理 、负载均衡
bash
[root@martin-host web]# cat zuulDockerfile
FROM adoptopenjdk/openjdk8:latest
COPY zuul-server-1.0-SNAPSHOT.jar /zuul-server.jar
CMD ["java", "-jar", "/zuul-server.jar"]
[root@martin-host web]#
[root@martin-host web]# docker build -t zuul-server:1.0 -f zuulDockerfile ./
[root@martin-host web]# docker run -tid --name=zuul-server --hostname=zuul-server --net=host --restart=always zuul-server:1.0
[root@martin-host web]# docker logs zuul-server
2024-07-19 02:56:57.602 INFO 1 --- [ main] .s.c.n.e.s.EurekaAutoServiceRegistration : Updating port to 9000
3、部署业务
3.1 部署会员服务
bash
[root@martin-host web]# cat memberDockerfile
FROM adoptopenjdk/openjdk8:latest
COPY member-server-1.0-SNAPSHOT.jar /member-server.jar
CMD ["java", "-jar", "/member-server.jar"]
[root@martin-host web]# docker build -t member-server:1.0 -f memberDockerfile ./
[root@martin-host web]# docker run -tid --name=member-server --hostname=localhost --net=host --restart=always member-server:1.0
2024-07-19 03:26:42.696 INFO 1 --- [ main] .s.c.n.e.s.EurekaAutoServiceRegistration : Updating port to 8083
3.2 部署商品展示业务
bash
[root@martin-host web]# cat goodDockerfile
FROM adoptopenjdk/openjdk8:latest
COPY good-server-1.0-SNAPSHOT.jar /good-server.jar
CMD ["java", "-jar", "/good-server.jar"]
[root@martin-host web]#
[root@martin-host web]# docker build -t good-server:1.0 -f goodDockerfile ./
[root@martin-host web]# docker run -tid --name=good-server --hostname=localhost --net=host --restart=always good-server:1.0
2024-07-19 03:29:49.555 INFO 1 --- [ main] .s.c.n.e.s.EurekaAutoServiceRegistration : Updating port to 8084
3.3 部署秒杀业务
bash
[root@martin-host web]# cat seckillDockerfile
FROM adoptopenjdk/openjdk8:latest
COPY seckill-server-1.0-SNAPSHOT.jar /seckill-server.jar
CMD ["java", "-jar", "/seckill-server.jar"]
[root@martin-host web]#
[root@martin-host web]# docker build -t seckill-server:1.0 -f seckillDockerfile ./
[root@martin-host web]# docker run -tid --name=seckill-server --hostname=localhost --net=host --restart=always seckill-server:1.0
2024-07-19 03:32:56.665 INFO 1 --- [ main] o.s.a.r.c.CachingConnectionFactory : Created new connection: rabbitConnectionFactory#1c6804cd:0/SimpleConnection@4fdfa676 [delegate=amqp://guest@192.168.183.10:5672/, localPort= 34446]
2024-07-19 03:32:56.851 INFO 1 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8085 (http) with context path ''
2024-07-19 03:32:56.853 INFO 1 --- [ main] .s.c.n.e.s.EurekaAutoServiceRegistration : Updating port to 8085
3.4 部署web前端
bash
[root@martin-host web]# cat frontendDockerfile
FROM adoptopenjdk/openjdk8:latest
COPY frontend-server-0.0.1-SNAPSHOT.jar /frontend-server.jar
CMD ["java", "-jar", "/frontend-server.jar"]
[root@martin-host web]#
[root@martin-host web]# docker build -t frontend-server:1.0 -f frontendDockerfile ./
[root@martin-host web]# docker run -tid --name=frontend-server --hostname=localhost --net=host --restart=always frontend-server:1.0
2024-07-19 03:36:03.029 INFO 1 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
3.4 部署websocket, 实现http长连接
bash
[root@martin-host web]# cat websocketDockerfile
FROM adoptopenjdk/openjdk8:latest
COPY websocket-server-0.0.1-SNAPSHOT.jar /websocket-server.jar
CMD ["java", "-jar", "/websocket-server.jar"]
[root@martin-host web]#
[root@martin-host web]# docker build -t websocket-server:1.0 -f websocketDockerfile ./
[+] Building 0.5s (7/7) FINISHED docker:default
=> [internal] load build definition from websocketDockerfile 0.0s
=> => transferring dockerfile: 193B 0.0s
=> [internal] load metadata for docker.io/adoptopenjdk/openjdk8:latest 0.0s
=> [internal] load .dockerignore 0.0s
=> => transferring context: 2B 0.0s
=> [internal] load build context 0.2s
=> => transferring context: 21.84MB 0.2s
=> CACHED [1/2] FROM docker.io/adoptopenjdk/openjdk8:latest 0.0s
=> [2/2] COPY websocket-server-0.0.1-SNAPSHOT.jar /websocket-server.jar 0.2s
=> exporting to image 0.1s
=> => exporting layers 0.1s
=> => writing image sha256:7d681a377f627c30765b92088008d4ac265f182be9ee93efa118400e5667482c 0.0s
=> => naming to docker.io/library/websocket-server:1.0 0.0s
[root@martin-host web]#
[root@martin-host web]# docker run -tid --name=websocket-server --hostname=localhost --net=host --restart=always websocket-server:1.0
9fb88729fe38c44841a2691562deb47b428013150fcdc5974d8c06f43630e679
2024-07-19 03:39:14.976 INFO 1 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8088 (http) with context path ''