Create a container Django in docker is a bit more complicated than other framework, because and I don’t know the why, the official Django image is deprecated from your same creators, and they advise to use an approach based on the creation of a own Dockerfile and docker-compose.
So we start creating a new empty folder, inside this folder we create a file named Dockerfile
In Dockerfile insert this:
FROM python:2.7
ENV PYTHONUNBUFFERED 1
RUN mkdir /code
WORKDIR /code
ADD requirements.txt /code/
RUN pip install -r requirements.txt
ADD . /code/
Now we go to the same folder to create a file called requirements.txt, and in this file we are going to include all the Django packages and libraries which we need in our project (it is the same thing of the command pip install).
Here an example of a file requirements.txt
Django==1.9.8
argcomplete==1.8.2
autopep8==1.2.4
beautifulsoup4==4.5.3
chardet==2.3.0
click==6.7
configparser==3.5.0
Django==1.9.8
django-appconf==1.0.1
django-carton==1.2.1
django-filer==1.2.5
django-filter==1.0.0
django-grappelli==2.9.1
django-image-cropping==1.0.4
django-mptt==0.8.6
Now ever in the same folder we go to create a file named docker-compose.yml, and we going the elements that we want run in docker, so our Django instance, a Mysql Database and a phpmyadmin.
Here how:
version: '2'
services:
db:
image: mysql
ports:
- "3306:3306"
environment:
MYSQL_ROOT_PASSWORD: mypassword
MYSQL_USER: root
MYSQL_PASSWORD: mypassword
MYSQL_DATABASE: django-test
web:
build: .
command: python manage.py runserver 0.0.0.0:8000
volumes:
- .:/code
ports:
- "8000:8000"
depends_on:
- db
phpmyadmin:
image: phpmyadmin/phpmyadmin
environment:
- PMA_ARBITRARY=1
restart: always
ports:
- 8082:80
volumes:
- /sessions
Now open the terminal, go inside the folder and launch this series of commands:
For create the instance:
docker-compose build
Now we check that instances are running:
docker-compose up
Now CTRL-C for stop the instance, and we go now to create a Django project inside the instance, in this way:
docker-compose run web django-admin.py startproject composeexample .
After we can start all the instance in daemon mode, with this:
docker-compose up -d
if it’s all ok, with the command docker ps we should get this in terminal:
And on our browser, at the address http://0.0.0.0:8000/
On the port :8082 (http://0.0.0.0:8082/) you will find phpmyadmin:
I hope that this post can be useful to someone, and for more info take a look at my repository here:
My weekly newsletter
Have a nice day!



Nessun commento:
Posta un commento