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
3

3 Answers 3

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

Sign up to request clarification or add additional context in comments.

4 Comments

Awesome, it helped me finally. Still not sure how does that HOST: 'db' work?
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.
Why do we need the MYSQL_ROOT_PASSWORD ? Application should not need the root password, right ?
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 docs.docker.com/compose/django which will support the web service config.
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.

4 Comments

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'
Now a days you don't even need to use links. Per 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."
what is the syntax .:/code means? Here i did not understand the use of . and : Is it mounting the current directory to /code?
. 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
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

2 Comments

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
Now a days you don't even need to use links. Per 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."

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.