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

cmssoen2  于 2022-12-18  发布在  Docker
关注(0)|答案(2)|浏览(157)

我对docker很陌生。我有一个很棒的stack应用程序,并且已经为前端和后端配置了dockerfile,如下所示。当我运行docker-compose up时,它似乎完成了,但是当我进入localhost时,我看到的是这个而不是我的stack应用程序。

我不知道为什么,但很感谢你的帮助。
后端停靠文件

FROM node

MAINTAINER Phil

WORKDIR /src

COPY . /src

RUN npm install

RUN npm install -g nodemon

EXPOSE 3000

CMD ["npm", "start"]

前端停靠文件

FROM nginx
MAINTAINER Phil

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

EXPOSE 80

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"

控制台输出:

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

后端包. 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"
  }
}

这是我的目录布局,以防与此相关

ozxc1zmp

ozxc1zmp1#

看起来你实际上并不需要Dockerfile作为前端,你肯定需要删除VOLUME声明,但我建议删除整个文件并使用“image:nginx”直接添加到您的docker-compose. yml中。
由于您没有构建前端应用程序,因此在运行docker-compose up之前,需要确保frontend/dist中有index.html。
另外,后端应用程序无法启动,因为它似乎试图运行node ./package.json。请检查package.jsonstart任务的定义。
进行这些更改后,您可以尝试重新运行您的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"
eqqqjvef

eqqqjvef2#

你指向了错误的文件夹。你的index.html文件在/dist/my-app,就像我在你上传的照片中看到的那样:https://postimg.cc/MntD9mFM .
修改您的Dockerfile和docker-compose以指向正确的文件夹。

构建版本1

停靠文件

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

对接-组合:

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"

构建版本2(更好)

您也可以忽略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"

相关问题