I am trying to connect my Nestjs application to Redis as follows:
This the content of my docker-compose.yml
version: '3.7'
services:
main:
container_name: main
build:
context: .
target: development
volumes:
- .:/usr/src/app
- /usr/src/app/node_modules
ports:
- ${PORT}:${PORT}
- 9229:9229
command: yarn start:dev
env_file:
- .env
networks:
- webnet
redis:
image: 'redis:alpine'
networks:
webnet:
In order to boostrap redis functionality in my nestjs application, I am using nestjs-redis and the relevant portion of app.module.ts looks like this:
import { RedisModule } from 'nestjs-redis';
#Module({
imports: [
RedisModule.register({})
]
})
However, when I try to run docker-compose up in my setup, I get the following error:
[ioredis] Unhandled error event: Error: connect ECONNREFUSED 127.0.0.1:6379
Thank you for helping!
As you are using docker-compose.yml, try to use the internal links capability.
version: '3.7'
services:
main:
container_name: main
build:
context: .
target: development
volumes:
- .:/usr/src/app
- /usr/src/app/node_modules
ports:
- ${PORT}:${PORT}
- 9229:9229
command: yarn start:dev
links:
- redis
env_file:
- .env
networks:
- webnet
redis:
image: 'redis:alpine'
ports:
- 6379:6379
networks:
- webnet
networks:
webnet:
and in your snippet, pass the host in the following manner
import { RedisModule } from 'nestjs-redis';
#Module({
imports: [
RedisModule.register({host: "redis"})
]
})
Related
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?
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
I am running dev containers on my project that utilizes docker-compose for multiple containers.
My issue is that I cannot view my docker-compose logs. I am not sure how to access it.
Inside the folder .devcontainer I have two files:
devcontainer.json:
{
"name": "TrendR",
"dockerComposeFile": [
"../docker-compose.yml",
"docker-compose.yml"
],
"service": "api",
"workspaceFolder": "/workspace",
"settings": {
"python.pythonPath": "/usr/local/bin/python",
"python.linting.enabled": true,
"python.linting.pylintEnabled": true,
},
"extensions": ["ms-python.python","ms-azuretools.vscode-docker"]
}
docker-compose.yml:
version: '3.8'
services:
api:
volumes:
- .:/workspace:cached
- /var/run/docker.sock:/var/run/docker.sock
command: /bin/sh -c "while sleep 1000; do :; done"
This is the main docker-compose.yml inside the project folder.
version: "3.8"
services:
db:
container_name: db
image: postgres:13
ports:
- "5433:5432"
environment:
- POSTGRES_USER=${POSTGRES_USER}
- POSTGRES_PASSWORD=${POSTGRES_PASSWORD}
- POSTGRES_DB=${POSTGRES_DB}
volumes:
- /var/lib/postgresql/data
api:
build:
context: ./api/
dockerfile: Dockerfile
volumes:
- ./api/app:/app/app
ports:
- "1000:80"
depends_on:
- db
env_file:
- .env
command: ["/start-reload.sh"]
labels:
- "traefik.enable=true"
- "traefik.http.routers.${API_SUBDOMAIN}.rule=Host(`${API_SUBDOMAIN}.${DOMAIN}`)"
frontend:
build:
context: ./frontend/
dockerfile: Dockerfile
ports:
- "3000:3000"
volumes:
- /app/node_modules
- ./frontend:/app
environment:
- NODE_ENV=development
stdin_open: true
links:
- api
labels:
- "traefik.enable=true"
- "traefik.http.routers.${CLIENT_SUBDOMAIN}.rule=Host(`${CLIENT_SUBDOMAIN}.${DOMAIN}`)"
redis:
container_name: trendr_redis
image: "redis:alpine"
ports:
- "6379:6379"
traefik:
image: traefik:v2.4
ports:
- "80:80"
- "8080:8080"
volumes:
- "/var/run/docker.sock:/var/run/docker.sock:ro"
- "$PWD/traefik/traefik.dev.toml:/etc/traefik/traefik.toml"
If you open a terminal and run docker-compose up in the same location as the docker-compose file you should see your logs
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.
ERROR: Encountered errors while bringing up the project.
Docker-compose up -d --build
Docker ps
------------- Docker-compose.yml -----------------
version: "3"
services:
php:
build:
context: ./docker/php
container_name: 'web'
env_file: ./docker/php/.env
restart: 'always'
ports:
- "80:80"
- "443:443"
links:
- mssql
volumes:
- ./:/var/www/html
- ./docker/config/php/php.ini:/usr/local/etc/php/php.ini
- ./docker/config/vhosts:/etc/apache2/sites-enabled
- ./docker/logs/apache2:/var/log/apache2
- ./docker/logs/php:/var/log/php
environment:
PHP_IDE_CONFIG: "serverName=local.dev.com"
mssql:
build: ./docker/mssql
container_name: 'mssql'
env_file: ./docker/mssql/.env
ports:
- "1433:1433"
volumes:
- ./docker/data/mssql:/var/opt/mssql
Make sure your port 80 isn't already in use.
Also, here's a thread with several possible solutions.