Docker Commands: A Beginner's Guide to Key Commands
Docker has become an essential tool for developers and system administrators due to its ability to package and distribute applications as lightweight containers. With its popularity, it's important to understand the key Docker commands and how they can be utilized effectively. In this article, we will cover the top essential Docker commands that every developer should know. We will provide explanations and code examples to help you grasp the concept easily.
Prerequisites
Before diving into the Docker commands, make sure you have Docker installed on your system. You can download and install Docker from their official website based on your operating system. Once installed, open a terminal or command prompt and verify the installation by running the following command:
docker version
If Docker is successfully installed, you will see the version information along with the details about the server and client components.
Now that Docker is up and running, let's explore the essential commands.
docker run
The docker run
command is used to create and start a new container based on a specific image. It pulls the image from the Docker registry if it is not already available locally, creates a new container, and then starts it.
Syntax:
docker run [options] image [command] [args]
Example:
docker run -it ubuntu:latest bash
In this example, the docker run
command creates and starts a new container using the latest version of the Ubuntu image, and runs the bash
command inside the container.
docker pull
The docker pull
command is used to download Docker images from a registry to your local machine. It is typically the first step before running a container.
Syntax:
docker pull image
Example:
docker pull nginx:latest
In this example, the docker pull
command downloads the latest version of the Nginx image from the Docker registry to the local machine.
docker ps
The docker ps
command is used to list running containers. It shows detailed information about each container, such as the container ID, image, status, ports, and names.
Syntax:
docker ps [options]
Example:
docker ps
Running this command will display a list of all running containers along with their details.
docker stop
The docker stop
command is used to stop one or more running containers. It sends a SIGTERM
signal to the main process running inside the container, allowing it to gracefully shut down.
Syntax:
docker stop [options] container
Example:
docker stop my-container
In this example, the docker stop
command stops the container with the name my-container
.
docker rm
The docker rm
command is used to remove one or more stopped containers from the local machine. It's important to note that you cannot remove running containers; you need to stop them first using the docker stop
command.
Syntax:
docker rm [options] container
Example:
docker rm my-container
In this example, the docker rm
command removes the container with the name my-container
from the local machine.
docker images
The docker images
command is used to list all the Docker images available on the local machine. It shows details such as the repository, tag, image ID, and size.
Syntax:
docker images [options]
Example:
docker images
The output of this command will display a table with information about all the images available on the local machine.
docker rmi
The docker rmi
command allows you to remove one or more Docker images from the local machine. You need to provide either the image name or image ID as an argument.
Syntax:
docker rmi [options] image
Example:
docker rmi nginx:latest
In this example, the docker rmi
command removes the Nginx image with the latest tag from the local machine.
docker exec
The docker exec
command allows you to run a command inside a running container. It is useful when you want to execute a command in a container that is already running.
Syntax:
docker exec [options] container command
Example:
docker exec -it my-container ls
In this example, the docker exec
command runs the ls
command inside the container with the name my-container
and displays the directory listing.
docker build
The docker build
command is used to build a Docker image from a specified Dockerfile. The Dockerfile contains a set of instructions to assemble the image layer by layer.
Syntax:
docker build [options] path
Example:
## Dockerfile
FROM ubuntu:latest
RUN apt-get update && apt-get install -y nginx
CMD ["nginx", "-g", "daemon off;"]
docker build -t my-nginx .
In this example, the docker build
command builds a Docker image based on the Dockerfile in the current directory. It installs Nginx and sets it up as the default command to run when a container is created from this image. The -t
flag specifies the name and tag for the image.
docker-compose up
The docker-compose up
command is used to start the containers defined in a Docker Compose file. Docker Compose is a tool for defining and running multi-container Docker applications.
Syntax:
docker-compose up [options]
Example:
# docker-compose.yml
version: '3'
services:
db:
image: mysql:latest
environment:
- MYSQL_ROOT_PASSWORD=secret
app:
build: .
ports:
- "8080:80"
depends_on:
- db
docker-compose up
In this example, the docker-compose up
command starts two containers: one for a MySQL database and another for an application built from the current directory. The depends_on
option ensures that the database container is started before the application container.
Conclusion
In this article, we covered the top essential Docker commands that every developer should know. We explored commands such as docker run
, docker pull
, docker ps
, docker stop
, docker rm
, docker images
, docker rmi
, docker exec
, docker build
, and docker-compose up
. Understanding these commands is crucial for efficiently working with Docker containers.
These are just a few of the most essential Docker commands. There are many other commands available, but these are a good starting point for learning how to use Docker.
Additional Resources: