Creación de Dockerfiles para imágenes
Aprenda a escribir Dockerfiles para definir cómo se empaquetan en una imagen el entorno y las dependencias de su aplicación.
Creación de Dockerfiles para imágenes es una lección gratuita de Docker & Kubernetes for Developers en CoddyKit. Esta es la lección 1 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 Docker & Kubernetes for Developers, y tu progreso se sincroniza en la web y la app de CoddyKit. El curso de Docker & Kubernetes for Developers incluye 4 lecciones en total.
Partes de esta lección aún no han sido traducidas y se muestran en inglés.
What are Dockerfiles?
Welcome to creating your own Docker images! The heart of this process is the Dockerfile.
A Dockerfile is a simple text file that contains a series of instructions. These instructions tell Docker exactly how to build a Docker image, step by step.
Why Use a Dockerfile?
Dockerfiles automate the image creation process. Instead of manually setting up an environment, you define it once in a Dockerfile.
- Reproducibility: Ensures everyone builds the exact same image.
- Version Control: Dockerfiles can be stored in Git, just like your code.
- Efficiency: Speeds up development and deployment workflows.
Starting Point: The FROM Instruction
Every Dockerfile must start with the FROM instruction. It specifies the base image your image will be built upon.
Think of it as choosing the operating system and initial software for your container. For example, ubuntu:22.04 or node:18-alpine.
FROM node:18-alpineRunning Commands: The RUN Instruction
The RUN instruction executes commands during the image build process. This is where you install software, update packages, or create directories.
Each RUN instruction creates a new layer in your image.
FROM node:18-alpine
RUN apk add --no-cache gitSetting the Workspace: WORKDIR
The WORKDIR instruction sets the working directory for any subsequent RUN, CMD, ENTRYPOINT, COPY, or ADD instructions.
It's good practice to set a specific directory for your application files.
FROM node:18-alpine
WORKDIR /appAdding Files: The COPY Instruction
The COPY instruction copies files or directories from your local machine (the build context) into the Docker image filesystem.
The first argument is the source path on your machine, the second is the destination path inside the image.
FROM node:18-alpine
WORKDIR /app
COPY package*.json ./Installing Dependencies
After copying your project's dependency files (like package.json for Node.js), you'll typically use RUN again to install them.
This step ensures your application has all it needs to run.
FROM node:18-alpine
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .Exposing Ports: The EXPOSE Instruction
The EXPOSE instruction informs Docker that the container listens on the specified network ports at runtime.
It's documentation for users, not an actual publishing of the port. You'll publish ports when you run the container.
FROM node:18-alpine
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
EXPOSE 3000Default Command: The CMD Instruction
The CMD instruction provides a default command to be executed when a container starts from the image.
Unlike RUN, CMD runs when the container is launched, not during the build. You can override it when running the container.
FROM node:18-alpine
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
EXPOSE 3000
CMD ["npm", "start"]Complete Dockerfile Example
Here's a full example of a simple Dockerfile for a Node.js web application. Each line contributes to building the final image.
# Use an official Node.js runtime as a parent image
FROM node:18-alpine
# Set the working directory in the container
WORKDIR /app
# Copy package.json and package-lock.json to install dependencies
COPY package*.json ./
# Install application dependencies
RUN npm install
# Copy the rest of the application code
COPY . .
# Make port 3000 available to the world outside this container
EXPOSE 3000
# Define the command to run your app
CMD ["npm", "start"]Dockerfile Instruction Check
Which Dockerfile instruction is used to execute commands during the image build process, such as installing packages or creating directories?
Recap: Crafting Dockerfiles
You've learned the essential instructions for crafting Dockerfiles:
FROM: Sets the base image.RUN: Executes commands during build.WORKDIR: Sets the working directory.COPY: Copies files into the image.EXPOSE: Declares listening ports.CMD: Sets the default command to run on container start.
Mastering these allows you to define your application's environment precisely!
Preguntas frecuentes
¿La lección «Creación de Dockerfiles para imágenes» es gratis?
Sí — el texto completo de «Creación de Dockerfiles para imágenes» 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 Docker & Kubernetes for Developers, actualiza a CoddyKit PRO. El curso de Docker & Kubernetes for Developers incluye 4 lecciones en total.
¿Qué aprenderé en «Creación de Dockerfiles para imágenes»?
Aprenda a escribir Dockerfiles para definir cómo se empaquetan en una imagen el entorno y las dependencias de su aplicación. Practicas Docker & Kubernetes for Developers 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 Docker & Kubernetes for Developers?
No se requiere experiencia previa. Docker & Kubernetes for Developers 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 1 de 4.
¿Cuánto tiempo toma la lección «Creación de Dockerfiles para imágenes»?
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 Docker & Kubernetes for Developers?
Sí. Cada lección de Docker & Kubernetes for Developers 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
- Creación de Dockerfiles para imágenes
- Creación de imágenes Docker personalizadas
- Aplicaciones con varios contenedores mediante Compose
- Persistencia de datos con volúmenes de Docker