Dockerize MERN stack using traefik - docker

I did follow this tutorial : https://rafrasenberg.com/posts/docker-container-management-with-traefik-v2-and-portainer/ to build a treafik reverse proxy on my server
And I tested it with a very simple application that render a "hello world" in a index.html:
version: "3"
services:
app:
image: nginx
environment:
PORT: ${PORT}
volumes:
- .:/usr/share/nginx/html/
networks:
- proxy
- default
labels:
- "traefik.enable=true"
- "traefik.docker.network=proxy"
- "traefik.http.routers.app-secure.entrypoints=websecure"
- "traefik.http.routers.app-secure.rule=Host(`my-test.localhost`)"
networks:
proxy:
external: true
it works!
Now I want to go on the next step and use it to build a MERN stack project and I'm a bit lost.
usually I dockerize a mern stack by:
create a dockerfile in /server
create a dockerfile in /client
create a docker-compose on the root directory
version: "3.7"
services:
server:
build:
context: ./server
dockerfile: Dockerfile
image: myapp-server
container_name: myapp-node-server
command: /usr/src/app/node_modules/.bin/nodemon server.js
volumes:
- ./server/:/usr/src/app
- /usr/src/app/node_modules
ports:
- 5000
depends_on:
- mongo
env_file: ./server/.env
environment:
- NODE_ENV=development
networks:
- app-network
- proxy
mongo:
image: mongo
volumes:
- data-volume:/data/db
ports:
- 27017
networks:
- app-network
- proxy
client:
build:
context: ./client
dockerfile: Dockerfile
image: myapp-client
container_name: myapp-react-client
command: npm start
volumes:
- ./client/:/usr/app
- /usr/app/node_modules
depends_on:
- server
ports:
- 3001
networks:
- app-network
labels:
- "traefik.enable=true"
- "traefik.docker.network=proxy"
- "traefik.http.routers.app2-secure.entrypoints=websecure"
- "traefik.http.routers.app2-secure.rule=Host(`test.localhost`)"
networks:
app-network:
driver: bridge
proxy:
external: true
volumes:
data-volume:
node_modules:
web-root:
driver: local
But seems that my proxy not working because the console doesn't return any error and it works on localhost:3000 but on test.localhost I have a "Gateway Timeout" error

Related

Docker-compose doesn't persist volumes

I'm having a problem persisting data with docker-compose.
I want my service chatmysql to persist data I put inside a database, but everytime i run docker-compose down it all vanishes.
I checked directory /var/lib/docker/volumes to see if it stores data there when containers are running and the volume was completely empty.
I didn't have that issue when I was running containers with docker run command so I guess its fault of my docker-compose.yaml file. Can someone help me?
I'm running this on Ubuntu 20.04.
version: '3'
services:
chatmysql:
image: mysql/mysql-server
container_name: chatmysql
hostname: db
user: root
networks:
- chatnet
ports:
- 3307:3306
volumes:
- chatmysqlvolume:/lib/var/mysql
chatbackend:
depends_on:
- chatmysql
build:
context: backend/src
container_name: chatbackend
hostname: backend
networks:
- chatnet
ports:
- 8080:8080
environment:
- MYSQLUSERNAME=${MYSQLUSERNAME:-user}
- MYSQLPASSWORD=${MYSQLPASSWORD:?database password not set}
- MYSQLHOST=${MYSQLHOST:-db}
- MYSQLPORT=${MYSQLPORT:-3306}
- MYSQLDBNAME=${MYSQLDBNAME:-test}
restart: always
deploy:
restart_policy:
condition: on-failure
chatfrontend:
build: frontend
container_name: chatfrontend
hostname: front
networks:
- chatnet
ports:
- 3000:3000
volumes:
chatmysqlvolume:
networks:
chatnet:
driver: bridge
You need to change the mounted volume, try this :
version: '3.7'
services:
chatmysql:
image: mysql/mysql-server
container_name: chatmysql
hostname: db
user: root
networks:
- chatnet
ports:
- 3307:3306
volumes:
- chatmysqlvolume:/var/lib/mysql
chatbackend:
depends_on:
- chatmysql
build:
context: backend/src
container_name: chatbackend
hostname: backend
networks:
- chatnet
ports:
- 8080:8080
environment:
- MYSQLUSERNAME=${MYSQLUSERNAME:-user}
- MYSQLPASSWORD=${MYSQLPASSWORD:?database password not set}
- MYSQLHOST=${MYSQLHOST:-db}
- MYSQLPORT=${MYSQLPORT:-3306}
- MYSQLDBNAME=${MYSQLDBNAME:-test}
restart: always
deploy:
restart_policy:
condition: on-failure
chatfrontend:
build: frontend
container_name: chatfrontend
hostname: front
networks:
- chatnet
ports:
- 3000:3000
volumes:
chatmysqlvolume:
networks:
chatnet:
driver: bridge

Docker compose container fails to read the application running in different container and in same network

I have docker-compose.yml file which contains frontend,backend,testing,postgres and pgadmin container. The containers except testing are able to communicate each other. But the testing container fails to communicate with backend and frontend container in docker-compose.
version: '3.7'
services:
frontend:
container_name: test-frontend
build:
context: ./frontend
dockerfile: Dockerfile.local
ports:
- '3000:3000'
networks:
- test-network
environment:
# For the frontend can be applied only during the build!
# (while it's applied when TS is compiled)
# You have to build manually without cache if one of those are changed at least for the prod mode.
- REACT_APP_BACKEND_API=http://localhost:8000/api/v1
- REACT_APP_GOOGLE_CLIENT_ID=1234567dfghjjnfd
- CI=true
- CHOKIDAR_USEPOLLING=true
postgres:
image: postgres
environment:
POSTGRES_USER: dev
POSTGRES_PASSWORD: dev
PGDATA: /data/postgres
volumes:
- postgres:/data/postgres
ports:
- "5432:5432"
networks:
- test-network
restart: unless-stopped
pgadmin:
image: dpage/pgadmin4
environment:
PGADMIN_DEFAULT_EMAIL: "dev#dev.com"
PGADMIN_DEFAULT_PASSWORD: dev
volumes:
- pgadmin:/root/.pgadmin
- ./pgadmin-config/servers.json:/pgadmin4/servers.json
ports:
- "5050:80"
networks:
- test-network
restart: unless-stopped
backend:
container_name: test-backend
build:
context: ./backend
dockerfile: Dockerfile.local
ports:
- '8000:80'
volumes:
- ./backend:/app
command: >
bash -c "alembic upgrade head
&& exec /start-reload.sh"
networks:
- test-network
depends_on:
- postgres
environment:
- GOOGLE_APPLICATION_CREDENTIALS=/app/.secret/secret.json
- APP_DB_CONNECTION_STRING=postgresql+psycopg2://dev:dev#postgres:5432/postgres
- LOG_LEVEL=debug
- SQLALCHEMY_ECHO=True
- AUTH_ENABLED=True
- CORS=*
- GCP_ALLOWED_DOMAINS=*
testing:
container_name: test-testing
build:
context: ./testing
dockerfile: Dockerfile
volumes:
- ./testing:/isp-app
command: >
bash -c "/wait
&& robot ."
networks:
- test-network
depends_on:
- backend
- frontend
environment:
- WAIT_HOSTS= frontend:3000, backend:8000
- WAIT_TIMEOUT= 3000
- WAIT_SLEEP_INTERVAL=300
- WAIT_HOST_CONNECT_TIMEOUT=300
volumes:
postgres:
pgadmin:
networks:
test-network:
driver: bridge
All the containers are mapped to test-network. When the testing container tried to connect to frontend:3000 or backend:8000, it throws "Host [ backend:8000] not yet available"
How to fix it?

Not able to configure Selenium Chrome to work with Codeception

I am trying to configure Selenium with Chrome to work with Codeception.
This is my docker-compose.yml file
version: "3.5"
services:
mysql:
image: mysql:8.0
container_name: my-board-mysql
working_dir: /application
command: --default-authentication-plugin=mysql_native_password
hostname: mysql
volumes:
- mysql_storage:/var/lib/mysql
env_file:
- .env
networks:
- backend
ports:
- "9834:3306"
webserver:
build:
dockerfile: ./docker/nginx/Dockerfile
context: .
container_name: my-board-server
working_dir: /application
depends_on:
- php-fpm
networks:
- backend
volumes:
- .:/application
- ./docker/nginx/nginx_local.conf:/etc/nginx/conf.d/default.conf
ports:
- "8924:80"
php-fpm:
build:
dockerfile: docker/php-fpm/Dockerfile-local
context: .
container_name: my-php-fpm
depends_on:
- mysql
volumes:
- .:/application
- ./docker/php-fpm/php-ini-overrides.ini:/etc/php/7.3/fpm/conf.d/99-overrides.ini
networks:
- backend
chrome:
image: selenium/standalone-chrome:latest
ports:
- 4444:4444
container_name: chrome
networks:
- backend
networks:
backend:
driver: bridge
ipam:
config:
- subnet: 172.24.0.0/22
volumes:
mysql_storage:
And this is my Codeception configuration file: acceptance.suite.yml:
actor: AcceptanceTester
modules:
enabled:
- WebDriver:
url: 'http://localhost:8924'
browser: chrome
- \App\Tests\Helper\Acceptance
When I navigate to http://localhost:8924 I can see the site, and when I navigate to http://localhost:4444 I can see Selenium page.
When I run Codeception using vendor/bin/codecept run --steps, I can see this failure:
[Facebook\WebDriver\Exception\UnknownErrorException] unknown error: net::ERR_CONNECTION_REFUSED
(Session info: chrome=89.0.4389.82)
It seems that it is not possible to open localhost from webserver which is under docker. Any advice how can I fix this?
I am running this on Mac if it matters.

Cannot curl/ping to the host ip from inside docker container

I used docker and have nginx as a reverse proxy with just 80 and 443 ports exposed on host machine. I also have other containers and nginx under same network with bridge driver. The problem is I am unable to curl/ping host machine IP from inside docker container. I can access other containers with local DNS though. Please help.
DOCKER-COMPOSE FILE
version: "3"
services:
mongo:
image: mongo
container_name: mongo
environment:
- MONGO_INITDB_ROOT_USERNAME=rootmongo
- MONGO_INITDB_ROOT_PASSWORD=p4Ss#1234#
# For Persistance
volumes:
- db_data:/data/db
- db_backup:/backup
- db_dump:/dump
restart: always
networks:
- app-network
wealth_advisor_admin:
build:
context: ./adminpanel
dockerfile: Dockerfile
image: wealth_advisor_admin
depends_on:
- mongo
restart: unless-stopped
container_name: wealth_advisor_admin
networks:
- app-network
wealth_advisor_web:
build:
context: .
dockerfile: Dockerfile
image: wealth_advisor_web
container_name: wealth_advisor_web
restart: unless-stopped
depends_on:
- mongo
networks:
- app-network
webserver:
image: nginx:mainline-alpine
container_name: webserver
restart: unless-stopped
ports:
- "80:80"
- "443:443"
volumes:
- web-root:/var/www/html
- ./nginx-conf:/etc/nginx/conf.d
- certbot-etc:/etc/letsencrypt
- certbot-var:/var/lib/letsencrypt
- dhparam:/etc/ssl/certs
depends_on:
- wealth_advisor_web
- wealth_advisor_admin
networks:
- app-network
certbot:
image: certbot/certbot
container_name: certbot
volumes:
- certbot-etc:/etc/letsencrypt
- certbot-var:/var/lib/letsencrypt
- web-root:/var/www/html
depends_on:
- wealth_advisor_web
command: ---------------
volumes:
certbot-etc:
web-root:
certbot-var:
db_data:
db_backup:
db_dump:
dhparam:
driver: local
driver_opts:
type: none
device: /root/trade/dhparam/
o: bind
networks:
app-network:
driver: bridge

How to open phpmyadmin as domain in localhost?

I have one docker-compose file that have nginx, mysql and phpmyadmin.
phpmyadmin is open in port 8080 in this address mydomain.com:8080 as well.
How can I convert this address to http://phpmyadmin.mydomain.com or http://pma.mydomain.com in my server?
I have used this docker-compose file:
version: '3'
services:
#PHP Service
app:
build:
context: .
dockerfile: ./Dockerfile/Dockerfile
container_name: app
restart: unless-stopped
tty: true
environment:
SERVICE_NAME: app
SERVICE_TAGS: dev
working_dir: /var/www
volumes:
- ./Beh:/var/www
- ./Config/php/local.ini:/usr/local/etc/php/conf.d/local.ini
networks:
- app-network
#Nginx Service
webserver:
image: nginx:alpine
container_name: webserver
restart: unless-stopped
tty: true
ports:
- "80:80"
# - "443:443"
volumes:
- ./Beh:/var/www
- ./Config/nginx/conf.d/:/etc/nginx/conf.d/
networks:
- app-network
database:
image: mariadb:latest
container_name: database
environment:
- "MYSQL_USERNAME=root"
- "MYSQL_ROOT_PASSWORD=secret"
ports:
- "3306:3306"
networks:
- app-network
phpmyadmin:
image: phpmyadmin/phpmyadmin
container_name: phpmyadmin
environment:
- "MYSQL_USERNAME=root"
- "MYSQL_ROOT_PASSWORD=secret"
- "PMA_HOST=database"
links:
- database
ports:
- "8080:80"
networks:
- app-network
#Docker Networks
networks:
app-network:
driver: bridge
#Volumes
volumes:
dbdata:
driver: local
I don't know how to configure nginx file to achieve this goal.
You can use the repo -> https://github.com/jwilder/nginx-proxy
This is what I'm using to create new local domain name for each project.
You have to add "VIRTUAL_HOST" to the environment, expose the ports ("expose: 80") and also add the new domain in your /etc/hosts.

Resources