Learn / Lessons · Docker

Docker intermediate

Running someone else's image is a start. Now build your own.

Chapters

  1. 0:00Introduction
  2. 0:05With Server AI Hub: Logs together
  3. 0:17What you gain: The benefits
  4. 0:28The alternative: By hand
  5. 0:37The recipe: A Dockerfile
  6. 0:50Make it: Build and run
  7. 1:00Speed: Layers and cache
  8. 1:10Keep it clean: Leave things out
  9. 1:21Look inside: Logs and shell
  10. 1:29Your files: Volume or bind mount
  11. 1:40Many parts: Docker Compose
  12. 1:53Settings: Pass in settings
  13. 2:02Share it: Tag and push
  14. 2:11Recap: You've got it
  15. 2:16Wrap up
Read the transcript

Introduction

Running someone else's image is a start. Now build your own.

With Server AI Hub: Logs together

With Server AI Hub, you can read the logs of several containers, from both of your Sparks, on one screen. Each log sits under its container's status, memory and CPU.

What you gain: The benefits

Compose projects are grouped, showing how many parts are up. Pull an image, or copy it to your other Spark, from a button. And there's no log command to type.

The alternative: By hand

The other way is by hand, in a Terminal. Writing a Dockerfile and building your own image is done there in any case. Here's how it works.

The recipe: A Dockerfile

A Dockerfile is the recipe for an image. Start from a base image, and set a working folder. Copy the list of requirements and install them. Then copy your code, and say what to run.

FROM python:3.12-slim
WORKDIR /app
COPY requirements.txt .
RUN pip install -r requirements.txt
COPY . .
CMD ["python", "app.py"]

Make it: Build and run

Build the image and give it a name. Run it in the background, with a name of its own. Publish a port so you can reach it, here only from this machine.

docker build -t myapp .
docker run -d --name web \
  -p 127.0.0.1:8000:8000 myapp

Speed: Layers and cache

Images are built in layers, and Docker reuses the ones that haven't changed. So put the slow, steady steps first, and your fast changing code last.

Keep it clean: Leave things out

Add a docker ignore file. Leave out version control folders and local clutter. Keep secret files out of the build. Smaller input means faster builds.

Look inside: Logs and shell

When something's wrong, read the logs. Open a shell inside the running container. And check what's running.

docker logs -f web
docker exec -it web sh
docker ps

Your files: Volume or bind mount

A volume is storage that Docker manages. It suits databases and app data. A bind mount shares a folder from your machine. It's handy while you're developing.

Many parts: Docker Compose

Most apps have several parts. Compose describes them in one file, then starts them all together. Down stops and removes them. Adding -v also deletes volumes, so take care.

docker compose up -d
docker compose down

Settings: Pass in settings

The same image can run with different settings. Pass one with -e, or keep several in a file, and keep that file out of Git.

docker run -e LOG_LEVEL=debug myapp
docker run --env-file .env myapp

Share it: Tag and push

To share your image, give it a name in your registry, sign in, and push it. Anyone with access can then pull it.

docker tag myapp you/myapp:1.0
docker login
docker push you/myapp:1.0

Recap: You've got it

Write a Dockerfile, build, run, and let Compose join the parts.

Wrap up

Build your image by hand, then let Server AI Hub watch it run: logs side by side, images a button away, and your projects at a glance.

Sources

More lessons