Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linking django and mysql containers using docker-compose

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
like image 343
michaelmwangi Avatar asked Jun 24 '15 20:06

michaelmwangi


People also ask

How do I connect to a MySQL Docker container?

Here are the steps you can follow to install the Dockerhub MySQL Container: Step 1: Pull the Docker Image for MySQL. Step 2: Deploy and Start the MySQL Container. Step 3: Connect with the Docker MySQL Container.


2 Answers

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

like image 137
AnotherLongUsername Avatar answered Sep 28 '22 21:09

AnotherLongUsername


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.

like image 24
Thomasleveil Avatar answered Sep 28 '22 21:09

Thomasleveil