docker-compose failed on friend's linux computer - node.js

I'm starting to learn Docker, and i'm following this (very) long tutorial: https://www.youtube.com/watch?v=9zUHg7xjIqQ
I have a Dockerfile and a docker-compose.yml .
It work fine on my fine on my machine (we all know this sentence), but when my friend pulled what i've done, he obtains this error message:
Error response from daemon: invalid mount config for type "volume": invalid mount path: 'node_back/node_modules' mount path must be absolute
Here are my files:
Dockerfile in node_back/ :
WORKDIR /app
COPY package.json .
RUN npm install
COPY . ./
EXPOSE 3000
CMD ["npm", "run", "dev"]
docker-compose at the root:
services:
back-container:
build: ./node_back
image: back-image:tag
ports:
- "3000:3000"
volumes:
- ./node_back:/app
- ./node_back/node_modules
environment:
- PORT=3000
I used the command:
sudo docker-compose up -d
to launch the docker script.
Can somebody explain my error, and why it work on my side?
Thanks!

Because you forgot ':' on volume path line.
./node_back/node_modules
you should be used ':' for separate your local volume and docker volume
./node_back/node_modules:WHERE-YOUR-WANT-DOCKER-PATH

Related

Update docker container packages in a node app

I have a issue.
I'm using docker to host my node apps while I'm studying.
It runs normally when build for first time, but, if I add a new package it not been installed in host.
So the question is, i have to run "docker-compose up --build" every time I install new package or change my package.json?
Here is an example of my Dockerfile and docker-compose.yml
Dockerfile
FROM node:alpine
WORKDIR /home/app_node
COPY package.json ./
RUN npm install
COPY . .
EXPOSE 3333
CMD ["npm", "start"]
docker-compose.yml
version: "3"
services:
app:
build: .
image: testeeee
command: npm start
ports:
- "3333:3333"
volumes:
- .:/home/app_node
- /home/app_node/node_modules

Docker compose does not work if node_modules are not installed on local machine

My goal is to create docker dev environment for a full-stack app: React, NodeJS and MongoDb (with hot reloading). My current dev environment works, but I noticed that "docker compose up" will only work, if the node_modules are installed on my local machine - it otherwise returns an error that nodemon is not installed, or react-scripts not found. It seems like docker is looking for the node files in my local machine, but it should be installing them in the container during the compose, right?
docker-compose.yml
version: '3.8'
services:
server:
build: ./server
container_name: server_backend
ports:
- '7000:7000'
volumes:
- ./server:/app
client:
build: ./client
container_name: client_frontend
ports:
- '3000:3000'
volumes:
- ./client:/app
stdin_open: true
tty: true
Dockerfile (backend server)
FROM node:16-bullseye-slim
WORKDIR /app
COPY package.json .
COPY package-lock.json .
RUN npm install
COPY . .
EXPOSE 7000
CMD ["node", "index.js"]
Dockerfile (frontend)
FROM node:16-bullseye-slim
WORKDIR /app
COPY package.json .
RUN npm install
COPY . .
EXPOSE 3000
CMD ["npm", "start"]
app architecture
- MyApp
-- client
-- server
Can it be that you are copying the whole directory with the node_modules when you execute the COPY . . command, try and either specifiy the directory to copy or add a .dockerignore.
Found this in the documentation, https://docs.docker.com/language/nodejs/build-images/#create-a-dockerignore-file

Docker network communication issue with multiple node server container-intercommunication

I'm trying to setup a docker environment in which multiple containers communicate with each other via REST. My simplified system architecture looks somewhat like this:
Let's say I have two services foo and bar, that run on a node server (NestJS projects). The Dockerfile for both look something like this:
FROM node:14-alpine
ENV NODE_ENV=production \
NPM_CONFIG_PREFIX=/home/node/.npm-global \
PATH=$PATH:/home/node/.npm-global/bin:/home/node/node_modules/.bin:$PATH
RUN apk add --no-cache tini
RUN apk add iputils
RUN mkdir -p /usr/src/app/node_modules
RUN chown -R node:node /usr/src/app
USER node
WORKDIR /usr/src/app
COPY --chown=node:node package*.json ./
RUN npm ci --only=production
RUN npm cache clean --force
COPY --chown=node:node . ./
RUN ls -l
EXPOSE 80
ENTRYPOINT ["/sbin/tini", "--"]
CMD [ "npm", "start" ]
The docker-compose looks like this:
version: "3.8"
services:
foo:
image: "node:14-slim"
container_name: foo-service.${ENV}
build:
context: .
dockerfile: Dockerfile
args:
env: ${ENV}
port: ${PORT}
user: "node"
working_dir: /usr/src/app
environment:
- NODE_ENV=production
- VERSION=1.0
volumes:
- .:/usr/src/app
- /usr/app/node_modules
ports:
- ${PORT}:80
tty: true
command: "npm start"
networks:
my-network:
aliases:
- foo-service.${ENV}
networks:
my-network:
driver: "bridge"
name: my-network
I connect the containers via the my-network and made sure (docker network inspect my-network) that both containers share the same network. I can even ping one another (docker exec [foo] ping [bar])
When I'm running the application (making a REST call from the web interface) to foo, foo is then unable to "connect" to bar. I call bar from foo using the alias like this:
this.httpService.get('http://bar-service.dev:3002/...').
I get this error message:
I guess the containers still know each other since the DNS is resolved automatically by docker (I made sure that the IP-Address of bar is correct).
After some hours of trial and error and research I'd like to ask you guys. It might be an issue with alpine (some people had issues pinging node servers before). But it's also just as likely that I missed something important all along and can't seem to realize it...
Thanks in advance!

Docker react express ap not running after one process run

Hi Dockerized a reactjs and expressjs project, everything is worked good when i have written separate docker compose file.
But now i written one compose file
docker-compose-all-dev.yml file
version: '3.7'
services:
client:
container_name: react-dev
build:
context: ./client
dockerfile: Dockerfile.react-dev
ports:
- 3000:3000
server:
container_name: server-dev
build:
context: ./server
dockerfile: Dockerfile.server-dev
ports:
- 5000:5000
Now it's running client server only, why not running backend server?
But it works when i run it in two different files like this.
docker-compose-client.yml file:
version: '3.7'
services:
client:
container_name: react-dev
build:
context: ./client
dockerfile: Dockerfile.react-dev
ports:
- 3000:3000
and docker-compose-server.yml file
version: '3.7'
services:
server:
container_name: server-dev
build:
context: ./server
dockerfile: Dockerfile.server-dev
ports:
- 5000:5000
Can anyone tell me what is the possible issue of not running the both app when i run in one compose file? how can i solve it?
For your reference.
My Dockerfile-server-dev file
FROM node:14
WORKDIR /usr/src/app
COPY package*.json ./
RUN npm install
COPY . .
EXPOSE 8080
CMD [ "node", "server.js" ]
and my Dockerfile.react-dev file
FROM node:14.1-alpine as build
WORKDIR /app
COPY . /app
ENV PATH /app/node_modules/.bin:$PATH
RUN yarn config delete proxy
Run npm config rm proxy
RUN npm config rm https-proxy
RUN npm install
RUN npm start
I dont know what is the issue actually running two development server in one docker-compose file
There are at least few problems, your Dockerfile.react-dev is missing entrypoint and CMD parts, you should not start your server with RUN clause. Instead use Entrypoint and possibly CMD for starting it. Another problem is, that you are exposing different port on Dockerfile-server-dev than on your compose file.
solved the issue.
I just add this line: CMD ["npm", "start"] and removed npm start now it is working

Avoid restarting docker when code is changed

I'm using docker for the first time, so it could be a stupid question for you.
I configured: dockerfile and docker-compose.
Every time i change my code ( nodejs code ), I need to run the command:
docker-compose up --build
If i want to see the changes, I want to know if there is a way to update my code without have to run that command every time.
Thanks.
Dockerfile:
FROM node:latest
RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app
COPY package.json /usr/src/app/
RUN npm install
COPY . /usr/src/app
ENV NODE_PATH=/app/node_modules
EXPOSE 1337
CMD ["npm", "start"]
docker-compose:
version: '3'
services:
node_api:
container_name: app
restart: always
build: .
ports:
- "1337:1337"
links:
- mongo
mongo:
container_name: mongo
image: mongo:latest
environment:
- MONGO_INITDB_ROOT_USERNAME=admin
- MONGO_INITDB_ROOT_PASSWORD=admin
- MONGO_INITDB_DATABASE=test
ports:
- "27017:27017"
restart: always
You can mount the directory containing your source code in the container and use a tool such as nodemon, which will watch files and restart the application on changes.
See the article Docker Tips : Development With Nodemon for details.

Resources