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.
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
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.
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.
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.
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)
How do I check if Docker is installed?
Run docker --version to check if Docker is installed on your system.
Why is my Docker container not starting?
Check logs using docker logs container_name to diagnose the issue.
How do I restart the Docker daemon?
Use sudo systemctl restart docker to restart the Docker service.
How do I pull an image from Docker Hub?
Run docker pull image_name to download an image.
What is the best way to sort docker ps output?
Use options like docker ps --sort to format the output based on your needs.