DEV Community

Gustavo Jhon
Gustavo Jhon

Posted on • Edited on

Django Project with docker container

python3 -m venv env
source env/bin/activate
# . .env/bin/activate
pip3 install django
django-admin startproject django_practice .
pip freeze > requirements.txt
Enter fullscreen mode Exit fullscreen mode

Dockerfile

# Utiliza una imagen base de Python compatible con Django
FROM python:3.9

# Establece el directorio de trabajo dentro del contenedor
WORKDIR /app

# Copia el archivo de requerimientos de Python al contenedor
COPY requirements.txt .

# Instala las dependencias del proyecto
RUN pip install --no-cache-dir -r requirements.txt

# Copia el código del proyecto al contenedor
COPY . .

# Expone el puerto 8000 para acceder a la aplicación
EXPOSE 8080

# Comando para iniciar el servidor de desarrollo de Django
CMD ["python", "manage.py", "runserver", "0.0.0.0:8080"]
Enter fullscreen mode Exit fullscreen mode

docker-compose.yml

version: '3'

services:
  db:
    image: mysql:5.7
    restart: always
    environment:
      MYSQL_DATABASE: nombre_base_de_datos
      MYSQL_USER: nombre_usuario
      MYSQL_PASSWORD: contraseña
      MYSQL_ROOT_PASSWORD: contraseña_root
    volumes:
      - mysql-data:/var/lib/mysql

  web:
    build:
      context: .
      dockerfile: Dockerfile
    restart: always
    ports:
      - 8080:8080
    depends_on:
      - db
    volumes:
      - .:/app

volumes:
  mysql-data:
Enter fullscreen mode Exit fullscreen mode

image

docker build -t django_practice .
Enter fullscreen mode Exit fullscreen mode

run

docker run -p 8080:8080 django_practice
Enter fullscreen mode Exit fullscreen mode

http://localhost:8080

Heroku

Simplify your DevOps and maximize your time.

Since 2007, Heroku has been the go-to platform for developers as it monitors uptime, performance, and infrastructure concerns, allowing you to focus on writing code.

Learn More

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay