制作业务镜像
前端镜像
第一种:构建层镜像
# build environment
FROM node:12.13.0-alpine as build
LABEL version="1.0" description="前端服务" by="zisefeizhu"
ARG DEPLOY_ENV
ENV PATH /app/node_modules/.bin:$PATH
WORKDIR /app
COPY package.json /app
COPY yarn.lock /app/yarn.lock
RUN yarn
COPY . /app
RUN cross-env DEPLOY_ENV=${DEPLOY_ENV} webpack --config build/webpack.dist.pro.config.js && rm -rf node_modules
# production environment
FROM nginx:1.16-alpine
ARG API_SERVER_HOST
ARG EWS_SERVER_HOST
ARG NGINX_CONFIG
COPY --from=build /app/dist /usr/share/nginx/html
ADD ./configs/nginx/${NGINX_CONFIG} /etc/nginx/conf.d/default.conf
RUN sed -i -e "s#{{API_SERVER_HOST}}#$API_SERVER_HOST#g" -e "s#{{EWS_SERVER_HOST}}#$EWS_SERVER_HOST#g" /etc/nginx/conf.d/default.conf
CMD ["nginx", "-g", "daemon off;"]
# # build environment
# FROM node:12.13.0-alpine as build
# RUN npm install -g yarn --registry=https://registry.npm.taobao.org && \
# yarn config set registry https://registry.npm.taobao.org -g && \
# yarn config set sass_binary_site http://cdn.npm.taobao.org/dist/node-sass -g
# WORKDIR /app
# ARG CI_COMMIT_SHORT_SHA
# ARG PORT
# ARG DEPLOY_ENV
# ARG PUBLIC_PATH
# ENV PATH /app/node_modules/.bin:$PATH
# ADD package.json /app/package.json
# ADD yarn.lock /app/yarn.lock
# RUN yarn install
# ADD . /app
# RUN cross-env CI_COMMIT_SHORT_SHA=${CI_COMMIT_SHORT_SHA} DEPLOY_ENV=${DEPLOY_ENV} PUBLIC_PATH=${PUBLIC_PATH} next build
# CMD next start -H 0.0.0.0 -p 9201
# EXPOSE 9201
# Install dependencies only when needed
FROM node:alpine AS deps
# Check https://github.com/nodejs/docker-node/tree/b4117f9333da4138b03a546ec926ef50a31506c3#nodealpine to understand why libc6-compat might be needed.
RUN apk add --no-cache libc6-compat
WORKDIR /app
COPY package.json yarn.lock ./
RUN yarn install --frozen-lockfile
# Rebuild the source code only when needed
FROM node:alpine AS builder
WORKDIR /app
ENV NODE_ENV production
ENV PATH /app/node_modules/.bin:$PATH
ARG CI_COMMIT_SHORT_SHA
ARG DEPLOY_ENV
ARG PUBLIC_PATH
COPY . .
COPY --from=deps /app/node_modules ./node_modules
RUN cross-env CI_COMMIT_SHORT_SHA=${CI_COMMIT_SHORT_SHA} DEPLOY_ENV=${DEPLOY_ENV} PUBLIC_PATH=${PUBLIC_PATH} next build
# Production image, copy all the files and run next
FROM node:alpine AS runner
WORKDIR /app
ENV NODE_ENV production
ENV PATH /app/node_modules/.bin:$PATH
ARG PORT
ARG DEPLOY_ENV
ARG PUBLIC_PATH
ARG CI_COMMIT_SHORT_SHA
# You only need to copy next.config.js if you are NOT using the default configuration
COPY --from=builder /app/build ./build
COPY --from=builder /app/configs ./configs
COPY --from=builder /app/.env.production ./
COPY --from=builder /app/next.config.js ./
COPY --from=builder /app/next-less.config.js ./
COPY --from=builder /app/public ./public
COPY --from=builder /app/src ./src
COPY --from=builder /app/.next ./.next
COPY --from=builder /app/node_modules ./node_modules
RUN ls -la /app/
RUN addgroup -g 1001 -S nodejs
RUN adduser -S nextjs -u 1001
RUN chown -R nextjs:nodejs /app
USER nextjs
# Next.js collects completely anonymous telemetry data about general usage.
# Learn more here: https://nextjs.org/telemetry
# Uncomment the following line in case you want to disable telemetry.
# RUN npx next telemetry disable
CMD next start -H 0.0.0.0 -p 9201
EXPOSE 9201
第二种:非构建层镜像
FROM node:12.13.0-alpine
LABEL version="1.0" description="前端服务" by="zisefeizhu"
RUN npm install -g yarn --registry=https://registry.npm.taobao.org
ARG PUBLIC_PATH
ENV PATH /app/node_modules/.bin:$PATH
WORKDIR /app
ADD package.json /app
COPY yarn.lock /app/yarn.lock
RUN yarn
ADD . /app
RUN cross-env DEPLOY_ENV=${DEPLOY_ENV} PUBLIC_PATH=${PUBLIC_PATH} \
&& yarn build
CMD yarn start
EXPOSE ${PORT}
后端镜像
python
FROM python:3.7-slim-stretch
LABEL version="1.0" description="python后端服务" by="zisefeizhu"
COPY requirements.txt /project
RUN pip install --no-cache-dir -r ./requirements.txt -i https://mirrors.aliyun.com/pypi/simple
COPY . /project
ENTRYPOINT ["python", "manage.py", "runserver"]
#CMD ["uvicorn", "src.main:app", "--host", "0.0.0.0", "--port", "9999", "--workers", "5"]
go
FROM golang:1.14.3-alpine as build
LABEL version="1.0" description="go后端服务" by="zisefeizhu"
WORKDIR /app
COPY go.mod go.sum ./
RUN go env -w GO111MODULE=on \
&& go env -w GOPROXY=https://goproxy.io,direct
RUN go mod download
COPY . .
RUN go build -o server main.go && chmod +x ./server
FROM alpine:latest
WORKDIR /app
COPY --from=build /app/server /app
COPY ./config.yaml /app
EXPOSE 8080
CMD ["./server"]
过手如登山,一步一重天