DEV Community

Adarsh BP
Adarsh BP

Posted on

Mastering Docker: A Comprehensive Guide to Docker Commands

Docker has revolutionized the way developers build, ship, and run applications by providing a consistent environment across different machines. Whether you're a seasoned Docker user or just getting started, mastering Docker commands and best practices is essential for efficient containerization and management of your applications. In this guide, we'll explore a comprehensive list of Docker commands and best practices to help you navigate the Docker ecosystem effectively.

General Commands

Viewing Docker Information

  • docker version: Provides detailed information about your Docker CLI and daemon versions.
  • docker system info: Lists data about your Docker environment, including active plugins, containers, and images.
  • docker help: Displays the help index, a reference of all supported commands.
  • docker <command> --help: Shows help information about a particular command, including detailed information on supported option flags.

Building Images

Building Docker Images from Dockerfiles

  • docker build .: Builds the Dockerfile in your working directory into a new image.
  • docker build -t example-image:latest .: Builds the Dockerfile and tags the resulting image as example-image:latest.
  • docker build -f docker/app-dockerfile: Builds the Dockerfile at a specific path.
  • docker build --build-arg foo=bar .: Sets a build argument while building an image.
  • docker build --pull .: Instructs Docker to pull updated versions of images referenced in FROM instructions before building.
  • docker build --quiet .: Builds an image without emitting any output during the build process.

Creating Images

  • docker commit <container> new-image:latest: Saves the current state of a container to a new image.

Running Containers

Managing Containers

  • docker run example-image:latest: Runs a new container using the specified image.
  • docker run --rm example-image:latest: Automatically removes the container when it exits.
  • docker run -d example-image:latest: Detaches your terminal from the running container, leaving it in the background.
  • docker run -it example-image:latest: Attaches your terminal’s input stream and TTY to the container for interactive commands.
  • docker run --name my-container example-image:latest: Names the new container.
  • docker run --hostname my-container example-image:latest: Sets the container’s hostname.
  • docker run --env foo=bar example-image:latest: Sets environment variables inside the container.
  • docker run -p 8080:80 example-image:latest: Binds ports between the host and container.
  • docker run -v /host-directory:/container-directory example-image:latest: Sets up volume mounts.
  • docker run --network my-network example-image:latest: Connects the container to a Docker network.

Inspecting Containers

  • docker inspect <container>: Retrieves detailed information about a container in JSON format.

Pausing and Unpausing Containers

  • docker pause <container> and docker unpause <container>: Pauses and unpauses the processes running within a container.

Restart Policies

  • docker run --restart unless-stopped example-image:latest: Sets the container to start automatically when the Docker daemon starts, unless manually stopped. Other restart policies are also supported.

Managing Running Containers

  • docker ps: Lists all running containers.
  • docker ps -a: Lists all containers, including stopped ones.
  • docker attach <container>: Attaches your terminal to a container’s foreground process.
  • docker kill <container>: Sends a SIGKILL signal to force stop a container.
  • docker stop <container> and docker start <container>: Stops and starts a container.
  • docker rm <container>: Deletes a container.

Copying to and from Containers

Copying Files

  • docker cp example.txt my-container:/data: Copies a file from the host to a container.
  • docker cp my-container:/data/example.txt /demo/example.txt: Copies a file from a container to the host.

Executing Commands in Containers

Running Commands

  • docker exec my-container demo-command: Runs a command inside a running container.
  • docker exec -it my-container demo-command: Runs a command interactively.

Accessing Container Logs

Viewing Logs

  • docker logs <container>: Streams log output from a container.
  • docker logs <container> --follow: Streams new logs as they’re stored.
  • docker logs <container> -n 10: Gets the last 10 logs from a container.

Managing Images

Working with Images

  • docker images: Lists all stored images.
  • docker rmi <image>: Deletes an image by its ID or tag.
  • docker tag <image> example-image:latest: Adds a new tag to an existing image.

Pulling and Pushing Images

Interacting with Registries

  • docker push example.com/user/image:latest: Pushes an image to a remote registry.
  • docker pull example.com/user/image:latest: Pulls an image from a remote registry.

Managing Networks

Configuring Networks

  • docker create network my-network: Creates a new network.
  • docker network connect <network> <container>: Connects a container to a network.
  • docker network disconnect <network> <container>: Removes a container from a network.
  • docker network ls: Lists all Docker networks.
  • docker network rm <network>: Deletes a network.

Managing Volumes

Handling Storage

  • docker volume create my-volume: Creates a new volume.
  • docker volume ls: Lists volumes on the host.
  • docker volume rm: Deletes a volume.

Using Configuration Contexts

Managing Docker Daemons

  • docker context create my-context --host=tcp://host:2376: Creates a new context.
  • docker context update <context>: Modifies a context’s configuration.
  • docker context ls: Lists available contexts.
  • docker context use <context>: Switches to a named context.
  • docker context rm <context>: Deletes a context.

Creating SBOMs

Generating Software Bill of Materials

  • docker sbom example-image:latest: Generates an SBOM for an image.
  • docker sbom example-image:latest --output sbom.txt: Saves an SBOM to a file.
  • docker sbom example-image:latest --format spdx-json: Generates an SBOM in a standard format.

Scanning for Vulnerabilities

Security Scanning

  • docker scan example-image:latest: Scans for vulnerabilities in an image.
  • docker scan example-image:latest --file Dockerfile: Provides detailed vulnerability information when the Dockerfile is available.
  • docker scan example-image:latest --severity high: Reports only high severity vulnerabilities.

Volume Management

Inspecting Volumes

  • docker volume inspect <volume>: Displays detailed information about a volume, including its mount point and configuration.

Docker Compose

Running Docker Compose

  • docker-compose up: Starts services defined in a Docker Compose file.
  • docker-compose down: Stops and removes containers, networks, and volumes defined in a Docker Compose file.

Docker Swarm

Initializing Docker Swarm

  • docker swarm init: Initializes a Docker Swarm on the current node.

Joining Nodes to Swarm

  • docker swarm join: Adds a node to an existing Docker Swarm as either a manager or worker.

Deploying Services in Swarm

  • docker service create: Creates a new service in a Docker Swarm.

Scaling Services

  • docker service scale <service>=<replicas>: Scales the number of replicas for a service in a Docker Swarm.

Listing Services

  • docker service ls: Lists all services in a Docker Swarm.

Inspecting Services

  • docker service inspect <service>: Displays detailed information about a service in a Docker Swarm.

Docker Network Operations

Inspecting Networks

  • docker network inspect <network>: Shows detailed information about a Docker network, including its configuration and connected containers.

Pruning Networks

  • docker network prune: Removes unused networks.

Managing Docker Contexts

Setting Default Context

  • docker context use default: Sets the default context for Docker commands.

Docker Hub Account

Interacting with Docker Hub

  • docker login: Logs in to your Docker Hub account.
  • docker logout: Logs out of your Docker Hub account.
  • docker search nginx: Searches Docker Hub for images.

Cleaning Up Unused Resources

Resource Management

  • docker system prune: Removes unused data.
  • docker system prune -a: Deletes all unused images.
  • docker system prune --volumes: Includes volume data in the prune process.
  • docker image prune, docker network prune, docker volume prune: Prunes unused images, networks, and volumes respectively.
  • docker system df: Reports total disk usage.

Docker Machine

Managing Docker Hosts

  • docker-machine create: Creates a new Docker host using Docker Machine.
  • docker-machine ls: Lists all Docker hosts created with Docker Machine.
  • docker-machine ssh <machine>: SSH into a Docker Machine.

Docker BuildKit

Using BuildKit

  • DOCKER_BUILDKIT=1 docker build .: Enables Docker BuildKit for building Docker images, providing advanced features like caching and parallel execution.

By mastering these Docker commands and best practices, you can streamline your containerization workflow, optimize resource utilization, and enhance the security of your Dockerized applications. Experiment with these commands in your development environment to become proficient in Docker management and deployment.

Top comments (0)