Remember to maintain security and privacy. Do not share sensitive information. Procedimento.com.br may make mistakes. Verify important information. Termo de Responsabilidade

How to Create and Execute a Dockerfile on Windows

Docker is a platform that allows developers to automate the deployment of applications inside lightweight, portable containers. A Dockerfile is a script containing a series of commands and instructions that are used to create a Docker image. This article will guide you on how to create and execute a Dockerfile on a Windows environment using both Command Prompt (CMD) and PowerShell.

Prerequisites

Before you begin, ensure that Docker Desktop is installed on your Windows machine. Docker Desktop provides the Docker Engine, Docker CLI client, Docker Compose, and Kubernetes, all in one package. It's available for Windows 10 and Windows 11.

Step-by-Step Guide

Step 1: Create a Dockerfile

  1. Open Notepad or your preferred text editor.

  2. Write the following content to create a simple Dockerfile that uses a base image of Ubuntu and installs the curl package:

    # Use an official Ubuntu as a parent image
    FROM ubuntu:latest
    
    # Set the working directory
    WORKDIR /app
    
    # Run a command to update the package manager and install curl
    RUN apt-get update && apt-get install -y curl
    
    # Specify the command to run on container start
    CMD ["bash"]
  3. Save this file as Dockerfile in a new directory, for example, C:\DockerDemo.

Step 2: Build the Docker Image

  1. Open Command Prompt or PowerShell.

  2. Navigate to the directory where your Dockerfile is located:

    cd C:\DockerDemo
  3. Build the Docker image using the following command:

    docker build -t my-ubuntu-curl .

    This command tells Docker to build an image with the tag my-ubuntu-curl using the Dockerfile in the current directory (.).

Step 3: Run the Docker Container

  1. After building the image, you can create and start a container using:

    docker run -it my-ubuntu-curl

    The -it flag allows you to interact with the container via the terminal.

  2. Once the container starts, you will be inside the Ubuntu shell. You can verify that curl is installed by typing:

    curl --version

Additional Tips

  • To list all Docker images on your system, use the command:

    docker images
  • To stop a running container, first find its ID with:

    docker ps

    Then stop it using:

    docker stop <container_id>
  • To remove a container, use:

    docker rm <container_id>

To share Download PDF

Gostou do artigo? Deixe sua avaliação!
Sua opinião é muito importante para nós. Clique em um dos botões abaixo para nos dizer o que achou deste conteúdo.