Introduction to Docker
What containers and images are, why they make deploying so much easier, and the Docker commands you need to run, inspect and clean up your first containers.
Updated 1 November 20265 min read
"It works on my machine" is one of the oldest problems in software. Your development environment is almost never identical to the server your app ends up on: a different OS version, a missing library, a different runtime. Containers solve this by packaging your app together with everything it needs. In this tutorial we look at what Docker is and get hands-on with the commands you will use every day.
What is a container?
A container is a portable, isolated environment that holds everything your app needs to run: the system libraries, dependencies, configuration and the app itself. Any machine that can run containers can run your app, exactly the same way.
Every container is created from an image. Think of an image as a blueprint and a container as a house built from it. You can start as many containers from the same image as you like, and each one runs on its own.
Images are described in a Dockerfile: a plain text file (without extension) with the steps to build the image. You build it with docker build. We won't write our own Dockerfile in this post (that's what the follow-up is for). Instead we'll use ready-made images from Docker Hub, the public registry for Docker images. The full syntax lives in the Dockerfile reference.
About Docker
Docker is the most widely used tool to build, share and run containers. The engine behind it is open source, and Docker Desktop gives you a friendly app on Windows, macOS and Linux (free for personal use, education and small businesses). The same commands work for a single hello-world container and for production systems running dozens of services.
Why use containers?
- Portability
- All dependencies ship inside the container, so "missing package" errors on the server are a thing of the past.
- Each container is sandboxed and isolated from the host machine. 2. Agility
- Deploy to any machine that runs containers, with no manual setup on the host.
- Development, CI and production all run the same image. 3. Scalability
- Scale horizontally by running more instances of the same image.
- Give a container more CPU or memory when it needs it.
- A natural fit for splitting a system into smaller services.
Setting up Docker
Install Docker Desktop on Windows or macOS, or the Docker Engine on a Linux server. Then check that it works:
docker --versionIf you see a version number like this, you are good to go:
Using Docker for the first time
Make sure Docker Desktop (or the Docker service) is running before you continue.
Hello, world
We start with the smallest container there is. It prints a message and exits:
docker run hello-worldThe output should look something like this:
The first two lines are the interesting ones:
Unable to find image 'hello-world:latest' locally: Docker could not find the image on your machine, so it downloads it from Docker Hub.latest: Pulling from library/hello-world: Docker pulls thelatesttag. To pin a specific version, useimage:tag, for examplenginx:1.27.
Docker then creates a container from the image and starts it. It prints "Hello from Docker!" and exits. Congratulations, you just ran your first container!
Where is my container?
Let's list the running containers:
docker ps
# long form: docker container lsThe list is empty. That's because the hello-world container already exited, and docker ps only shows running containers by default. Add -a to see all of them:
docker ps -aNow you see the hello-world container with the status Exited. Remove it with:
docker rm <container-id>You don't need to type the full ID: the first few characters are enough, as long as they are unique. You can also use the container's name.
Images, the blueprints of containers
The container is gone, but the image it came from is still on your machine:
docker images
# long form: docker image lsI have quite a few images on my machine. You will probably only see hello-world. Now run the container twice:
docker run hello-world && docker run hello-worldThis time there is no download: the image is already available locally. Run docker ps -a again and you'll find two containers. Both use the same image, but they have different container IDs, because each container is its own instance.
Clean them up with docker rm, or remove every stopped container at once with docker container prune. Tip: start a container with --rm (docker run --rm hello-world) and Docker deletes it automatically when it exits.
Finally, remove the image itself:
docker rmi hello-world
# long form: docker image rm hello-worldRunning a whole Linux system in a container
Time for something more fun: a full Linux shell inside a container. We'll use Alpine, a tiny Linux distribution of only a few megabytes.
docker pull alpine
docker run -it --rm alpine-i keeps the input open and -t gives you a terminal, so together they let you interact with the container. You should see a shell prompt:
This shell supports the usual Linux commands. Try:
echo "Hello from Alpine!"
lsThis is the container's own filesystem, completely separate from your host. You can experiment as much as you like without breaking anything on your machine. Type exit to leave. Thanks to --rm, the container is removed straight away.
Running in the background
Now start Alpine in detached mode, so it keeps running in the background. We give it a name, so we don't have to copy IDs around:
docker run -dit --name my-alpine alpineTo open a shell inside a running container, use docker exec:
docker exec -it my-alpine shYou are back inside the container. Type exit to leave the shell. The container keeps running, because exec only started an extra process in it. Stop and remove it when you're done:
docker stop my-alpine
docker rm my-alpineNeed help?
Add --help to any command (for example docker run --help) to see all its options, or run docker help for the full command list. The Docker documentation is excellent too.
Conclusion
You now know what containers and images are, and you can run, inspect, enter and clean up containers. That covers a surprising amount of everyday Docker work. Ready for the next step? Learn how to build and deploy your own images.
Frequently asked questions
What is the difference between a Docker image and a container?
An image is a read-only template that describes everything an app needs. A container is a running instance created from that image. You can start many containers from the same image.
Why doesn't docker ps show my container?
docker ps only lists running containers. A container that has already exited, like hello-world, only shows up with docker ps -a.
- Docker
- DevOps