如何为docker-compose构建阶段指定构建选项?

时间:2018-01-21 12:08:23

标签: docker build docker-compose dockerfile docker-build

我有一个主 docker-compose.yml:

version: '3'
services:
  recognizer:  
    container_name: recognizer
    build: ./recognizer
    hostname: recognizer
    restart: always   
    ports:
      - 8084:8084
    network_mode: "host" 

我的文件夹识别器包含 Dockerfile docker-compose.yml

Dockerfile:

FROM openjdk:8

RUN mkdir -p /app/

COPY . /app

WORKDIR /app

RUN chmod 777 /app/gradlew

RUN apt-get update && apt-get install -y netcat-traditional

RUN nc -w 2 -v localhost 5432 </dev/null; status=$?; exit $status;

RUN ./gradlew build

CMD ["./gradlew", "run"]

EXPOSE 8084

搬运工-compose.yml

recognizer:
  build:
    context: .
  ports:
    - "8084:8084"

我的文件结构是:

.
├── docker-compose.yml
├── recognizer
│   ├── build
│   ├── build.gradle
│   ├── docker-compose.yml
│   ├── Dockerfile
│   ├── gradle
│   ├── gradlew
│   ├── gradlew.bat
│   ├── settings.gradle
│   └── src

因此,我需要在localhost阶段连接到build。有用。如果我用选项--network=host构建Docker镜像,那样:

docker build --network=host -t recognizer .

但我不知道如何为docker-compose指定构建选项。

1 个答案:

答案 0 :(得分:0)

在 docker-compose.yml 中有一个设置网络的选项,如下所示:

build:
  context: ./
  dockerfile: Dockerfile
  network: host

然后 docker-compose build your-service 将使用主机网络进行构建阶段。 这为我解决了问题