Docker Essentials: First Steps and Basic Commands
Introduction
If you have just installed Docker using my Universal Guide to Installing Docker on Debian and Ubuntu Systems, you might be wondering: "What now?".
A container is an isolated environment for your code. It has no knowledge of your host operating system or your files unless you explicitly configure it. Containers contain everything your code needs to run, down to a base operating system (like Alpine or Ubuntu).
This guide serves as a "cheat sheet" for the most popular Docker commands you will use daily.
Getting help
Docker has a built-in documentation system. If you ever forget a command, you can ask Docker itself.
List all available commands and flags:
xinit@localhost:~$ docker --help
You can also get specific help for any subcommand. For example, to see all arguments available for managing images:
xinit@localhost:~$ docker images --help
System information
Before starting, it is useful to check the state of your Docker installation, including the number of running containers and images.
Display system-wide information:
xinit@localhost:~$ docker info
Working with images
Everything starts with an Image. Think of an image as a read-only template or a "class" in programming. You cannot edit an image directly; you use it to create running containers.
Download (pull) an image from Docker Hub (the default registry) to your local machine:
xinit@localhost:~$ docker pull ubuntu
Check the list of all images currently stored on your machine:
xinit@localhost:~$ docker images
Running containers
A Container is a running instance of an image. You can modify files inside a container, but these changes are lost when the container is removed (unless you use Volumes, which we will cover in future articles).
Interactive mode
To run a container and immediately drop into its shell, use the interactive flags.
- -i: Interactive (keeps STDIN open).
- -t: Allocate a pseudo-TTY.
- --name: Assigns a readable name to the container (easier than using IDs).
Start an Ubuntu container and open a terminal inside it:
xinit@localhost:~$ docker run -it --name MyLocalUbuntu ubuntu
To exit this container, simply type exit or press CTRL+D.
Detached mode (background services)
For web servers or databases, you usually want the container to run in the background. It is also necessary to map ports so the outside world can access the service.
- -d: Detached mode (run in background).
- -p host_port:container_port: Maps a port on your computer to a port inside the container.
Run a web server in the background, mapping port 80 of the host to 8080 of the container:
xinit@localhost:~$ docker run -d -p 80:8080 docker/getting-started
Monitoring containers
Once containers are running, you need to manage them.
List currently running containers:
xinit@localhost:~$ docker ps
List all containers, including those that have stopped or exited:
xinit@localhost:~$ docker ps -a
Stopping and pausing
You can control the execution state of your containers.
Gracefully stop a running container (sends SIGTERM, waits, then SIGKILL):
xinit@localhost:~$ docker stop <container_id_or_name>
Forcefully kill a container immediately (sends SIGKILL):
xinit@localhost:~$ docker kill <container_id_or_name>
Pause all processes within a container (freezes the state):
xinit@localhost:~$ docker pause <container_id_or_name>
Unpause a container:
xinit@localhost:~$ docker unpause <container_id_or_name>
Cleaning up
Docker does not automatically delete stopped containers or unused images; they take up disk space.
Remove a specific container (it must be stopped first):
xinit@localhost:~$ docker rm <container_id_or_name>
Force remove a container (even if it is running):
xinit@localhost:~$ docker rm -f <container_id_or_name>
Remove an image from your local storage:
xinit@localhost:~$ docker rmi <image_id_or_name>
Conclusion
These commands form the backbone of Docker usage. Whether you are developing a Python Django application or configuring a database, you will use pull, run, ps, and rm constantly. Mastering these basics is the key to efficient container management and a prerequisite for more complex orchestrations.