The Dockerfile being used:
FROM node:8-alpine
WORKDIR /usr/src/app
COPY . .
RUN npm install
CMD ["npm", "run", "serve"]
EXPOSE 8080
And the docker-compose.yml file:
version: '3'
services:
app:
container_name: app
restart: always
build:
context: ./app
dockerfile: Dockerfile
ports:
- "8080:8080"
volumes:
- ./app:/usr/src/app
- ./logs:/logs
The folder structure is the following:
project/
|-- docker-compose.yml
|-- logs/
|-- app/
|-- Dockerfile
|-- package.json
When running docker-compose up --build from project/, the npm install step outputs the following after about one minute:
added 1684 packages from 1297 contributors and audited 36429 packages in 56.23s
found 0 vulnerabilities
However, at the npm run serve step, the output basically consists in saying that no npm module can be found, and among other things, this line:
npm WARN Local package.json exists, but node_modules missing, did you mean to install?
How comes npm install is actually and definitely executed, but npm complains that node_modules cannot be found?
I had the same problem and I solved it just following this instruction. Add one line of code - /usr/src/app/node_modules to the docker-compose.yml file in the volumes:
volumes:
- ${PWD-.}/name_of_your_app:/usr/src/app
- /usr/src/app/node_modules
Update: I just ended up using only ./app/src folder as a volume, instead of ./app.
This way, /app/node_modules is not overridden by the host's volume.
version: '3'
services:
app:
container_name: app
restart: always
build:
context: ./app
dockerfile: Dockerfile-dev
ports:
- "8080:8080"
volumes:
- ./app/src:/usr/src/app/src # <---- this
- ./logs:/logs
Related
I'm new to docker and I've been trying to create a custom node-red image with custom flow for influxdb. Docker doesn't seem to use my dockerfile for the image creation.
This is my docker-compose:
node-red:
container_name: node-red
build: node-red
environment:
- TZ=Europe/Amsterdam
ports:
- "1880:1880"
volumes:
- node-red-data:/tmp/node-red_data
networks:
- node-red-net
Then, inside a folder called node-red I have this dockerfile:
FROM nodered/node-red AS base
COPY package.json .
RUN npm install --only=production
COPY nodered_flow.json /data/flows.json
CMD ["npm", "start"]
Both the package.json and the nodered_flow.json are in the same folder as the dockerfile. What am I doing wrong here?
In service.build, you need the path to the build context, in case the it is in the same directory you could use .. So this should work:
node-red:
container_name: node-red
build: .
environment:
- TZ=Europe/Amsterdam
ports:
- "1880:1880"
volumes:
- node-red-data:/tmp/node-red_data
networks:
- node-red-net
For more information check: https://docs.docker.com/compose/compose-file/compose-file-v3/#build
Your Dockerfile is wrong, it's putting your package.json in /usr/src/node-red and when you run npm install it will remove Node-RED.
It should look like this:
FROM nodered/node-red AS base
WORKDIR /data
COPY package.json /data
COPY nodered_flow.json /data/flows.json
RUN npm install --only=production
WORKDIR /usr/src/node-red
I want to build my next js project by docker tool, but I got some trouble like this:
Error: Could not find a production build in the '/var/app/.next' directory. Try building your app with 'next build' before starting the production server. https://nextjs.org/docs/messages/production-start-no-build-id
Dockerfile:
FROM node:16-alpine
RUN mkdir -p /var/app
COPY ["./", "/var/app"]
WORKDIR /var/app
RUN npm i -g next
EXPOSE 3002
RUN npm run build
CMD ["npm", "run", "start"]
docker-compose.yml
version: '3.3'
services:
next-project:
container_name: next-project
build: ./
working_dir: /var/app
restart: 'unless-stopped'
volumes:
- ./:/var/app
env_file:
- .env
ports:
- "54000:3002"
I do run commands like this
docker-compose build && docker-compose up -d
the build was successful but when it run is failed, is there any missing configuration?
When you map your current directory to /var/app, all the files that are in that directory in the container become hidden and replaced with the files in the current directory.
Since you don't have a .next directory in the host directory, the container can't find the built files.
To get it to run, you need to remove the mapping of the current directory, so your docker-compose file becomes
version: '3.3'
services:
next-project:
container_name: next-project
build: ./
working_dir: /var/app
restart: 'unless-stopped'
env_file:
- .env
ports:
- "54000:3002"
I am very new to Docker so please forgive me, but I have a working dockerfile and docker-compose when the Main.go is at root-level but in this project the app will break if I put main.go at root.
File Structure
queue-backend
- .idea
- cmd
- appCode
- handler.go
- helper.go
- main.go
- routes.go
- pkg
- forms
- models
- mongodb
- models.go
- tmp
.gitignore
docker-compose.yml
Dockerfile
go.mod
README.md
db
extensions
Anyway... my dockerfile looks like this
FROM golang:1.16
WORKDIR /app
COPY go.mod .
COPY go.sum .
RUN go mod download
COPY ../.. .
RUN curl -sSfL https://raw.githubusercontent.com/cosmtrek/air/master/install.sh | sh -s -- -b $(go env GOPATH)/bin
CMD ["air"]
Docker-compose.yml looks like
version: '3.9'
services:
backend:
build: ""
ports:
- 8000:8000
volumes:
- .:/app
depends_on:
- mongodb_container
mongodb_container:
image: mongo:latest
environment:
MONGO_INITDB_ROOT_USERNAME: queue-delivery
MONGO_INITDB_ROOT_PASSWORD: password
ports:
- 27017:27017
volumes:
- mongodb_data_container:/data/db
volumes:
mongodb_data_container:
I have tried to set WORKDIR to /app/cmd/appCode or /cmd/appCode and matching the same in the docker-compose to .:/app/cmd/appCode and .:/cmd/appCode which none work, it always returns this, or the stated paths above instead of just the '/app' path
backend_1 | no Go files in /app
backend_1 | failed to build, error: exit status 1
At this point, I'm not sure what else to try...
To resolve Dockerfile in docker-compose.yml you need to change the build section as below
version: '3.9'
services:
backend:
build:
context: .
dockerfile: Dockerfile
ports:
- 8000:8000
volumes:
- .:/app
depends_on:
- mongodb_container
mongodb_container:
image: mongo:latest
environment:
MONGO_INITDB_ROOT_USERNAME: queue-delivery
MONGO_INITDB_ROOT_PASSWORD: password
ports:
- 27017:27017
volumes:
- mongodb_data_container:/data/db
volumes:
mongodb_data_container:
Your Dockerfile has some issues,
FROM golang:1.16
WORKDIR /app
# File changes must be added at the very end, to avoid the installation of dependencies again and again
RUN curl -sSfL https://raw.githubusercontent.com/cosmtrek/air/master/install.sh | sh -s -- -b $(go env GOPATH)/bin
COPY go.mod .
COPY go.sum . # can not find this file in the directory structure
RUN go mod download
COPY ../.. . # doesn't make sense, just use COPY . .
CMD ["air"]
This is the docker-compose file I have already set the folder to share by Virtual Box VM but it is still not working.
version: '3'
services:
postgres:
image: 'postgres:latest'
deploy:
restart_policy:
condition: on-failure
window: 15m
redis:
image: 'redis:latest'
nginx:
restart: always
build:
dockerfile: Dockerfile.dev
context: ./nginx
ports:
- '3050:80'
api:
build:
dockerfile: Dockerfile.dev
context: ./server
volumes:
- /usr/src/app/node_modules
- ./server:/usr/src/app
environment:
- REDIS_HOST=redis
- REDIS_PORT=6379
- PGUSER=postgres
- PGHOST=postgres
- PGDATABASE=postgres
- PGPASSWORD=postgres_password
- PGPORT=5432
client:
build:
dockerfile: Dockerfile.dev
context: ./client
volumes:
- /usr/src/app/node_modules
- ./client:/usr/src/app
worker:
build:
dockerfile: Dockerfile.dev
context: ./worker
volumes:
- /usr/src/app/node_modules
- ./worker:/usr/src/app
I am running it on Windows 7 sp1. Whenever I run docker-compose up - I get an error:
api_1 | npm ERR! code ENOENT
api_1 | npm ERR! syscall open
api_1 | npm ERR! path /usr/src/app/package.json
api_1 | npm ERR! errno -2
api_1 | npm ERR! enoent ENOENT: no such file or directory, open '/usr/src/
app/package.json'
api_1 | npm ERR! enoent This is related to npm not being able to find a fi
le.
api_1 | npm ERR! enoent
api_1 |
api_1 | npm ERR! A complete log of this run can be found in:
api_1 | npm ERR! /root/.npm/_logs/2020-05-28T04_06_56_121Z-debug.log
complex_api_1 exited with code 254
Thanks in advance, please help.
I am trying to run a Fibonacci project from the Udemy course of Docker and Kubernetes complete guide.
Each service has its own package.json and other files.
Server Docker File :
FROM node:alpine
WORKDIR /usr/src/app
COPY package.json .
RUN npm install
COPY . .
CMD ["npm", "run", "dev"]
Worker Docker File :
FROM node:alpine
WORKDIR /usr/src/app
COPY package.json .
RUN npm install
COPY . .
CMD ["npm", "run", "dev"]
Client Docker File :
FROM node:alpine
WORKDIR /usr/src/app
COPY package.json .
RUN npm install
COPY . .
CMD ["npm", "run", "start"]
If you want to share data between containers
services:
client:
build:
dockerfile: Dockerfile.dev
context: ./client
volumes:
- datavolume:/usr/src/app/node_modules
- ./client:/usr/src/app
worker:
build:
dockerfile: Dockerfile.dev
context: ./worker
volumes:
- datavolume:/usr/src/app/node_modules
- ./worker:/usr/src/app
volumes:
datavolume: {}
Since it looks like your dev, I would suggest mount your workspace folder into container
services:
client:
build:
dockerfile: Dockerfile.dev
context: ./client
volumes:
- ./node_modules:/usr/src/app/node_modules
- ./client:/usr/src/app
worker:
build:
dockerfile: Dockerfile.dev
context: ./worker
volumes:
- ./node_modules:/usr/src/app/node_modules
- ./worker:/usr/src/app
And better way is treating every service a standalone project. Each of them should own their self package.json and node_modules.
services:
client:
build:
dockerfile: Dockerfile.dev
context: ./client
volumes:
- ./client:/usr/src/app
worker:
build:
dockerfile: Dockerfile.dev
context: ./worker
volumes:
- ./worker:/usr/src/app
In my opinion, it doesn't make sense to use same libraries in different project which in different purpose.
I had the same error! Actually I solved moving my project to /c/Users/currentUser from c/Program Files/Docker Toolbox. Maybe you have your project folder inside Program Files directory and not inside Users one, is It right? Try with this, Just Copy your project folder inside users and running your docker-compose from there. Let me know!
Can you help with running gulp tasks inside the Docker container with docker-compose, so it would compile SCSS files?
My file structure at host machine:
/application
--/config
--/models
--/public
----/scss
----/css
----index.html
----gulfile.js
--/routes
.dockerignore
.gitignore
Dockerfile
docker-compose.yml
package.json
server.js
/application/public/gulpfile.js:
var gulp = require('gulp');
var sass = require('gulp-sass');
gulp.task('sass', function() {
gulp.src('./scss/styles.scss')
.pipe(sass().on('error', sass.logError))
.pipe(gulp.dest('./css'));
});
gulp.task('sass:watch', function() {
gulp.watch('./scss/styles.scss', ['sass']);
});
Dockerfile:
FROM node:6
RUN mkdir -p /app
WORKDIR /app
COPY . /app
RUN npm install nodemon -g && npm install bower -g && npm install gulp -g
RUN cd /app
RUN npm install
RUN cd /app/public && bower install --allow-root
RUN cd /app
COPY . /app
docker-compose.yml:
version: '2'
services:
web:
build: .
command: nodemon /app/server.js
volumes:
- .:/app/
- /app/node_modules
- /app/public
- /app/config
- /app/models
- /app/routes
ports:
- "3000:3000"
depends_on:
- mongo
mongo:
image: mongo:latest
ports:
- "27018:27017"
restart: always
Ideally, it would be good to run the command from the docker-compose.yml file to start watching scss files inside the /application/public folder, but I wasted couple of days to solve this problem.
Also, I tried to run gulp inside the container. Actually, it works ok, but changes were not reflected at host machine.
Please do not suggest to use ready-made Docker-Hub images. I have used them and they did not solve my issue.
I'll be thankful for any help, links, info or ready-solution.
You can add a separate service in your compose file to run a separate command in a separate container (see the web line & the gulp service line below
version: '2'
services:
web: &web
build: .
command: nodemon /app/server.js
volumes:
- .:/app/
- /app/node_modules
- /app/public
- /app/config
- /app/models
- /app/routes
ports:
- "3000:3000"
depends_on:
- mongo
gulp: *web
command: <gulp command to watch files>
mongo:
image: mongo:latest
ports:
- "27018:27017"
restart: always