Reputation: 525
I have a Django app in one container, trying to compose it with Redis and Mysql containers. However, calling
redis://redis:6379
from Django app, gives me
ConnectionError: Error -2 connecting redis://redis:6379. Name or service not known.
I do not see any difference in my setup, compare to other working solutions. Do I miss something? My docker-compose.yml
version: '3'
services:
web:
build: .
environment:
- REDIS_HOST=redis
restart: always
command: bash -c "python manage.py runserver --settings=settings.production_cs 0.0.0.0:8000"
container_name: eprofi
volumes:
- .:/eprofi
ports:
- "8000:8000"
depends_on:
- db
- redis
links:
- db:postgres
- redis:redis
db:
image: mysql:latest
command: mysqld --default-authentication-plugin=mysql_native_password
volumes:
- "./mysql:/var/lib/mysql"
ports:
- "3306:3306"
restart: always
environment:
- MYSQL_ROOT_PASSWORD=secret123
- MYSQL_DATABASE=django_app
- MYSQL_USER=django_app
- MYSQL_PASSWORD=django_app123
redis:
restart: always
image: redis:latest
ports:
- "6379:6379"
My Dockerfile
# We Use an official Python runtime as a parent image
FROM python:2.7.13
ENV DEBIAN_FRONTEND noninteractive
ENV PYTHONDONTWRITEBYTECODE 1
# The enviroment variable ensures that the python output is set straight
# to the terminal with out buffering it first
ENV PYTHONUNBUFFERED 1
# create root directory for our project in the container
RUN mkdir /www
# Set the working directory to /www
WORKDIR /www
#Upgrade pip
RUN pip install pip -U
#Install dependencies
ADD requirements.txt /www/
RUN pip install -r requirements.txt --src /usr/local/src
# Copy the current directory contents into the container at /www
ADD . /www/
Then I do
docker-compose build
docker-compose up
Everything starts as expected, but calling Redis with "redis://redis:6379" gives the above "Name or service not known." error.
My tools
docker-compose version 1.17.1, build unknown
docker-py version: 2.5.1
Client: Docker Engine - Community
Version: 19.03.4
UPDATE 1 How I call Redis from python app (old Django).
Settings of Django app:
CACHES = {
'my_cache': {
'BACKEND': 'redis_cache.RedisCache',
'LOCATION': 'redis://{}:{}'.format(REDIS_HOST, REDIS_PORT),
'KEY_PREFIX': 'project/my_cache',
'TIMEOUT': 60*60*24, # expire in 24 hours
'OPTIONS': {
'DB': 1,
'PARSER_CLASS': 'redis.connection.HiredisParser'
}
},
}
Call:
cache = get_cache('my_cache')
cache.get(cache_key)
UPDATE 2
Command
ping redis
run in the container gives me an error
ping: unknown host
Upvotes: 8
Views: 22646
Reputation: 3730
I've had a similar issue with the V2 release of Docker Compose introduced in Docker Desktop. For now, simply unchecking the option solves the problem in my case.
Not sure if this is because links are going to be deprecated in docker-compose or not, it seems that the doc still lists the command without deprecation warning.
Upvotes: 3
Reputation: 4877
The links
option is deprecated according to the documentation. In order the services to call each other, they should be on the same network. So for your example the solution is something like this. You can learn more about network here.
version: '3'
services:
web:
...
networks:
- my_network
db:
...
networks:
- my_network
redis:
...
networks:
- my_network
networks:
my_network:
external:
name: my_network
Upvotes: 1