Challenge, Easy,  on  ContainersProgramming

Write a Dockerfile for an Application With System-Level Dependencies

In this challenge, you will write a Dockerfile to containerize a simple file type detection service.

The application is located in the ~/app/ directory. It's a small Flask web server that uses the python-magic package to detect the type of a submitted file.

Take a moment to explore the code and dependencies before you begin:

cat ~/app/server.py
cat ~/app/requirements.txt

If you'd like to test the application locally before containerizing it, cd into the ~/app/ directory and run:

pip install -r requirements.txt
python server.py

Then open a second terminal and verify the application works:

curl -F "file=@/etc/hostname" localhost:3000/api/detect
# {"filename":"hostname","type":"text/plain"}
curl -F "file=@/bin/bash" localhost:3000/api/detect
# {"filename":"bash","type":"application/x-sharedlib"}

Your goal is to:

  1. Create a Dockerfile in the ~/app/ directory.
  2. Build a Docker image named my-app:v1.0.0.
  3. Ensure the containerized application starts correctly and responds on port 3000.

You can test your image at any point by running:

docker run -p 3000:3000 my-app:v1.0.0
Hint 1

A Dockerfile is a text file that contains instructions for building a Docker image. Your Dockerfile should have the following instructions:

  • FROM - specify a base image (e.g., python:3-slim)
  • COPY - copy your application files into the image
  • RUN - install dependencies (both system packages and Python packages)
  • CMD - specify the command to start the application

Check out the Dockerfile reference for more details.

Container image composition and the main factors that affect it.
Hint 2

For a refresher on how to build a Docker image, check out this challenge:

Build and Publish a Container Image With Docker

Hint 3

If the container crashes immediately after starting, check the logs by running it in the foreground:

docker run my-app:v1.0.0

Make sure your Dockerfile copies the application files into the image and installs the required Python- and OS-level packages.

Hint 4

Note that some Python packages depend on shared C libraries that may not be included in slim base images like python:3-slim. While pip install may succeed (pulling only the wrapper Python package code), the application will crash at runtime if the required shared library is missing from the image.

If you see an error sounding like failed to find libmagic, it means the python-magic package can't find its underlying system-level dependency in the image - the libmagic C library.

The python-magic package is just a Python wrapper around libmagic, and you need to install the actual shared library separately at the OS level using apt-get (or dnf, apk, etc.).

Hint 6

For the exact name of the system package you need to install in your Dockerfile, check out the installation instructions for the python-magic package.