I'm having issues running a pm2 app on a container, I tried accessing through docker port and with an nginx proxy, but none of these solutions are working. Here's my docker config:
version: '3.5'
services:
webapp:
build:
context: .
image: ${DOCKER_IMAGE}
container_name: mypm2app
stdin_open: true
networks:
- "default"
restart: always
ports:
- "8282:8282"
extra_hosts:
- host.local:${LOCAL_IP}
db:
image: mongo:4.2.6
container_name: mongodb
restart: always
environment:
MONGO_INITDB_ROOT_USERNAME: ${MONGO_INITDB_ROOT_USERNAME}
MONGO_INITDB_ROOT_PASSWORD: ${MONGO_INITDB_ROOT_PASSWORD}
MONGO_INITDB_DATABASE: ${MONGO_INITDB_DATABASE}
volumes:
- ${MONGO_SCRIPT_PATH}:${MONGO_SCRIPT_DESTINATION_PATH}
networks:
- "default"
networks:
default:
external:
name: ${NETWORK_NAME}
Also I have this dockerfile:
FROM image
WORKDIR /var/www/html/path
COPY package.json /var/www/html/path
RUN npm install
COPY . /var/www/html/path
EXPOSE 8282/tcp
CMD pm2-runtime start ecosystem.config.js --env development
pm2 is starting the service, but I cannot access it through localhost:port.
I tried to add an nginx proxy:
nginx:
depends_on:
- webapp
restart: always
build:
dockerfile: Dockerfile.dev
context: ./nginx
ports:
- "3002:80"
networks:
default:
ipv4_address: ${nginx_ip}$
with this docker-file
FROM nginx
COPY ./default.conf /etc/nginx/conf.d/default.conf
This is the nginx configuration, default.conf:
upstream mypm2app {
server mypm2app:8282;
}
server {
listen 80;
server_name mypm2app.local;
location / {
proxy_pass http://mypm2app/;
}
}
I would appreciate any suggestion or answer to this issue.
Related
I have a node-express server running inside a dockerized container with expose port 3001. My nginx is installed on OS with sudo apt install nginx. What I want is everytime a call is made to app.domain.com:3001 I want to redirect that call to localhost:3001.I am new to nginx configuration. I would prefer I could do the same with *.conf file in conf.d folder of nginx.Also the response of API should have same domain .domain.com so that I can set httpOnly cookies on a angular app running on app.domain.com
My node docker-compose file:
version: "3"
services:
container1:
image: node:18.12-alpine
working_dir: /usr/src/app
container_name: container1
depends_on:
- container_mongodb
restart: on-failure
env_file: .env
ports:
- "$APP_PORT:$APP_PORT"
volumes:
- .:/usr/src/app
networks:
- network1
command: ./wait-for.sh container_mongodb:$MONGO_PORT -- npm run dev
container_mongodb:
image: mongo:6.0.3
container_name: container_mongodb
restart: on-failure
env_file: .env
environment:
- MONGO_INITDB_ROOT_USERNAME=$MONGO_USERNAME
- MONGO_INITDB_ROOT_PASSWORD=$MONGO_PASSWORD
ports:
- "$MONGO_EXPOSE_PORT:$MONGO_PORT"
volumes:
- container_mongodb_data:/data/db
- ./src/config/auth/mongodb.key:/data/mongodb.key
networks:
- network1
entrypoint:
- bash
- -c
- |
cp /data/mongodb.key /data/mongodb.copy.key
chmod 400 /data/mongodb.copy.key
chown 999:999 /data/mongodb.copy.key
exec docker-entrypoint.sh $$#
command: ["mongod", "--replSet", "rs0", "--bind_ip_all", "--keyFile", "/data/mongodb.copy.key"]
networks:
network1:
external: true
volumes:
container_mongodb_data:
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
I'm trying to make requests to my server container from my client app in another container. The Docker Compose docs state that the network is setup automatically, so shouldn't all ports be accessible from all containers? When I make a curl request to port 4000 from outside of the container (in a fresh terminal), it works. However when I enter the client container (selektor-client) and try the same request, it fails.
curl --request POST http://localhost:4000/api/music
What am I doing wrong?
docker-compose.yaml:
version: "3"
services:
client:
container_name: selektor-client
restart: always
build: ./client
ports:
- "3000:3000"
volumes:
- ./client/:/client/
- /client/node_modules/
command: ["yarn", "start"]
server:
container_name: selektor-server
restart: always
build: ./server
ports:
- "4000:4000"
volumes:
- ./server/:/server/
depends_on:
- mongo
command: ["yarn", "start"]
mongo:
container_name: selektor-mongo
command: mongod --noauth
build: .
restart: always
volumes:
# - ./mongo-init.js:/docker-entrypoint-initdb.d/mongo-init.js:ro
- data-volume:/data/db
ports:
- "27017:27017"
volumes:
data-volume:
When you call containers of the same stack each other, the host name is, by default, the service name, and the port is the internal port: <servicename>:<internal_port>. So, based on this part of your example:
version: "3"
server:
container_name: selektor-server
restart: always
build: ./server
ports:
- "4000:4000"
volumes:
- ./server/:/server/
depends_on:
- mongo
command: ["yarn", "start"]
The url you client have to use to reach the server is http://server:4000
I had the same problem . my solution was registering network and giving subnet mast to the network and register the static ip to each container :
version: "3"
networks:
my_network:
driver: bridge
ipam:
config:
- subnet: 172.20.0.0/24
services:
mongodb:
image: mongo:4.2.1
container_name: mongo
command: mongod --auth
hostname: mongo
networks:
my_network:
ipv4_address: 172.20.0.5
volumes:
- /data/db/mongo:/data/db
ports:
- "27017:27017"
rabitmq:
hostname: rabbitmq
container_name: rabbitmq
image: rabbitmq:latest
networks:
my_network:
ipv4_address: 172.20.0.3
volumes:
- /var/lib/rabbitmq:/data/db
ports:
- "5672:5672"
- "15672:15672"
restart: always
you can access through IP addresses
My client isn't able to send requests to backend because it can't resolve the host. I'm trying to pass down the container connection info via an environment variable and use it in the client to connect. However, it is unable to do the requests at all. Any help? Nginx works fine for the frontend part but doesn't work for proxying the backend.
docker-compose.yml
version: '3.2'
services:
server:
restart: always
build:
dockerfile: Dockerfile
context: ./nginx
depends_on:
- backend
- frontend
- database
ports:
- '5000:80'
database:
image: postgres:latest
container_name: database
ports:
- "5432:5432"
restart: always
hostname: database
expose:
- 5432
backend:
build:
context: ./backend
dockerfile: ./Dockerfile
image: kalendae_backend:latest
hostname: backend
container_name: backend
ports:
- "5051:5051"
environment:
- WAIT_HOSTS=database:5432
- DATABASE_HOST=database
- DATABASE_PORT=5432
- PORT=5051
links:
- database
expose:
- 5051
frontend:
build:
context: ./frontend
dockerfile: ./Dockerfile
image: kalendae_frontend:latest
ports:
- "5050:5050"
hostname: frontend
container_name: frontend
environment:
- WAIT_HOSTS=backend:5051
- REACT_APP_BACKEND_HOST=backend
- REACT_APP_BACKEND_PORT=5051
links:
- backend
expose:
- 5050
Nginx config
upstream frontend {
server frontend:5050;
}
upstream backend {
server backend:5051;
}
upstream server {
server server:5000;
}
server {
listen 80;
location / {
proxy_pass http://frontend;
}
location backend {
proxy_pass http://backend;
}
location /backend {
proxy_pass http://backend;
}
}
I would like to know if it's possible to use my docker container name as host instead of the IP.
Let me explain, here's my docker-compose file :
version : "3"
services:
pandacola-logger:
build: ./
networks:
- logger_db
volumes:
- "./:/app"
ports:
- 8060:8060
- 10060:10060
command: npm run dev
logger-mysql:
image: mysql
networks:
- logger_db
command: --default-authentication-plugin=mysql_native_password
environment:
MYSQL_ROOT_PASSWORD: Carotte1988-
MYSQL_DATABASE: logger
MYSQL_USER: logger-user
MYSQL_PASSWORD: PandaCola-
ports:
- 3306:3306
adminer:
networks:
- logger_db
image: adminer
restart: always
ports:
- 8090:8090
networks:
logger_db: {}
Sorry the intentation is a bit messy
I would like to set the name of my logger-mysql in a the .env file of my webservice (the pandacola-logger) instead of his IP adress
here's the .env file
HOST=0.0.0.0
PORT=8060
NODE_ENV=development
APP_NAME=AdonisJs
APP_URL=http://${HOST}:${PORT}
CACHE_VIEWS=false
APP_KEY=Qs1GxZrmQf18YZ9V42FWUUnnxLfPetca
DB_CONNECTION=mysql
DB_HOST=0.0.0.0 <---- here's where I want to use my container's name
DB_PORT=3306
DB_USER=logger-user
DB_PASSWORD=PandaCola-
DB_DATABASE=logger
HASH_DRIVER=bcrypt
If you can tell me first, if it's possible, and then, how to do it, it would be lovely.
By default Compose sets up a single network for your app. Each container for a service joins the default network and is both reachable by other containers on that network, and discoverable by them at a hostname identical to the container name.
Reference
For Example:
version: '2.2'
services:
redis:
image: redis
container_name: cache
expose:
- 6379
app:
build: ./
volumes:
- ./:/var/www/app
ports:
- 7731:80
environment:
- REDIS_URL=redis://cache
- NODE_ENV=development
- PORT=80
command:
sh -c 'npm i && node server.js'
networks:
default:
external:
name: "tools"