Visualizzazione post con etichetta django. Mostra tutti i post
Visualizzazione post con etichetta django. Mostra tutti i post

mercoledì 11 aprile 2018

My Django Docker image


A description, step by step of how I builded my docker django image, how i loaded it on docker hub and how can be use it and customized.

STEP 1 — Create Dockerfile and push it on Docker Hub

In a folder I create a file named Dockerfile and an another named requirements.txt
Dockerfile
FROM python:2.7
LABEL maintainer "Pierangelo Orizio <pierangelo1982@gmail.com>"
RUN apt-get update -qq && apt-get install build-essential g++ flex bison gperf ruby perl \
  mysql-client \ 
  libsqlite3-dev libmysqlclient-dev libfontconfig1-dev libicu-dev libfreetype6 libssl-dev \
  postgresql-client \
  libpng-dev libjpeg-dev python libx11-dev libxext-dev -y
ENV PYTHONUNBUFFERED 1
RUN mkdir /code
WORKDIR /code
ADD requirements.txt /code/
COPY . /code
RUN pip install -r requirements.txt
#ADD . /code/
RUN django-admin.py startproject djangoproject
RUN mkdir /code/djangoproject/media && mkdir /code/djangoproject/static
VOLUME /code
WORKDIR /code/djangoproject
EXPOSE 8000
CMD ["python", "manage.py", "runserver", "0.0.0.0:8000"]
requirements.txt
Django==1.9.8
django-filer==1.2.5
django-filter==1.0.0
django-image-cropping==1.0.4
django-mptt==0.8.6
from terminal inside the folder, I build the local image:
docker build -t django .
tag the local image:
docker tag django pierangelo1982/django
sign in to your docker account:
docker login
push the image on your Docker Hub account:
docker push pierangelo1982/django
P.S: pierangelo1982 is my docker account username, change it with yours.

STEP 2 — create and implement container

pull the image:
docker pull pierangelo1982/django
For work and implement our project, we have need to share the project folders of the container on our local host. For do that we have need to build a volume and copy the file.
create a volume:
docker volume create --name django-volume
connect the volume to the container for can copy the project folder
docker run --name django-test \
 -v django-volume:/code \
 -p 8000:8000 \
 -d pierangelo1982/django
copy project folders in your host
docker cp django-test:/code ~/my/local/path/myfolder
remove the container
docker rm -f django-test
recreate the container with the volume that point to our local folder where before we have copy the folders of the project.
docker run --name django-test \
 -v ~/my/local/path/myfolder:/code \
 -p 8000:8000 \
 -d pierangelo1982/django

Using django and python commands:

docker exec django-test python manage.py

how install new packages:

docker exec django-pierangelo pip install
Source code:

Docker Hub:

Subscribe my weekly newsletter:

Bye!

giovedì 29 marzo 2018

Django with mysql and phpmyadmin in Docker

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!

Install Elasticsearch on Ubuntu

ELK STACK install Java sudo apt-get install default-jdk Elasticsearch 6 wget -qO - https://artifacts.elastic.co/GPG-KEY-elasticsearch...