Docker Basic Commands on Linux

Docker is an essential tool for developers and system administrators working with containerized applications. This guide provides a comprehensive overview of Docker’s basic commands on Linux, covering everything from creating and managing containers to troubleshooting common issues.

In This Tutorial, You Will Learn:

  • How to use essential Docker commands
  • How to troubleshoot common Docker issues
  • Best practices for managing Docker containers
Docker Basic Commands on Linux
Docker Basic Commands on Linux

Software Requirements and Linux Command Line Conventions

Software Requirements and Linux Command Line Conventions
Category Requirements, Conventions, or Software Version Used
System Linux (Ubuntu, Debian, CentOS, or any distribution supporting Docker)
Software Docker Engine and Docker CLI
Other Internet access to pull images from Docker Hub
Conventions # – Requires commands to be executed with root privileges, either directly as root or using sudo.
$ – Requires commands to be executed as a regular non-privileged user.

Docker Basic Commands on Linux

INTRODUCTION TO DOCKER CLI

Docker provides a powerful command-line interface to manage containers efficiently. With just a few simple commands, you can build, run, and manage lightweight, portable applications.

💡 Did you know? Docker was inspired by shipping containers! Just like standardized containers revolutionized global trade by making transportation more efficient, Docker containers help developers package applications with all their dependencies, ensuring they run seamlessly across different environments.

This tutorial covers essential Docker commands that allow you to interact with containers, manage images, and troubleshoot issues effectively.

Example-by-Example Instructions

  1. Listing Running Containers: Use this command to view active containers.
    $ docker ps

    The docker ps command lists currently running containers along with their details. Visit the following page for a more complex docker ps sorting Bash functions.

  2. Starting a New Container: Run a basic container using an official image.
    $ docker run -d --name mycontainer nginx

    This command starts a detached Nginx container named mycontainer.

    Starting a New Container
    Starting a New Container
  3. Stopping and Removing a Container: Use the following commands to stop and remove a container.
    $ docker stop mycontainer && docker rm mycontainer

    This sequence stops and removes the specified container.

    Stopping and Removing a Container
    Stopping and Removing a Container
  4. Checking Docker Status: Once you start the Docker daemon you can use the following commands to verify whether Docker is running.
    $ systemctl status docker

    Use this command to check if the Docker service is active and running.

    Checking Docker Status
    Checking Docker Status
  5. Pulling an Image from Docker Hub: Download an image from the repository.
    $ docker pull ubuntu

    The above docker pull command fetches the latest Ubuntu image from Docker Hub.

    Pulling an Image from Docker Hub
    Pulling an Image from Docker Hub
  6. Viewing Container Logs: Check the logs for a specific container.
    $ docker logs mycontainer

    This retrieves the log output from the container named mycontainer.

    Viewing Container Logs
    Viewing Container Logs
  7. Building an Image from a Dockerfile: Follow these steps to create, build, and run a simple Docker container that prints “LinuxConfig.org!”.
    1. Create a Dockerfile: Create a file named Dockerfile with the following content:

    # Use BusyBox as the base image
    FROM busybox
    
    # Set the default command to print "LinuxConfig.org!"
    CMD echo "LinuxConfig.org!"
    

    2. Build the Docker Image: Run the following command to build an image named busybox-hello from the current directory:

    $ docker build -t busybox-hello .

    3. Run the Container: Execute the built image to print “LinuxConfig.org!”.

    $ docker run --rm busybox-hello

    This command runs the container and removes it after execution.

    Building an Image from a Dockerfile
    Building an Image from a Dockerfile
  8. Connecting to a Running Container: Use the exec command to access a running container’s shell.
    $ docker exec -it mycontainer /bin/bash

    This opens an interactive terminal session within the running container mycontainer.

    Connecting to a Running Container
    Connecting to a Running Container

DOCKER COMMAND REFERENCE

The Docker CLI offers a vast set of commands to build, manage, and run containers efficiently. Whether you’re starting a container, inspecting logs, or optimizing images, the right command can save you time and effort.🔍 Pro Tip: You can use docker --help to quickly explore available commands without leaving your terminal.For an extensive list of commands, refer to the official
Docker documentation.

Conclusion

Understanding and mastering Docker commands is crucial for efficient container management. This guide provides a starting point, but continuous practice and troubleshooting are key to becoming proficient.

Frequently Asked Questions (FAQ)

  1. How do I check if Docker is installed?

    Run docker --version to check if Docker is installed on your system.

  2. Why is my Docker container not starting?

    Check logs using docker logs container_name to diagnose the issue.

  3. How do I restart the Docker daemon?

    Use sudo systemctl restart docker to restart the Docker service.

  4. How do I pull an image from Docker Hub?

    Run docker pull image_name to download an image.

  5. What is the best way to sort docker ps output?

    Use options like docker ps --sort to format the output based on your needs.

 



Comments and Discussions
Linux Forum