Essential Docker CLI Commands: A Beginner’s Guide

Docker CLI commands are vital for managing containerized applications. This guide covers essential commands like `docker run`, `docker ps`, and `docker build` to streamline your development workflow.
Table of Contents
essential-docker-cli-commands-a-beginners-guide-2

Docker CLI: Essential Commands for Beginners

Docker is a powerful platform that enables developers to automate the deployment of applications inside lightweight containers. Containers encapsulate an application and its dependencies, ensuring that it runs seamlessly across different environments. While Docker provides a graphical user interface (GUI) in some instances, the command line interface (CLI) is the most efficient and preferred way to interact with Docker. In this article, we’ll dive deep into essential Docker CLI commands, providing a comprehensive guide for beginners aiming to harness the power of Docker.

What is Docker?

Before we delve into the CLI commands, let’s briefly review what Docker is. Docker is an open-source containerization platform that enables developers to package applications into containers. A container includes the application code, runtime, libraries, and system tools needed to run the application. This isolation allows developers to be confident that their application will behave the same way regardless of where it is run, whether on a developer’s machine, a staging server, or in production.

Installing Docker

Before you can start using Docker CLI commands, you need to install Docker on your machine. Docker provides detailed instructions for various operating systems—Windows, macOS, and Linux. The installation process typically involves downloading Docker Desktop for Windows and macOS or installing Docker Engine for Linux distributions.

Installation Steps

  1. Download Docker: Visit the Docker website and download the appropriate version for your operating system.

  2. Install Docker: Follow the installation instructions specific to your OS. On Linux, you might need to configure the Docker repository and install Docker via your package manager.

  3. Start Docker: Ensure Docker is running on your machine. On Docker Desktop, you may see an icon in your system tray indicating that Docker is active.

  4. Test Installation: Open your terminal and run the command:

    docker --version

    This should return the installed version of Docker if everything was set up correctly.

Docker CLI Basics

The Docker CLI allows users to interact with the Docker daemon and manage Docker containers, images, networks, and volumes. The basic syntax of any Docker command is:

docker [OPTIONS] COMMAND [ARG...]

To get familiar with Docker commands, you can always start by running:

docker help

This command will provide you with a brief overview of available commands.

Essential Docker CLI Commands

1. Docker Images

Images are the blueprints for containers. They contain everything needed to run applications, including the code, libraries, and runtime. The following commands are fundamental when working with Docker images.

a. Listing Images

To list all the images on your local system, use:

docker images

This command displays a list of images, including their repository, tag, image ID, creation date, and size.

b. Pulling an Image

To download a Docker image from Docker Hub, use:

docker pull [IMAGE:TAG]

For example:

docker pull nginx:latest

This command fetches the latest version of the Nginx image.

c. Building an Image

You can create your own Docker image using a Dockerfile. Use the following command to build an image:

docker build -t [IMAGE_NAME:TAG] [PATH_TO_DOCKERFILE]

Example:

docker build -t myapp:1.0 .

This command builds an image named myapp with the tag 1.0 using the Dockerfile in the current directory.

d. Removing an Image

To remove an image, use:

docker rmi [IMAGE_ID]

If an image is being used by a container, you might need to remove the container first or use the -f flag to force the removal.

2. Docker Containers

Containers are instances of Docker images that run applications. You can create, start, stop, and manage containers using various Docker CLI commands.

a. Listing Containers

To list running containers, use:

docker ps

To see all containers (including stopped ones), run:

docker ps -a

b. Running a Container

To create and start a new container from an image, use:

docker run [OPTIONS] [IMAGE:TAG]

For example, to run a new Nginx container:

docker run -d -p 80:80 nginx:latest

The -d option runs the container in detached mode, and -p maps port 80 of the container to port 80 of the host.

c. Stopping a Container

To stop a running container, use:

docker stop [CONTAINER_ID]

You can also use the container name as an identifier.

d. Removing a Container

To remove a stopped container, use:

docker rm [CONTAINER_ID]

For removing all stopped containers, you can run:

docker container prune

e. Viewing Logs

To view the logs from a container, use:

docker logs [CONTAINER_ID]

This is particularly useful for debugging applications running inside a container.

3. Docker Networks

Docker allows you to create and manage networks, enabling containers to communicate with each other.

a. Listing Networks

To see all available networks, use:

docker network ls

b. Creating a Network

To create a new network:

docker network create [NETWORK_NAME]

Example:

docker network create my_network

c. Connecting a Container to a Network

To connect an existing container to a network:

docker network connect [NETWORK_NAME] [CONTAINER_ID]

d. Disconnecting a Container from a Network

To disconnect a container from a network:

docker network disconnect [NETWORK_NAME] [CONTAINER_ID]

4. Docker Volumes

Volumes are used to persist data generated by and used by Docker containers. They provide a way to store data outside of containers, allowing data to remain intact even if containers are stopped or removed.

a. Listing Volumes

To list all volumes, use:

docker volume ls

b. Creating a Volume

To create a new volume:

docker volume create [VOLUME_NAME]

c. Inspecting a Volume

To get detailed information about a volume:

docker volume inspect [VOLUME_NAME]

d. Removing a Volume

To remove a volume:

docker volume rm [VOLUME_NAME]

To remove all unused volumes:

docker volume prune

5. Docker Compose

Although not a single command, Docker Compose is an essential tool for managing multi-container applications. It allows you to define and run multi-container Docker applications using a single YAML file.

a. Defining a Compose File

Create a docker-compose.yml file where you define your services, networks, and volumes.

Example:

version: '3'
services:
  web:
    image: nginx
    ports:
      - "80:80"
  db:
    image: mysql
    environment:
      MYSQL_ROOT_PASSWORD: example

b. Starting Services

To start the services defined in your docker-compose.yml file, run:

docker-compose up

Add the -d flag to run in detached mode.

c. Stopping Services

To stop running services, use:

docker-compose down

This command stops and removes the containers defined in the docker-compose.yml file.

Docker Security

While Docker simplifies application deployment, security is crucial, especially in production environments. Understanding the security implications of containerization can help you mitigate risks. Here are some best practices:

  1. Use Official Images: Whenever possible, use official images from Docker Hub. These are regularly updated and maintained by trusted sources.

  2. Limit Container Privileges: Run containers with the least privileges necessary. Use the --user option to specify a user and avoid running as root.

  3. Regularly Scan Images: Use tools like Docker Bench Security or other scanning tools to check for vulnerabilities in your images.

  4. Control Resource Usage: Limit the amount of CPU and memory a container can use with the --memory and --cpus options when running a container.

  5. Network Security: Isolate containers using networks and limit access to only those that need it.

Conclusion

Understanding Docker CLI and its essential commands is crucial for any developer looking to leverage the power of containerization. The commands outlined in this article provide a solid foundation for beginners, allowing you to manage images, containers, networks, and volumes effectively.

As you advance in your Docker journey, consider exploring more complex scenarios such as multi-container applications with Docker Compose, orchestration with Kubernetes, and continuous integration and deployment (CI/CD) pipelines that integrate Docker for automated deployments.

By mastering Docker CLI, you will enhance your development workflow, increase the consistency of your applications, and ultimately deliver better software faster. Happy Dockerizing!