Table of Contents
Introduction
In today’s fast-paced DevOps environment, Docker has become a cornerstone technology, simplifying the process of deploying and managing applications in containerized environments. Whether you’re a beginner or an experienced professional, having a handy reference for Docker CLI commands is essential. This Docker CLI cheat sheet will guide you through the most important commands you need to know, helping you manage containers and images with ease.
Why Docker is Essential for DevOps
Docker has revolutionized the way we deploy applications, offering a consistent environment across various stages of development, testing, and production. It enables teams to package applications with all their dependencies, ensuring they run seamlessly across different computing environments. This capability makes Docker an indispensable tool in DevOps, where speed, consistency, and reliability are key.
Building Docker Images
How to Build an Image from a Dockerfile
One of the first steps in using Docker is building an image from a Dockerfile
. This command packages your application and its dependencies into an image that can be run anywhere Docker is installed.
$ docker build -t devopsroles/centos:latest .
- Explanation: The
-t
option tags the image with a name (devopsroles/centos
) and a version (latest
). The.
at the end specifies the build context, which is typically the directory containing your Dockerfile.
Common Issues When Building Images
Building Docker images is generally straightforward, but you may encounter issues such as large image sizes or failed builds. To minimize image size, consider using multi-stage builds or Alpine-based images. If a build fails, check your Dockerfile for syntax errors or missing dependencies.
Running Containers
Running a Command in an Image
Running a command in a Docker container is as simple as using the docker run
command. This command starts a new container and executes the specified command.
docker run [options] IMAGE
- Options: Docker provides various options to customize how your container runs. For example, you can use the
-d
option to run the container in detached mode or the-it
options to run it interactively.
Example: Running a Bash Shell in Fedora
$ docker run -it fedora bash
- Explanation: This command runs a Bash shell in an interactive Fedora container. The
-it
options allow you to interact with the container in real time.
Managing Docker Containers
Creating and Starting Containers
Creating a Docker container from an image is often the first step in deploying an application. The docker create
command allows you to set various options for your container.
docker create [options] IMAGE
Important Options for Container Creation
-a, --attach
: Attach to stdout/err.-i, --interactive
: Attach stdin (interactive mode).-t, --tty
: Pseudo-tty (useful for terminal-based applications).--name NAME
: Name your container for easier management.-p, --publish 5000:5000
: Port mapping from the container to the host.--expose 5432
: Expose a specific port to linked containers.-v, --volume $(pwd):/app
: Mount a directory from the host to the container.-e, --env NAME=hello
: Set environment variables.
Example: Creating and Starting a Container
$ docker create -t -i fedora bash
- Explanation: This command creates a Fedora container with an interactive Bash shell. The
-t
and-i
options allow for terminal interaction.
Executing Commands in Running Containers
Docker allows you to run commands inside a running container using the docker exec
command. This is useful for tasks such as troubleshooting or running administrative tasks.
docker exec [options] CONTAINER COMMAND
Key Options for docker exec
-d, --detach
: Run the command in the background.-i, --interactive
: Keep stdin open even if not attached.-t, --tty
: Allocate a pseudo-TTY for interactive use.
Example: SSH into a Docker Container
$ docker exec -it 59e59adcc0b4 /bin/bash
- Explanation: This command opens an interactive Bash shell in a running container with the ID
59e59adcc0b4
.
Starting and Stopping Containers
How to Start a Docker Container
Starting a stopped container is straightforward with the docker start
command. This command resumes a container that was previously stopped.
docker start [options] CONTAINER
- Options: The
-a
and-i
options attach to stdout/err and stdin, respectively, allowing you to interact with the container as it starts.
Stopping a Running Docker Container
To stop a running container, use the docker stop
command. This gracefully shuts down the container.
docker stop [options] CONTAINER
- Tip: If you need to force-stop a container, use
docker kill
instead ofdocker stop
.
Docker Images Management
Viewing Docker Images
Docker images are the building blocks of containers. You can view the images on your system using the docker images
command.
$ docker images
- Explanation: This command lists all images available locally, showing details such as repository name, tags, and image IDs.
Deleting Docker Images
To free up disk space or remove outdated images, you can delete Docker images using the docker rmi
command.
$ docker rmi b750fe78269d
- Explanation: This command removes the image with the ID
b750fe78269d
. Be cautious when removing images, as containers depending on them will fail to start.
Advanced Container Management
Using docker ps
to Manage Containers
The docker ps
the command is your go-to tool for listing running containers. You can view all containers, including stopped ones, by adding the -a
option.
$ docker ps
$ docker ps -a
- Explanation: The first command lists only running containers, while the second lists all containers, regardless of their state.
Killing a Docker Container
Sometimes, a container may become unresponsive and need to be forcefully terminated. The docker kill
command sends a SIGKILL signal to the container, immediately stopping it.
$ docker kill $ID
- Tip: Use the container’s ID or name to target the right one. Be careful with this command, as it does not allow the container to gracefully shut down.
Frequently Asked Questions (FAQs)
What is Docker used for?
Docker is a platform that enables developers to package applications into containers—standardized units of software that include everything the app needs to run. This ensures consistency across different environments, from development to production.
How does Docker differ from a virtual machine (VM)?
Docker containers share the host system’s kernel, making them more lightweight and faster to start than VMs. While VMs include a full OS, containers only include the application and its dependencies.
What are the key benefits of using Docker in DevOps?
Docker offers several benefits, including consistent environments across different stages, rapid deployment, efficient resource utilization, and ease of scaling applications.
Can I run multiple containers on the same host?
Yes, Docker allows you to run multiple containers on the same host. Each container operates in isolation but can communicate with others if needed through networks.
How do I update a running Docker container?
To update a running container, you typically create a new image with the desired changes, stop the old container, and start a new one with the updated image. You can use docker commit
to save changes from a running container, but this is generally not recommended for production environments.
Conclusion
Docker CLI is a powerful tool that can significantly simplify the management and deployment of applications in containerized environments. Whether you’re building images, running containers, or managing your Docker infrastructure, this cheat sheet provides the essential commands you need to master Docker. As you continue to work with Docker, you’ll find these commands becoming second nature, enabling you to focus more on developing and deploying great software.
By using this Docker CLI cheat sheet, you’ll be well-equipped to handle various tasks in your DevOps role efficiently. Remember, Docker is continuously evolving, so staying up-to-date with the latest commands and best practices is crucial for maximizing your productivity. Thank you for reading the DevopsRoles page!