Docker #1: How to create a custom Docker image on Windows

Containerization, led by tools such as Docker, is becoming a cornerstone of modern software engineering, providing developers with flexibility, scalability, and simplified application management. In this article I will guide you through the process of creating a custom Docker container in a Windows environment.

In today's rapidly evolving world of software engineering, containerization is becoming a key technology that enables developers to build deploy and manage applications more efficiently. Docker, as one of the leading tools for containerization, offers a flexible and powerful platform for software development. This article is intended for beginning developers or IT professionals who want to learn the basics of Docker and application containerization.

What is containerization?

Containerization is the process of "wrapping" software together with its necessary libraries, dependencies and configurations into standardized units, known as containers. The goal is to ensure that software works consistently and reliably in different computing environments.

Key The advantages of containerization are the following:

  1. Isolation: Each container is isolated and contains everything necessary for running the application. This separation ensures that the application is not affected by changes in other applications or on the host system.

  2. Lightweight: Containers share the core host operating system, but are much lighter than traditional virtual machines, because they don't require a whole operating system for each instance. This leads to significantly less resource consumption and faster startup.

  3. Portability: Since containers include everything needed to run an application, they can be easily ported between different environments, such as different servers or cloud platforms, without the necessity of additional modifications.

  4. Consistency: Containerization ensures consistency of the environment across the development, test and production environment, which reduces "it worked on my computer" problems.

  5. Development efficiency: Developers can work in localized and independent environments, which minimizes conflicts between different projects and simplifies dependency management.

  6. Scalability and Management: Containers can be easily scaled up and down, allowing effective management of resources. They also facilitate automation processes and orchestration, as is continuous integration and deployment (CI/CD).

Step 1: Install Docker on Windows 11

  1. Download Docker Desktop

    • Visit Docker official site and download Docker Desktop for Windows.
    • Make sure that your system meets the installation requirements (e.g. system version, available disk space).
  2. Docker Desktop installation

    • Run the download installation file and follow the on screen instructions.
    • During installation you may be asked to allow changes to your device.
    • Restarting the computer

      • After installation is recommended to restart the computer to ensure correct settings.
    • Verify Installation

      • Open a command prompt (CMD) or PowerShell.
      • Run docker --version and docker-compose --version to verify, that Docker has been installed properly and is functional.
    • Enable WSL 2

      • Docker on Windows uses Windows Subsystem for Linux (WSL 2). Make sure that WSL 2 is enabled in system settings.
      • If it isn't, you can enable it by official Microsoft guide.

After completing these steps you should be Docker Desktop installed and ready to use on your Windows 11. 

Step 2: Create Basic PHP Application

  1. Create Folders for Application

      < li>On your computer, create a new folder for your Docker project, for example php-docker-app.
    • This folder will include all files related to your application and Docker.
  2. Writing PHP Script

    • In this folder create the file index.php.
    • Open this file in a text editor and write simple PHP script. For example a script that accepts two numbers as input and prints their sum.
<?php
if (isset($_GET['a']) && isset($_GET['b'])) {
    $a = intval($_GET['a']);
    $b = intval($_GET['b']);
    echo "Total: " . ($a + $b);
} else {
    echo "Type 'a' a 'b' in URL.";
}

This script uses the superglobal variable $_GET to get the values a a b from the URL and then calculates and displays their sum.

At this moment your project folder should contain only the file index.php.

This step created the base for your PHP application, which we will containerize using Docker.

Step 3: Prepare Dockerfile

In the project folder (php-docker-app) create a file without an extension with the name Dockerfile. Dockerfile is a recipe that Docker uses to create an image of your application.

Specifications Base Image

At the beginning of theDockerfile specify the base image from which you want to start. For PHP In Dockerfile: put the following content:

FROM php:7.4-cli
COPY index.php /usr/src/myapp/
WORKDIR /usr/src/myapp
CMD [ "php", "-S", "0.0.0.0:80", "index.php" ]
  • FROM php:7.4-cli
    This line instructs Docker to use the PHP 7.4 CLI image as the base for your container.
  • COPY index.php /usr/src/myapp/
    WORKDIR /usr/src/myapp

    These commands copy index.php to the specified folder in the image and set this folder as the working directory.
  • CMD [ "php", "-S", "0.0.0.0:80", "index.php" ]
    This command starts the built-in PHP server, listening on port 80, and uses index.php as the entry point.

With this step, you have prepared everything necessary to create a Docker image of your PHP application.

Step 4: Build Docker Image

  1. Open Command Line or PowerShell

    • Open Command Prompt (CMD) or PowerShell on your computer.
  2. Go to Project Folder< /strong>

    • Go to the folder where you created your Dockerfile (eg php-docker-app), using the command cd. For example:
cd cesta\k\složce\php-docker-app

Start build the Docker image process with the following command:

docker build -t php-docker-app .

This command tells Docker to build an image of Dockerfile in your current folder (.) and name this image php-docker-app.

After the build process has completed, you can verify that your image has been successfully created using the following command:

docker images

You should see php-docker-app in the list of available Docker images.

Step 5: Start Container from Image

Open Command Prompt (CMD) or PowerShell.

Start Container from your created Docker image using the following command:

docker run -p 8000:80 php-docker-app

This command tells Docker to start a container from your php-docker-app image.

The -p switch 8000:80 maps port 8000 on your host computer to port 80 on the container, which is the port on which the PHP server is running .

port number so that it is not in collision.
docker run -p 8080:80 php-docker-app

Open a web browser and enter the URL http://localhost:8080.

You should see the output of your PHP application or a message requesting to enter the numbers a a b in the URL.

With this step you have successfully launched The container from your Docker image and your PHP application should be accessible and functional. by entering the following addresses into your URL browser:

http://localhost:8080/?a=3&b=4

With this step, you have successfully tested the functionality of your Dockerized PHP application. You now have a functional environment for further development and experimentation.

In conclusion

In this guide we have gone through the whole process of creating a custom Docker container from beginning to end in a Windows environment.

As a result, you have gained practical skills in Docker, which is a key component in modern software development. how to use Docker to isolate and deploy applications in a standardized environment, giving you flexibility and easy application management. These skills are valuable for developers who want to improve their ability to build and manage containerized applications.