Skip to content

Latest commit

 

History

History
executable file
·
1103 lines (859 loc) · 25.1 KB

Dockerize_django.md

File metadata and controls

executable file
·
1103 lines (859 loc) · 25.1 KB

Dockerizando Django y +

Django on Docker

🚧 En la carpeta Docker se encuentra el ejemplo dj_docker

🐳 + 🐍 Docker + Django

Descargar la imagen de PostgreSQL

docker pull postgres

Iniciar una instancia de PostgreSQL

docker run --name some-postgres -e POSTGRES_PASSWORD=mysecretpassword -d postgres

Ya que el contenedor/imagen se este ejecutando, en otra terminal(o en la misma), ejecutar el siguiente comando, para poder entrar al contenedor

docker exec -it postgres psql -U postgres

Crear una carpeta dj_docker

Verificar la version de pip

python3 -m pip --version

Instalar pipenv

python3 -m pip install --user pipenv

Crear carpeta con el nombre del projecto

mkdir dj_docker

Crear el ambiente virtual

# --tree QUE OCUPE PYTHON 3
$ pipenv --three

Instalar django

pipenv install django

Instalar gunicorn

pipenv install gunicorn

Activar ambiente virtual

pipenv shell

Crear un proyecto en django

pipenv run django-admin startproject dj_docker .

BD local y migraciones

python manage.py makemigrations
python manage.py migrate
python manage.py createsuperuse
#
USER: dj_doker_2020
PWD: dj docker 2020

Actualizar dentro de settings.py

# importar os
import os
...
# DEBUG can be True/False or 1/0
DEBUG = int(os.environ.get('DEBUG', default=1))

Crear el archivo .env en carpeta raiz solo con la info de DEBUG=1

touch .env
# EDITAR EL ARCHIVO Y PONER SOLO LA INFO
DEBUG=1

Probar la configuración con

gunicorn dj_docker.wsgi:application --bind 0.0.0.0:8000

En local browser ir a la dirección http://localhost:8000/

Crear el archivo Dockerfile la imagen y el contenedor

Dentro de la carpeta del proyecto dj_docker crear el archivo Dockerfile

touch Dockerfile

Estructura de archivos y carpetas del proyecto

.
├── db.sqlite3
├── dj_docker
│   ├── asgi.py
│   ├── __init__.py
│   ├── settings.py
│   ├── urls.py
│   └── wsgi.py
├── Dockerfile
├── .env
├── install-packages.sh
├── manage.py
├── Pipfile
└── Pipfile.lock

El archivo Dockerfile ocupa la imagen

docker pull $ docker pull python:3.9-slim-buster
# BASE IMAGE
FROM python:3.9-slim-buster

# RUN ITS CONTENT THE FILE TO INSTALL
# UPDATES FROM THE DEBIAN REPOSITORIES
COPY install-packages.sh .
RUN chmod +x install-packages.sh
RUN ./install-packages.sh

# CREATE AND SET WORKING DIRECTORY
RUN mkdir /app
WORKDIR /app

# ADD CURRENT DIRECTORY CODE TO WORKING DIRECTORY
ADD . /app/

# SET DEFAULT ENVIRONMENT VARIABLES
ENV PYTHONUNBUFFERED 1
ENV PYTHONDONTWRITEBYTECODE 1
ENV DEBIAN_FRONTEND=noninteractive
# ENV LANG C.UTF-8

# SET PROJECT ENVIRONMENT VARIABLES
# GRAB THESE VIA PYTHON'S os.environ
# THESE ARE 100% OPTIONAL HERE
ENV PORT=8000

RUN apt-get update \
  # && apt-get install -y apt-utils \
  # DEPENDENCIES FOR BUILDING PYTHON PACKAGES
  && apt-get install -y build-essential \
  # PSYCOPG2 DEPENDENCIES
  && apt-get install -y libpq-dev \
  # TRANSLATIONS DEPENDENCIES
  && apt-get install -y gettext \
  # INSTALL GIT
  && apt-get install -y git \
  # CLEANING UP UNUSED FILES
  && apt-get purge -y --auto-remove -o APT::AutoRemove::RecommendsImportant=false \
  && rm -rf /var/lib/apt/lists/*

# INSTALL ENVIRONMENT DEPENDENCIES
RUN pip3 install --upgrade pip
RUN pip3 install pipenv

# INSTALL DEPENDENCIES FOR PROJECT FROM PIPFILE
RUN pipenv install --skip-lock --system --dev

EXPOSE 8888
CMD gunicorn dj_docker.wsgi:application --bind 0.0.0.0:$PORT

👀 En el mismo nivel se crea el archivo install-packages.sh el cual tiene por objetivo instalar las actualizaciones de debian por separado (hay que hacer más pruebas para obtener menor peso de la imagen)

#!/bin/bash

# Bash "strict mode", to help catch problems and bugs in the shell
# script. Every bash script you write should include this. See
# http://redsymbol.net/articles/unofficial-bash-strict-mode/ for
# details.
set -euo pipefail

# Tell apt-get we're never going to be able to give manual
# feedback:
export DEBIAN_FRONTEND=noninteractive

# Update the package listing, so we know what package exist:
apt-get update

# Install security updates:
apt-get -y upgrade

apt-get -y install --no-install-recommends apt-utils

# Install a new package, without unnecessary recommended packages:
apt-get -y install --no-install-recommends syslog-ng

# Delete cached files we don't need anymore:
apt-get clean
rm -rf /var/lib/apt/lists/*

Construir la imagen Docker

docker build -t simple-django-on-docker -f Dockerfile .

Ejecutar el contenedor

docker run -it -p 80:8888 simple-django-on-docker

En la dirección http://localhost para comprobar que se esta ejecutando

[Volver al inicio]

🚧 🚧 Django on Docker in progress 🚧 :construction

Crear una carpeta core

Instalar pipenv

python3 -m pip install --user pipenv

Crear carpeta con el nombre del projecto

mkdir core

Crear el ambiente virtual

# --tree QUE OCUPE PYTHON 3
pipenv --three

Instalar django & libreria para PosrgreSQL

pipenv install django
# psycopg2
pipenv install psycopg2
# django-environ
pipenv install django-environ

🚨 Activar ambiente virtual

pipenv shell

Crear un proyecto en django

pipenv run django-admin startproject core .

👀 Tener previamente PostgreSQL :octocat: PostgreSQL

Cambiar la configuración de conección en settings.py para que se conecte a la BD de PostgreSQL(local) y hacer pruebas de conexión

DATABASES = {
 'default': {
     'ENGINE': 'django.db.backends.postgresql_psycopg2',
     'NAME': 'sandbox',
     'USER': 'user_sandbox',
     'PASSWORD': 'user_sandbox_2020',
     # 'HOST': 'db', # SET IN docker-compose.yml
     'HOST': 'localhost',
     'PORT': '5432',
 }
}

BD local y migraciones

python manage.py makemigrations
python manage.py migrate
python manage.py createsuperuse
#
USER: core_2020
PWD: dj pg docker 2020

Actualizar dentro de settings.py

# importar os
import os
...
# DEBUG can be True/False or 1/0
DEBUG = int(os.environ.get('DEBUG', default=1))

Crear el archivo .env en carpeta raiz solo con la info de DEBUG=1

touch .env
# EDITAR EL ARCHIVO Y PONER SOLO LA INFO
DEBUG=1

En la carpeta core

El archivo Dockerfile ocupa la imagen

docker pull docker pull python:3.9-slim-buster
# BASE IMAGE
FROM python:3.9-slim-buster

# RUN ITS CONTENT THE FILE TO INSTALL
# UPDATES FROM THE DEBIAN REPOSITORIES
COPY install-packages.sh .
RUN chmod +x install-packages.sh
RUN ./install-packages.sh

# CREATE AND SET WORKING DIRECTORY
RUN mkdir /app
WORKDIR /app

# ADD CURRENT DIRECTORY CODE TO WORKING DIRECTORY
ADD . /app/

# SET DEFAULT ENVIRONMENT VARIABLES
ENV PYTHONUNBUFFERED 1
ENV PYTHONDONTWRITEBYTECODE 1
ENV DEBIAN_FRONTEND=noninteractive
# ENV LANG C.UTF-8

# SET PROJECT ENVIRONMENT VARIABLES
# GRAB THESE VIA PYTHON'S os.environ
# THESE ARE 100% OPTIONAL HERE
# ENV PORT=8000

RUN apt-get update \
  # && apt-get install -y apt-utils \
  # DEPENDENCIES FOR BUILDING PYTHON PACKAGES
  && apt-get install -y build-essential \
  # PSYCOPG2 DEPENDENCIES
  && apt-get install -y libpq-dev \
  # TRANSLATIONS DEPENDENCIES
  && apt-get install -y gettext \
  # INSTALL GIT
  && apt-get install -y git \
  # CLEANING UP UNUSED FILES
  && apt-get purge -y --auto-remove -o APT::AutoRemove::RecommendsImportant=false \
  && rm -rf /var/lib/apt/lists/*

# INSTALL ENVIRONMENT DEPENDENCIES
RUN pip3 install --upgrade pip
RUN pip3 install pipenv

# INSTALL DEPENDENCIES FOR PROJECT FROM PIPFILE
RUN pipenv install --skip-lock --system --dev

EXPOSE 8000

👀 En el mismo nivel se crea el archivo install-packages.sh el cual tiene por objetivo instalar las actualizaciones de debian por separado (hay que hacer más pruebas para obtener menor peso de la imagen)

🗄️ local.yml

version: '3.9'

services:
  postgres:
    image: postgres
    env_file:
      - ./.envs/.local/.postgres
    container_name: postgres
    volumes:
      - local_postgres_data:/var/lib/postgresql/data
      # - local_postgres_data_backups:/backups
    ports:
      - "5432:5432"
  django:
    build: .
    env_file:
      - ./.envs/.local/.django
      - ./.envs/.local/.postgres
    container_name: django
    command: python /app/manage.py migrate
    command: python /app/manage.py runserver 0.0.0.0:8000
    volumes:
      - .:/app
    ports:
        - "8000:8000"
    depends_on:
        - postgres
volumes:
  local_postgres_data:

Estructura de archivos y carpetas del projecto

.
├── core
│   ├── asgi.py
│   ├── __init__.py
│   ├── settings.py
│   ├── urls.py
│   └── wsgi.py
├── Dockerfile
├── install-packages.sh
├── local.yml
├── manage.py
├── Pipfile
└── Pipfile.lock

Como la imagen esta previamente desarrollada con un proyecto

# COMPOSE_FILE
export COMPOSE_FILE=local.yml

docker compose build
docker compose up
docker compose ps
docker compose down

Cuando esta creado el archivo local.yml para revisar si quedo corectamente configurado se ejecuta el siguiente comando

docker compose -f local.yml config

Se mostrarán las variables de ambiente con sus valores correctamente asignados y las otras configuraciones

docker compose -f local.yml up --build
#
docker compose -f local.yml ps

Cookiecutter Django list of commands

Verificar la version de pip

python3 -m pip --version

Actualizr pip

python3 -m pip install --user --upgrade pip
#
python3 -m pip install --upgrade pip
  1. Crear ambiente local
python3 -m venv [NOMBRE-DEL-ENTORNO-VIRTUAL]
  1. Dentro del ambiente local, y estar ubicados donde se desee que este la carpeta del proyecto
pip install "cookiecutter>=2.4.0"
  1. Instalar cookiecutter-django
cookiecutter https://github.com/pydanny/cookiecutter-django
# OR
cookiecutter gh:cookiecutter/cookiecutter-django

Configuración posible para cookiecutter

project_name [My Awesome Project]: root-backend-for-projects
project_slug [root_backend]:
description [Behold My Awesome Project!]: Backend for different projects
author_name [Daniel Roy Greenfeld]: Rodolfo Ugalde
domain_name [example.com]: mack.host
email [rodolfo-ugalde@example.com]: my_mail@gmail.com
version [0.1.0]:
Select open_source_license:
1 - MIT
2 - BSD
3 - GPLv3
4 - Apache Software License 2.0
5 - Not open source
Choose from 1, 2, 3, 4, 5 [1]: 1
timezone [UTC]: America/Mexico_City
windows [n]: n
use_pycharm [n]: y
use_docker [n]: y
Select postgresql_version:
1 - 14
2 - 13
3 - 12
4 - 11
5 - 10
Choose from 1, 2, 3, 4, 5 [1]: 1
Select cloud_provider:
1 - AWS
2 - GCP
3 - None
Choose from 1, 2, 3 [1]: 1
Select mail_service:
1 - Mailgun
2 - Amazon SES
3 - Mailjet
4 - Mandrill
5 - Postmark
6 - Sendgrid
7 - SendinBlue
8 - SparkPost
9 - Other SMTP
Choose from 1, 2, 3, 4, 5, 6, 7, 8, 9 [1]: 1
use_async [n]: n
use_drf [n]: y
Select frontend_pipeline:
1 - None
2 - Django Compressor
3 - Gulp
Choose from 1, 2, 3 [1]: 1
use_celery [n]: y
use_mailhog [n]: y
use_sentry [n]: n
use_whitenoise [n]: n
use_heroku [n]: n
Select ci_tool:
1 - None
2 - Travis
3 - Gitlab
4 - Github
Choose from 1, 2, 3, 4 [1]: 1
keep_local_envs_in_vcs [y]: y
debug [n]: n

👀 También puede configurar la variable de entorno COMPOSE_FILE apuntando a local.yml

export COMPOSE_FILE=local.yml
  1. Build the Stack
docker compose -f local.yml build
  1. Run the Stack
docker compose -f local.yml up
#
docker compose -f local.yml down
  1. Comandos de administración
    Como con cualquier comando de shell que deseamos ejecutar en nuestro contenedor, esto se hace usando el
docker compose -f local.yml run --rm
docker compose -f local.yml run --rm django python manage.py makemigrations
docker compose -f local.yml run --rm django python manage.py migrate
docker compose -f local.yml run --rm django python manage.py createsuperuser

Docker + Django + Postgresql fast way

  1. mkdir sandbox_dj_docker
  2. Crear el archivo requirements.txt
Django >= 3.2
psycopg2-binary >= 2.9
  1. Crear el archivo Dockerfile
# GET THE IMAGE SLIM-BUSTER
FROM python:3.9-slim-buster
# FORCE STDIN, STDOUT AND STDERR TO BE TOTALLY UNBUFFERED. ON SYSTEMS WHERE IT MATTERS, ALSO PUT STDIN, STDOUT AND STDERR IN BINARY MODE.
ENV PYTHONUNBUFFERED=1
WORKDIR /code
COPY requirements.txt /code/
RUN pip3 install -r requirements.txt
COPY . /code/
#
  1. Crear el archivo docker compose.yml
version: "3.9"

services:
  db:
    image: postgres
    volumes:
      - ./data/db:/var/lib/postgres/data
    environment:
      - POSTGRES_DB=postgres
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=postgres
  web:
    build: .
    command: python3 manage.py runserver 0.0.0.0:8000
    volumes:
      - .:/code
    ports:
      - "8000:8000"
    depends_on:
      - db
  1. ejecutar el siguiente comando para crear el proyecto en la carpeta que se creo
docker compose run web django-admin startproject dj_docker .

🚨 Se instala como root

  1. Modificar en el settings.py
...
import psycopg2.extensions
...
ALLOWED_HOSTS = ['0.0.0.0']
...
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': 'postgres',
        'USER': 'postgres',
        'PASSWORD': 'postgres',
        'HOST': 'db',  # SET IN docker compose.yml
        'PORT': '5432',
    },
    'OPTIONS': {
        'isolation_level': psycopg2.extensions.ISOLATION_LEVEL_SERIALIZABLE,
    },
}
  1. Ejecutar los comando para ejecutar docker
# REALISAR LAS MIGRACIONES
 docker compose run web python manage.py makemigrations
#
 docker compose run web python manage.py migrate
# PARA LEVANTAR/INICIAR docker compose
docker compose up
# PARA DETENER docker compose
docker compose down

Docker + Django + Postgresql

🚧 🚧 (WIP) 🚧 🚧

  1. mkdir django-on-docker && cd django-on-docker
  2. touch Dockerfile
# GET THE IMAGE SLIM-BUSTER
FROM python:3.10.6-slim-buster

# RUN ITS CONTENT THE FILE TO INSTALL
# UPDATES FROM THE DEBIAN REPOSITORIES
# COPY install-packages.sh .
# RUN chmod +x install-packages.sh
# RUN ./install-packages.sh

# INSTALL APT PACKAGES
RUN apt-get update && apt-get install --no-install-recommends -y \
    # dependencies for building Python packages
    build-essential \
    # psycopg2 dependencies
    libpq-dev \
    && rm -rf /var/lib/apt/lists/*

# CREATE AND SET WORKING DIRECTORY
# RUN mkdir /app
WORKDIR /app

# FORCE STDIN, STDOUT AND STDERR TO BE TOTALLY UNBUFFERED. ON SYSTEMS WHERE IT MATTERS, ALSO PUT STDIN, STDOUT AND STDERR IN BINARY MODE.
# SET DEFAULT ENVIRONMENT VARIABLES
ENV PYTHONUNBUFFERED 1
# PYTHON FROM COPYING PYC FILES TO THE CONTAINER
ENV PYTHONDONTWRITEBYTECODE 1

# COPY /requirements/requirements.txt /app
COPY /requirements /app

RUN python3 -m pip install --no-cache-dir --upgrade \
    pip \
    setuptools \
    wheel
# RUN python3 -m pip install --no-cache-dir -r requirements.txt
RUN python3 -m pip install --no-cache-dir -r local.txt

# INSTALL REQUIRED SYSTEM DEPENDENCIES
RUN apt-get update && apt-get install --no-install-recommends -y \
    # psycopg2 dependencies
    libpq-dev \
    # Translations dependencies
    gettext \
    # cleaning up unused files
    && apt-get purge -y --auto-remove -o APT::AutoRemove::RecommendsImportant=false \
    && rm -rf /var/lib/apt/lists/*

# ADD CURRENT DIRECTORY CODE TO WORKING DIRECTORY
COPY . /app
  1. touch local.yml
version: "3.9"

volumes:
  local_postgres_data: {}
  local_postgres_data_backups: {}

services:
  django: &django
    build:
      context: .
      dockerfile: ./compose/local/django/Dockerfile
    image: root_local_django
    container_name: django
    depends_on:
        - postgres
    volumes:
      - .:/app
    env_file:
      - ./.envs/.local/.django
      - ./.envs/.local/.postgres
    ports:
        - "8000:8000"
    command: /start

  postgres:
    build:
      context: .
      dockerfile: ./compose/production/postgres/Dockerfile
    image: root_production_postgres
    container_name: postgres
    volumes:
      - local_postgres_data:/var/lib/postgresql/data
      - local_postgres_data_backups:/backups
    env_file:
      - ./.envs/.local/.postgres
    ports:
      - "5431:5432"
  1. touch install-packages.sh
#!/bin/bash

# Bash "strict mode", to help catch problems and bugs in the shell
# script. Every bash script you write should include this. See
# http://redsymbol.net/articles/unofficial-bash-strict-mode/ for
# details.
set -euo pipefail

# Tell apt-get we're never going to be able to give manual
# feedback:
export DEBIAN_FRONTEND=noninteractive

# Update the package listing, so we know what package exist:
apt-get update

# Install security updates:
apt-get -y upgrade

# Install a new package, without unnecessary recommended packages:
apt-get -y install --no-install-recommends syslog-ng

# INSTALL APT PACKAGES
# build-essential -> DEPENDENCIES FOR BUILDING PYTHON PACKAGES
# libpq-dev -> psycopg2 DEPENDENCIES
apt-get update && apt-get install --no-install-recommends -y \
    build-essential \
    libpq-dev \
    && rm -rf /var/lib/apt/lists/*

# Delete cached files we don't need anymore (note that if you're
# using official Docker images for Debian or Ubuntu, this happens
# automatically, you don't need to do it yourself):
apt-get clean
# Delete index files we don't need anymore:
rm -rf /var/lib/apt/lists/*
  1. Crear el proyecto, sin que se esten ejecutando los contenedores.
# CREAR EL PROYECTO EN LA CARPETA
docker compose -f local.yml run django django-admin startproject root .
#
# EN LUGAR DE HACER Build the Stack CON
# docker compose -f local.yml build
  1. Modificar en el settings.py para setear las las variables de entorno de postgres
...
import psycopg2.extensions
import environ
# ENVIRON SETTINGS
env = environ.Env()

# SECURITY WARNING: don't run with debug turned on in production!
# DEBUG = True
DEBUG = env.bool("DJANGO_DEBUG", False)
...
ALLOWED_HOSTS = ['localhost', '0.0.0.0', '127.0.0.1']
...
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': env.str('POSTGRES_DB'),
        'USER': env.str('POSTGRES_USER'),
        'PASSWORD': env.str('POSTGRES_PASSWORD'),
        'HOST': env.str('POSTGRES_HOST'),
        'PORT': env.str('POSTGRES_PORT')
    },
    'OPTIONS': {
        'isolation_level': psycopg2.extensions.ISOLATION_LEVEL_SERIALIZABLE,
    },
}
  1. Hacer las migraciones para comprobar que estan bien las variables de entorno

La bandera --rm lo que hace es que crea un contenedor solo para el fin indicado y cuando acabe de ejecutarse el comando mata el contenedor

docker compose -f local.yml run --rm django python manage.py makemigrations
#
docker compose -f local.yml run --rm django python manage.py migrate
  1. Se levantan los servicio para comprobar errores
docker compose -f local.yml up
  1. Se crea el super-usuario
docker compose -f local.yml run --rm django python manage.py createsuperuser
  1. Dar de baja los servicios y comprobar el estado de los servicios levandados
docker compose -f local.yml down
# WATCH PROCESS UP
docker compose -f local.yml ps
  1. Comandos de imagenes y contenedores

Eliminar todas las imagenes.

docker rmi $(docker images -aq)

Eliminar todos los contenedores.

docker rm $(docker ps -a -q)

Eliminar volumenes

docker volume rm [NOMBRE_DEL_VOLUME]

Docker básicos

docker images
docker container
docker volume
docker network
# PARA CADA UNO
ls
rm
prune
-a
-q
  1. 🥮 Conectarse al servicio de \_postgres\* cuando se esta ejecutando con pgAdmin4
  • Identificar cual es el contenedor de posrgres
docker container ls -a
# OUTPUT
CONTAINER ID   IMAGE                     COMMAND                  CREATED             STATUS             PORTS                                       NAMES
c3705fe5e238   django-on-docker_django   "python3 manage.py r…"   26 minutes ago      Up 26 minutes      0.0.0.0:8000->8000/tcp, :::8000->8000/tcp   django
bfe4f282013d   postgres                  "docker-entrypoint.s…"   About an hour ago   Up About an hour   0.0.0.0:5432->5432/tcp, :::5432->5432/tcp   postgres
  • Inspeccionar el contenedor bfe4f282013d para saber la IP en particular
docker inspect bfe4f282013d | grep IPAddress

Tambien ↕️

docker inspect --format='{{json .NetworkSettings.Networks}}' bfe4f282013d
  • Ejecutar pgAdmin4 y conectarse a esa IP con las credenciales del archivo .envs > .postgres

Identificar que el los volumenes de declarados se hayan creado correctamente

docker volume inspect <NOMBRE-DEL-VOLUMEN-DECLARADO>

Entrar dentro del contenedor de postgres para revisar dentro de la ruta los volumenes creados.

docker exec -it <ID-DEL-CONTENEDOR-POSTGRES> bash

Para ejecutar el script de backup de postgres, dentro de la carpeta del proyecto.

docker compose -f local.yml exec postgres backup

output

SUCCESS: 'root_base_backend' database backup 'backup_2022_10_07T22_21_18.sql.gz' has been created and placed in '/backups'.

Para visualizar los backups realizados, dentro de la carpeta del proyecto.

docker compose -f local.yml exec postgres backups

output

These are the backups you have got:
total 16K
-rw-r--r-- 1 root root 5.4K Oct  7 22:25 backup_2022_10_07T22_25_08.sql.gz
-rw-r--r-- 1 root root 5.4K Oct  7 22:21 backup_2022_10_07T22_21_18.sql.gz

Habilitar debugger/Hacer modiciaciones/migraciones

Cuando sea necesario hacer modificaciones/presentan problemas con las migraciones y una opción es que se elimine el volumen de la BD donde se almacena la data tiene la terminación NOMBRE DEL PROYECTO_postgres_data

  1. Primero se tiene que detener la ejecucion de docker compose
docker compose -f local.yml down
# Mostrar los volunenes de docker
docker volume ls
# Eliminar el volimen NOMBRE DEL PROYECTO_postgres_data
docker volume rm NOMBRE DEL PROYECTO_postgres_data
  1. Para correr el stack de contenedores
# -f, --file FILE             Specify an alternate compose file
docker compose -f local.yml up
  1. Saber con que nombre esta el contenedor
# -f, --file FILE             Specify an alternate compose file
# ps List containers
docker compose -f local.yml ps
  1. Matar el docker django
# -f, --force     Force the removal of a running container (uses SIGKILL)
# -l, --link      Remove the specified link
# -v, --volumes   Remove anonymous volumes associated with the container
docker rm -f <ID>
  1. Despues de sacar/matar el docker de django para levantar lo de nuevo es:
# run,   Run a one-off command
# --rm,   Remove stopped containers
# --service-ports,   Run command with the service's ports enabled and mapped to the host.
docker compose -f local.yml run --rm --service-ports django
# Hacer migraciones
docker compose -f local.yml run --rm django python manage.py makemigrations
# Migrar a la BD
docker compose -f local.yml run --rm django python manage.py migrate
# EJEMPLO PARA CREAR SUPER-USUARIO
docker compose -f local.yml run --rm django python manage.py createsuperuser

# Entrar al shell de django +
docker compose run --rm django python manage.py shell_plus

Para crear una aplicación dentro de del proyecto, la carpeta se tiene que crear.

docker compose -f local.yml run --rm django python manage.py startapp <NOMBRE_APP> ./root/<NOMBRE_APP>
#
docker compose -f local.yml run --rm django django-admin startapp <NOMBRE_APP> ./root/<NOMBRE_APP>

[Volver al inicio]