Docker intermediate
Running someone else's image is a start. Now build your own.
Chapters
- 0:00Introduction
- 0:05With Server AI Hub: Logs together
- 0:17What you gain: The benefits
- 0:28The alternative: By hand
- 0:37The recipe: A Dockerfile
- 0:50Make it: Build and run
- 1:00Speed: Layers and cache
- 1:10Keep it clean: Leave things out
- 1:21Look inside: Logs and shell
- 1:29Your files: Volume or bind mount
- 1:40Many parts: Docker Compose
- 1:53Settings: Pass in settings
- 2:02Share it: Tag and push
- 2:11Recap: You've got it
- 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 myappSpeed: 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 psYour 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 downSettings: 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 myappShare 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.0Recap: 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
- docs.docker.com/get-started/docker-concepts/building-images/writing-a-dockerfile
- docs.docker.com/build/cache
- docs.docker.com/build/concepts/context
- docs.docker.com/engine/storage/volumes
- docs.docker.com/engine/network/port-publishing
- docs.docker.com/compose
- docs.docker.com/reference/cli/docker/compose/down
- docs.docker.com/compose/how-tos/environment-variables
- docs.docker.com/get-started/docker-concepts/building-images/build-tag-and-publish-an-image
- docs.docker.com/reference/cli/docker/container/run
More lessons
- CUDA intermediate 2:04
- CUDA advanced 2:08
- Docker basics 3:24
- Docker advanced 2:44
- Docker networking 2:28
- GitHub basics 1:51