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!
Related
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
Picking up docker, a little late to the show, but better late than never.
Following a few online tutorials i landed at a docker file and docker-compose for my 1st microservice node+mongo.
Terrible setup for dev so now will implement trusty pm2: https://dev.to/itmayziii/step-by-step-guide-to-setup-node-with-docker-2mc9
Production would want the below, but dev i would want pm2 instance mgr to reboot on file change..
But the obvious question i now have is how to differentiate between dev and prod in the Dockerfile?
Dockerfile
FROM node:12-alpine
RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app
COPY package.json /usr/src/app/
RUN npm i
COPY . /usr/src/app
EXPOSE 3000
CMD node ./build/server.js
docker-compose
version: "3"
services:
ms-authentication-service:
image: "ms-authentication-image"
depends_on:
- mongodb
build:
dockerfile: Dockerfile
context: .
links:
- mongodb
networks:
- default
ports:
- "8080:8000"
restart: always
mongodb:
image: mongo:4.2
container_name: "ms-authentication-mongo-image"
environment:
MONGO_INITDB_ROOT_USERNAME: bob
MONGO_INITDB_ROOT_PASSWORD: bob
networks:
- default
ports:
- 27017:27017
In general, managing environment like the staging or production based on ENV is common practice, but in the case of Docker the best approach is tag.
It's better to use tag for dev, stage and production in case of Docker. There are many reasons, one reason is mount code in development environment is fine but it is not recommended in the production environment.
When building images, always tag them with useful tags which codify
version information, intended destination (prod or test, for
instance), stability, or other information that is useful when
deploying the application in different environments. Do not rely on
the automatically-created latest tag.
Docker App development best-practices
But if still want to go with ENV approach then you can use the docker-entrypoint script.
Dockerfile
FROM node:alpine
RUN npm install pm2 -g
COPY . /app
WORKDIR /app
ENV NODE_ENV=development
RUN chmod +x docker-entrypoint.sh
ENTRYPOINT ["sh","docker-entrypoint.sh"]
Docker-entrypoint
#!bin/sh
if [ $NODE_ENV = development ]; then
pm2 start server.js
else
node server.js
fi
So you are good to go and you will able to change this in Dockerfile or run time
docker run --env NODE_ENV=production -it --rm node:production
or
docker run --env NODE_ENV=development -it --rm dev
I have an api in nodeJs, which run under micro-services.
So far it work, what i want to do next is to be able to run unit-test (mocha) on those images.
docker-compose.yml:
version: '3'
services: db:
image: mongo
ports:
- 27017:27017
db-seeds:
build: ./services/mongo-seeds
links:
- db
gateway:
build: ./services/gateway
ports:
- 4000:4000
depends_on:
- A
- B
- C
A:
build: ./services/A
ports:
- 4003:4000
depends_on:
- db
B:
build: ./services/B
ports:
- 4001:4000
depends_on:
- db
C:
build: ./services/C
ports:
- 4002:4000
depends_on:
- db
One of a DockerFile:
FROM node:latest
COPY . /src
WORKDIR /src
RUN yarn
EXPOSE 4000
CMD yarn start
What i did so far is to make another docker-compose file which will run other docker file (DockerFile.test) :
FROM node:latest
COPY . /src
WORKDIR /src
RUN yarn
EXPOSE 4000
CMD yarn test
also tried:
FROM node:latest
COPY . /src
WORKDIR /src
RUN yarn
EXPOSE 4000
CMD yarn start
RUN sleep 240
CMD yarn test
They both fail, at this point yarn test is launch before my gateway and servers are up. What i want to do, is to launch my servers and then run my unit-test in the images, but i'm new to docker and lack knowledge on how to implement that.
yarn test:
"test": "NODE_ENV=test ./node_modules/.bin/mocha"
You have to run mocha on the same host with your docker-compose file. This can be a docker image too (which can be started with docker-compose if you want ;)
You have to run docker-compose with your services before you run yarn test.
Create/Use a Dockerfile with docker and docker-compose and node (for your case)
copy your sources on it (docker-compose.yml and other relevant sources)
run docker-compose up -d on it
run yarn test on it
Tools like CloudFoundry or OpenStack might do this job.
If you are with CloudFoundry I can give you concrete details.
To concatenate several commands use the unix-cli syntax:
RUN docker-compose up -d && \
service1 && \
yarn test
I'm having problems configuring docker for my nodejs app.
I have previously set up containers for both php and rails with port forwarding working flawlessly, but for this instance i can't seem to get it to work.
Running: docker ps, i get the following:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a60f9c82d600 29c7d94a8c58 "/bin/sh -c 'npm s..." 5 seconds ago Up 3 seconds 3000/tcp romantic_albattani
As you can see I'm not getting the usual: 0.0.0.0:3000->3000/tcp that I am expecting.
docker-compose ps gives:
Name Command State Ports
------------------------------
My docker-compose.yml:
web:
build: .
volumes:
- .:/app
volumes_from:
- box
ports:
- "3000:3000"
box:
image: busybox
volumes:
- /node_modules
My Docker file:
FROM node:8.7.0
# The base node image sets a very verbose log level.
ENV NPM_CONFIG_LOGLEVEL warn
WORKDIR /tmp
COPY package.json /tmp/
RUN npm install
WORKDIR /app
ADD . /app
RUN cp -a /tmp/node_modules /app/
#ENV PORT=3000
EXPOSE 3000
CMD npm start
I'm running the command: docker-compose up --build
Any help at this point is appreciated.
I don't know if a docker inspect would be useful, but if so, tell me and i will also post it.
Edit: Changed my Dockerfile to follow the answer.
Your docker-compose.yml file has bad formatting, since you are not getting any errors i will assume you pasted it here wrong, here is the version with the fixed indenting:
web:
build: .
volumes:
- .:/app
volumes_from:
- box
ports:
- "3000:3000"
box:
image: busybox
volumes:
- /node_modules
Your Dockerfile has a bug, you are missing the ENTRYPOINT and/or CMD stanzas, instead you are using the RUN stanza with the wrong intent, here is a working Dockerfile with the fix applied:
FROM node:8.7.0
# The base node image sets a very verbose log level.
ENV NPM_CONFIG_LOGLEVEL warn
WORKDIR /tmp
COPY package.json /tmp/
RUN npm install
WORKDIR /app
ADD . /app
RUN cp -a /tmp/node_modules /app/
#ENV PORT=3000
EXPOSE 3000
CMD npm start
Your Dockerfile halted the execution of docker-compose at the docker image building stage because of the RUN npm start which is a process that starts and listens until stopped (because you want it to start your node app and listen for connections) causing docker-compose to never finish the docker image creating step, let alone the other steps like creating the needed containers and finish the entire docker-compose runtime process.
In short:
When you use RUN it is meant to run a command do some work and return sometime to continue the building process, it should return and exit code of 0 and the process will move on to the next Dockerfile stanza, or return another exit code and the building process will fail with an error.
When you use CMD you tell the docker image what is the starting command of all the containers started from this image (it can also be overridden at run time with docker run). It is tightly related to the ENTRYPOINT stanza, but for basic usage you are safe with the default.
Further reading: ENTRYPOINT, CMD and RUN
I have a web application written in Node.js that I'm trying to get into Docker. Running the image with docker run -p 80:80 image works just fine; I'm able to access the webpage it's hosting. However, when I try to run it in a stack, I'm unable to access the page, and Chrome just sits "Waiting for localhost..." forever.
Dockerfile:
FROM readytalk/nodejs
WORKDIR /app
ADD . /app
RUN npm i
EXPOSE 80
CMD []
ENTRYPOINT ["/nodejs/bin/npm", "start"]
docker-compose.yml:
version: "3"
services:
web:
image: image_name
deploy:
replicas: 5
resources:
limits:
cpus: "0.1"
memory: 50M
restart_policy:
condition: on-failure
ports:
- "80:80"
networks:
- webnet
networks:
webnet:
Any help would be greatly appreciated.
EDIT: Added some logging and it seems that the HTTP request is never actually making it to the Node.js app. It seems like Docker has recieved the request, but hasn't routed it to the running app.
Some time your docker container run your IP address.
you can check it by running this command docker info
second option
Go to terminal and write
in window
ipconfig
and see preferred IP then access your container with that IP with specifying port
in Ubuntu
ifconfig
Hope this will solve your problem and try to access using 127.0.0.1:port
You can check this slide which shows and run hello world node+docker
docker-node-hello-world-application
And I will recommend using this Docker file.
Node_DockerFile
FROM alpine
RUN apk update && apk upgrade
RUN apk add nodejs
RUN mkdir -p /app
ADD app/package.json /app
WORKDIR /app/
ENV HOME /app
ENV NODE_ENV development
RUN npm install
ADD app /app
EXPOSE 3000
CMD npm start