Roadmaps / DevOps & Cloud / Lab 2

Lab 2 of 8 · Containers with Docker

Package your app as a Docker image

Write a Dockerfile, build an image of your app, run it as a container and check its logs.

Time
45–60 minutes
Cost
Free.

What you learn

A container packages your app with everything it needs, so it runs the same on your laptop, a CI server and the cloud. Almost every modern deployment starts with an image.

  • Images, containers and layers
  • Writing a Dockerfile that runs as a non-root user
  • Ports, logs and stopping containers
  • Keeping images small with .dockerignore

In the AI era: AI models and AI apps are shipped as container images too. The same skills you learn here are how teams package model servers.

Before you start

  • Finish lab 1.

  • Install Docker Desktop (Windows: turn on the WSL integration). Check with docker --version.

Step by step

  1. 1 Write the Dockerfile

    Each line is a step in building the image. The app runs as a normal user, not as root: a basic security habit.

    File: Dockerfile
    FROM python:3.12-slim
    
    WORKDIR /app
    COPY requirements.txt .
    RUN pip install --no-cache-dir -r requirements.txt
    COPY app ./app
    
    RUN useradd --create-home appuser
    USER appuser
    
    EXPOSE 8000
    CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "8000"]
    
  2. 2 Keep junk out of the image

    Without this file, Docker would copy your virtual environment and Git history into the build.

    File: .dockerignore
    .venv
    .git
    __pycache__
    .pytest_cache
    tests
    
  3. 3 Build the image

    The -t flag gives the image a name and version tag. The dot means "build from this folder".

    Run this
    docker build -t ai-status-app:1.0 .
    docker images ai-status-app
    You should see:

    A line for ai-status-app with tag 1.0.

  4. 4 Run it as a container

    -d runs it in the background and -p 8000:8000 connects port 8000 on your computer to port 8000 in the container.

    Run this
    docker run -d --name ai-status -p 8000:8000 ai-status-app:1.0
    curl http://localhost:8000/health
    You should see:

    {"status":"ok"}.

  5. 5 Look inside

    Logs are the first thing you check when something breaks. docker exec runs a command inside the running container.

    Run this
    docker logs ai-status
    docker exec ai-status whoami
    docker ps
    You should see:

    Uvicorn startup lines, then appuser, then your container in the list.

  6. 6 Stop and remove it

    Containers are disposable. The image stays, so you can start a new one any time.

    Run this
    docker stop ai-status
    docker rm ai-status
  7. 7 Commit and push

    Your repository now describes how to build the app anywhere.

    Run this
    git add Dockerfile .dockerignore
    git commit -m "Add Dockerfile"
    git push

You are done when

  • docker run serves {"status":"ok"} on port 8000.

  • docker exec ai-status whoami printed appuser, not root.

  • The Dockerfile is on GitHub.

Finished? Get your proof page

We check your public GitHub repository and give you a page to share with recruiters, and can add the project to your resume.