Dockerfile样例

时间:2024-10-19 09:00:36

一、基础jar镜像制作

## Dockerfile
FROM registry.openanolis.cn/openanolis/anolisos:8.9
RUN mkdir /work
ADD jdk17.tar.gz fonts.tar.gz /work/
RUN yum install fontconfig ttmkfdir -y && yum clean all && \
    chmod -R 755 /work/fonts
ADD fonts.conf /etc/fonts/fonts.conf
ADD Shanghai /etc/localtime
ENV JAVA_VERSION="17" \
    JAVA_HOME="/work/jdk17" \
    PATH="${PATH}:/work/jdk17/bin" \
    LANG=zh_CN.UTF-8
EXPOSE 8080

或者:

## Dockerfile
FROM registry.openanolis.cn/openanolis/anolisos:8.9
RUN mkdir /work
ADD jdk17.tar.gz /work/
RUN yum install fontconfig ttmkfdir -y && yum clean all
ADD Shanghai /etc/localtime
ENV JAVA_VERSION="17" \
    JAVA_HOME="/work/jdk17" \
    PATH="${PATH}:/work/jdk17/bin" \
    LANG=zh_CN.UTF-8
EXPOSE 8080


# 构建基础环境

# docker build -t 192.168.2.51/crm/base:v1 .

说明:
    此基础镜像修改了时区为上海,添加了字体、jdk17,使用8080作为应用默认端口

二、用上面的基础镜像制作jar包应用镜像

## # cat Dockerfile 
FROM 192.168.2.51/crm/base:v1
COPY platform-crm-api.jar /work/app.jar
CMD  ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/work/app.jar"]


## 构建
# docker build -t 192.168.2.51/crm/crm-api:v8 .

## 上传
# docker push 192.168.2.51/crm/crm-api:v8

说明:
    此应用的配置文件是通过调用系统变量获取到如数据及中间件的连接地址及账户的。所以这里不用使用-D传输变量。
    在后期通过k8s进行传输变量到镜像即可。

应用变量使用截图:

k8s传输变量截图:

docker 运行传入变量一:

# docker run --env-file env-file.txt 192.168.2.51/crm/crm-api:v8

# docker run --env-file env-file.txt 192.168.2.51/crm/crm-api:v8  env  # 查看传入变量

docker 运行传入变量二:

# docker run -d -e database_password=Dev_tmp_0507 -e database_url=192.168.2.154:3306/hs_crm_sys -e database_username=kyc_cle -e redis_host=192.168.2.193 -e redis_port=6379 192.168.2.51/crm/crm-api:v8

三、vue应用镜像制作

## 应用镜像
# cat Dockerfile 
FROM registry.openanolis.cn/openanolis/nginx:1.14.1-8.6
ADD dist.tar /usr/share/nginx/html

## configmap

# cat configmap.yaml 
apiVersion: v1
kind: ConfigMap
metadata:
  name: environment
  namespace: crm
data:
  environment.js: |
    window.webConfig={
      "VITE_DOMAIN_URL_VUE_BOOT":"http://192.168.2.34:31201"
    }


## yaml文件

# cat deploy.yaml 
apiVersion: apps/v1
kind: Deployment
metadata:
  name: crm-web
  namespace: crm
spec:
  replicas: 2
  selector:
    matchLabels:
      app: crm-web
  template:
    metadata:
      labels:
        app: crm-web
    spec:
      containers:
      - name: crm-web
        image: 192.168.2.51/crm/platform-crm-web:v4
        ports:
        - containerPort: 80
        envFrom:
        volumeMounts:
        - name: crm-web
          mountPath: /usr/share/nginx/html/environment.js
          subPath: environment.js
        - name: tz
          mountPath: /etc/localtime
        livenessProbe:
          tcpSocket:
            port: 80
          initialDelaySeconds: 40
          periodSeconds: 10
        readinessProbe:
          tcpSocket:
            port: 80
          initialDelaySeconds: 40
          periodSeconds: 10
      volumes:
      - name: crm-web
        configMap:
          name: environment
          items:
          - key: "environment.js"
            path: "environment.js"
      - name: tz
        hostPath:
          path: /etc/localtime
      dnsPolicy: "None"
      dnsConfig:
        nameservers:
          - 10.96.0.10
        searches:
          - svc.cluster.local
          - cluster.local
---
apiVersion: v1
kind: Service
metadata:
  name: crm-web
  namespace: crm
spec:
  type: NodePort
  selector:
    app: crm-web
  ports:
  - protocol: TCP
    port: 80
    targetPort: 80
    nodePort: 31641

configmap在deploymen中挂载截图

四、未测试的方法

# 基础镜像
FROM ubuntu:latest
 
# 设置工作目录
WORKDIR /app
 
# 创建目录
RUN mkdir -p /dir1 /dir2
 
# 复制多个文件到多个目录
RUN cp /app/file1.txt /dir1/ && \
    cp /app/file2.txt /dir2/ && \
    cp /app/file3.txt /dir1/ && \
    cp /app/file4.txt /dir2/