I have just started learning docker-compose and I am using a nodejs image. I want to install gulp to create some tasks and have one of them working on the background.
When I run: docker-compose run --rm -d server gulp watch-less
I get this error: ERROR: oci runtime error: container_linux.go:247: starting container process caused "exec: \"gulp\": executable file not found in $PATH"
Here are my file:
# Dockerfile
FROM node:6.10.2
RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app
COPY package.json /usr/src/app
RUN npm install --quiet
COPY . /usr/src/app
CMD ["npm", "start"]
# docker-compose.yml
version: "2"
services:
server:
build: .
ports:
- "5000:5000"
volumes:
- ./:/usr/src/app
I also have a .dockerignore to ignore the node_modules folder and the npm-debug.log
EDIT:
When I run docker-compose run --rm server npm install package-name I don't have any problem and the package is installed.
Try adding gulp install in Dockerfile:
# Dockerfile
FROM node:6.10.2
RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app
RUN npm install -g gulp
COPY package.json /usr/src/app
RUN npm install --quiet
COPY . /usr/src/app
CMD ["npm", "start"]
I have found a solution that works but maybe is not the best solution. I have created an script that runs a gulp task on the package.json and if I run:
docker-compose run --rm server npm run gulp_task it works and does what it has to do.
I find that just referencing gulp via
./node_modules/.bin/gulp
instead of directly works fine. ./node_modules/.bin isn't in the path by default. Another option would be to add that dir to the PATH.
Related
I am using Docker to run my React universal application using docker-compose as an control tool. Unfortunately, after webpack successfully creates the bundles my pm2 inside docker can't find the script and my container exits with error.
What is the best way to debug this or solve the problem?
Dockerfile
FROM node:erbium
WORKDIR /app
RUN npm install pm2 -g
RUN npm install bower -g
COPY package*.json /app/
RUN npm install
COPY . /app/
RUN bower install
RUN npm run build
EXPOSE 8080
CMD ["pm2-runtime", "start", "ecosystem.config.js", "--env", "production"]
docker-compoe service
client:
container_name: client_folder
image: client_image
build: ./finstead-client
volumes:
- ./client_folder:/app
- /app/node_modules
restart: "always"
ports:
- "4000:8080"
the application is inside folder client_folder and I use volumes to attach my files to the docker container. But In my opinion it doesn't attach volumes after buildtime and on runtime it can't find the files.
I would appreciate a lot if you could give me advice or help me find out the root of the problem.
I'm trying to turn this nodeJS video game into a docker container:
https://github.com/navignaw/TooManyChefs
But I'm getting "ERR_EMPTY_RESPONSE" when I try to access the started docker container:
http://localhost:3000/
I think the issue is my dockerfile:
# pull official base image
FROM node:13.12.0-alpine
# set working directory
WORKDIR /usr/src/app
ENV PATH /app/node_modules/.bin:$PATH
COPY package*.json ./
RUN npm install
COPY . ./
EXPOSE 3000
CMD ["npm", "start"]
Docker commands:
docker build -t hal/chefs .
docker run -p 3000:3000 -d hal/chefs
Docker logs:
docker logs 92928695b528a7ed4059bcc32af1d58a309f855294b48d49c60a2bb977755c4e
> TooManyChefs# start /usr/src/app
> watchify -o js/bundle.js -v -d js/main.js
Can someone please give advice how I can fix or troubleshoot? I really appreciate your help!
-Hal
change the following line:
ENV PATH /app/node_modules/.bin:$PATH
to :
ENV PATH /usr/src/app/node_modules/.bin:$PATH
Edit-01:
Also update your npm install command to following:
npm install --no-optional && npm cache clean --force
I'm trying to get docker-compose working on my Windows 8 box. I have the following docker-compose file
version: '3'
services:
testweb:
build: .
command: npm run install
volumes:
- .:/usr/app/
working_dir: /app
ports:
- "3000:3000"
However when I run this using docker-compose I get an error saying cannot find package.json. I know this is something to do with how the paths are mapped. So I moved my folder to c:\users and tried with the same issue. I then moved to c:\users\ and tried, ended up with the same issue. The mapping on my virtual box is as follows
Does anyone know how to fix this?
Attached is my Dockerfile
FROM node:7.7.2-alpine
WORKDIR /usr/app
RUN apk update && apk add postgresql
COPY package.json .
RUN npm install --quiet
COPY . .
It may be because you set the working directory to /app in your docker-compose file, but your package.json is in /usr/app. So the container runs npm run install from the wrong directory.
For a docker container to stay up you need to have a running process. In this case its the command npm start which will keep running as long as the container is up. So you need to have a cmd or Entrypoint that will start a running process. Please try something like this. Let me know if you have any questions
FROM node:7.7.2-alpine
WORKDIR /usr/app
RUN apk update && apk add postgresql
COPY package.json .
RUN npm install --quiet
COPY . .
EXPOSE 8080
CMD [ "npm", "start" ]
Put file docker-compose.yml in project/app directory.
Then run docker compose up in project/app directory.
reference: https://forums.docker.com/t/use-docker-compose-in-get-started-tutorial-solved/129415
I have issues where gulp is not making any files. It says finished, but no file is being created.
If I log in to my docker instance using:
docker exec -t -i myservice-service /bin/bash
and if I run the gulp command, then it creates it properly
Then all the files defined in the gulpfile.js are created. In other words, public/dist/ is populated with the main.js and other css files.
This is my Dockerfile.
FROM node:9
RUN mkdir -p /usr/src/app
RUN mkdir -p /usr/src/logs
WORKDIR /usr/src/app
# GULP Installation
RUN npm install -g gulp
RUN npm install gulp
COPY package*.json /usr/src/app/
COPY .npmrc /usr/src/app/
RUN cd /usr/src/app/ && npm install && npm install -g nodemon
COPY . /usr/src/app
RUN chown -R node:node /usr/src/app && chown -R node:node /usr/src/logs
USER node
EXPOSE 3000
RUN gulp
CMD ["npm", "run-script", "start" ]
And this is my composer file (development):
version: "3"
services:
myservice-service:
build: .
image: myservice-service
container_name: myservice-service
volumes:
- .:/usr/src/app
- /usr/src/app/node_modules
environment:
- NODE_ENV=dev
ports:
- 3000:3000
command: nodemon --delay 2 ./bin/www
I run it as:
docker-compose -f docker-compose.development.yml up --build
When I run it like that, it does not create any files. I get the same output on the screen, when I run the command manually.
I have spent hours trying to make it work, I tried with setting permissions and what not, but it just does not work.
My expectation was to have public/dist/ populated with files.
Any help is appreciated.
UPDATE. It works, but I have doubts:
I manage to make it work by using command inside the composerfile itself.
So in my case:
command: bash -c "gulp && nodemon --delay 2 ./bin/www"
In my reasoning, gulp should be done inside the Dockerfile itself, not on the composer files. But then again, it is out of my scope of knowledge.
The Dockerfile is run at build time and will COPY all the files in your local directory into the container, then run gulp and create any files.
You then mount the local folder over the docker containers file system, pretty much overwriting what was done in the docker file with the original files, as gulp ran on the files in the container, it did not effect the original files so you are undoing the changes.
The solutions are either to do as as you have mentioned in your question (add it to the command in docker-compose.yml or run it via docker-compose exec) or write a custom entrypoint script that will run gulp and then the command, something like:
bin/entrypoint.sh
#!/bin/sh
gulp
exec "$#"
Dockerfile
FROM node:9
COPY bin/entrypoint.sh /entrypoint.sh
RUN chmod 755 /entrypoint.sh
RUN mkdir -p /usr/src/app
RUN mkdir -p /usr/src/logs
WORKDIR /usr/src/app
# GULP Installation
RUN npm install -g gulp
RUN npm install gulp
COPY package*.json /usr/src/app/
COPY .npmrc /usr/src/app/
RUN cd /usr/src/app/ && npm install && npm install -g nodemon
COPY . /usr/src/app
RUN chown -R node:node /usr/src/app && chown -R node:node /usr/src/logs
USER node
EXPOSE 3000
ENTRYPOINT ["/entrypoint.sh"]
CMD ["npm", "run-script", "start" ]
This will make your build a little less predictable though as it will run gulp each time the container starts (e.g. after every deployment) if you use the same Dockerfile in dev and production.
I am using Docker with Docker Compose and these are my files:
#DOCKERFILE
FROM mhart/alpine-node
# Create app directory
RUN mkdir -p /home/app
# Bundle app soure
COPY . /home/app
# From now on we work in /home/app
WORKDIR /home/app
# Install yarn and node modules
RUN echo -e 'http://dl-cdn.alpinelinux.org/alpine/edge/main\nhttp://dl-
cdn.alpinelinux.org/alpine/edge/community\nhttp://dl-
cdn.alpinelinux.org/alpine/edge/testing' > /etc/apk/repositories \
&& apk add --no-cache yarn \
&& yarn
EXPOSE 8080
This is the docker-compose file for dev:
app:
build: .
command: yarn start:dev
environment:
NODE_ENV: development
ports:
- '8080:8080'
volumes:
- .:/home/app
- /home/app/node_modules
The problem I am having is that this setup seems to work just once because no matter which new module I add to the package.json, whenever I run docker-compose build it will not install the new package.
The reason why I am using the volumes is because nodemon would not work without .:/home/app, but if the node modules are not installed in the host then it will fail, reason why I need /home/app/node_modules. I suspect this could be the cause of my error, but I am not sure how to circumvent that.
I solved this by moving my src code inside an src directory.
This means my docker-compose.yml file now looks like this:
app:
build: .
command: yarn start:dev
environment:
NODE_ENV: development
ports:
- '8080:8080'
volumes:
- ./src:/home/app/src
Since I am not mounting the whole dir with the node_modules, new ones seem to be installed correctly.
The package.json should be copied into app directory and "npm install" should be invoked in Dockerfile before copying the bundle line.
#DOCKERFILE
FROM mhart/alpine-node
# Create app directory
RUN mkdir -p /home/app
WORKDIR /home/app
# Install app dependencies
COPY package.json /home/app
RUN npm install
# Bundle app soure
COPY . /home/app
# Install yarn and node modules
RUN echo -e 'http://dl-cdn.alpinelinux.org/alpine/edge/main\nhttp://dl-
cdn.alpinelinux.org/alpine/edge/community\nhttp://dl-
cdn.alpinelinux.org/alpine/edge/testing' > /etc/apk/repositories \
&& apk add --no-cache yarn \
&& yarn
EXPOSE 8080
If there is any new dependency registers in package.json, it should be installed when the docker build command is invoked.