Docker - Hello world

Explore the basics with Docker Hello World. Our guide provides step-by-step instructions and practical tips for a smooth introduction to Docker containerization
E
Edtoks4:58 min read

In this section, we'll guide you through the process of creating your first Dockerized program— the classic "Hello World" application. This simple yet fundamental example will help you understand the basic steps involved in creating, building, and running a Docker container. Let's dive into the details.

1. Writing the Hello World Program

Our "Hello World" program will be a basic Python script. If you don't have Python installed on your system, you can choose any other programming language you are comfortable with. Create a file named hello_world.py with the following content:

# hello_world.py

def main():
    print("Hello, Docker!")

if __name__ == "__main__":
    main()
 

This Python script defines a simple main function that prints "Hello, Docker!" when executed.

2. Creating a Dockerfile

A Dockerfile is a script that contains instructions for building a Docker image. It specifies the base image, any dependencies, and the commands to run when the image is instantiated. Create a file named Dockerfile (no file extension) in the same directory as your hello_world.py:

# Dockerfile

# Use an official Python runtime as a parent image
FROM python:3.8-slim

# Set the working directory to /app
WORKDIR /app

# Copy the current directory contents into the container at /app
COPY . /app

# Define environment variable
ENV NAME World

# Run hello_world.py when the container launches
CMD ["python", "hello_world.py"]

Let's break down the key elements of this Dockerfile:

  • FROM python:3.8-slim: Specifies the base image as Python 3.8 on a slim Linux distribution.
  • WORKDIR /app: Sets the working directory inside the container to /app.
  • COPY . /app: Copies the contents of the current directory into the container's /app directory.
  • ENV NAME World: Defines an environment variable named NAME with the default value "World".
  • CMD ["python", "hello_world.py"]: Specifies the command to run when the container starts.

3. Building the Docker Image

Navigate to the directory containing both hello_world.py and Dockerfile using the command line. Run the following command to build the Docker image:

docker build -t hello-world .
  • docker build: Initiates the image-building process.
  • -t hello-world: Tags the image with the name "hello-world".
  • .: Specifies the build context, which is the current directory.

This command reads the instructions in the Dockerfile, executes them step by step, and creates a Docker image named "hello-world."

4. Running the Docker Container

Now that we have our Docker image, let's run a container based on it. Execute the following command:

docker run hello-world

This command runs a container based on the "hello-world" image. You should see the output:

Hello, Docker

Congratulations! You've just executed your first Dockerized program.

5. Understanding the Output

Let's break down the steps and understand what happened behind the scenes:

  • Docker Build Process:

    • The docker build command reads the instructions in the Dockerfile.
    • It starts with the specified base image (python:3.8-slim), sets the working directory, copies the application code, defines an environment variable, and sets the command to run.
    • Each instruction creates a new layer in the Docker image.
  • Docker Run Process:

    • The docker run command creates an instance (container) of the "hello-world" image.
    • The container is an isolated environment that inherits the image's layers.
    • The CMD instruction in the Dockerfile is executed, resulting in the execution of python hello_world.py.
  • Output Explanation:

    • The Python script hello_world.py is executed within the container.
    • The script prints "Hello, Docker!" to the standard output.
    • This output is displayed on your terminal.

6. Docker Image Layers

Docker images are composed of multiple layers. Each instruction in the Dockerfile creates a new layer. Understanding layers is crucial for optimizing Docker images.

When you build an image, Docker caches intermediate layers. If you make changes to your code but not to the Dockerfile instructions, Docker reuses the cached layers, resulting in faster builds.

Layers are additive, and changes in upper layers do not affect lower layers. This allows for efficient use of resources and reduces the size of image updates.

7. Cleaning Up

To keep your system tidy, you might want to remove the Docker image and container:

# Remove the container
docker rm <container_id_or_name>

# Remove the image
docker rmi hello-world

Replace <container_id_or_name> with the actual container ID or name. If you're unsure about the container ID, you can find it using docker ps -a.

8. Conclusion

You've successfully created, built, and run your first Dockerized program! This "Hello World" example lays the foundation for more complex containerized applications. As you delve deeper into Docker, explore additional features, such as Docker Compose for managing multi-container applications and Docker Hub for sharing images with

Let's keep in touch!

Subscribe to keep up with latest updates. We promise not to spam you.