docker run basics
- Docker & Kubernetes course
- Beginner
- Lesson 13/82
- 4 min
Learn the basic use of the docker run command.
docker run basics
It has the form docker run [options] image [command]. A command written after the image replaces the image's default start command:
docker run ubuntu echo "Merhaba" # echo runs, the container stops
docker run ubuntu cat /etc/os-release # prints the Ubuntu version infoA container stops the moment its main process (here echo) ends. That is why long-running processes like a web server keep the container open, while one-off commands close immediately.
The image tag
An image name has the form name:tag. If no tag is given, latest is used; but latest doesn't mean "newest", it's just the default tag. For reproducibility the version should always be written:
docker run ubuntu:24.04 cat /etc/os-release
docker run python:3.12-slim python --version # Python 3.12.xPractice Passing a command
Write the command that runs python --version in the python:3.12-slim image and deletes the container when done.
Starter code
# buraya yazExpected output
Python 3.12.7
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 What is the container's state after docker run ubuntu echo merhaba?
- Paused
- Stopped (Exited), because the main process ended Correct answer
- Running
- Removed
Explanation: A container's lifetime is tied to its main process.
2 Use a specific Ubuntu version.
docker run ubuntu____ cat /etc/os-release
docker run ubuntu:24.04 cat /etc/os-release
Explanation: The tag is written with a colon.
3 Match.
| Term | Meaning |
|---|---|
ubuntu |
ubuntu:latest |
echo merhaba |
Replaces the default command |
Exited |
Main process ended |
python:3.12-slim |
Versioned tag |