Getting Started with Docker

Docker has become an indispensable tool for developers, enabling consistent environments across different machines. Whether you're building, shipping, or running applications, understanding basic Docker commands is crucial. This guide provides a cheat sheet for essential commands to get you up and running.

Checking Docker Installation

Before you can use Docker, you need to ensure it's installed and accessible on your system. The simplest way to verify this is by checking the Docker version.

Check Docker Version

docker --version

This command will output the installed Docker version, confirming that the Docker client and daemon are functioning correctly. If you encounter an error, it means Docker is not installed or not properly configured in your system's PATH.

Running Your First Container

The docker run command is the cornerstone of Docker operations. It creates and starts a new container from a specified image. Let's start with the classic hello-world image.

Run the Hello-World Container

docker run hello-world

When you execute this command, Docker will first check if the hello-world image is present locally. If not, it will pull the image from Docker Hub. Subsequently, it will run a container based on this image, which prints a confirmation message and exits. This is a great way to test your Docker installation.

Managing Containers

Once you start running containers, you'll need ways to monitor and manage them. Docker provides commands to list running and stopped containers.

List Running Containers

docker ps

The docker ps command displays all containers that are currently active and running. It shows information such as the Container ID, Image used, Command being executed, Creation time, Status, Ports, and Names.

List All Containers (Running and Stopped)

To see a comprehensive list of all containers, including those that have exited, use the -a flag.

docker ps -a

This is particularly useful for debugging or identifying containers that might have stopped unexpectedly. You can then use their Container IDs to interact with them further, such as stopping, starting, or removing them.

Stop a Container

To stop a running container, you need its Container ID or Name.

docker stop <container_id_or_name>

This command sends a SIGTERM signal to the primary process inside the container, allowing it to shut down gracefully. If the container doesn't stop within a certain grace period, Docker sends a SIGKILL signal.

Start a Stopped Container

If you have a stopped container, you can restart it using its ID or Name.

docker start <container_id_or_name>

This resumes a stopped container. Note that this command only starts the container; it doesn't necessarily restart the application within it if the application itself had crashed.

Restart a Container

For a quick restart (stop and then start), use:

docker restart <container_id_or_name>

Remove a Container

Once you no longer need a container, you can remove it to free up disk space. You must stop a container before you can remove it, unless you use the -f flag for force removal.

docker rm <container_id_or_name>

For convenience, you can remove a running container by using the -f flag, though this is generally not recommended for graceful shutdowns.

docker rm -f <container_id_or_name>

A common pattern is to remove all stopped containers with a single command:

docker container prune

This command will prompt for confirmation before removing all stopped containers.

Managing Images

Docker images are the building blocks for containers. They are read-only templates that contain instructions for creating a container. Managing images involves listing, pulling, and removing them.

List Downloaded Images

To see all the Docker images you have downloaded locally:

docker images

This command lists images, showing their Repository, Tag, Image ID, Creation date, and Size. This helps you keep track of what images are available on your system.

Pull an Image

If you need an image that isn't on your local machine, you can pull it from a registry, typically Docker Hub.

docker pull <image_name>[:<tag>]

For example, to pull the latest Ubuntu image: docker pull ubuntu:latest.

Remove an Image

To remove an image from your local storage:

docker rmi <image_id>

You cannot remove an image if it is currently used by any container (running or stopped). You must remove the containers first. Similar to containers, you can prune unused images:

docker image prune

This command removes all dangling images (images that are not tagged and not referenced by any container). Use docker image prune -a to remove all unused images, including those not dangling.

Building Images

While pulling pre-built images is common, you'll often need to build your own custom images using a Dockerfile.

Build an Image

Navigate to the directory containing your Dockerfile and run:

docker build -t <image_name>:<tag> .

The -t flag tags the image with a name and optional tag. The . indicates the build context (the current directory), which Docker uses to find the Dockerfile and any files referenced within it.

Viewing Logs

Sometimes, containers run in the background and you need to inspect their output. The docker logs command is essential for this.

View Container Logs

docker logs <container_id_or_name>

This command fetches the logs of a container. You can use the -f flag to stream the logs in real-time, similar to the tail -f command in Linux.

docker logs -f <container_id_or_name>

This is invaluable for troubleshooting applications running inside containers. You can also combine it with other flags to view recent logs or timestamps.

Executing Commands in a Container

Often, you need to execute a command inside a running container, perhaps to inspect its filesystem or run a utility.

Execute a Command

The docker exec command allows you to run commands in a running container.

docker exec -it <container_id_or_name> <command>

The -i flag keeps stdin open, and -t allocates a pseudo-TTY, effectively giving you an interactive shell session. For example, to get a bash shell inside an Ubuntu container:

docker exec -it <container_id_or_name> bash

This command is your gateway into a running container's environment, essential for debugging and ad-hoc tasks.

System-Wide Docker Commands

Beyond managing individual containers and images, Docker offers commands to inspect and manage the Docker system itself.

Inspect Docker Objects

The docker inspect command provides detailed low-level information about Docker objects (containers, images, networks, volumes, etc.) in JSON format.

docker inspect <container_id_or_name>

This is useful for understanding the configuration and state of a particular Docker resource.

System Prune

Docker can accumulate unused data over time, including stopped containers, dangling images, unused networks, and build cache. The docker system prune command cleans this up.

docker system prune

Use the -a flag with system prune to remove all unused images, not just dangling ones. This command can free up significant disk space but should be used with caution.

Conclusion

This cheat sheet covers the most fundamental Docker commands. Mastering these will provide a solid foundation for working with containers, enabling you to manage your development and deployment workflows more effectively. As you progress, you'll explore more advanced commands for networking, volumes, and orchestration.