'docker-compose up not running app correctly
I'm very new to docker. I have a mean stack app that and have configured the dockerfile for frontend and backend as you can see below. 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.
I'm not really sure why, but appreciate any help with this.
backend Dockerfile
FROM node
MAINTAINER Phil
WORKDIR /src
COPY . /src
RUN npm install
RUN npm install -g nodemon
EXPOSE 3000
CMD ["npm", "start"]
frontend Dockerfile
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"
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 | > [email protected] 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
{
"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
Solution 1:[1]
It looks like you don't actually need the Dockerfile for frontend. 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.
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
.
Also your backend applications fails to start because it looks like it tries to run node ./package.json
. Check the definition of your start
task in package.json
.
After making these changes you can try and rerun your 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"
Solution 2:[2]
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.
Modify your Dockerfile and docker-compose to point to the correct folder.
Build 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)
You can also ignore Dockerfile cause docker-compose point already to your volume so your new docker-compose will look like:
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"
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Mihai |
Solution 2 | Community |