docker 构建自定义镜像
Last updated on November 20, 2024 am
🧙 Questions
使用docker构建,代码编译镜像和代码运行镜像
☄️ Ideas
构建nginx镜像
vim Dockerfile
基于最轻量的镜像alpine构建,可以通过apk的方式安装工具
FROM alpine:latest
# 使用阿里云软件源
RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories
RUN apk update
RUN apk add --no-cache nginx
RUN apk add --no-cache openjdk8
RUN apk add --no-cache bash
EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]
构建镜像
docker build -t isxcode/nginx-and-jdk:0.0.1 -f ./Dockerfile .
启动镜像
docker run --restart=always \
--name demo1 \
-p 8889:80 \
-d isxcode/nginx-and-jdk:0.0.1
构建源码打包镜像
一般推荐使用centos8为基础镜像构建
可以先启动一个centos8镜像,尝试构建好镜像
docker run --privileged --name centos-base -v /Users/ispong/Downloads/spark-yun:/spark-yun -it centos:8
容器可选执行命令
# 更新centos8
yum upgrade -y
# 安装epel软件源,阿里的软件源有问题
yum install -y epel-release
yum clean all && yum makecache
编写dockerfile文件
vim Dockerfile
FROM centos:8
VOLUME /spark-yun
VOLUME /root/.gradle
VOLUME /root/.pnpm-store
# 修复centos8的镜像链接问题
RUN sed -i 's/mirrorlist/#mirrorlist/g' /etc/yum.repos.d/CentOS-*
RUN sed -i 's|#baseurl=http://mirror.centos.org|baseurl=http://vault.centos.org|g' /etc/yum.repos.d/CentOS-*
# 使用fnm安装node18版本
RUN yum install -y unzip
RUN curl -fsSL https://fnm.vercel.app/install | bash
ENV PATH="/root/.local/share/fnm:$PATH"
RUN echo "`fnm env`" >> /etc/profile
RUN echo "$(fnm env --use-on-cd)" >> /etc/profile
RUN fnm install v18.14.2
RUN source /etc/profile && fnm use v18.14.2 && npm install pnpm -g
RUN source /etc/profile && pnpm config set store-dir /root/.pnpm-store
RUN yum install python3 make gcc-c++ -y
# 安装openjdk8
RUN yum install java-1.8.0-openjdk-devel java-1.8.0-openjdk -y
# 安装gradle7.6.1
RUN yum install wget -y
RUN wget https://services.gradle.org/distributions/gradle-7.6.1-bin.zip -O /opt/gradle-7.6.1-bin.zip
RUN unzip -d /opt/ /opt/gradle-7.6.1-bin.zip
ENV JAVA_HOME="/usr/lib/jvm/java-1.8.0-openjdk"
ENV GRADLE_HOME="/opt/gradle-7.6.1"
ENV GRADLE_USER_HOME="/root/.gradle"
ENV PATH="/root/.local/share/fnm:$PATH:$GRADLE_HOME/bin"
# 提前下载项目依赖
RUN wget https://github.com/isxcode/spark-yun/raw/main/install.sh -O install.sh
RUN chmod a+x install.sh
RUN source /etc/profile && bash install.sh
# 构建源码打包脚本
RUN echo "#!/bin/bash" >> build.sh
RUN echo "source /etc/profile" >> build.sh
RUN echo "cp -r /resources /spark-yun/" >> build.sh
RUN echo "cd /spark-yun && gradle install && gradle package --info" >> build.sh
RUN chmod a+x build.sh
# 执行构建脚本
CMD bash build.sh
镜像打包
docker buildx uninstall
docker build -t isxcode/zhiqingyun-build:latest -f ./Dockerfile .
# 推送到阿里云
docker login --username=ispong registry.cn-shanghai.aliyuncs.com
docker tag 8a608bbb76d8b75bc5c799b7611f4263171f0648f7023da83e07cd5fa7645cc5 registry.cn-shanghai.aliyuncs.com/isxcode/zhiqingyun-build:latest
docker push registry.cn-shanghai.aliyuncs.com/isxcode/zhiqingyun-build:latest
下载代码,镜像编译源码
# /root/.pnpm-store 前端依赖缓存目录
# /root/.gradle 后端依赖缓存目录
# /spark-yun 项目代码目录
git clone https://github.com/isxcode/spark-yun.git
docker run \
--rm \
-v /Users/ispong/Downloads/spark-yun:/spark-yun \
-v /Users/ispong/.pnpm-store:/root/.pnpm-store \
-v /Users/ispong/.gradle:/root/.gradle \
-d registry.cn-shanghai.aliyuncs.com/isxcode/zhiqingyun-build
构建本地运行镜像
编写dockerfile脚本
vim Dockerfile
FROM centos:8
# 挂载源码路径
VOLUME /spark-yun
VOLUME /root/.gradle
VOLUME /root/.pnpm-store
# 设置运行参数
ARG ENV_TYPE
# 修复centos8的镜像链接问题
RUN sed -i 's/mirrorlist/#mirrorlist/g' /etc/yum.repos.d/CentOS-*
RUN sed -i 's|#baseurl=http://mirror.centos.org|baseurl=http://vault.centos.org|g' /etc/yum.repos.d/CentOS-*
# 安装pnpm
RUN yum install -y unzip
RUN curl -fsSL https://fnm.vercel.app/install | bash
ENV PATH="/root/.local/share/fnm:$PATH"
RUN echo "`fnm env`" >> /etc/profile
RUN echo "$(fnm env --use-on-cd)" >> /etc/profile
RUN fnm install v18.14.2
RUN source /etc/profile && fnm use v18.14.2
RUN source /etc/profile && npm install pnpm -g
RUN source /etc/profile && pnpm config set store-dir /root/.pnpm-store
RUN yum install python3 make gcc-c++ -y
# 安装openjdk8
RUN yum install java-1.8.0-openjdk-devel java-1.8.0-openjdk -y
# 安装gradle7.6.1
RUN yum install wget -y
RUN wget https://services.gradle.org/distributions/gradle-7.6.1-bin.zip -O /opt/gradle-7.6.1-bin.zip
RUN unzip -d /opt/ /opt/gradle-7.6.1-bin.zip
RUN echo "export JAVA_HOME=/usr/lib/jvm/java-1.8.0-openjdk" >> /etc/profile
RUN echo "export GRADLE_HOME=/opt/gradle-7.6.1" >> /etc/profile
RUN echo "export PATH=\$PATH:\$GRADLE_HOME/bin:\$JAVA_HOME/bin" >> /etc/profile
# 设置环境变量,给run.sh文件使用
ENV ENV_TYPE=${ENV_TYPE}
# 构建源码打包脚本
RUN echo "#!/bin/bash" >> run.sh
RUN echo "source /etc/profile" >> run.sh
RUN echo "cd /spark-yun" >> run.sh
# 启动官网 3000端口号
RUN echo "if [ \"\$ENV_TYPE\" = \"WEB\" ]; then" >> run.sh
RUN echo "gradle web" >> run.sh
RUN echo "fi" >> run.sh
# 启动前端 5173端口号
RUN echo "if [ \"\$ENV_TYPE\" = \"FRONTEND\" ]; then" >> run.sh
RUN echo "gradle frontend" >> run.sh
RUN echo "fi" >> run.sh
# 启动后端 8080端口号
RUN echo "if [ \"\$ENV_TYPE\" = \"BACKEND\" ]; then" >> run.sh
RUN echo "gradle backend" >> run.sh
RUN echo "fi" >> run.sh
# 启动所有 8080端口号
RUN echo "if [ \"\$ENV_TYPE\" = \"ALL\" ]; then" >> run.sh
RUN echo "gradle start" >> run.sh
RUN echo "fi" >> run.sh
RUN chmod a+x run.sh
# 执行构建脚本
CMD bash run.sh
构建镜像
docker buildx uninstall
docker build -t isxcode/zhiqingyun-local:latest -f ./Dockerfile .
# 推送到阿里云
docker login --username=ispong registry.cn-shanghai.aliyuncs.com
docker tag 64a48ae5f7b83b8195fc7327c623aa6d12cab6b0af43093708a8e2da433b356e registry.cn-shanghai.aliyuncs.com/isxcode/zhiqingyun-local:latest
docker push registry.cn-shanghai.aliyuncs.com/isxcode/zhiqingyun-local:latest
启动镜像,运行本地代码
# 产品启动项目
docker run \
-v /Users/ispong/Downloads/spark-yun:/spark-yun \
-e ENV_TYPE='WEB' \
-v /Users/ispong/.pnpm-store:/root/.pnpm-store \
-p 8899:3000 \
-d registry.cn-shanghai.aliyuncs.com/isxcode/zhiqingyun-local:latest
# 测试启动项目
docker run \
-v /Users/ispong/Downloads/spark-yun:/spark-yun \
-e ENV_TYPE='ALL' \
-p 8899:8080 \
-d registry.cn-shanghai.aliyuncs.com/isxcode/zhiqingyun-local:latest
# 前端启动项目
docker run \
-v /Users/ispong/Downloads/spark-yun:/spark-yun \
-v /Users/ispong/.gradle:/root/.gradle \
-e ENV_TYPE='BACKEND' \
-p 8899:8080 \
-d registry.cn-shanghai.aliyuncs.com/isxcode/zhiqingyun-local:latest
# 后端启动项目
docker run \
-v /Users/ispong/Downloads/spark-yun:/spark-yun \
-v /Users/ispong/.pnpm-store:/root/.pnpm-store \
-e ENV_TYPE='FRONTEND' \
-p 8899:5173 \
-d registry.cn-shanghai.aliyuncs.com/isxcode/zhiqingyun-local:latest
🔗 Links
docker 构建自定义镜像
https://ispong.isxcode.com/kubernetes/docker/docker 构建自定义镜像/