I am trying to do a very basic thing with docker and node. I have a client with Nginx and a backend with nodejs.
My problem is that when I tried to run docker-compose up -d
it gives me an ok
but the backend is not running.
I think this is happening due to some problem with the "volumes", because if I get rid of this part in the docker-compose then it works. But I do want to have a volume to save and store all the backend related files.
This is my docker-compose
version: '3.3'
services:
nodeserver:
container_name: nodebackend
build:
context: .
dockerfile: Dockerfile
volumes:
- ${APP_PATH}:/usr/src/app
environment:
TZ: 'Europe/Madrid'
ports:
- "5000:5000"
nginx:
container_name: client
image: nginx:latest
volumes:
- ${APP_PATH}:/var/www/api
- ${NGINX_APIX_LOGS}:/var/log/nginx/
environment:
TZ: 'Europe/Madrid'
ports:
- '8080:80'
And this is my Dockerfile (in the same directory)
# pull the Node.js Docker image
FROM node:12.15.0-alpine
# create the directory inside the container
WORKDIR /usr/src/app
# copy the package.json files from local machine to the workdir in container
COPY package*.json ./
# run npm install in our local machine
RUN npm install --quiet
RUN npm install realm --quiet
# copy the generated modules and all other files to the container
COPY . /usr/src/app
# our app is running on port 5000 within the container, so need to expose it
EXPOSE 5000
# the command that starts our app
CMD ["node", "app.js"]
The directory tree is this:
-rw-r--r-- 1 user staff 27 Dec 29 11:41 .dockerignore
-rw-r--r--@ 1 user staff 231 Dec 29 11:02 .env
-rw-r--r--@ 1 user staff 575 Dec 29 12:46 Dockerfile
drwxr-xr-x 3 user staff 96 Dec 29 12:03 api.files
-rw-r--r-- 1 user staff 592 Dec 29 12:47 docker-compose.yml
drwxr-xr-x 3 user staff 96 Dec 29 11:02 logs
drwxr-xr-x 53 user staff 1696 Dec 29 11:53 node_modules
-rw-r--r-- 1 user staff 31286 Dec 29 11:53 package-lock.json
-rw-r--r-- 1 user staff 338 Dec 29 11:53 package.json
The .env file is this one:
# Application's path (absolute or relative)
APP_PATH=/Users/user/Development/ContactListWebApp/api.files/
# Logs path
NGINX_APIX_LOGS=/Users/user/Development/ContactListWebApp/logs/nginx
If I run docker-compose up -d
I get this message:
Starting client ... done
Recreating nodebackend ... done
But if I run docker ps -a
This is what I have:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
d55463667a3d contactlistwebapp_nodeserver "docker-entrypoint.s…" 24 seconds ago Exited (1) 23 seconds ago nodebackend
4b85504235a3 nginx:latest "/docker-entrypoint.…" 18 minutes ago Up 23 seconds 0.0.0.0:8080->80/tcp client
The package.json files have this information:
{
"name": "contactlistwebapp",
"version": "1.0.0",
"description": "Test application to list contacts on web app linked to iOS app",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "Josman Pérez",
"license": "MIT",
"dependencies": {
"express": "^4.17.1"
}
}
What I am doing wrong? If I comment the volume part in the docker-compose then it runs
Thank you
volumes:
to be a good solution: otherwise you're hiding everything the Dockerfile does, and you're really just runningnode
in a particularly roundabout way. What problems do you run into if you do that?api.files
and I wanted to do the same with the backend files... but it doesn't respond to a problem rather than just learning how to do it