0Pricing
Flask Academy · Lección

Escriba un Dockerfile para producción

Compile una imagen de contenedor ligera y reproducible.

Escriba un Dockerfile para producción es una lección gratuita de Flask Academy en CoddyKit. Esta es la lección 3 de 4. Puedes leer la lección completa abajo gratuitamente — luego la practicas en el navegador con un editor de código integrado y un tutor de IA 24/7. Forma parte de la ruta de aprendizaje de Flask Academy, y tu progreso se sincroniza en la web y la app de CoddyKit. El curso de Flask Academy incluye 4 lecciones en total.

Partes de esta lección aún no han sido traducidas y se muestran en inglés.

Why Containerize

A Docker image packs your code, Python, and every dependency into one unit that runs the same on any machine. No more works on my laptop. 🐳

The Dockerfile

A Dockerfile is a recipe. Each line is a step Docker runs to build your image, from base layer to final run command.

Pick a Slim Base

Start from an official python image. The slim variant keeps the image small while still giving you a working Python runtime.

FROM python:3.12-slim

Set a Working Directory

Use WORKDIR to choose where your code lives inside the image. Every later command runs relative to this folder.

WORKDIR /app

Copy Requirements First

Copy requirements.txt before your code. This lets Docker cache the install layer and skip it when only your code changes.

COPY requirements.txt .

Install Dependencies

Run pip install with no cache to keep the layer lean. This bakes Flask and Gunicorn right into the image.

RUN pip install --no-cache-dir -r requirements.txt

Copy the Rest of the Code

Now COPY the rest of your project in. Because it comes after the install, edits to your code do not bust the dependency cache.

COPY . .

Document the Port

Use EXPOSE to declare which port your app listens on. It is documentation plus a hint for tooling that wires things up.

EXPOSE 8000

Define the Start Command

The CMD line is what runs when the container starts. Point it at Gunicorn so the container boots straight into your server.

CMD ["gunicorn", "--bind", "0.0.0.0:8000", "wsgi:app"]

Ignore the Junk

Add a .dockerignore file so caches, venvs, and git data never enter the image. Smaller builds are faster and safer.

__pycache__/
.venv/
.git/

Build and Run It

Build with docker build, then run the image and map a port. Your packaged Flask app is now portable to any host.

docker build -t myapp .
docker run -p 8000:8000 myapp

Quick Check

Why copy requirements.txt before the rest of your code?

Recap

Your Dockerfile builds from a slim base, installs deps, copies code, and launches Gunicorn. One image runs anywhere. Next: put Nginx in front. 🔧

Preguntas frecuentes

¿La lección «Escriba un Dockerfile para producción» es gratis?

Sí — el texto completo de «Escriba un Dockerfile para producción» es gratis para leer aquí en la web. Para practicarla de forma interactiva (editor de código integrado y tutor de IA 24/7) y desbloquear el resto del curso de Flask Academy, actualiza a CoddyKit PRO. El curso de Flask Academy incluye 4 lecciones en total.

¿Qué aprenderé en «Escriba un Dockerfile para producción»?

Compile una imagen de contenedor ligera y reproducible. Practicas Flask Academy con código real que ejecutas directamente en el navegador, y un tutor de IA 24/7 responde tus preguntas mientras trabajas en la lección.

¿Necesito experiencia previa para empezar Flask Academy?

No se requiere experiencia previa. Flask Academy en CoddyKit está estructurado para principiantes hasta estudiantes avanzados, así que puedes empezar aquí o desde el inicio y avanzar a tu ritmo. Esta es la lección 3 de 4.

¿Cuánto tiempo toma la lección «Escriba un Dockerfile para producción»?

La mayoría de las lecciones de CoddyKit toman alrededor de 5–10 minutos. Cada una es compacta e interactiva, así que avanzas constantemente y retomas exactamente por donde dejaste en la web y la app.

¿Puedo escribir y ejecutar código en esta lección de Flask Academy?

Sí. Cada lección de Flask Academy incluye un editor de código integrado, así que escribes y ejecutas código real directamente en tu navegador y obtienes retroalimentación instantánea de IA — sin configuración local necesaria.

Todas las lecciones de este curso

  1. Por qué no usar el servidor de desarrollo integrado
  2. Ejecute Flask con Gunicorn
  3. Escriba un Dockerfile para producción
  4. Colóquelo delante de Nginx y Compose
← Volver a Flask Academy