简体   繁体   English

如何通过 docker-compose.yml 构建 docker 镜像

[英]how to build docker image by docker-compose.yml

Here is my docker-compose.yml :这是我docker-compose.yml

version: '2'
services:
web:
  build: .
  context: ./
  dockerfile: Dockerfile
  ports:
    - "8080:8080"
  container_name: demo
  volumes:
    - .:/images

I got an error:我有一个错误:

ERROR: The Compose file './docker-compose.yml' is invalid because: Unsupported config option for services.web: 'dockerfile'错误:撰写文件“./docker-compose.yml”无效,因为:services.web 的配置选项不受支持:“dockerfile”

So I would like to build the container and also run.所以我想构建容器并运行。 I will have a few more containers to build and run.我将有更多的容器来构建和运行。 Is it possible by docker-compose? docker-compose 可以吗?

If I'not mustaken the issue is after build you have a dot, the dockerfile and context are not correct idented.如果我不认为问题是在build之后你有一个点,那么dockerfilecontext没有正确识别。 And I think the bar in the context should not be there.我认为context的栏不应该存在。

version: "3.7"
services:
  api:
    image: image-name
    build:
      context: .
      dockerfile: path/to/Dockerfile
    container_name: container-name

I think this resolves the issue you have, but couldn't test it.我认为这解决了您遇到的问题,但无法对其进行测试。

You need to respect the indentations , here is an example for building Nginx docker image https://github.com/wshihadeh/three_methods_to_share_assets/blob/master/precompile_during_docker_build/docker-compose.yml您需要尊重缩进,这是构建 Nginx docker 镜像的示例https://github.com/wshihadeh/three_methods_to_share_assets/blob/master/precompile_during_docker_build/docker-compose.yml

version: '3.7'
services:
  web:
    build:
      context: nginx
      dockerfile: Dockerfile
    command: server
    ports:
      - 80:80

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM