I try to reload my node js app code inside docker container. I use pm2 as process manager. Here is my configurations:
Dockerfile
FROM node:6.9.5
LABEL maintainer "denis.ostapenko2#gmail.com"
RUN mkdir -p /usr/src/koa2-app
COPY . /usr/src/koa2-app
WORKDIR /usr/src/koa2-app
RUN npm i -g pm2
RUN npm install
EXPOSE 9000
CMD [ "npm", "run", "production"]
ecosystem.prod.config.json (aka pm2 config)
{
"apps" : [
{
"name" : "koa2-fp",
"script" : "./bin/www.js",
"watch" : true,
"merge_logs" : true,
"log_date_format": "YYYY-MM-DD HH:mm Z",
"env": {
"NODE_ENV": "production",
"PROTOCOL": "http",
"APP_PORT": 3000
},
"instances": 4,
"exec_mode" : "cluster_mode",
"autorestart": true
}
]
}
docker-compose.yaml
version: "3"
services:
web:
build: .
volumes:
- ./:/koa2-app
ports:
- "3000:3000"
npm run production -
pm2 start --attach ecosystem.prod.config.json
I run 'docker-compose up' in the CLI and it works, I'm able to interact with my app on localhost:3000. But if make some change to code it will not show up in web. How can I configure code reloading inside docker?
P.S. And best practices question: Is it really OK to develop using docker stuff? Or docker containers is most preferable for production use.
It seems that you COPY your code in one place and the volume is in another place.
Try:
version: "3"
services:
web:
build: .
volumes:
- ./:/usr/src/koa2-app
ports:
- "3000:3000"
Then, when you change the js code outside container (your IDE), now the PM2 is able to see the changes, and therefore reload the application (you need to be sure of that part).
Regarding the use of Docker in development environment: it is a really good thing to do because of many reasons. For instance, you manage the same app installation for different environments reducing a lot of bugs, etc.
Related
I'm using docker compose to run nodejs and mongo containers. I'm getting Error: Cannot find module '#babel/preset-typescript' when I'm running the node container from docker-compose, however if I run the image directly with docker, it works like a charm. any ideas?
I'm quite new to docker btw, so if you spot any "non best practices" do point them out as well.
// docker-compose.yml
version: '3'
services:
api-server:
build: .
ports:
- '3000:3000'
volumes:
- .:/home/node/code
- /home/node/code/node_modules
links:
- db
environment:
MONGO_CONNECTION_STRING: mongodb://db:27017
db:
image: mongo:3
// Dockerfile
FROM node:latest
RUN yarn global add nodemon
USER node
RUN mkdir /home/node/code
WORKDIR /home/node/code
COPY --chown=node:node yarn.lock package.json ./
RUN yarn
COPY --chown=node:node . .
CMD ["nodemon", "--exec", "babel-node", "server/index.js"]
// .babelrc
{
"presets": [
"#babel/preset-typescript",
[
"#babel/preset-env",
{
"targets": {
"node": true
}
}
]
],
"plugins": [
"inline-dotenv"
]
}
Thanks!
I am trying to run my Node project as well as the Firestore Emulator with docker-compose locally in a dev environment.
I have a Dockerfile for my Node project that looks like this:
WORKDIR /app
ADD package*.json ./
RUN npm install
ADD bin ./bin
CMD [ "npm", "run", "dev" ]
Then I have a seperate Dockerfile called Dockerfile.firestorefor containerizing the Firestore Emulator. This Dockerfile looks like this:
FROM node:alpine
RUN apk add openjdk11
RUN npm install -g firebase-tools
WORKDIR /app
CMD [ "firebase", "--project=xrechnung-app", "emulators:start", "--only", "firestore" ]
The docker-compose.yml is written in the following way:
version: "3"
services:
api:
image: api
build:
context: api
dockerfile: Dockerfile.dev
depends_on:
- db
environment:
- PORT=3000
ports:
- 3000:3000
volumes:
- ./api/src:/app/src
db:
image: firestore
build:
context: api
dockerfile: Dockerfile.firestore
ports:
- 4000:4000
- 8080:8080
volumes:
- .cache/firebase/emulators/:/app/.cache/firebase/emulators/
I'm not sure about the last two lines but I found a hint in the Google Cloud docs that this could prevent multiple downloads of the emulator.
When spinning the container up with docker-compose up the Node project runs without problem and is available at localhost:3000. Also the Emulator spins up. The console logs that its running. But I can't make it available on the prescribed ports (4000 and 8080)
Did anyone try a similar thing already? I appreciate your help.
You probably need to set the host in the firebase.json file, like this:
{
"emulators": {
"firestore": {
"port": 8080,
"host": "0.0.0.0"
}
}
}
By default, the emulator runs only for localhost.
I'm really doing the best as I can but I don't know what am I doing wrong.
I have this existing project that can be started and debugged both on Win10 and Linux containers. But I want to be able to attach the debugger in WSL container on Win10 and I'm really loosing my patience now.
So, I have VS Code on Win10 and WSL2 installed. I have Remote-WSL extension (also Remote-Containers, but can't get that to work either, so let's skip that for the time being). And I would prefer to keep Dockerfile as-is, if possible.
When I try to attach to node debugger by running "Docker: Attach to Node" configuration, I get the following error:
Error processing attach: Error: Could not connect to debug target at http://localhost:9229: Promise was canceled
at e (/home/ozren_admin/.vscode-server/bin/3dd905126b34dcd4de81fa624eb3a8cbe7485f13/extensions/ms-vscode.js-debug/src/extension.js:1:92915)
Here are my files...
/docker-compose.yml
version: '3'
services:
web:
build:
context: .
dockerfile: ./Dockerfile
env_file: ./myapp.env
depends_on:
- db
volumes:
- "./app:/src/app"
ports:
- "5000:4000"
- "9229:9229"
expose:
- 9229
links:
- db:myapp_mobile_apis_db
db:
image: mysql:5.7
container_name: myapp_mobile_apis_db
restart: always
environment:
# see: https://hub.docker.com/_/mysql
MYSQL_DATABASE: 'myapp'
MYSQL_USER: 'myapp_user'
MYSQL_PASSWORD: 'myapp_user_pwd'
MYSQL_ROOT_PASSWORD: '****************'
ports:
- '3306:3306'
expose:
- '3306'
volumes:
- my-db:/mnt/c/Projects/MyApp/Local/data/
volumes:
my-db:
/Dockerfile
FROM node:alpine
RUN mkdir /src
WORKDIR /src
COPY app/ /src/app/
COPY gulpfile.js /src/
COPY .sequelizerc /src/
ADD app/package.json /src/package.json
RUN npm install --global gulp-cli
RUN npm install
COPY myapp.env /src/.env
EXPOSE 80
CMD gulp prod
/.vscode/launch.json
{
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "attach",
"name": "Docker: Attach to Node",
"port": 9229,
"address": "localhost",
"localRoot": "${workspaceRoot}/app",
"remoteRoot": "/src/app",
"protocol": "inspector"
}
]
}
When I add the following to docker.compose.yml
command: sh -c "npm run start:debug:docker"
I get this on docker-compose up
web_1 | npm ERR! missing script: start:debug:docker
I tried googling for additional info and the idea is probably to run node in the debugging mode, but I obviously can't get it to work.
Any help appreciated!
There was this:
/gulpfile.js
'use strict';
let gulp = require('gulp');
let nodemon = require('gulp-nodemon');
let started = !1;
let paths = {
js: [
'**/*.js',
'!node_modules'
]
};
gulp.task('prod', (e) => {
nodemon({
script: './app/app.js',
ext: 'js',
watch: paths.js,
ignore: ['./app/node_modules/**']
}).on('start', () => {
started || (started = !0, e());
});
});
And when I added this line to nodemon options to enable debug over inspect:
nodeArgs: ['--inspect=0.0.0.0:9229'],
...all of a sudden the debugger started working.
DISCLAIMER: I started my journey to Node.js ecosystem from .Net world, where stuff like remote debugging and other basics usually aren't hidden behind... packages, but eh. Live and learn.
I'm relatively new to docker and I've been having a really strange problem.
The docker setup I have below runs perfectly, however though there seems to be an instance that is always running even after stopping and removing all containers and quitting the docker application.
When I access localhost in my browser, My app is always live and running.
I've tried running docker-compose stop ; docker-compose rm to stop and remove all container.
'docker-compose ps' and 'docker ps' both show no containers running at all. But whenever I access localhost, my app is there live and running.
Like i said i have tried quitting the docker application (I'm running on mac). i tried restarting the machine and the app would still be running.
The weird thing is when i check to see which if any processes are using port 80 (thus making my app accessible via localhost) by running 'sudo lsof -i tcp:80' the list is empty.
I'm new to docker and I know there must be something I'm overlooking.
Thanks in advance, any help and ideas are welcomed.
Here is my folder structure: screenshot
The Dockerfile for my app:
FROM node:10
WORKDIR /usr/src/app
COPY package*.json ./
RUN npm install
COPY . .
RUN npm run build
EXPOSE 3000
ENV NUXT_HOST=0.0.0.0
ENV NUXT_PORT=3000
CMD [ "npm", "start" ]
docker-compose.yml
version: '3'
services:
nuxt:
build: ./app/
container_name: nuxt
restart: always
ports:
- '1880:1880'
command: 'npm run start'
nginx:
image: nginx:1.13
container_name: nginx
ports:
- '80:80'
volumes:
- ./nginx:/etc/nginx/conf.d
depends_on:
- nuxt
I'm trying to attach the Visual Studio Code debugger to a node.js app that is running inside a Docker container.
I start the app like:
node --debug-brk app.js
I expose the debugger port in docker-compose.yml:
app:
build: .
working_dir: /code
volumes:
- .:/code
command: npm run debug
ports:
- "3004:3000"
- "5858:5858"
My launch.json looks like:
{
"version": "0.1.0",
"configurations": [
{
"name": "Attach",
"type": "node",
"address": "localhost",
"port": 5858
}
]
}
Now, when I start the application and attach the debugger this will correctly connect (I can see the values flashing in the debugger UI already), but then it will stop, telling me the following:
Error opening 'app.js' (File not found: /code/app.js).
This is due to the fact that docker will not mount the app in root but in /code (see volumes in docker-compose.yml) and VS code is confused by the sudden offset.
When I run the application outside the container (i.e. locally, without offset) it works just as expected and I can use the debugger as expected.
There seems to be a cwd option for the launch configuration but I am not sure if that makes any difference in my case.
Can I fix this path offset? Am I missing something else here?
This feature is now officially supported by VSCode: https://github.com/Microsoft/vscode-node-debug/issues/8
I think your debugger is being tricked because your app path inside docker is /code and on your computer it is something different.
Maybe something like /home/m90/code.
So when your debugger tries to look at your code on your local machine. It looks at /code which exists only inside of docker. This path makes no sense outside of docker.
If you could tell your debugger to look for you code at the correct place on your machine (again outside of the docker container) that would fixed it.
Another way would be to have the same path for your code inside and outside of docker.
Try this modified version of docker-compose.yml. (with copy /code directory in docker to your host directory /code)
docker-compose.yml
app:
build: .
working_dir: /code
volumes:
- /code:/code
command: npm run debug
ports:
- "3004:3000"
- "5858:5858"
only changed one line: /code:/code instead of .:/code
You could try bind-mounting your current directory on /code so that VS can find your source code there: sudo mount --bind . /code
For me it didn't work until I used inspect: node --inspect=5858 app.js