Docker Commands - Part 1

meghasharmaaaa

Megha Sharma

Posted on November 14, 2024

Docker Commands - Part 1

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 --version
Enter fullscreen mode Exit fullscreen mode

๐Ÿ‘‰ 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 info
Enter fullscreen mode Exit fullscreen mode

๐Ÿ‘‰ docker pull [image name]: Download a Docker image from a registry (e.g., Docker Hub).

docker pull [OPTIONS] IMAGE_NAME[:TAG]
Enter fullscreen mode Exit fullscreen mode

[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:

docker pull nginx
Enter fullscreen mode Exit fullscreen mode

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:

docker pull nginx:1.21
Enter fullscreen mode Exit fullscreen mode

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.

docker images
Enter fullscreen mode Exit fullscreen mode

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:

REPOSITORY        TAG             AGE ID          CREATED          SIZE
ubuntu           latest        775349758637      2 weeks ago      64.2MB
nginx            latest        4cd66d967c32      3 weeks ago      133MB
alpine           3.14          a24bb4013296      4 weeks ago      6.64MB
Enter fullscreen mode Exit fullscreen mode

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.

docker ps
Enter fullscreen mode Exit fullscreen mode

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:

CONTAINER ID     IMAGE             COMMAND               CREATED         STATUS       PORTS      NAMES
21a45c4e1c1a   nginx:latest   "nginx -g 'daemon ofโ€ฆ"   2 minutes ago   Up 2 minutes   80/tcp     web_server
d482e72545ef   mysql:latest   "docker-entrypoint.sโ€ฆ"   5 minutes ago   Up 5 minutes   3306/tcp   mysql_db
Enter fullscreen mode Exit fullscreen mode

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.

docker ps -a
Enter fullscreen mode Exit fullscreen mode

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:

CONTAINER ID     IMAGE              COMMAND               CREATED         STATUS                   PORTS      NAMES
21a45c4e1c1a   nginx:latest   "nginx -g 'daemon ofโ€ฆ"   2 minutes ago   Up 2 minutes                80/tcp     web_server
d482e72545ef   mysql:latest   "docker-entrypoint.sโ€ฆ"   5 minutes ago   Exited (0) 3 minutes ago               mysql_db
Enter fullscreen mode Exit fullscreen mode

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.

docker run [OPTIONS] IMAGE [COMMAND] [ARG...]
Enter fullscreen mode Exit fullscreen mode

[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:

docker run -it ubuntu /bin/bash
Enter fullscreen mode Exit fullscreen mode

Here:

  • -itspecifies 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.

docker exec [OPTIONS] CONTAINER COMMAND [ARG...]
Enter fullscreen mode Exit fullscreen mode
  • [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:

docker exec my_container ls
Enter fullscreen mode Exit fullscreen mode

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):

docker exec -it my_container /bin/bash
Enter fullscreen mode Exit fullscreen mode

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.

docker stop [OPTIONS] CONTAINER [CONTAINER...]
Enter fullscreen mode Exit fullscreen mode
  • [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:

docker stop my_container
Enter fullscreen mode Exit fullscreen mode

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:

docker stop container1 container2 container3
Enter fullscreen mode Exit fullscreen mode

This will stop all the specified containers. If you want to stop all running containers at once, you can use the following command:

docker stop $(docker ps -q)
Enter fullscreen mode Exit fullscreen mode

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.

docker start [container name]
Enter fullscreen mode Exit fullscreen mode

Replace [container]with either the container's ID or its name. For example:

docker start my_container
Enter fullscreen mode Exit fullscreen mode

or

docker start abc123def456
Enter fullscreen mode Exit fullscreen mode

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.

docker rm [OPTIONS] CONTAINER [CONTAINER...]
Enter fullscreen mode Exit fullscreen mode
  • [OPTIONS]: Common options include -f to force removal of the container even if it's running, -vto 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:

docker rm my_container
Enter fullscreen mode Exit fullscreen mode

If you have multiple containers you want to remove, you can specify their names or IDs separated by spaces:

docker rm container1 container2 container3
Enter fullscreen mode Exit fullscreen mode

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:

docker rm $(docker ps -a -q)
Enter fullscreen mode Exit fullscreen mode

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.

docker rmi [OPTIONS] IMAGE [IMAGE...]
Enter fullscreen mode Exit fullscreen mode
  • [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:

docker rmi my_image
Enter fullscreen mode Exit fullscreen mode

If you have multiple images you want to remove, you can specify their names or IDs separated by spaces:

docker rmi image1 image2 image3
Enter fullscreen mode Exit fullscreen mode

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:

docker image prune
Enter fullscreen mode Exit fullscreen mode

This command will remove all dangling images from your system, freeing up disk space.

๐Ÿ’– ๐Ÿ’ช ๐Ÿ™… ๐Ÿšฉ
meghasharmaaaa
Megha Sharma

Posted on November 14, 2024

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

ยฉ TheLazy.dev

About