17

I've been following with the docker-compose tutorial here (linking django and postgres container). Although I was able to go through with the tutorial I'm however not able to proceed with repeating the same using a mysql container. The following are my dockerfile and docker-compose.yml `

db:
  image: mysql
web:
  build: .
  command: python manage.py runserver 0.0.0.0:8000
  volumes:
    - .:/code
  ports:
    - "8000:8000"
  links:
    - db:db

` dockerfile

FROM python:2.7
RUN mkdir /code
WORKDIR /code
RUN pip install mysql-python
RUN pip install django

They both build fine when I do docker-compose up but it seems the db environment variables are not passed to the django container since when I run os.environ.keys() in one of my django views I can't see any of the expected DB_* environment variables. So does mysql require a different setup or am I missing something. Thank you.

[EDIT] Docker compose version

docker-compose version: 1.3.0
CPython version: 2.7.9
OpenSSL version: OpenSSL 1.0.1e 11 Feb 2013

Docker version

Docker version 1.6.2, build 7c8fca2
michaelmwangi
  • 284
  • 1
  • 2
  • 13
  • What versions of Docker and docker-compose are you using? Can you share the output of `docker-compose up`? Also can you share the output of `docker-compose run web env` as suggested by @dnozay – Thomasleveil Jun 25 '15 at 06:36
  • https://hub.docker.com/_/mysql/#environment-variables – user81269 Sep 10 '15 at 00:38
  • https://github.com/pahaz/docker-compose-django-postgresql-radis-example - django + redis + postgres + nginx compose example – pahaz Jun 24 '16 at 16:29

3 Answers3

15

In Django settings.py file make sure you have something like:

DATABASES = {
'default': {
    'ENGINE': 'django.db.backends.mysql',
    'NAME': 'django1',
    'USER': 'django',
    'PASSWORD': 'password', 
    'HOST': 'db',
    'PORT': 3306,
    }
}

then in your docker-compose.yml file make sure you have something along the lines of:

db:
  image: mysql
  environment:
    MYSQL_ROOT_PASSWORD: docker
    MYSQL_DATABASE: docker
    MYSQL_USER: docker
    MYSQL_PASSWORD: docker

then as per the docker/django tutorial you are following run the following again to rebuild everything and things should start working

docker-compose run web django-admin.py startproject composeexample .

In response to a further question, the mysql root password variable is required by docker when creating new databases.

EDIT: added run to docker-compose above; see edit comment

Marc L.
  • 3,296
  • 1
  • 32
  • 42
  • Awesome, it helped me finally. Still not sure how does that `HOST: 'db'` work? – sam Jul 10 '17 at 17:54
  • 1
    Hi Sam, the HOST is the fully qualified domain name, or IP address of a remote database host. If you leave the value blank it means use a database on the localhost. I have also had success with specifying localhost as 'HOST': '127.0.0.1' but you should be able to just leave it blank. – AnotherLongUsername Jul 12 '17 at 08:35
  • Why do we need the `MYSQL_ROOT_PASSWORD` ? Application should not need the root password, right ? – Rakmo Mar 20 '18 at 16:37
  • Thank you for making me realise the obvious error in my docker-compose.yml. This configuration does indeed work although for the user new to docker the above mentioned docker-compose config is just for the database. You will still need the service "web:" as shown here https://docs.docker.com/compose/django/ which will support the web service config. – S.Jamal Mar 15 '19 at 13:06
3

you don't need to worry about environment variable. When linking containers together you just use the container alias defined by the link as if it was the hostname.

for instance if your docker-compose.yml file were:

db:
  image: postgres
web:
  build: .
  command: python manage.py runserver 0.0.0.0:8000
  volumes:
    - .:/code
  ports:
    - "8000:8000"
  links:
    - db:mydb

In your django settings you would have to set the database host to mydb.

Thomasleveil
  • 95,867
  • 15
  • 119
  • 113
  • 1
    Thanks for your answer please check my edit. I had made the mistake of using postgres in the question instead of mysql in the docker-compose.yml.It was a copy paste error. I have however tried your approach and I get Unknown mysql server host 'mydb' – michaelmwangi Jun 25 '15 at 04:34
  • 1
    Now a days you don't even need to use `links`. Per https://docs.docker.com/compose/compose-file/#links : "Links are not required to enable services to communicate - by default, any service can reach any other service at that service’s name." – Jorge Orpinel Pérez Mar 13 '18 at 03:18
  • what is the syntax .:/code means? Here i did not understand the use of . and : Is it mounting the current directory to /code? – milan Jul 26 '18 at 14:45
  • `.` is for _current directory_, the one the docker-compose.yml file is in. `:` is just a separator. `/code` is the path inside the container on which the content of the local current directory will be mounted on – Thomasleveil Jul 26 '18 at 18:12
2

First you need to modify the settings file...

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': 'postgres',
        'USER': 'postgres',
        'HOST': 'db',
        'PORT': 5432,
    } }

Then if you used the docker-compose command properly, the containers should be linked, and it should resolve the hostname db properly based on the links in the docker-compose.yml file.

Still, if you want to check the environment...

~/django-example: docker-compose run web env
Starting djangoexample_db_1...
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
HOSTNAME=66ff09ed8632
TERM=xterm
DJANGOEXAMPLE_DB_1_PORT=tcp://172.17.0.35:5432
DJANGOEXAMPLE_DB_1_PORT_5432_TCP=tcp://172.17.0.35:5432
DJANGOEXAMPLE_DB_1_PORT_5432_TCP_ADDR=172.17.0.35
DJANGOEXAMPLE_DB_1_PORT_5432_TCP_PORT=5432
DJANGOEXAMPLE_DB_1_PORT_5432_TCP_PROTO=tcp
DJANGOEXAMPLE_DB_1_NAME=/djangoexample_web_run_2/djangoexample_db_1
DJANGOEXAMPLE_DB_1_ENV_affinity:container==52c78c810792b0e7b9a231eab7ab7a3d50c95b76faf0abb8ec38a7d1ff0c7e5f
DJANGOEXAMPLE_DB_1_ENV_LANG=en_US.utf8
DJANGOEXAMPLE_DB_1_ENV_PG_MAJOR=9.4
DJANGOEXAMPLE_DB_1_ENV_PG_VERSION=9.4.4-1.pgdg70+1
DJANGOEXAMPLE_DB_1_ENV_PGDATA=/var/lib/postgresql/data
DB_PORT=tcp://172.17.0.35:5432
DB_PORT_5432_TCP=tcp://172.17.0.35:5432
DB_PORT_5432_TCP_ADDR=172.17.0.35
DB_PORT_5432_TCP_PORT=5432
DB_PORT_5432_TCP_PROTO=tcp
DB_NAME=/djangoexample_web_run_2/db
DB_ENV_affinity:container==52c78c810792b0e7b9a231eab7ab7a3d50c95b76faf0abb8ec38a7d1ff0c7e5f
DB_ENV_LANG=en_US.utf8
DB_ENV_PG_MAJOR=9.4
DB_ENV_PG_VERSION=9.4.4-1.pgdg70+1
DB_ENV_PGDATA=/var/lib/postgresql/data
DB_1_PORT=tcp://172.17.0.35:5432
DB_1_PORT_5432_TCP=tcp://172.17.0.35:5432
DB_1_PORT_5432_TCP_ADDR=172.17.0.35
DB_1_PORT_5432_TCP_PORT=5432
DB_1_PORT_5432_TCP_PROTO=tcp
DB_1_NAME=/djangoexample_web_run_2/db_1
DB_1_ENV_affinity:container==52c78c810792b0e7b9a231eab7ab7a3d50c95b76faf0abb8ec38a7d1ff0c7e5f
DB_1_ENV_LANG=en_US.utf8
DB_1_ENV_PG_MAJOR=9.4
DB_1_ENV_PG_VERSION=9.4.4-1.pgdg70+1
DB_1_ENV_PGDATA=/var/lib/postgresql/data
LANG=C.UTF-8
PYTHON_VERSION=2.7.10
PYTHON_PIP_VERSION=7.0.3
PYTHONUNBUFFERED=1
HOME=/root
dnozay
  • 23,846
  • 6
  • 82
  • 104
  • Hi thanks for your answer am sorry but I had made a copy pasting error please see my edited docker-compose.yml file. Thank you – michaelmwangi Jun 25 '15 at 04:36
  • Now a days you don't even need to use `links`. Per https://docs.docker.com/compose/compose-file/#links : "Links are not required to enable services to communicate - by default, any service can reach any other service at that service’s name." – Jorge Orpinel Pérez Mar 13 '18 at 03:19