yifanzheng / utils

:wrench:收集一些开发中常用的工具类代码及优秀类库
2 stars 0 forks source link

Docker:Dockerfile 模板集合 #25

Closed yifanzheng closed 8 months ago

yifanzheng commented 2 years ago

Java

# 构建 jar
mvn -Prelease clean package -Dmaven.test.skip

rm -rf docker/$PROJECT_NAME
mkdir -p docker/$PROJECT_NAME
cp -r target/$PROJECT_NAME-1.0.0.jar docker/$PROJECT_NAME/

cd docker

# Generate docker file
cat <<EOF > Dockerfile
FROM openjdk:8-jdk-alpine
# 可以在 docker 容器中使用 jvm 工具
RUN apk add --no-cache tini
ENTRYPOINT ["/sbin/tini", "--"]

# 如果存在 TSL 验证,可以加上
# RUN sed -i 's/^jdk.tls.disabledAlgorithms=SSLv3, TLSv1, TLSv1.1,/jdk.tls.disabledAlgorithms=SSLv3,/' /usr/lib/jvm/java-1.8-openjdk/jre/lib/security/java.security

MAINTAINER DAE
COPY $PROJECT_NAME /opt/$PROJECT_NAME
WORKDIR /opt/$PROJECT_NAME

EXPOSE 8080

CMD ["java", "-server", "-jar", "$PROJECT_NAME-1.0.0.jar", \
  "--spring.profiles.active=gdev", \
  "--jasypt.encryptor.password=Test",\
  "--server.port=8080"]

EOF
# End Generate docker file

docker_register=${镜像仓库}

# Push docker image
tag=`date "+%Y%m%d%H%M%S"`
export docker_image_name=${PROJECT_NAME}:${tag}
export docker_image_address=$docker_register/$docker_image_name

echo $docker_image_name > docker_image

echo "> begin to build docker image: $docker_image_address"
docker build -t $docker_image_address .

echo "> begin to push image."
docker login -u $username-p $password $docker_register
docker push $docker_image_address
echo "> finished to build and push image."
# End Push docker image
yifanzheng commented 2 years ago

Node 项目(Angular)

npm install

deploy_env=gdev

echo -e ">> $(date --rfc-3339=s) begin build app"
./node_modules/@angular/cli/bin/ng build --configuration=$deploy_env 

# generate dockerfile
echo -e ">> $(date --rfc-3339=s) begin generate dockerfile"
cat <<EOF > Dockerfile
## Copy and Setup
  FROM nginx

  LABEL authors="Star"
  MAINTAINER DAE

  RUN mkdir /ng-alain
  WORKDIR /ng-alain

  # Just copy the dist directory and nginx config file 
  COPY ./dist ./dist
  COPY _nginx/${deploy_env}.conf /etc/nginx/conf.d/nginx.conf

  RUN rm -rf /usr/share/nginx/html/*

  COPY /dist /usr/share/nginx/html

  CMD [ "nginx", "-g", "daemon off;"]

EOF
echo -e ">> $(date --rfc-3339=s) end generate dockerfile \n"

# build and push image: different environments cannot share images
docker_register= ${镜像仓库地址}
tag=`date "+%Y%m%d%H%M%S"`

export docker_image_name=$docker_register/${GROUP_NAME}/${PROJECT_NAME}-${deploy_env}:${tag}
echo $docker_image_name > docker_image

echo -e ">> $(date --rfc-3339=s) begin to build docker image: $docker_image_name"
docker build -t $docker_image_name .

echo -e ">> $(date --rfc-3339=s) begin to push image."

echo  $password | docker login -u $username --password-stdin $docker_register
docker push $docker_image_name

echo -e ">> $(date --rfc-3339=s) end to build and push image. \n"
# End build and push docker image