Docker-compose.yaml:
version: "3"
services:
mysql:
image: mysql:5.7
environment:
MYSQL_HOST: localhost
MYSQL_DATABASE: mydb
MYSQL_USER: mysql
MYSQL_PASSWORD: 1234
MYSQL_ROOT_PASSWORD: root
ports:
- "3307:3306"
expose:
- 3307
volumes:
- /var/lib/mysql
- ./mysql/migrations:/docker-entrypoint-initdb.d
restart: unless-stopped
web:
build:
context: .
dockerfile: web/Dockerfile
volumes:
- ./:/web
ports:
- "32768:3000"
environment:
NODE_ENV: development
PORT: 3000
links:
- mysql:mysql
depends_on:
- mysql
expose:
- 3000
command: ["./wait-for-it.sh", "mysql:3306", "--", "npm start"]
Web Dockerfile:
FROM node:6.11.2-slim
RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app
COPY package.json /usr/src/app/
RUN npm install
COPY . /usr/src/app
CMD [ "npm", "start" ] # So this is overridden by the wait script and doesn't execute
I'm using this wait script:
https://github.com/vishnubob/wait-for-it
The wait script works fine, however it overrides the existing start command for the web container:
CMD [ "npm", "start" ]
As you can see in the docker-compose file I'm using this approach to kick off npm start:
command: ["./wait-for-it.sh", "mysql:3306", "--", "npm start"]
I have tried a few alternative e.g:
command: ["./wait-for-it.sh", "mysql:3306", "--", "CMD ['npm', 'start'"]
command: ["./wait-for-it.sh", "mysql:3306", "--", "docker-entrypoint.sh"]
Only it is not working. I get this error from the web container:
web_1 | ./wait-for-it.sh: line 174: exec: npm start: not found
What's going on here?
So first of all if you use command in docker-compose then it will override the the CMD and that is an expected behavior. How can docker know you want to execute both of them.
Next your approach is a bit wrong with the CMD
command: ["./wait-for-it.sh", "mysql:3306", "--", "npm start"]
translates to you executing
./wait-for-it.sh mysql:3306 -- "npm start"
Which is supposed to fail as there is no command npm start it is npm which takes start as the argument. So change the command to
command: ["./wait-for-it.sh", "mysql:3306", "--", "npm", "start"]
or
command: ./wait-for-it.sh mysql:3306" -- npm start
Whichever format you like
Related
When I run the following Dockerfile,in the middle of the process, after installing prisma, the RUN command db-migrate up is stopped. But when I used docker exec bin bash to run it, it worked without any problem. I don't think I can run the app before serving it. But there is a workaround like putting the migration commands as a service in docker-compose.yml. How to achieve it? or if there's any way to run those RUN commands of migration in this Dockerfile?
Dockerfile
FROM node:16.15.0-alpine
WORKDIR /app
COPY package*.json ./
# generated prisma files
COPY prisma ./prisma/
# COPY ENV variable
COPY .env ./
# COPY
COPY . .
RUN npm install
RUN npm install -g db-migrate
RUN npm install -g prisma
RUN db-migrate up
RUN prisma db pull
RUN prisma generate
EXPOSE 3000
CMD ["npm", "run", "dev"]
docker-compose.yml
version: '3.8'
services:
mysqldb:
image: mysql:5.7
restart: unless-stopped
env_file: ./.env
environment:
- MYSQL_ROOT_PASSWORD=$MYSQLDB_ROOT_PASSWORD
- MYSQL_DATABASE=$MYSQLDB_DATABASE
ports:
- $MYSQLDB_LOCAL_PORT:$MYSQLDB_DOCKER_PORT
volumes:
- db:/var/lib/mysql
auth:
depends_on:
- mysqldb
build: ./auth
restart: unless-stopped
env_file: ./.env
ports:
- $NODE_LOCAL_PORT:$NODE_DOCKER_PORT
environment:
- DB_HOST=mysqldb
- DB_USER=$MYSQLDB_USER
- DB_PASSWORD=$MYSQLDB_ROOT_PASSWORD
- DB_NAME=$MYSQLDB_DATABASE
- DB_PORT=$MYSQLDB_DOCKER_PORT
stdin_open: true
tty: true
volumes:
db:
I want to run my node application with only one command : npm start and based on the NODE_ENV, it will either run nodemon app.js if NODE_ENV=dev or node app.js if NODE_ENV=production but in DETACHED MODE.
What I have so far :
package.json
"start": "docker-compose up --build --force-recreate api",
"api:dev": "nodemon app.js",
"api:prod": "node app.js"
Dockerfile
FROM node:16-alpine
WORKDIR /app
COPY package.json .
RUN npm install
ENTRYPOINT ["/bin/sh"]
CMD ["-c", "if [ \"$NODE_ENV\" = \"dev\" ]; then npm run api:dev; else npm run api:prod; fi"]
And finally the docker-compose file
api:
restart: always
container_name: my_api
build:
context: .
dockerfile: ./docker/api/Dockerfile
depends_on:
- postgres
ports:
- "${API_PORT}:${API_PORT}"
env_file: .env
networks:
- back
volumes:
- .:/app
- node_modules:/app/node_modules
logging:
options:
max-file: "10"
max-size: "10m"
Where/how can I setup the conditional detach mode? I know I should use -d for the detach docker-compose up but I want it conditional based on the NODE_ENV.
I am following this tutorial but when I ran docker-compose run --no-deps app rails new . --force --database=mysql I got the error mentioned in the question title. Below are the contents of some of my files:
Dockerfile:
FROM ruby:2.6
RUN apt-get update -qq && apt-get install -y nodejs
WORKDIR /essence
COPY Gemfile /essence/Gemfile
COPY Gemfile.lock /essence/Gemfile.lock
RUN bundle install
COPY . /essence
# Add a script to be executed every time the container starts
COPY ./entrypoint.sh /usr/bin/
RUN chmod +x /usr/bin/entrypoint.sh
ENTRYPOINT ["/usr/bin/entrypoint.sh"]
EXPOSE 3000
# Configure the main process to run when running the image
CMD ["rails", "server", "-b", "0.0.0.0"]
docker-compose.yml:
version: '2'
services:
db:
image: mysql:8.0
restart: always
environment:
MYSQL_ROOT_PASSWORD: *****
MYSQL_DATABASE: *****
MYSQL_USER: *****
MYSQL_PASSWORD: *****
app:
build: .
command: bash -c "rm -f /essence/tmp/pids/server.pid && bundle exec rails s -p 3000 -b '0.0.0.0'"
volumes:
- ".:/app"
ports:
- "3000:3000"
depends_on:
- db
links:
- db
environment:
DB_USER: *****
DB_NAME: *****
DB_PASSWORD: *****
DB_HOST: *****
I tried about 20 different things but none of them resolved this error. Does anyone know on a first glance what could be wrong with my setup? Thank you!
I used docker-compose to create docker containers in my React, Node.js, and Postgres structured project.
After I created Dockerfile and docker-compose.yml, I did docker up 'docker-compose up --build.'
Then, I wasn't be able to create containers, and get an errors.
I get Error says:
Error 1
Error 2
Error 3
How can I fix it and successfully build containers?
Here is a docker-compose.yml file in './'
version: '3'
services:
server:
container_name: mylivingcity_server
build: ./server
expose:
- 3001
ports:
- 3001:3001
volumes:
- ./server/config:/usr/src/app/server/config
- ./server/controllers:/usr/src/app/server/controllers
- ./server/db:/usr/src/app/server/db
command: npm run start
postgres:
image: postgres:12
container_name: mylivingcity_postgres
ports:
- 5432:5432
volumes:
- ./postgres/data:/var/lib/postgresql/data
environment:
- POSTGRES_DB=mylivingcity
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=postgres
frontend:
container_name: mylivingcity_frontend
build: ./frontend
expose:
- 3000
ports:
- 3000:3000
volumes:
- ./frontend/src:/usr/src/app/frontend/src
- ./frontend/public:/usr/src/app/frontend/public
command: npm start
stdin_open: true
Here is a Dockerfile in './frontend'
FROM node:12
# Create frontend directory
RUN mkdir -p /usr/src/app/frontend/
WORKDIR /usr/src/app/frontend/
# Install dependencies
COPY package*.json /usr/src/app/frontend/
RUN npm install
COPY . /usr/src/app/frontend/
CMD [ "npm" , "start" ]
Here is a Dockerfile in './server'
FROM node:12
# Create server directory
RUN mkdir -p /usr/src/app/server/
WORKDIR /usr/src/app/server/
# Install dependencies
COPY package*.json /usr/src/app/server/
RUN npm install
COPY . /usr/src/app/server/
CMD [ "npm" , "run" , "start" ]
I'm trying to get nodemon and docker-compose work with each others and I'm having a problem: Every time I run docker-compose up I get the following error: sh: missing ]. Here are the relevant files:
Dockerfile
FROM node:11.1.0-alpine
MAINTAINER TileHalo
WORKDIR /usr/src/app
RUN npm install -g nodemon
COPY package*.json ./
RUN npm install --save
COPY . .
EXPOSE 3000
CMD [ "nodemon", "./bin/www"]
and docker-compose.yml
version: "2"
services:
web:
build: .
ports:
- "3000:3000"
depends_on:
- postgres
volumes:
- .:/usr/src/app
postgres:
image: "postgres:alpine"
environment:
POSTGRES_PASSWORD: supersalainen
POSTGRES_USER: kipa
EDIT: Works on pure docker after the comma fix, but doesn't work on docker-compose