Dockerfile COPY命令在Docker-compose中无法正常工作

时间:2017-07-31 22:22:39

标签: docker nginx docker-compose dockerfile

我正在尝试通过docker-compose运行Nginx以在Node.js后端上执行负载平衡。我尝试了方法:

  1. 在将nginx.conf作为卷传递时,在docker-compose.yml中编写所有配置,如下所示:

    version: '3'
    services:
    
    #nginx
        nginx:
            image: nginx
            container_name: books-nginx
            ports: 
                - 80:80
            links:
                - node1:node1
                - node2:node2
                - node3:node3
            volumes:
                - ./nginx/nginx.conf:/etc/nginx/nginx.conf
    
    # Back-end
        node1:
            image: node
            container_name: books-api1
            ports:
                - 3000
            volumes:
                - ./books-backend:/app
            links:
                - mongodb
            environment:
                - admin=admin-password
                - secret=strong-password
            command: bash -c "cd /app && npm install && npm start"
    
        node2:
            .
            .
            .
    
        node3:
            .
            .
            .
    
    # MongoDB
        mongodb:
            image: mongo
            container_name: books-mongo
            ports:
                - 27017
            volumes:
                - ./db/mongo:/data/db
    
  2. 在这种情况下,nginx运行完美。

    1. 在“nginx”目录下的dockerfile中编写配置,然后从docker-compose开始,如下所示:
    2. 的nginx / Dockerfile:

      FROM nginx
      
      COPY nginx.conf /etc/nginx/nginx.conf
      

      搬运工-撰写:

      #nginx
          nginx:
              build: ./nginx
              container_name: books-nginx
              ports: 
                  - 80:80
              links:
                  - node1:node1
                  - node2:node2
                  - node3:node3
      

      但是在这种情况下,每当我向后端发送请求(例如,用于管理员身份验证)时,我都会收到以下错误:

      books-nginx | 2017/07/31 22:19:33 [error] 6#6: *1 open() "/usr/share/nginx/html/api/admin/authenticate" failed (2: No such file or directory), client: 172.18.0.1, server: localhost, request: "POST /api/admin/authenticate HTTP/1.1", host: "localhost"
      books-nginx | 172.18.0.1 - - [31/Jul/2017:22:19:33 +0000] "POST /api/admin/authenticate HTTP/1.1" 404 571 "-" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.96 Safari/537.36" "-"
      

      关于如何使第二种方式像第一种方式一样工作的想法(使用COPY命令)?

      更新

      我尝试创建以下dockerfile

      FROM nginx
      
      MAINTAINER Tamer Mohamed Bahgat
      
      RUN rm -v /etc/nginx/nginx.conf
      RUN rm /etc/nginx/conf.d/default.conf
      
      ADD nginx/nginx.conf /etc/nginx/
      
      RUN echo "daemon off;" >> /etc/nginx/nginx.conf
      
      CMD service nginx start
      

      然后使用docker build -t test-nginx . separtely创建图像并使用image: test-nginx在docker-compose.yml中使用它。它工作并没有错误。

      但是,使用build: .(。是同一个dockerfile的位置)仍然会给我同样的错误。

0 个答案:

没有答案