Dockerizing a Simple ‘Hello World’ Application

First things first: What is Docker? Well, it’s this fancy technology that allows us to package up our code and all its dependencies into a nice little container. This way, no matter where the application runs (on your laptop or on some server in the cloud), everything works perfectly!

So let’s say we have a simple “Hello World” program written in Python:

# This script prints out the phrase "Hello world!" using the print() function.

# The print() function takes in a string as an argument and displays it on the screen.

# To run this script, simply click the "Run" button or use the shortcut "Ctrl + Enter".

print("Hello world!") # This line calls the print() function and passes in the string "Hello world!" as an argument.

To turn this into a Docker container, we need to create a new file called `Dockerfile`. This is where all the magic happens.

Here’s what our `Dockerfile` might look like:

# This is a Dockerfile script that creates a container for a Python application

# Use the Python 3.8 slim buster image as the base image for our container
FROM python:3.8-slim-buster

# Set the working directory inside the container to /app
WORKDIR /app

# Copy all the files from the current directory into the /app directory in the container
COPY . /app

# Install the dependencies listed in the requirements.txt file using pip
RUN pip install --no-cache-dir -r requirements.txt

# Set the command to be executed when the container is run, in this case, run the main.py file using python
CMD ["python", "main.py"]

Let’s break this down:
1. `FROM` specifies the base image we want to use for our container (in this case, Python version 3.8).
2. `WORKDIR` sets the working directory inside the container. This is where all of our files will be stored and executed from.
3. `COPY` copies all of our code into the container at a specific location (in this case, we’re copying everything in the current directory to `/app`).
4. `RUN` runs a command inside the container. In this case, we’re using pip to install any dependencies that are listed in our `requirements.txt` file.
5. Finally, `CMD` specifies what should happen when someone starts up our container (in this case, running our main Python script).

Once you have your Dockerfile set up, it’s time to build the image! This can be done with a simple command:

bash
# This line specifies the command to build a Docker image and tag it as "my-hello-world"
docker build -t my-hello-world .

- `docker build` is the command to build a Docker image
- `-t` is used to tag the image with a name
- `my-hello-world` is the name given to the image
- `.` specifies the current directory as the build context for the image

bash
# This line installs dependencies listed in the requirements.txt file
RUN pip install -r requirements.txt

- `RUN` is a Dockerfile instruction that executes a command in the image
- `pip install` is the command to install Python packages
- `-r requirements.txt` specifies the file containing the list of dependencies to be installed

bash
# This line sets the working directory to /app
WORKDIR /app

- `WORKDIR` is a Dockerfile instruction that sets the working directory for any subsequent commands
- `/app` is the directory where the application code will be copied to in the image

bash
# This line copies the current directory into the image at /app
COPY . /app

- `COPY` is a Dockerfile instruction that copies files or directories from the build context into the image
- `.` specifies the current directory to be copied
- `/app` is the destination directory in the image

bash
# This line specifies the command to run when the container is started
CMD python main.py

- `CMD` is a Dockerfile instruction that specifies the command to be executed when the container is started
- `python main.py` is the command to run the main Python script in the container

This will create an image called `my-hello-world`, which we can then run using this command:

# This script runs a docker container called "my-hello-world" in interactive mode and removes it after it is finished running.

# The "docker run" command is used to run a container.
# The "--rm" flag removes the container after it is finished running.
# The "-it" flag enables interactive mode, allowing the user to interact with the container.
# The "my-hello-world" argument specifies the name of the container to run.

docker run --rm -it my-hello-world

And that’s it! You should see the output “Hello world!” printed to your terminal.

SICORPS