How to Set Up Docker on Windows

How to Set Up Docker on Windows: Installation and Project Setup

What is Docker?

Docker is a powerful platform that allows developers to package applications and their dependencies into lightweight containers, ensuring seamless functionality across different environments.

Prerequisites for Installing Docker

  • Windows 10/11 64-bit (Pro, Enterprise, or Education editions).
  • Windows Subsystem for Linux (WSL 2) installed and enabled.
  • Virtualization enabled in BIOS settings.
  • Administrator privileges.

Installing Docker Using Command Prompt

Follow these steps to install Docker using the Command Prompt:

  1. Open the Command Prompt as an administrator.
  2. Download the Docker Desktop installer using the following command:
    curl -fsSL https://desktop.docker.com/win/stable/DockerDesktopInstaller.exe -o DockerDesktopInstaller.exe
  3. Run the installer silently with the following command:
    DockerDesktopInstaller.exe install
  4. Restart your computer after the installation completes.

Docker Desktop will now be installed on your system.

Step 3: Configure Docker Desktop

After installation:

  • Launch Docker Desktop from the Start Menu.
  • Sign in or create a Docker account.
  • Ensure WSL 2 is selected as the default backend under Settings.

Step 4: Test Docker Installation

Verify the installation by running the following command in the Command Prompt:

docker --version

If the Docker version is displayed, you are ready to use Docker!

Setting Up a Sample Project

Here’s how to set up and run a sample Docker project:

1. Create a Simple Web Server

Open your terminal and create a project folder:

mkdir my-docker-project && cd my-docker-project

Create a file named Dockerfile with the following content:

FROM python:3.9-slim
WORKDIR /app
COPY . /app
RUN pip install flask
CMD ["python", "app.py"]

2. Add a Sample Python Application

Create a file named app.py with the following content:

from flask import Flask
app = Flask(__name__)

@app.route('/')
def hello():
    return "Hello, Docker!"

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=5000)

3. Build and Run the Docker Container

  1. Build the Docker image:
    docker build -t my-flask-app .
  2. Run the Docker container:
    docker run -p 5000:5000 my-flask-app
  3. Open your browser and navigate to http://localhost:5000 to see your app running!

Common Issues and Troubleshooting

  • Cannot find WSL 2: Install WSL 2 and set it as the default backend using:
    wsl --set-default-version 2
  • Docker not starting: Restart your computer or reinstall Docker Desktop.
  • Permission errors: Run the commands with administrator privileges.

Conclusion

By following this guide, you’ve installed Docker on Windows, configured it, and set up a basic project. Docker is a versatile tool that will make your development workflow more efficient and scalable.