FROM
后面跟上AS
生成阶段指定名称
FROM openjdk:8 AS practice-service
ARG logs=/logs
ENV TZ Asia/Shanghai
ENV address=${address} \
port=${port} \
namespace=${namespace} \
logs=${logs}
COPY ./practice.jar /opt/practice.jar
CMD ["java", "-Dfile.encoding=utf-8", "-Xms512M","-Xmx1024M","-jar", "/opt/practice.jar","--logging.file.name=${logs}/practice.log"]
FROM openjdk:8 AS case-service
ARG logs=/logs
ENV TZ Asia/Shanghai
ENV logs=${logs}
COPY ./case.jar /opt/case.jar
CMD ["java", "-Dfile.encoding=utf-8", "-Xms512M","-Xmx1024M","-jar", "/opt/case.jar","--logging.file.name=${logs}/case.log"]
执行下面的命令就能同时构建两个镜像了,其中 -t
是生成的镜像名称, --target
是 AS
后面生成阶段, 例如:
docker build -t practice-service:1.0.0 --target practice-service .
docker build -t case-service:1.0.0 --target case-service .
AS
默认情况,每个构建阶段是没有命名的,通过整型编号进行引用(类似数组下标索引,0-第一个FROM
,1-第二个FROM
,依次类推, 以下面的Dockefile为例, 第二个COPY:
把前构建阶段–from=0
的/go/src/github.com/helloworld/app
文件复制到 ./root/
目录
FROM golang:1.13.5
WORKDIR /go/src/github.com/helloworld/
# RUN go get -d -v golang.org/x/net/html
RUN go get -d -v github.com/go-sql-driver/mysql
COPY app.go .
RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o app .
FROM alpine:latest
RUN apk --no-cache add ca-certificates
WORKDIR /root/
COPY --from=0 /go/src/github.com/helloworld/app .
CMD ["./app"]
如果存在
AS
阶段名的话, Dockfile里面还可以通过--from=<阶段名>
调用
version: "3.6"
services:
practice-service:
build: #build对应build镜像的操作
context: . #路径
dockerfile: Dockerfile
target: practice-service #对应镜像阶段
image: practice-service:1.0.0
container_name: practice-service
restart: always
deploy: #容器的资源限制
resources:
limits:
cpus: "2.00"
memory: 5G
reservations: #初始化资源,只支持内存参数,CPU参数不支持
memory: 200M
volumes:
- /home/sdp/bin/log/practice-service:/logs
- /etc/localtime:/etc/localtime
ports:
- "8888:8888"
environment:
- TZ=Asia/Shanghai
- logs=/logs
logging: # 日志大小限制
driver: "json-file"
options:
max-size: "1000k"
max-file: "20"
case-service:
build:
context: .
dockerfile: Dockerfile
target: case-service
image: case-service:1.0.0
container_name: case-service
restart: always
volumes:
- /home/sdp/bin/log/ase-service:/logs
- /etc/localtime:/etc/localtime
ports:
- "8890:8890"
environment:
- TZ=Asia/Shanghai
- logs=/logs
logging:
driver: "json-file"
options:
max-size: "1000k"
max-file: "20"
docker-compose的target
选项是在version: "3.4"
之后才支持的, 不支持该参数的话需要升级更新, 具体介绍见 https://github.com/docker/compose ,安装步骤如下
wget https://github.com/docker/compose/releases/download/v2.10.2/docker-compose-linux-x86_64
mv docker-compose-linux-x86_64 /usr/local/bin/docker-compose
chmod 755 /usr/local/bin/docker-compose
source /etc/profile
执行 docker-compose up --build -d
会在在后台启动容器并在启动前构建镜像
docker-compose start <服务名>
可以启动单个服务
docker-compose stop <服务名>
可以停止单个服务
docker-compose down --rmi all
会删除所有镜像和容器
如果遇到下面的报错,可以先执行export DOCKER_BUILDKIT=0
设置环境变量后再执行docker-cmpose的命令
[root@node product]# docker-compose up
[+] Running 0/2
⠿ practice-service Error 2.8s
⠿ exam-service Error 2.9s
[+] Building 0.0s (0/0)
listing workers for Build: failed to list workers: Unavailable: connection error: desc = "transport: Error while dialing unable to upgrade to h2c, received 404"
1. docker-compose限制容器cpu和内存
2. Docker多阶段构建镜像
3. https://github.com/containers/podman/issues/13889