Docker Volumes - Changes not reflected - docker

Using node.js
I am having an issue with docker volumes. I set up a volume in my docker-compose.yml file, but for some reason, changes I am making locally are not being reflected. Any idea why?
I have the current docker-compose.yml file
version: "3"
services:
posts:
build:
dockerfile: Dockerfile.dev
context: ./posts
ports:
- "4000:4000"
volumes:
- /app/node_modules
- ./posts:/app
...// more services here
Excerpt from index.js in posts
app.get("/posts", (req, res) => {
console.log("Quackss!");
res.send(posts);
});
Now lets say I run
docker-compose up --build posts
When I make my first request via postman to /posts
I see "Quackss!" in my console.
Now when I change the code to
app.get("/posts", (req, res) => {
console.log("Double Quack");
res.send(posts);
});
and save, then make a request via postman
I still see "Quacks!!" instead of "Double Quack".
I do have nodemon setup, so I didn't think that was the issue.
I ran docker ps to see the name of the container
Then ran docker exec -it <container name> sh
then cat index.js
If volumes were setup correctly, I'd expect to see
app.get("/posts", (req, res) => {
console.log("Double Quack");
res.send(posts);
});
Instead, I saw the original
app.get("/posts", (req, res) => {
console.log("Quackss!");
res.send(posts);
});
Here is my posts package.json
{
"name": "posts",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"start": "nodemon"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"axios": "^0.19.2",
"cors": "^2.8.5",
"express": "^4.17.1",
"nodemon": "^2.0.4"
}
}
And dockerfile.dev
FROM node:alpine
WORKDIR /usr/app/
COPY ./package.json ./
RUN npm install
COPY ./ ./
CMD ["npm", "start"]
Any idea why this is happening?

Your dockerfile.dev has a WORKDIR of /usr/app/ and you're copying the source code into that directory.
Your docker-compose.yml file is mapping the volumes to /app instead.
One solution is to change the WORKDIR to /app/ and rebuild your image.

Related

Nestjs Docker Debug

I have a simple Dockerfile
FROM node:12.13-alpine As development
WORKDIR /usr/src/app
COPY package*.json ./
RUN apk add --no-cache bash && npm install --only=development
COPY . .
RUN npm run build
FROM node:12.13-alpine as production
ARG NODE_ENV=production
ENV NODE_ENV=${NODE_ENV}
WORKDIR /usr/src/app
COPY package*.json ./
RUN npm install --only=production
COPY . .
COPY --from=development /usr/src/app/dist ./dist
CMD ["node", "dist/main"]
my docker-compose is:
version: '3.7'
services:
main:
container_name: main
build:
context: .
target: development
volumes:
- .:/usr/src/app
- /usr/src/app/node_modules
ports:
- ${SERVER_PORT}:${SERVER_PORT}
- 9229:9229
command: npm run start:debug
env_file:
- .env
Inside my package.json and script, I have:
"start:debug": "nest start --debug 0.0.0.0:9229 --watch"
I added the conf in my visual Studio Code (.vscode/launch.json)
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "attach",
"name": "Debug",
"address": "127.0.0.1",
"port": 9229,
"sourceMaps": true,
"restart": true,
"localRoot": "${workspaceFolder}",
"remoteRoot": "/usr/src/app",
"skipFiles": ["<node_internals>/**"]
}
]
}
After that I can run docker-compose up --build and start my debugger but the code doesn't stop at my breakpoint.
I use Mac 12.6 and Docker version 20.10.13

Can not get cypress to work in docker container

I want to run cypress in docker container and for this purpose i created.
For this i followed some guides and tutorials
e2e directory that has Dockerfile
FROM cypress/base:14
WORKDIR /app
# dependencies will be installed only if the package files change
COPY package.json .
COPY cypress.json .
RUN npm install
RUN npx cypress open
RUN npx cypress verify
cypress.json
{
"baseUrl": "http://localhost:3000",
"reporter": "junit",
"reporterOptions": {
"mochaFile": "cypress/results/output.xml"
}
}
package.json
{
"name": "e2e",
"version": "1.0.0",
"description": "Cypress tests",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"private": true,
"devDependencies": {
"cypress": "9.5.0"
}
}
and .npmrc
registry=http://registry.npmjs.org/
save-exact=true
progress=false
package-lock=true
modified docker-compose.yml at the root to be
version: '3.8'
services:
nextjsApp:
// rest
cypress:
image: "cypress/included:3.2.0"
depends_on:
- nextjs
environment:
- CYPRESS_baseUrl=http://nextjs:3000
working_dir: /e2e
command: npx cypress run
volumes:
- ./:/e2e
networks:
backend:
external: true```
Running docker-compose build does not cause or show any errors
But when i run docker-compose up i get
cypress_1 | Could not find any tests to run.
cypress_1 |
cypress_1 | We looked but did not find a cypress.json file in this folder: /e2e
How can this be solved ?

Nodemon doesn't reload my app.js on my docker container

I decided to make a server with Express in a container and installed nodemon to watch and reload my code modifications, but, for some reason, the nodemon on container doesn't reload when I modify my code. How can I fix that?
My Dockerfile:
FROM node:14-alpine
WORKDIR /usr/app
RUN npm install -g nodemon
COPY . .
EXPOSE 3000
CMD ["npm","start"]
My package.json:
{
"name": "prog-web-2",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "nodemon app.js"
},
"repository": {
"type": "git",
"url": "git+https://github.com/willonf/prog-web-2.git"
},
"keywords": [],
"author": "",
"license": "ISC",
"bugs": {
"url": "https://github.com/willonf/prog-web-2/issues"
},
"homepage": "https://github.com/willonf/prog-web-2#readme",
"dependencies": {
"express": "^4.17.1"
}
}
My app.js:
const express = require("express")
const app = express()
app.get("/", (req, res) => {
res.end("Hello, World!")
});
app.listen(3000);
You are copying all of your files into the docker container.
COPY . .
So when you modify your local file, you are not modifying the file inside the docker container and the nodemon inside the docker container can't detect any changes.
It is possible to get the behavior you want using Docker Volumes. You can configure them, so that the docker container shares the working directory with the host system. If you change a file on the host nodemon would detect changes in this case.
The answer in this post is showing an example on how to accomplish that.

Cannot find module 'apollo-server' when deploying on AWS EC2 using docker-compose

I have set up a GraphQL server and tried to deploy it on EC2 using docker-compose.
There is an error that "nodemon not found in npm". After adding "--global nodemon" in the Docker file, I ran docker-compose down, docker container prune, docker image rm [image name], then docker-compose up --build.
Then there is this Error: Cannot find module 'apollo-server'.
enter image description here
docker-compose.yml
version: '3.6'
services:
graphql-server:
container_name: backend
build: ./
volumes:
- ./:/src/graphqlserver
command: npm start
working_dir: /src/graphqlserver
ports:
- "4000:4000"
Dockerfile
FROM node:latest
RUN mkdir -p /src/graphqlserver
WORKDIR /src/graphqlserver
COPY . /src/graphqlserver/
RUN npm install --global nodemon
EXPOSE 4000
CMD [ "npm", "start" ]
server.js
const { ApolloServer } = require('apollo-server');
const typeDefs = require('./typeDefs');
const resolvers = require('./resolvers');
const server = new ApolloServer({ typeDefs, resolvers });
server.listen().then(({ url }) => {
console.log(`🚀 Server ready at ${url}`);
});
package.json
"dependencies": {
"apollo-server": "^2.25.2",
"graphql": "^15.5.1"
},
"devDependencies": {
"nodemon": "^2.0.9"
}

Docker - Module not found: Can't resolve 'react-plotly.js'

This question is specific to my docker configuration.
Super new to Docker and hence this problem.
I tried all the possible solutions on the internet, but none of them worked.
Closest Question: React.js Docker - Module not found
Dockerization of React App
Below are my docker files
Dockerfile
FROM node:10
WORKDIR /app
# add `/app/node_modules/.bin` to $PATH
ENV PATH /app/node_modules/.bin:$PATH
# install dependencies
COPY package*.json ./
RUN npm install --silent
RUN npm install react-scripts -g --silent
COPY . .
EXPOSE 3000
CMD ["npm", "start"]
My system has node version 10.15.3
docker-compose.yml
version: '3'
services:
django:
build: ./api
command: ["python", "manage.py", "runserver", "0.0.0.0:8000"]
volumes:
- ./api:/app
ports:
- "8000:8000"
frontend:
build: ./frontend
volumes:
- ./frontend:/app
- /app/node_modules
ports:
- "3000:3000"
volumes:
node-modules:
Folder Structure:
api and frontend both have Dockerfile in it, but it's just the frontend Dockerfile that is causing the issue.
Cache messages
package.json
{
"name": "frontend",
"version": "0.1.0",
"private": true,
"dependencies": {
"#testing-library/jest-dom": "^5.11.5",
"#testing-library/react": "^11.1.0",
"#testing-library/user-event": "^12.1.10",
"moment": "^2.29.1",
"react": "^17.0.1",
"react-big-calendar": "^0.28.1",
"react-dom": "^17.0.1",
"react-router-dom": "^5.2.0",
"react-scripts": "4.0.0",
"web-vitals": "^0.2.4"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"eslintConfig": {
"extends": [
"react-app",
"react-app/jest"
]
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
}
}
I faced the same issue but below steps helped me solve the issue.
While adding a new package to our React project and running it with docker-compose following these steps:
Stop any docker-composeif running, with docker-compose down -v
Now add your npm module in your react application, npm install react-plotly.js (in your case)
docker-compose up -d --build
After looking at your docker file it looks fine to me, so I think it's the way you're installing the package is causing the issue.

Resources