Key Docker Commands Overview
To conclude, we have compiled an overview of essential Docker commands discussed in previous sections. These commands are gathered in one place to help you quickly recall their functionality.
-
Downloads an image from Docker Hub to your local environment. Docker fetches the latest version unless a specific tag is specified.
docker pull [image_name]
-
Runs a container based on the specified image. If the image is not found locally, Docker will attempt to pull it from Docker Hub before launching the container.
docker run [image_name]
-
Fetches the official Nginx HTTP server image from Docker Hub. If the latest version is not available locally, it downloads it automatically. You can specify a version using a tag, e.g.,
docker pull nginx:1.19
.docker pull nginx
-
Runs an Nginx container and maps port
80
in the container to port8080
on the host. This allows access to the Nginx server viahttp://localhost:8080
.docker run -p 8080:80 nginx
-
Displays a list of currently running containers along with useful details.
docker ps
-
Stops a running Docker container. The container ID can be retrieved using
docker ps
.docker stop [container_id]
-
Builds a Docker image from a Dockerfile, assigning a name and tag to the resulting image.
docker build -t [image_name]:[tag] [path_to_Dockerfile]
-
Lists all images available in the local Docker environment.
docker images
-
Runs a container in detached mode, mapping ports and allowing the terminal to remain available.
docker run -d -p [host_port]:[container_port] [image_name]
-
Displays logs from a running container in real-time. Press
CTRL+C
to exit.docker logs [container_id] --follow
-
Opens an interactive Bash shell inside a running container.
docker exec -it [container_id] /bin/bash
-
Launches services defined in a
docker-compose.yml
file, building images if necessary and running them in the background.docker-compose up -d
-
Displays logs from all containers managed by Docker Compose. You can view logs for a specific service using
docker-compose logs [service_name]
.docker-compose logs
Data Persistence in Docker
By default, data inside a Docker container is ephemeral. This means that when a container is removed, all data stored within it is also lost. However, in many cases—such as databases, logs, or configuration files—persistent storage is required. Docker provides several solutions for this, including Volumes, Bind Mounts, and Tmpfs Mounts.
Stopping vs. Removing Containers
-
Stopping a Container — Temporarily halts a container. This sends a
SIGTERM
signal, followed bySIGKILL
if necessary. Stopping a container does not delete its data, allowing it to be restarted.docker stop [container_id]
To restart a stopped container, use:
docker start [container_id]
-
Removing a Container — Permanently deletes a container and its associated data (unless stored in a volume). Once removed, a container cannot be restored.
docker rm [container_id]
Docker Compose Logging
Docker Compose retains container logs even after stopping the containers, as logs are stored within Docker’s internal log database. Logs persist until the container is removed.
docker-compose down
In large environments, retrieving logs from all services can be done using:
docker-compose logs
To filter logs for a specific service, use:
docker-compose logs [service_name]