Update 3/23: I used author's package.json, npm install on my Mac, upgrade react-scripts to 3.4.0 and Dockerfile to fix a few issues and now this version works: https://github.com/harrywang/my-flask-react-auth/tree/6e65a7deaf89244a41a7c91843f07f4756956f95 however, this does not explain why the previous version did not work.
Update 3/23: if I only replace package.json and package-lock.json at https://github.com/harrywang/my-flask-react-auth/tree/master/services/client with the authors' versions at https://gitlab.com/testdriven/flask-react-auth/-/tree/master/services%2Fclient, it will work. Don't know why.
Following the tutorial at https://testdriven.io/courses/auth-flask-react/
Docker 2.2.0.4 Desktop on Mac
My code repo is at https://github.com/harrywang/my-flask-react-auth, where you can see the Dockerfile and docker-compose.yml, you can clone and run docker-compose up -d --build to reproduce my problem.
When I run docker-compose up -d --build, the flask and database containers work well but the node container exits with error code 0 when "Starting the development server..."
One thing I noticed is that I don't see [wds] webpack related info locally on my Mac. I don't know what they are.
but when I go to /services/client and run npm start, the node server starts and works well locally.
There is no error message during the docker building process. I have spent a few hours on this and cannot figure it out. Please help!! Thanks a lot!!
However, the author's repo at https://gitlab.com/testdriven/flask-react-auth with older versions does not have this issue:
I had the same problem. I am currently working through the same course. I have done every exactly like written in the guide. I was able two start 2 containers, the third one (flask-react-auth_client_1) stoped with 0.
I first tried to get into the container and execute docker run -it --entrypoint sh <image> and then npm start
No problem was detected. So I did a little research.
Apparently with react-scripts#3.4.0 or react-scripts#3.4.1+ it is impossible to run in the background now without the CI flag enabled.
My fix was, to add CI=true to the docker-compose.yml under client > environment.
I go the idea from https://github.com/facebook/create-react-app/issues/8688
It seems that the following part is missing from the client app Dockerfile:
COPY src /usr/src/app/src
COPY public /usr/src/app/public
This will copy the source folders into the Docker image. These two lines can be set above the following line in your Dockerfile.
The way you can troubleshoot such issues is by doing a docker run -it --entrypoint sh <image> to spin up a new container and get a shell to it. Then you can run the command that the server intended to run (npm start in our case). From there, you can spot errors that may not have been propagated to docker-compose. In our case, the error was the following:
react-scripts start Could not find a required file.
Name: index.html Searched in: /usr/src/app/public
In my case, just simply add CI=true into npm start scrpts, in package.json:
"start": "CI=true react-scripts start",
Thank to p1umyi 's answer above
You must include command line on docker-compose.yml :
version: '3'
services:
redis-server:
image: 'redis'
node-app:
command: npm start #this line
build: .
ports:
- "4001:8081"
Related
I had an issue with not being able to debug a node application running in docker where the dockerfile was based on a tweaked version from an earlier project.
In order to investigate the issue I used a simplified version of the dockerfile:
FROM node:16-alpine
COPY package.json package.json
RUN npm install
COPY ./index.js .
CMD ["node", "."]
where the contents of index.js were only console.log('Hello World!').
Findings:
docker build -t debug-test . && docker run debug-test
Hello World!
docker build -t debug-test . && docker run -p "9229:9229" debug-test node --inspect-brk=0.0.0.0 index.js
Debugger listening on ws://0.0.0.0:9229/c29fa1e4-0256-44ce-898b-45d7b43c667e
but the chrome node debugger does not connect.
I tried lots of permutations of ports, docker compose and mounting of code as volumes without any luck.
Also, used this repo to establish that chrome debugger was not broken.
The problem turned out to be the base docker image. The original dockerfile I had copied used node:12-alpine but I tweaked it to be node:16-alpine to match the version of the application I was dockerising. My detective work found that starting from node:14-alpine something has changed to egress which prevents debugging.
Switching the base image from an alpine image to node:16 resolved the problem and allowed debugging of the app running in docker. I'm not clear what the change would have been but if anyone knows I'd be interested.
I have a express application. And I use the docker-compose to run it. To run my app I use command:
docker-compsoe up
If I run it at first time and don't have any node_modules - I have an error in terminal, sth like "The module 'express' not found, please install it and try again...". So, I just open one more terminal, and run next command:
docker-compose exec backend npm i
Modules are installed for a few seconds. And and my app start working in the previous terminal. I allways use this method, but now I found command run for docker-compose. It allows you to exec some command in container, when it is not raised. So I wanted to try this command and I deleted ./node_modules directory, stop all containers, close all terminals, open terminal and run command:
docker-compose run backend npm i
Modules started to install, I wait for about 10 minutes but it is stops in the middle. I don't understand why? If I try up and npm i in second terminal it works, but with command run - not. What I do wrong?
You should not install your node modules in a running container. Instead, you shoud install it in your image via your Docker file and then run it via docker or docker-compose.
Your Dockerfile should look like something like this:
FROM node:10 # or the version of node you are using
WORKDIR /usr/src/app #replace this with your app code path
COPY package.json /usr/src/app
RUN npm install
COPY app-code/ /usr/src/app/app-code # again, use your own path
EXPOSE 3000
CMD ["npm", "start"]
You have to run npm install from your dockerfile and not copy your development node folder because the environment from the container may differ from your development environment.
Then you can just run it from your docker-compose file.
I wrote a DockerFile for a node application. This is the docker file:
FROM node:10.15.0
COPY frontend/ frontend/
WORKDIR frontend/
RUN npm install
RUN npm start
When I try to build this Dockerfile, I get this error: ERROR in ./app/main.js Module not found: Error: Can't resolve './ResetPwd' in '/frontend/app'
So I added RUN ls & RUN ls /app in Dockerfile. Both of the files are there! I'm not familiar with NodeJS and it's build process at all. Can anybody help me with this?
Point: I'm not sure if it helps or not, but I'm using Webpack too.
The problem was that our front-end developer considered that node imports are case insensitive and he was using windows. I tried to run Dockerfile on mac and that's why it couldn't find the modules. Module name was resetPass!
This question saved me!
hope this helps somebody else.
I have an angular app and I was trying to containerize it using docker.
I build the app on a windows machine. and I was trying to build it inside a linux container.
the app was building fine on my windows machine and failing with the following error in the docker environment:
ERROR in folder1/folder2/name.component.ts: - error TS2307: Cannot find module '../../../folder1/File.name'.
import { Interface1} from '../../../folder1/File.name';
Cannot find module '../../../node_modules/rxjs/Observable.d.ts'.
import { Observable } from 'rxjs/observable';
it was driving me nuts.
I saw this question and at first did not think that it was what was going on. the next day I decided to build the same app in a linux environment just to make sure. Used WSL 2 and boom:
the real problem!
ERROR in error TS1149: File name '/../../node_modules/rxjs/observable.d.ts' differs from already included file name '/../../node_modules/rxjs/Observable.d.ts' only in casing.
6 import { Observable } from 'rxjs/observable';
SO it was a casing issue. I corrected the casing and it builds fine!
I cant say if this will work for sure since I don't know if npm start actually triggers webpack, but if it doesn't you'll have to add an extra RUN line after the COPY frontend / line
There are a few issues here, try using this docker file instead
FROM node:10.15.0
# Copy dependency files and install packages
WORKDIR frontend
COPY frontend/package.* .
RUN npm install
# Copy src down and other stuff
COPY frontend /
# cd to the file with the package.json
WORKDIR /appDir/frontend
# Command that executes when container starts up
CMD ["npm", "start"]
Make sure that you also update your .dockerignore to include node_modules. You'll have to build and run the container with the following commands.
docker build -t frontendApp .
docker run -p 8080:8080 frontendApp
The -p and 8080:8080 have to do with exposing internal ports to the outside world so you can view it in a browser, just change it to whatever port web pack is using to display your stuff.
I had to rebuild the disruptive package, like in this issue for node-sass
The command would be npm rebuild <package-name>
For me, this was npm rebuild node-sass
I installed all npm dependencies inside to the container. So I don't want to install dependencies to my host machine. Everything is okay, it works. But there is a problem with Webstorm.
It says "Unresolved function" for npm dependencies.
How to fix that problem? How can I say "Hey webstorm, node_modules directory is inside the container :)"
WebStorm expects node_modules to be located in the project folder.
You can try setting up NODE_PATH in Node.js run configuration template: Run | Edit Configurations..., expand Templates node, select Node.js configuration, specify NODE_PATH in Environment variables field
Please see comments in https://youtrack.jetbrains.com/issue/WEB-19476.
But I'm not sure it will work for modules installed in container...
Even though you expose the container's node_modules folder it's likely to not work as expected because npm dependencies are built according their host environment, which will not be the same as your local dev machine.
This statement applies even stronger if you want to run some CLI developments tools
- which sometimes are compiled binary files.
TLDR;
Trick is to update the path inside docker container from /your-path to /opt/project.
Detail solution
The issue with webstorm is they don't allow you to define the path from where you can pick node_modules. But they have a default path from which they pick it. I was facing the same issue while I wanted to integrate remote debugging for a backend node service running inside docker.
You need to update your docker file. Suppose you were using Dockerfile was something like this
# pull official base image
FROM node:12.11-buster
# set working directory
WORKDIR /app
COPY ./package.json ./package-lock.json /app
RUN npm install
Now this won't be detecting the node_modules for remote debugging or any other integration you need in webstorm.
But if you update the dockerfile to something like this
# pull official base image
FROM node:12.11-buster
# set working directory
# This done particularly for enabling debugging in webstorm.
WORKDIR /opt/project
COPY ./package.json ./package-lock.json ./.npmrc /opt/project
RUN npm install
Then the webstorm is able to detect everything as expected.
Trick is to update the path from /your-path to /opt/project
And your docker-compose file should look something like this:
version: "3.7"
services:
backend-service:
build:
dockerfile: ./Dockerfile.local
context: ./
command: nodemon app.js
volumes:
- ./:/opt/project
- /opt/project/node_modules/
ports:
- 6060:6060
You can check more details around this in this blog
I have a container ==> FROM node:5
Node should restart after each change in the code.
But there is no way for me restart the Node server without restarting the whole docker container.
I have many npm install on dockerfile that runs each time I restart the container, and it's annoying to wait for all of them after each change in the code.
I'm already using shared folder to have the latest code in my container.
If you just need the Node.js server to restart after changes, instead of starting the server with the node command you can use a tool like node-dev or nodemon. To leverage these, you'd need to update the command in your Dockerfile, e.g. CMD [ "node-dev", "server.js" ].
Since you already have your local files as a volume in the Docker container, any change to those files will restart the server.
Here's how I did it. In your docker-compose.yml, in the entry for the appropriate service, add an entrypoint to run npx nodemon /path/to/your/app
This will work even if you don't have nodemon installed in the image.
e.g.
services:
web:
image: foo
entrypoint: ["npx", "nodemon", "/usr/src/app/app.js"]
I think that's not the optimal way to Docker. You should try to build your own Docker image which includes your code changes. In your own Docker image you could make the npm install step part of the container build so you do not need to run this command on container startup.