Getting Started with Docker: A Simple Guide to Creating Your First Container

A Step-by-Step Guide to Building and Running a Docker Container on Windows

Randike
3 min readMay 25, 2024

What Is a Container?

Think of a container as a portable box that contains all the code and all its dependencies and isolates the software from its environment so that it can run on any computing environment. Docker containers run on the Docker engine are lightweight because they share the machine’s OS kernel, and therefore does not require an OS for each application.

Building and running a Docker file on Windows:

  1. Download and install Docker Desktop
  2. Write docker file
  3. Build Docker image
  4. Run Docker image to create a container of your application

Before getting started, ensure that you have downloaded and installed Docker Desktop and have it running in the background.

or this demonstration we will be containerizing a simple python file that prints Hello world!when run. If you are following along download the project files from this repository.

Writing Our Docker File

The contents of the Docker file are run in the order in which they are written. We follow the following steps to generate a Docker image for our python application.

  1. Create a root directory of our source files named Dockerfile
our-project-repo/
├── main.py
├── requirments.txt
├── Dockerfile
└── ...

2. Get the base image from DockerHub. This base image contains the foundation on which we build our application’s containerized application. Since were running a python-3.11 application we import this base image as:

FROM python:3.11-slim

3. Next, we must set the working directory of the container. From this point on, the subsequent steps in the docker file will be run from this directory in the container.

FROM python:3.11-slim

WORKDIR /app

4. Since our app simply prints Hello world! we do not have any dependencies that need to be installed, therefore this step is optional. If our had any dependencies (it almost always does) we need to install them to ensure our python application runs smoothly. To so this we first import the application’s requirements.txt file into our container’s working directory as using the COPY instruction in the form COPY <local directory to copy from> <container directory to copy to>

FROM python:3.11-slim

WORKDIR /app

COPY requirements.txt ./

5. We install all the dependencies defined in the requirements.txt inside our container by using the RUN instruction that runs commands inside our container.

FROM python:3.11-slim

WORKDIR /app

COPY requirements.txt ./

RUN pip install -r requirements.txt

6. Next, we copy all of the source files into our containers using another COPY instruction.

FROM python:3.11-slim

WORKDIR /app

COPY requirements.txt ./

RUN pip install -r requirements.txt

COPY . .

7. Finally, we use the CMD instruction to define the default command to run when the container starts is python main.py. There is only one CMD instruction per Dockerfile and, unlike the RUNinstruction, it is written in exec form which does not start up a shell session.

# Use an official Python runtime as a base image
FROM python:3.11-slim

# Set the working directory in the container
WORKDIR /app

# Copy the requirements file into the working directory
COPY requirements.txt ./

# Install the Python dependencies
RUN pip install -r requirements.txt

# Copy the rest of the application code into the working directory
COPY . .

# Set the command to run the main application
CMD ["python", "main.py"]

Budling An Image

Using this Dockerfile we now we need to build an image for our container. This can be done by using the docker build command as follows:

docker build -t drsnaj/python_hello_world:v1.0 .
  • -t: Used to define a name tag for our container image
  • <dockerhub-username>/<name-of-image>:<version>: The name of the image. Although it is not required, naming the image starting with your docker hub username followed by a / will make it easier to push your images into dockerhub later on.
  • <path-to-dockerfile> the location of the dockerfile to generate the image. In our case it is . as we are already in the directory containing the dockerfile.

Starting Or Container

After building image, we can start our container using the docker run command followed by the image’s ID or the tag name we gave it. In our case, we will run we will run this command as:

docker run drsnaj/python_test

If all went well running this command should print Hello world! in your terminal

Conclusion

We’ve explored the basics of Docker containers and how to build and run a Docker container on Windows. By containerizing a simple Python application, we’ve shown how Docker ensures consistent performance across environments. Happy containerizing!

--

--