Build using docker-compose and run app and mongo using docker run - node.js

I have a docker-compose file in below format
services:
production-api:
build: .
environment:
- MONGO_URI=mongodb://mongodb:27017/productiondb
volumes:
- .:/app
ports:
- "3000:3000"
depends_on:
- mongodb
# condition: service_healthy
mongodb:
# user: node
image: mongo
# volumes:
# - ./data:/data/db
ports:
- "27017:27017"
# healthcheck:
# test: echo 'db.runCommand("ping").ok' | mongo mongo:27017/productiondb --quiet 1
# interval: 10s
# timeout: 10s
# retries: 5
and docker-compose.prod.yml
version: "2.4"
services:
production-api:
container_name: "production"
command: yarn start
environment:
- NODE_ENV=production
How do I build using docker-compose -f and run both mongo and production using docker run?

Related

Need some help regarding docker-compose params

I have a project with docker-compose.yml file:
version: "3.9"
services:
nginx:
build:
context: ./
dockerfile: nginx/Dockerfile
container_name: nginx_lb
volumes:
- vxx_data:/var/www/html:ro
- ./certbot/conf:/etc/letsencrypt
- ./certbot/www:/var/www/certbot
ports:
- "80:80"
- "443:443"
command: "/bin/sh -c 'while :; do sleep 6h & wait $${!}; nginx -s reload; done & nginx -g \"daemon off;\"'"
depends_on:
- web
restart: unless-stopped
nodejs:
build:
context: ./
dockerfile: nodejs/Dockerfile
container_name: nodejs
volumes:
- vxx_data:/app
depends_on:
- web
restart: unless-stopped
certbot:
image: certbot/certbot
restart: unless-stopped
container_name: ssl_cert
volumes:
- ./certbot/conf:/etc/letsencrypt
- ./certbot/www:/var/www/certbot
entrypoint: "/bin/sh -c 'trap exit TERM; while :; do certbot renew; sleep 12h & wait $${!}; done;'"
depends_on:
- nginx
volumes:
vxx_data:
db-data:
I need to understand this line with :ro
volumes:
- vxx_data:/var/www/html:ro
Normally we access the volume data without :ro and It works but here I do not understand this switch and when I down the docker-compose it reset all work and files. This is the main issue.

Docker/ Traefik: Cannot route services to the host provided

I'm new to traefik, and I can't figure why my configuration do not work as expected.
Here is the context :
2 front-end app, builded and copy/pasted into a nginx folder using Dockerfile
1 rest api, compiled and running with node using Dockerfile
1 postgres database and pgadmin
1 mongo database and mongo-express.
All of this is setup in a docker-compose.
Frontend Dockerfile:
FROM node:lts-alpine as build
WORKDIR /app/frontend-app1 -- frontend-app2 for the other
COPY package*.json ./
RUN npm install
COPY . ./
RUN npm run build
FROM nginx:alpine
COPY nginx/default.conf /etc/nginx/conf.d/default.conf
COPY --from=build /app/frontend-app1/build /usr/share/nginx/html/frontend-app1
EXPOSE 3001 -- 3002 from frontend-app2
CMD ["nginx", "-g", "daemon off;"]
API Dockerfile:
FROM node:14-alpine
WORKDIR /usr/src/app
COPY package*.json ./
RUN npm install
COPY . .
RUN npm run build
ARG NODE_ENV=qual
ENV NODE_ENV=${NODE_ENV}
EXPOSE 3000
CMD ["node", "dist/main"]
Docker-compose:
version: "3.8"
networks:
backend-network:
traefik-network:
name: traefik-network
external: true
services:
mongo:
container_name: wow_mongo
image: mongo
restart: always
ports:
- 27017:27017
networks:
- backend-network
environment:
MONGO_INITDB_ROOT_USERNAME: root
MONGO_INITDB_ROOT_PASSWORD: xxx
MONGO_INITDB_DATABASE: xxx
MONGO_INITDB_USERNAME: xxx
MONGO_INITDB_PASSWORD: xxx
volumes:
- ./init-mongo.sh:/docker-entrypoint-initdb.d/init-mongo.sh
mongo-express:
container_name: wow_mongo_express
image: mongo-express
depends_on:
- mongo
restart: always
ports:
- 8081:8081
networks:
- traefik-network
- backend-network
environment:
ME_CONFIG_MONGODB_ADMINUSERNAME: xxx
ME_CONFIG_MONGODB_ADMINPASSWORD: xxx
ME_CONFIG_MONGODB_URL: mongodb://xxx:xxx#mongo:27017/
labels:
- "traefik.enable=true"
- "traefik.http.routers.mongo-express.rule=Host(`me-qual.xxx.com`)"
- "traefik.http.routers.mongo-express.entrypoints=web"
postgres:
container_name: wow_postgres
image: postgres
restart: always
ports:
- 5432:5432
networks:
- backend-network
environment:
POSTGRES_PASSWORD: xxx
POSTGRES_USER: xxx
POSTGRES_DB: xxx
pgadmin:
container_name: wow_pgadmin
image: dpage/pgadmin4
depends_on:
- postgres
restart: always
networks:
- traefik-network
- backend-network
ports:
- 5050:80
environment:
PGADMIN_DEFAULT_EMAIL: xxx
PGADMIN_DEFAULT_PASSWORD: xxx
labels:
- "traefik.enable=true"
- "traefik.http.routers.pgadmin.rule=Host(`pgadmin-qual.xxx.com`)"
- "traefik.http.routers.pgadmin.entrypoints=web"
- "traefik.docker.network=traefik-network"
traefik:
container_name: wow_traefik
image: traefik:v2.6
restart: always
ports:
- "80:80"
- "443:443"
volumes:
- /var/run/docker.sock:/var/run/docker.sock
- $PWD/traefik-conf/traefik.yml:/etc/traefik/traefik.yml
- $PWD/traefik-conf/acme.json:/letsencrypt/acme.json
labels:
- "traefik.enable=true"
- "traefik.http.routers.api.rule=Host(`traefik.xxx.com`)"
- "traefik.http.routers.api.service=api#internal"
- "traefik.http.routers.api.entrypoints=web"
- "traefik.http.routers.api.middlewares=auth"
- "traefik.http.middlewares.auth.basicauth.users=xxx:xxx"
# - "traefik.http.routers.traefik.tls.certresolver=letsencrypt"
# - "traefik.http.middlewares.strip-www.redirectregex.regex=^https?://(www\\.)(.+)"
# - "traefik.http.middlewares.strip-www.redirectregex.replacement=https://$${2}"
# - "traefik.http.middlewares.strip-www.redirectregex.permanent=true"
api:
container_name: wow_api
depends_on:
- postgres
- mongo
restart: unless-stopped
image: wow_api:1.0.0
build:
context: ../wow-api
dockerfile: Dockerfile
ports:
- 3000:3000
networks:
- traefik-network
- backend-network
labels:
- "traefik.enable=true"
- "traefik.http.routers.wow-api.rule=Host(`api-qual.xxx.com`)"
- "traefik.http.routers.wow-api.entryPoints=web"
admin:
container_name: wow_admin
# depends_on:
# - api
restart: always
image: wow_admin:1.0.0
build:
context: ../wow-admin
dockerfile: Dockerfile
ports:
- 3002:80
networks:
- traefik-network
labels:
- "traefik.enable=true"
- "traefik.http.routers.wow-admin.rule=Host(`admin-qual.xxx.com`, `www.admin-qual.xxx.com`)"
- "traefik.http.routers.wow-admin.entrypoints=web"
# - "traefik.http.routers.blog.middlewares=strip-www"
# - "traefik.http.routers.blog.tls=true"
# - "traefik.http.routers.blog.tls.certresolver=letsencrypt"
consultant:
container_name: wow_consultant
# depends_on:
# - api
restart: always
image: wow_consultant:1.0.0
build:
context: ../wow-consultant
dockerfile: Dockerfile
ports:
- 3001:80
networks:
- traefik-network
labels:
- "traefik.enable=true"
- "traefik.http.routers.wow-consultant.rule=Host(`consultant-qual.xxx.com`, `www.admin-qual.xxx.com`)"
- "traefik.http.routers.wow-consultant.entrypoints=web"
And my traefik config is simple since I can't manage to make it work without tls.
global:
sendAnonymousUsage: false
log:
level: "INFO"
format: "common"
providers:
docker:
endpoint: "unix:///var/run/docker.sock"
exposedByDefault: true
swarmMode: false
watch: true
network: traefik-network
api:
dashboard: true
entryPoints:
web:
address: ":80"
# http:
# redirections:
# entryPoint:
# to: "websecure"
# scheme: "https"
# permanent: true
websecure:
address: ":443"
# http:
# tls:
# certResolver: "letsencrypt"
# certificatesResolvers:
# letsencrypt:
# acme:
# email: "xxx"
# storage: "/letsencrypt/acme.json"
# tlsChallenge: {}
I figure out to make traefik.my_domain.com to work and to log using basic auth.
I can also navigate throught my differents services using localhost:<PORT_BINDED>
But as soon as I want to reach, for example, a front end app, I get a Gateway timeout and can't manage to make it work if I don't specify the port.
For example, accessing to the frontend app which is related to the consultant service:
consulltant-qual.xxx.com result in a gateway timeout, but if I ask consulltant-qual.xxx.com:3001, I can access to my app.
Any help would be grateful,
Thanks

unable to run gatbsy site locally with docker-compose

This is my first time using docker and I have downloaded a docker-compose.yml from https://github.com/wodby/docker4wordpress which is a repo with docker images for wordpress . I am planning to use wordpress as a backend and use created blog posts from it's API in a Gatsby site . My problem is that since I don't have experience with docker I am unable to
run my gatsby site locally using the node image the above link provides inside my docker-compose.yml . I can run wordpress succesfully however as I had help from my boss to configure the above file before trying to use gatsby and node .
My project structure :
Wordpress_Project
|
+-- numerous wordpress folders
|
+-- gatsby_site folder
|
+-- docker-compose.yml
My docker-compose.yml
version: "3"
services:
mariadb:
image: wodby/mariadb:$MARIADB_TAG
container_name: "${PROJECT_NAME}_mariadb"
stop_grace_period: 30s
environment:
MYSQL_ROOT_PASSWORD: $DB_ROOT_PASSWORD
MYSQL_DATABASE: $DB_NAME
MYSQL_USER: $DB_USER
MYSQL_PASSWORD: $DB_PASSWORD
volumes:
# - ./mariadb-init:/docker-entrypoint-initdb.d # Place init .sql file(s) here.
- bedrock_dbdata:/var/lib/mysql # I want to manage volumes manually.
php:
image: wodby/wordpress-php:$PHP_TAG
container_name: "${PROJECT_NAME}_php"
environment:
PHP_SENDMAIL_PATH: /usr/sbin/sendmail -t -i -S mailhog:1025
DB_HOST: $DB_HOST
DB_USER: $DB_USER
DB_PASSWORD: $DB_PASSWORD
DB_NAME: $DB_NAME
PHP_FPM_USER: www-data
PHP_FPM_GROUP: www-data
# # Read instructions at https://wodby.com/docs/stacks/wordpress/local#xdebug
# PHP_XDEBUG: 1
# PHP_XDEBUG_MODE: debug
# PHP_IDE_CONFIG: serverName=my-ide
# PHP_XDEBUG_IDEKEY: "my-ide"
# PHP_XDEBUG_CLIENT_HOST: 172.17.0.1 # Linux
# PHP_XDEBUG_CLIENT_HOST: host.docker.internal # Docker 18.03+ Mac/Win
# PHP_XDEBUG_CLIENT_HOST: 10.0.75.1 # Windows
# PHP_XDEBUG_LOG: /tmp/php-xdebug.log
volumes:
- ./:${CODEBASE}:cached
## Alternative for macOS users: Mutagen https://wodby.com/docs/stacks/wordpress/local#docker-for-mac
# - mutagen:/var/www/html
# # For XHProf and Xdebug profiler traces
# - files:/mnt/files
crond:
image: wodby/wordpress-php:$PHP_TAG
container_name: "${PROJECT_NAME}_crond"
environment:
CRONTAB: "0 * * * * wp cron event run --due-now --path=${CODEBASE}/web/wp"
command: sudo -E LD_PRELOAD=/usr/lib/preloadable_libiconv.so crond -f -d 0
volumes:
- ./:${CODEBASE}:cached
mailhog:
image: mailhog/mailhog
container_name: "${PROJECT_NAME}_mailhog"
labels:
- "traefik.http.services.${PROJECT_NAME}_mailhog.loadbalancer.server.port=8025"
- "traefik.http.routers.${PROJECT_NAME}_mailhog.rule=Host(`mailhog.${PROJECT_BASE_URL}`)"
nginx:
image: wodby/nginx:$NGINX_TAG
container_name: "${PROJECT_NAME}_nginx"
depends_on:
- php
environment:
NGINX_STATIC_OPEN_FILE_CACHE: "off"
NGINX_ERROR_LOG_LEVEL: debug
NGINX_BACKEND_HOST: php
NGINX_VHOST_PRESET: wordpress
NGINX_SERVER_NAME: ${PROJECT_BASE_URL}
NGINX_SERVER_ROOT: ${CODEBASE}/web
volumes:
- ./:${CODEBASE}:cached
## Alternative for macOS users: Mutagen https://wodby.com/docs/stacks/wordpress/local#docker-for-mac
# - mutagen:/var/www/html
labels:
- "traefik.http.routers.${PROJECT_NAME}_nginx.rule=Host(`${PROJECT_BASE_URL}`)"
portainer:
image: portainer/portainer-ce
container_name: "${PROJECT_NAME}_portainer"
command: -H unix:///var/run/docker.sock
restart: unless-stopped
volumes:
- /var/run/docker.sock:/var/run/docker.sock
labels:
- "traefik.http.services.${PROJECT_NAME}_portainer.loadbalancer.server.port=9000"
- "traefik.http.routers.${PROJECT_NAME}_portainer.rule=Host(`portainer.${PROJECT_BASE_URL}`)"
traefik:
image: traefik:v2.4
container_name: "${PROJECT_NAME}_traefik"
command: --api.insecure=true --providers.docker
ports:
- '80:80'
# - '8080:8080' # Dashboard
volumes:
- /var/run/docker.sock:/var/run/docker.sock
#this is the node image where I want to run gatsby locally
node:
image: wodby/node:$NODE_TAG
container_name: "${PROJECT_NAME}_node"
working_dir: /app
labels:
- "traefik.http.services.${PROJECT_NAME}_node.loadbalancer.server.port=3000"
- "traefik.http.routers.${PROJECT_NAME}_node.rule=Host(`node.${PROJECT_BASE_URL}`)"
expose:
- "3000"
volumes:
- ./gatsby_site:/app
command: sh -c 'npm install && npm run start'
volumes:
bedrock_dbdata:
external: true
I am trying to configure the above node image but I am unsuccessfull so far . I try accessing the node url on localhost only to get error 404 .
I would appreciate your help.

Changing the source code does not live update using docker-compose and volumes on mern stack

I would like to implement a hot reloading functionality from development evinronement such that when i change anything in the source code it will reflect the changes up to the docker container by mounting the volume and hence see the changes live in my localhost.
Below is my docker-compose file
version: '3.9'
services:
server:
restart: always
build:
context: ./server
dockerfile: Dockerfile
volumes:
# don't overwrite this folder in container with the local one
- ./app/node_modules
# map current local directory to the /app inside the container
#This is a must for development in order to update our container whenever a change to the source code is made. Without this, you would have to rebuild the image each time you make a change to source code.
- ./server:/app
# ports:
# - 3001:3001
depends_on:
- mongodb
environment:
NODE_ENV: ${NODE_ENV}
MONGO_URI: mongodb://${MONGO_ROOT_USERNAME}:${MONGO_ROOT_PASSWORD}#mongodb
networks:
- anfel-network
client:
stdin_open: true
build:
context: ./client
dockerfile: Dockerfile
volumes:
- ./app/node_modules
- ./client:/app
# ports:
# - 3000:3000
depends_on:
- server
networks:
- anfel-network
mongodb:
image: mongo
restart: always
ports:
- 27017:27017
environment:
MONGO_INITDB_ROOT_USERNAME: ${MONGO_ROOT_USERNAME}
MONGO_INITDB_ROOT_PASSWORD: ${MONGO_ROOT_PASSWORD}
volumes:
# for persistence storage
- mongodb-data:/data/db
networks:
- anfel-network
# mongo express used during development
mongo-express:
image: mongo-express
depends_on:
- mongodb
ports:
- 8081:8081
environment:
ME_CONFIG_MONGODB_ADMINUSERNAME: ${MONGO_ROOT_USERNAME}
ME_CONFIG_MONGODB_ADMINPASSWORD: ${MONGO_ROOT_PASSWORD}
ME_CONFIG_MONGODB_PORT: 27017
ME_CONFIG_MONGODB_SERVER: mongodb
ME_CONFIG_BASICAUTH_USERNAME: root
ME_CONFIG_BASICAUTH_PASSWORD: root
volumes:
- mongodb-data
networks:
- anfel-network
nginx:
restart: always
depends_on:
- server
- client
build:
context: ./nginx
dockerfile: Dockerfile
ports:
- '8080:80'
networks:
- anfel-network
# volumes:
# - ./nginx/default.conf:/etc/nginx/conf.d/default.conf
networks:
anfel-network:
driver: bridge
volumes:
mongodb-data:
driver: local
Any suggestions would be appreciated.
You have to create a bind mount, this can help you

Docker-compose confuses building a frontend with a backend

Using Docker-compose I want to build 3 containers: backend(node.js), frontend(react.js) and MySQL.
version: '3.8'
services:
backend:
container_name: syberiaquotes-restapi
build: ./backend
env_file:
- .env
command: "sh -c 'npm install && npm run start'"
ports:
- '3000:3000'
volumes:
- ./backend:/app
- /app/node_modules
depends_on:
- db
frontend:
container_name: syberiaquotes-frontend
build: ./frontend
ports:
- '5000:5000'
volumes:
- ./frontend/src:/app/src
stdin_open: true
tty: true
depends_on:
- backend
db:
image: mysql:latest
container_name: syberiaquotes-sql
env_file:
- .env
environment:
- MYSQL_DATABASE=${SQL_DB}
- MYSQL_USER=${SQL_USER}
- MYSQL_ROOT_PASSWORD=${SQL_PASSWORD}
- MYSQL_PASSWORD=${SQL_PASSWORD}
volumes:
- data:/var/lib/mysql
restart: unless-stopped
ports:
- '3306:3306'
volumes:
data:
My files structure:
Everything worked fine until I've added a new 'frontend' container!
It seems that docker is treating my frontend container as second backend because it's trying to launch nodemon, and it's not even included in frontend dependencies!:
Obviously I have two Dockerfiles for each service, they are almost the same files.
Backend:
Frontend:
Do You have any ideas where the problem should be?
RESOLVED! I had to delete all images and volumes:
$ docker rm $(docker ps -a -q) -f
$ echo y | docker system prune --all
$ echo y | docker volume prune
and run it again:
$ docker-compose up

Resources