Dockerfile COPY命令在Docker-compose中没有像预期的那样工作
我试图通过docker-compose运行Nginx来执行Node.js后端的负载均衡。 我试过的方法:
-
在docker-compose.yml中写入所有configuration,同时按如下方式传递nginx.conf作为卷:
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
在这种情况下,nginx运行完美。
- 将configuration写入“nginx”目录中的dockerfile中,然后从docker-compose中将其启动,如下所示:
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
但在这种情况下,每当我发送一个请求(例如对于pipe理员身份validation)到后端我得到以下错误:
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命令)?
更新
我试着创build下面的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 docker build -t test-nginx .
创build图像docker build -t test-nginx .
并使用它在docker-compose.yml中使用image: test-nginx
。 它的工作,并没有给错误。
但是,使用build: .
(。是相同的dockerfile的位置)仍然给我同样的错误。