logo
Menu
Docker Commands - Part 1

Docker Commands - Part 1

Dive into Docker Commands - Part 1: Learn essential Docker commands to manage containers, images, and more efficiently.

Published Aug 30, 2024
Docker is a popular platform for developing, shipping, and running applications in containers. Here are some basic Docker commands that you might find useful:
๐Ÿ‘‰ docker version: The docker version command provides information about the Docker client and server versions that are installed on your system. Here's how you can use it:
๐Ÿ‘‰ docker info: The docker info command provides detailed information about the Docker system, including containers, images, volumes, networks, and various configuration details. Here's how you can use it:
๐Ÿ‘‰ docker pull [image name]: Download a Docker image from a registry (e.g., Docker Hub).
  • [OPTIONS]: This is where you can specify various options to customize the behavior of the pull operation. Common options include --all-tags to pull all tags for the given image, --platform to specify the platform for which to pull the image, etc.
  • IMAGE_NAME[:TAG]: This is the name of the Docker image you want to pull from the registry. You can optionally specify a tag to pull a specific version of the image. If you omit the tag, Docker will default to pulling the latest tag.
For example, to pull the nginx image from Docker Hub, you can use:
This command will download the latest version of the nginx image from the Docker Hub registry. If you want to pull a specific version, you can specify the tag:
This will download the nginx image with version 1.21 from Docker Hub. If the specified tag does not exist, Docker will return an error indicating that the image with the given tag was not found.
๐Ÿ‘‰ docker images: List all locally available Docker images on your machine.
When you run this command, Docker will output a list of all the Docker images stored locally on your system. The output typically includes information such as the repository and tag of each image, the image ID, when the image was created, and its size.
Hereโ€™s an example of what the output might look like:
In this example, there are three Docker images listed: ubuntu, nginx, and alpine, each with their respective tags (latest, latest, and 3.14). The IMAGE ID uniquely identifies each image, and CREATED indicates when the image was created. Finally, SIZE shows the size of the image.
๐Ÿ‘‰ docker ps: The docker ps command is used to list all running containers on your system.
When you run this command without any options, Docker will output a list of all containers that are currently running.
Hereโ€™s an example of what the output might look like:
In this example, there are two running containers listed: one running an Nginx web server and another running a MySQL database server. Each container has a unique CONTAINER ID, and their status is indicated under the STATUS column.
๐Ÿ‘‰ docker ps -a: Lists all Docker containers, including stopped ones.
When you run this command, Docker will output a list of all containers on your system, including those that are currently running and those that have exited or been stopped.
Hereโ€™s an example of what the output might look like:
In this example, there are two containers listed: one running an Nginx web server and another that previously ran a MySQL database server but has since exited. Each container has a unique CONTAINER ID, and their current status is indicated under the STATUS column. If a container is running, it will show "Up"; if it has exited, it will display "Exited" along with the exit code.
๐Ÿ‘‰ docker run [options] [image] [command]: Create and start a container based on a specific image.
[OPTIONS]: This is where you can specify various options to customize the behavior of the container. Options include -d for detached mode (running the container in the background), -p to map container ports to host ports, -v to mount volumes, etc. There are many more options available, so it's essential to consult the Docker documentation for a comprehensive list.
IMAGE: This is the Docker image from which you want to create the container. You can specify the image name along with an optional tag (e.g., ubuntu:latest).
[COMMAND] [ARG...]: This part is optional. It allows you to specify a command to run within the container. If you omit this, the default command specified in the Dockerfile of the image will be executed.
For example, to run a container based on the ubuntu image and start an interactive shell session within the container, you can use:
Here:
  • -it specifies interactive mode with a terminal attached.
  • ubuntu is the Docker image.
  • /bin/bash is the command to start a Bash shell session within the container.
๐Ÿ‘‰ docker exec [options] [container] [command]: Run a command inside a running container.
  • [OPTIONS]: This is where you can specify various options to customize the behavior of the docker exec command. Common options include -i for interactive mode (to keep STDIN open even if not attached), -t for allocating a pseudo-TTY, and -u
  • CONTAINER: This is the name or ID of the container where you want to execute the command.
  • COMMAND [ARG...]: This is the command you want to execute inside the container, along with any arguments it may require.
For example, to execute a ls command inside a container named my_container, you can use:
If you want to run an interactive shell session inside the container, you can use the -it options along with the shell command (e.g., /bin/bash for Bash):
This will start an interactive Bash shell session inside the my_container container, allowing you to run commands interactively as if you were working directly within the container's environment.
๐Ÿ‘‰ docker stop [container]: Stop a running container.
  • [OPTIONS]: This is where you can specify various options to customize the behavior of the docker stop command. Common options include -t to specify a timeout before killing the container, -f to force the container to stop immediately, and -t to specify a timeout value before stopping the container.
  • CONTAINER [CONTAINER...]: This is the name or ID of the container(s) you want to stop.
For example, to stop a container named my_container, you can use:
If you have multiple containers running and you want to stop all of them, you can specify multiple container names or IDs separated by spaces:
This will stop all the specified containers. If you want to stop all running containers at once, you can use the following command:
This command retrieves the IDs of all running containers (docker ps -q) and passes them as arguments to docker stop, causing all of them to stop.
๐Ÿ‘‰ **docker start [container]:**The docker start [container] command is used to start a stopped Docker container
Replace [container] with either the container's ID or its name. For example:
or
This command will attempt to start the specified container if itโ€™s stopped. If the container is already running, it will have no effect.
๐Ÿ‘‰ docker rm [container name]: The docker rm command is used to remove one or more stop containers from your system.
  • [OPTIONS]: Common options include -f to force removal of the container even if it's running, -v to remove associated volumes as well, and -l to remove the specified link (if any).
  • CONTAINER [CONTAINER...]: This is the name or ID of the container(s) you want to remove.
For example, to remove a container named my_container, you can use:
If you have multiple containers you want to remove, you can specify their names or IDs separated by spaces:
If the container is running, you need to stop it first before you can remove it. If you want to remove all stopped containers, you can use the following command:
This command retrieves the IDs of all containers (stopped or running) using docker ps -a -q and passes them as arguments to docker rm, causing all of them to be removed.
๐Ÿ‘‰ docker rmi [image name]: The docker rmi command is used to remove one or more Docker images from your system.
  • [OPTIONS]: Common options include -f to force removal of the image(s), -q to suppress output and only display the numeric IDs of the images that were removed, and --no-prune to disable automatic deletion of parent images that are not referenced anymore.
  • IMAGE [IMAGE...]: This is the name or ID of the Docker image(s) you want to remove.
For example, to remove an image named my_image, you can use:
If you have multiple images you want to remove, you can specify their names or IDs separated by spaces:
Be cautious when using docker rmi, as removing an image cannot be undone and can lead to loss of data. If the image is being used by any containers, Docker will refuse to delete it by default. You may need to stop and remove the associated containers before you can remove the image. If you want to remove all unused images (dangling images), you can use the following command:
This command will remove all dangling images from your system, freeing up disk space.
ย 

Comments