Construcción y despliegue de una imagen Docker y contenedor

20/12/2023 |
Loading...

This step-by-step guide will take you through containerizing your application with Docker. You'll create a , build the image, run it as a container, and if desired, upload it to Docker Hub.Dockerfile


Step 1: Create a Dockerfile

At the root of your project, create a file called (no extension).Dockerfile

Example (Flask application in Python):

# Imagen base de Python FROM python:3.10-slim  # Directorio de trabajo en el contenedor WORKDIR /app  # Copiar los archivos del proyecto COPY . .  # Instalar las dependencias RUN pip install --no-cache-dir -r requirements.txt  # Exponer el puerto de la aplicación EXPOSE 5000  # Comando para ejecutar la aplicación CMD ["python", "app.py"] 

Adjust commands based on your language or framework (e.g., Node.js, Java, etc.).


Step 2: Build the Docker Image

Open the terminal, navigate to your project folder, and run:

docker build -t my-app-image . 
  • -t: Assign a tag (name) to the image.

  • .: Indicates the current directory, where the Dockerfile is located.


Step 3: Run the Container

docker run -d -p 5000:5000 --name my-app-container my-app-image 
  • -d: Runs in the background.

  • -p: Maps the port of the host to the port of the container.

  • --name: Gives the container a custom name.

  • my-app-image: The name of the image created.


Step 4: Check the Container

To check if the container is working:

docker ps 

Then open your browser at:

http://localhost:5000 

Your app should be up and running.


(Optional) Step 5: Stop and Remove

Stop the container:

docker stop my-app-container 

Delete the container:

docker rm my-app-container 

Delete the image:

docker rmi my-app-image 

(Optional) Step 6: Upload to Docker Hub

  1. Sign in to Docker Hub:

    docker login 
  2. Tag the image:

    docker tag my-app-image tuusuario/my-app-image 
  3. Upload the image:

    docker push tuusuario/my-app-image 

Now you can download it from anywhere with:

docker pull tuusuario/my-app-image 

Conclusion

With Docker, you've packaged your application in a portable, lightweight container that works consistently in any environment. This greatly simplifies development, testing, and deployment, making them faster, more efficient, and more reliable.

 

¡Obtén Ofertas y Promociones Exclusivas!

Mantente al día con las últimas ofertas, descuentos y promociones especiales.

Update cookies preferences