简体   繁体   English

docker-compose up 未正确运行应用程序

[英]docker-compose up not running app correctly

I'm very new to docker.我对 docker 很陌生。 I have a mean stack app that and have configured the dockerfile for frontend and backend as you can see below.我有一个平均堆栈应用程序,并且已经为前端和后端配置了 dockerfile,如下所示。 When I run docker-compose up it appears to finish, but when I go to localhost I'm greeted with this instead of my mean stack app.当我运行docker-compose up它似乎完成了,但是当我转到 localhost 时,我看到的是这个而不是我的平均堆栈应用程序。

在此处输入图片说明

I'm not really sure why, but appreciate any help with this.我不确定为什么,但感谢您对此的任何帮助。

backend Dockerfile后端 Dockerfile

FROM node

MAINTAINER Phil

WORKDIR /src

COPY . /src

RUN npm install

RUN npm install -g nodemon

EXPOSE 3000

CMD ["npm", "start"]

frontend Dockerfile前端 Dockerfile

FROM nginx
MAINTAINER Phil


VOLUME /Users/Phil/Documents/myapp/myapp-docker/frontend/dist:usr/share/nginx/html

EXPOSE 80

docker-compose.yml docker-compose.yml

version: '3'
services:
    web:
        build: ./frontend
        ports:
            - "80:80"
        links:
        - node
        volumes:
        - "/Users/Phil/Documents/myapp/myapp-docker/frontend/dist:/usr/share/nginx/html"
    node:
        build: ./backend
        ports:
        - "3000:3000"

console output:控制台输出:

Building node
Gracefully stopping... (press Ctrl+C again to force)
Stopping myapp-docker_web_1 ... done

C:\Users\Phil\Documents\myapp\myapp-docker>docker-compose up --build
Building node
Step 1/8 : FROM node
 ---> 2f1ff44a8bb5
Step 2/8 : MAINTAINER Phil
 ---> Using cache
 ---> 78d7fb0bb77a
Step 3/8 : WORKDIR /src
 ---> Using cache
 ---> 70e753257f23
Step 4/8 : COPY . /src
 ---> Using cache
 ---> 2903a77a03ef
Step 5/8 : RUN npm install
 ---> Using cache
 ---> 582d4f3b0a7b
Step 6/8 : RUN npm install -g nodemon
 ---> Using cache
 ---> 1c9d67ce2743
Step 7/8 : EXPOSE 3000
 ---> Using cache
 ---> 0d77a648b020
Step 8/8 : CMD ["npm", "start"]
 ---> Using cache
 ---> 9adfbe7156f2
Successfully built 9adfbe7156f2
Successfully tagged myapp-docker_node:latest
Building web
Step 1/4 : FROM nginx
 ---> 2073e0bcb60e
Step 2/4 : MAINTAINER Phil
 ---> Using cache
 ---> 1fba608ad1fa
Step 3/4 : VOLUME /Users/Phil/Documents/myapp/myapp-docker/frontend/dist:usr/share/nginx/html
 ---> Running in 122358e91315
Removing intermediate container 122358e91315
 ---> 2a54fcb0a4e6
Step 4/4 : EXPOSE 80
 ---> Running in 95117506887d
Removing intermediate container 95117506887d
 ---> 36a7c7bac5b8
Successfully built 36a7c7bac5b8
Successfully tagged myapp-docker_web:latest
Recreating myapp-docker_node_1 ... done
Recreating myapp-docker_web_1  ... done
Attaching to myapp-docker_node_1, myapp-docker_web_1
node_1  |
node_1  | > backend@1.0.0 start /src
node_1  | > node ./package.json
node_1  |
myapp-docker_node_1 exited with code 0
web_1   | 2020/02/06 20:09:02 [error] 6#6: *1 open() "/usr/share/nginx/html/favicon.ico" failed (2: No such file or directory), client: 172.18.0.1, server: localhost, request: "GET /favicon.ico HTTP/1.1",
host: "localhost"
web_1   | 172.18.0.1 - - [06/Feb/2020:20:09:02 +0000] "GET /favicon.ico HTTP/1.1" 404 153 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:72.0) Gecko/20100101 Firefox/72.0" "-"
Gracefully stopping... (press Ctrl+C again to force)
Stopping myapp-docker_web_1    ... done

backend package.json后端 package.json

{
  "name": "backend",
  "version": "1.0.0",
  "description": "",
  "main": "app.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "node ./package.json"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "aws-sdk": "^2.612.0",
    "bcrypt": "^3.0.8",
    "body-parser": "^1.19.0",
    "create-hash": "^1.2.0",
    "crypto": "^1.0.1",
    "dotenv": "^8.2.0",
    "express": "^4.17.1",
    "jsonwebtoken": "^8.5.1",
    "mongoose": "^5.8.11",
    "mongoose-unique-validator": "^2.0.3",
    "multer": "^1.4.2",
    "multer-s3": "^2.9.0"
  }
}

Here's my directory layout in case that's relevant这是我的目录布局,以防万一

在此处输入图片说明

It looks like you don't actually need the Dockerfile for frontend.看起来您实际上并不需要前端的 Dockerfile。 You would definitely have to remove the VOLUME declaration but I suggest removing the whole file and using the "image: nginx" directly in your docker-compose.yml.您肯定必须删除 VOLUME 声明,但我建议删除整个文件并直接在 docker-compose.yml 中使用“image: nginx”。

Since you are not building your frontend application, you need to make sure you have index.html in frontend/dist before runnning docker-compose up .由于您不是在构建前端应用程序,因此在运行 docker docker-compose up之前,您需要确保在 frontend/dist 中有 index.html。

Also your backend applications fails to start because it looks like it tries to run node ./package.json .此外,您的后端应用程序无法启动,因为它看起来像是在尝试运行node ./package.json Check the definition of your start task in package.json .检查package.json start任务的定义。

After making these changes you can try and rerun your docker-compose:进行这些更改后,您可以尝试重新运行 docker-compose:

version: '3'
services:
    web:
        image: nginx
        ports:
            - "80:80"
        volumes:
        - "/Users/Phil/Documents/myapp/myapp-docker/frontend/dist:/usr/share/nginx/html"
    node:
        build: ./backend
        ports:
        - "3000:3000"

You're pointing at the wrong folder.你指向错误的文件夹。 Your index.html is in /dist/my-app, like i saw from the photo you posted here: https://postimg.cc/MntD9mFM .您的 index.html 位于 /dist/my-app 中,就像我从您在此处发布的照片​​中看到的一样: https : //postimg.cc/MntD9mFM

Modify your Dockerfile and docker-compose to point to the correct folder.修改您的 Dockerfile 和 docker-compose 以指向正确的文件夹。

Build 1构建 1

Dockerfile文件

FROM nginx
MAINTAINER Phil
#optional
RUN rm -rf /usr/share/nginx/html/*

VOLUME /Users/Phil/Documents/myapp/myapp-docker/frontend/dist/my-app:usr/share/nginx/html

EXPOSE 80

docker-compose:码头工人组成:

version: '3'
services:
    web:
        build: ./frontend
        ports:
            - "80:80"
        links:
        - node
        volumes:
        - "/Users/Phil/Documents/myapp/myapp-docker/frontend/dist/my-app:/usr/share/nginx/html"
    node:
        build: ./backend
        ports:
        - "3000:3000"

Build 2 (better)构建 2(更好)

You can also ignore Dockerfile cause docker-compose point already to your volume so your new docker-compose will look like:您还可以忽略 Dockerfile,因为 docker-compose 已经指向您的卷,因此您的新 docker-compose 将如下所示:

version: '3'
services:
    web:
        image: nginx
        ports:
            - "80:80"
        links:
        - node
        volumes:
        - "/Users/Phil/Documents/myapp/myapp-docker/frontend/dist/my-app:/usr/share/nginx/html"
    node:
        build: ./backend
        ports:
        - "3000:3000"

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

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