Loading

Docker搭建常用的开发环境

本地制作JDK17镜像:

制作jdk17镜像:
# 新建目录
mkdir -p /usr/local/dockerjdk17
cd /usr/local/dockerjdk17
# 下载jdk17包
wget https://download.oracle.com/java/17/latest/jdk-17_linux-x64_bin.tar.gz
# 解压后查看jdk版本
tar -zxvf jdk-17_linux-x64_bin.tar.gz

# 新建文件Dockerfile
vim Dockerfile
# Dockerfile文件内容
FROM centos:7
MAINTAINER emon
WORKDIR /usr/local/java
ADD jdk-17_linux-x64_bin.tar.gz  /usr/local/java/
ENV JAVA_HOME=/usr/local/java/jdk-17.0.8
ENV CLASSPATH=.:$JAVA_HOME/lib/jrt-fs.jar
ENV PATH=$PATH:$JAVA_HOME/bin

# 构建镜像
docker build -t='jdk17' .
# 启动容器
docker run -di --name jdk17 -d jdk17
# 进入容器
docker exec -it jdk17 /bin/bash
# 校验是否安装成功
java -version

部署mysql环境:

# 下载镜像
docker pull mysql:5.7

# 运行容器并进行目录挂载,并设置开机自启
docker run -di --restart=always --name mysql -v /home/dockerData/mysql/data:/var/lib/mysql -v /home/dockerData/mysql/conf:/etc/mysql/conf.d -v /home/dockerData/mysql/log:/var/log/mysql -p 3306:3306 -e TZ=Asia/Shanghai -e MYSQL_ROOT_PASSWORD=www.yyj.com mysql:5.7 --character-set-server=utf8mb4 --collation-server=utf8mb4_general_ci

部署nginx环境:

# 拉取镜像
docker pull nginx
# 运行一个测试容器
docker run --name nginx_test -d -p 80:80 nginx
# 从测试容器中同步配置文件等信息
docker cp nginx_test:/etc/nginx/conf.d /home/dockerData/nginx/conf.d
docker cp nginx_test:/etc/nginx/nginx.conf /home/dockerData/nginx/nginx.conf
docker cp nginx_test:/usr/share/nginx/html/ /home/dockerData/nginx/html/
docker cp nginx_test:/var/log/nginx/ /home/dockerData/nginx/log/
# 停用并删除测试容器
docker stop nginx_test
docker rm -f nginx_test
# 启动容器进行目录挂载并设置开机自启
docker run -di --restart=always --name nginx -p 80:80 -v /home/dockerData/nginx/log/:/var/log/nginx -v /home/dockerData/nginx/nginx.conf:/etc/nginx/nginx.conf -v /home/dockerData/nginx/conf.d:/etc/nginx/conf.d -v /home/dockerData/nginx/html/:/usr/share/nginx/html nginx

使用dockerfile制作微服务镜像文件并运行:

# gateway网关微服务
FROM jdk17
ADD /gateway-0.0.1-SNAPSHOT.jar //
ENTRYPOINT ["java", "-jar","/gateway-0.0.1-SNAPSHOT.jar"]

# member会员微服务
FROM jdk17
ADD /member-0.0.1-SNAPSHOT.jar //
ENTRYPOINT ["java", "-jar","/member-0.0.1-SNAPSHOT.jar"]

# business后台数据微服务
FROM jdk17
ADD /business-0.0.1-SNAPSHOT.jar //
ENTRYPOINT ["java", "-jar","/business-0.0.1-SNAPSHOT.jar"]

# 定时任务微服务
FROM jdk17
ADD /batch-0.0.1-SNAPSHOT.jar //
ENTRYPOINT ["java", "-jar","/batch-0.0.1-SNAPSHOT.jar"]

将打包好的jar包与上述dockerfile放同一文件夹下,并上传到待发布机器:

在各自的文件夹下构建微服务镜像,并运行容器:

# 构建train_gateway镜像
docker build -t='train_gateway' .
docker run -di --restart=always --name=train_gateway -p 8000:8000 train_gateway
# 重新打包生成时,停止容器删除镜像,重新构建
docker stop train_gateway
docker rm train_gateway
docker rmi train_gateway

# 构建train_member镜像
docker build -t='train_member' .
docker run -di --restart=always --name=train_member -p 8010:8010 train_member
# 重新打包生成时,停止容器删除镜像,重新构建
docker stop train_member
docker rm train_member
docker rmi train_member

# 构建train_business镜像
docker build -t='train_business' .
docker run -di --restart=always --name=train_business -p 8020:8020 train_business
# 重新打包生成时,停止容器删除镜像,重新构建
docker stop train_business
docker rm train_business
docker rmi train_business
docker exec -it train_business /bin/bash

# 构建train_batch镜像
docker build -t='train_batch' .
docker run -di --restart=always --name=train_batch -p 8030:8030 train_batch
# 重新打包生成时,停止容器删除镜像,重新构建
docker stop train_batch
docker rm train_batch
docker rmi train_batch

# 可以打印日志查看服务启动情况
docker logs train_gateway
docker logs train_member
docker logs train_business
docker logs train_batch

# 可以进入对应容器查看详细信息
docker exec -it [容器id/名称] /bin/bash

也可批量停止容器、删除镜像:

# 批量停止容器、删除容器、删除镜像
docker stop train_batch train_business train_member train_gateway
docker rm train_batch train_business train_member train_gateway
docker rmi train_batch train_business train_member train_gateway

 

posted @ 2023-08-12 23:30  1640808365  阅读(486)  评论(0编辑  收藏  举报