How to build a Dockerfile on Windows 10?

Building a Dockerfile on Windows 11 is a common workflow for containerized development. Here’s how you can do it—step by step:


1. Install Docker Desktop for Windows

  • Go to the Docker Desktop download page.
  • Download the Windows installer and run it.
  • Follow the prompts (ensure “WSL2” support is selected, if available).
  • NOTE: Windows 11 works best with WSL2.

2. Write Your Dockerfile

  • Use a code editor (e.g., VS Code, Notepad++).
  • Save your Dockerfile as simply named: Dockerfile (no extension).
  • Example (Python app):
FROM python:3.11-slim
WORKDIR /app
COPY . .
RUN pip install -r requirements.txt
CMD ["python", "app.py"]

3. Open a Terminal

  • On Windows, you can use:
  • Windows Terminal (wt command)
  • Command Prompt (cmd)
  • PowerShell
  • WSL2 Terminal (Recommended)

4. Navigate to the Dockerfile Location

Use the cd command to go to your project directory:

cd path\to\your\project

5. Build the Docker Image

Use the docker build command. For example:

docker build -t my-app:latest .
  • -t my-app:latest gives your image a name and tag (latest).
  • The final . means “use the Dockerfile in the current directory”.

6. Run the Docker Container (Optional)

Test your image:

docker run --rm -it my-app:latest

7. Troubleshooting

  • If Docker commands aren’t recognized, make sure Docker Desktop is running.
  • Check Windows Terminal settings if using WSL2, and ensure file sharing is set up for your code folders.

Summary Table

StepCommand/Action
Install DockerDownload & install Docker Desktop
Write DockerfileUse text editor, save as Dockerfile
Open TerminalPowerShell, CMD, WSL or Windows Terminal
Go to directorycd path\to\project
Build imagedocker build -t my-app:latest .
Run containerdocker run --rm -it my-app:latest

Tip: You can use Docker Desktop’s GUI to see your images and containers as well!


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *