From this documentation, it seems like that I can execute a single command from a service like this:
docker-compose run SERVICE CMD
But when I run
docker-compose up pwa npm test
I get the error
ERROR: No such service: npm
From my configurations, it will execute npm start, but I'd like to know how to execute other commands.
Files
Dockerfile:
From node:8
WORKDIR /app
copy package.json /app/
RUN npm install --quiet
CMD npm start
docker-compose.yml:
version: '3'
services:
pwa:
build: .
ports:
- '3000:3000'
volumes:
- ./src:/app/src
- ./public:/app/public
Versions
Docker version: 17.03
Docker compose version: 1.11.2
As docs say, the command is docker-compose run, not docker-compose up. The later expects all service names.
Do as this:
docker-compose run pwa npm test
Related
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
I am new to docker. I am trying to create a container for react and express and run both the containers on same network using docker compose.
Below is my dockerfile for frontend:
FROM node:alpine
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
EXPOSE 3000
CMD ["npm","run","start"]
Below is my dockerfile for backend
FROM node:alpine
WORKDIR /app
COPY package*.json ./
RUN NODE_ENV=development npm install
COPY . .
EXPOSE 5000
CMD ["npm","run","server"]
Below is my docker-compose.yml
version: '3'
services:
client:
build:
context: './frontend'
dockerfile: Dockerfile
ports:
- 3000:3000
container_name: react_cont
environment:
- WATCHPACK_POLLING=true
networks:
- mern
volumes:
- ./frontend:/app
depends_on:
- server
server:
build:
context: './backend'
dockerfile: Dockerfile
ports:
- 5000:5000
container_name: express_cont
networks:
- mern
volumes:
- ./backend:/app
networks:
mern:
react container is getting is created and running successfully but the express container is not getting created with an error
sh: nodemon: not found
I had installed nodemon as my dev dependency.
Any help is appreciated. Thanks in advance.
Answering my own Question.
I had installed nodemon globally in my machine but forgot to install it as a dependency for my current project.
Since nodemon was installed globally in my machine i was not getting any errors while i was trying to run my server.js using nodemon. nodemon server.js in scripts did not throw any error while i was in developing my project locally prior moving it to docker container.
But since neither my package.json had nodemon as my dependency and i had not installed it separately in my container nodemon did not get installed and it gave me error.
You can try to delete node_modules folder in your source code and add flag --production=false explicitly to the npm install command. I think it's caching problem.
You may need to install nodemon package globally in your Docker:
RUN NODE_ENV=development npm install && npm --global install nodemon
I have several applications (vue & react each of the applications is built on a different version of the node). I want to set up a project deployment so that I can run a docker container with the correct version of the node for each of the projects. A build (npm i & npm run build) should happen in the container, but I go to give the result from the container to /var/www/project_name already on the server itself.
Next, set up a container with nginx which, depending on the subdomain, will give the desired build
My question is how to return the folder with files from the container to the operating system area?
my docker-compose file:
version: "3.1"
services:
redis:
restart: always
image: redis:alpine
container_name: redis
build-adminapp:
build: adminapp/
container_name: adminapp
working_dir: /var/www/adminapp
volumes:
- ./adminapp:/var/www/adminapp
build-clientapp:
build: clientapp/
container_name: clientapp
working_dir: /var/www/clientapp
volumes:
- ./clientapp:/var/www/clientapp`
my docker files:
FROM node:10-alpine as build
# Create app directory
WORKDIR /var/www/adminapp/
COPY . /var/www/adminapp/
RUN npm install
RUN npm run build
second docker file:
FROM node:12-alpine as build
# Create app directory
WORKDIR /var/www/clientapp/
COPY . /var/www/clientapp/
RUN npm install
RUN npm run build
If you already have a running container, you can use docker cp command to move files between local machine and docker containers.
I am trying to dockerize nestjs application. I have to use approach of our devops, so I can't give all details of configuration.
Scripts in package.json typical for any nestjs application.
I have Dockerfile.backend:
FROM some.registry.net/docker/node16 as builder
WORKDIR /opt/app
COPY --chown=app:app ./nestjs/nest-project .
RUN yarn install --non-interactive --production --frozen-lockfile
FROM some.registry.net/docker/node16 as serve
WORKDIR /opt/app
ENV NODE_ENV=production
ENV APP_CONFIG=/opt/app/config/config.yaml
COPY --chown=app:app ./build/Procfile /opt/startup/Procfile
COPY --chown=app:app ./build/config.yaml ./config/config.yaml
COPY --chown=app:app --from=builder /opt/app/ ./
COPY --chown=app:app --from=builder /opt/app/node_modules ./node_modules
USER root
##RUN npm install pm2 -g
##RUN npm install -g nodemon
RUN npm run build
CMD ["/opt/startup/entrypoint.sh"]
And I have docker-compose.yml file:
version: "2"
services:
backend:
build:
context: .
dockerfile: ./build/Dockerfile.backend
command: npm run start
##volumes:
##- ./nestjs/nest-project:/app
##- /app/node_modules
ports:
- 4001:4001
- 9229:9229
environment:
- NODE_ENV=development
- PORT=4001
- REACT_APP_PROD=0
- REACT_APP_BACKEND_URL=http://127.0.0.1:4001
- FRONTEND_URL=http://localhost:4000
- APP_CONFIG=/opt/app/config/config.yaml
frontend:
build: ./frontend
command: npm start
##volumes:
##- ./frontend:/app
##- /app/node_modules
environment:
- NODE_ENV=development
- DISABLE_ESLINT_PLUGIN=true
- REACT_APP_BACKEND_URL=http://127.0.0.1:4001/backend
- PORT=4000
- REACT_APP_PROD=0
ports:
- 4000:4000
So the most interesting point lies in backend command part. I am able to start container only with npm run start:dev command. When I enter there npm start or npm run start:prod
container executes (I see in logs that nestjs app starts, successfully connects to database) and exits without any errors. I tried node dist/main and got the same result. I tried nodemon, with nodemon dist/main nodemon, even with verbose flag shows red line something like app crashed... and gives no more information. I tried pm2 with this command pm2 --name nestjs start npm -- start pm2 successfully starts and container exits without any information.
So far I see problem lies somewhere in my configuration, but I have no clue where to seek. Thanks in advance.
I'd like to start 2 containers using docker-compose. One for the database and one for a Node server using the default node image on DockerHub. In the Node container, I'd like to mount a local folder that's source controlled as a volume. I've seen many examples where a Dockerfile is created that copies source files into the image and then a RUN npm install command is used in the Dockerfile. But that results in a new image being created with the source files in it. In my case, I don't want to save source code files in the Node image. So I'd like the entrypoint of the Node container in my docker-compose file to run npm install as well as npm start, but I can't seem to get that combination to work. Here's my docker-compose.yml:
version: "3.8"
services:
server:
container_name: my_server
image: node:12.16.1
ports:
- "8000:8000"
volumes:
- ../my-server-files-source-controlled:/var/www
working_dir: /var/www
entrypoint: ["npm", "start"]
networks:
- my-network
db:
container_name: my_database
image: postgres
environment:
{ommitted}
ports:
- "5432:5432"
networks:
- my-network
networks:
my-network:
driver: bridge
The command above works fine, but when I change the entrypoint to include npm install, it fails. I've tried the following examples, as well as many others, and they all fail:
entrypoint: ["npm", "install", "&&", "npm", "start"]
entrypoint: ["npm install", "npm start"]
entrypoint: "npm install && npm start"
It seems like I can have npm install or npm start in entrypoint, but not both. How can get both those commands to work in my docker-compose file?
If you want to use multiple commands, you can do so with bash -c "commands":
services:
myservice:
command: bash -c "npm install && npm start"
By the way, in a production deployment, I would suggest using npm ci instead of npm install. Also consider using the --only=prod and --no-audit flags (depending on your setup).