案例
构建go(gin)项目
FROM golang:alpine # 为我们的镜像设置必要的环境变量 ENV GO111MODULE=on CGO_ENABLED=0 GOOS=linux GOARCH=amd64 GOPROXY="https://goproxy.cn,direct" WORKDIR /home/www/iter # 将代码复制到容器中 COPY . . RUN go mod tidy # 将我们的代码编译成二进制可执行文件 可执行文件名为 app RUN go build -o app ./main.go ## 移动到用于存放生成的二进制文件的 /dist 目录 WORKDIR /dist # #二进制文件 RUN cp /home/www/iter/app . # 声明服务端口 EXPOSE 9089 # 启动容器时运行的命令 CMD ["/dist/app"]
构建node项目
FROM node:latest WORKDIR /3d_chooser_service COPY . . RUN npm --registry https://registry.npm.taobao.org install RUN npm run build EXPOSE 3000 CMD node ./service/app.js
nginx
FROM nginx:1.16 COPY dist/ /usr/share/nginx/html COPY default.conf /etc/nginx/conf.d/default.conf EXPOSE 80
default.conf
server { listen 80; server_name localhost; #填写绑定证书的域名 root /usr/share/nginx/html; # Add index.php to the list if you are using PHP index index.php index.html index.htm index.nginx-debian.html; location /dev-api { proxy_set_header Host $host; #保留代理之前的host proxy_set_header X-Real-IP $remote_addr; #保留代理之前的真实客户端ip proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header HTTP_X_FORWARDED_FOR $remote_addr; #在多级代理的情况下,记录每次代理之前的客户端真实ip # limit_req zone=myRateLimit burst=20 nodelay; proxy_pass http://192.168.10.231:9031/; proxy_redirect default; #指定修改被代理服务器返回的响应头中的location头域跟refresh头域数值 } location / { # First attempt to serve request as file, then # as directory, then fall back to displaying a 404. #try_files $uri $uri/ =404; # limit_req zone=myRateLimit burst=20 nodelay; index index.html index.php index.htm index.nginx-debian.html; try_files $uri $uri/ /index.html; } }
java
FROM openjdk:8-jdk-alpine #启动jar包 VOLUME /tmp ADD hongyi-system/target/hongyi-system-1.0.0.jar app.jar #RUN bash -c "touch /app.jar" EXPOSE 8086 ENTRYPOINT ["java", "-jar", "app.jar", "--spring.profiles.active=dev", "--server.port=8086", "> /log/app.log"]
go
FROM golang:latest WORKDIR /root/micro-go-course/section10/userCOPY//root/micro-go-course/section10/user RUN go env-w GOPROXY=https://goproxy.cn,directRUN go build -o user ENTRYPOINT["./user"]
指令
CMD 启动指令,某些情况下时必要的