Basic Commands for Docker

Docker is a popular platform for creating, deploying, and running applications inside containers. Containers are lightweight and efficient, allowing you to run multiple applications on a single host without the need for a dedicated virtual machine. In this post, we’ll cover some of the basic commands for working with Docker.

1. docker run

The docker run command is one of the most commonly used commands in Docker. It is used to create and run a new Docker container based on a Docker image. The basic syntax for the docker run command is as follows:docker run [OPTIONS] IMAGE [COMMAND] [ARG…]

Here, OPTIONS are any additional options that you want to pass to the Docker engine while creating and running the container. The IMAGE argument specifies the Docker image that you want to use to create the container. The COMMAND and ARG arguments are optional and can be used to specify a command to run inside the container.

Let’s look at some common options that you can pass to the docker run command:

  • -d: This option runs the container in detached mode. This means that the container runs in the background, and you can continue to use the terminal to execute other commands.
  • -p: This option maps a container port to a port on the host. For example, -p 8080:80 maps port 80 inside the container to port 8080 on the host.
  • -v: This option mounts a volume from the host inside the container. For example, -v /path/on/host:/path/in/container mounts the directory /path/on/host from the host machine inside the container at /path/in/container.
  • –name: This option sets a custom name for the container. By default, Docker assigns a random name to the container.
  • -e: This option sets environment variables inside the container. For example, -e MYSQL_ROOT_PASSWORD=password sets the MYSQL_ROOT_PASSWORD environment variable inside the container to password.

Here’s an example docker run command:docker run -d -p 8080:80 –name mywebapp nginx:latest

This command creates and runs a new Docker container based on the nginx:latest image. The container runs in detached mode (-d) and maps port 80 inside the container to port 8080 on the host (-p 8080:80). The container is also given a custom name (–name mywebapp).

2. docker pull

The docker pull command is used to download a Docker image from a registry. A Docker image is a lightweight, stand-alone, executable package that includes everything needed to run an application, including the code, a runtime, libraries, and system tools. Docker images are stored in repositories, which can be public or private, and can be accessed from the Docker Hub or other registries.

The basic syntax for thedocker pull command is as follows:docker pull [OPTIONS] NAME[:TAG|@DIGEST]

Here, NAME is the name of the image that you want to download, and TAG or DIGEST is an optional version or hash that you want to download.

Here are some common options that you can pass to the docker pull command:

  • -a or –all-tags: This option downloads all available tags for the specified image.
  • –disable-content-trust: This option disables content trust verification for the downloaded image.
  • –platform: This option specifies the platform architecture of the image to download.

Here’s an example docker pull command:docker pull nginx:latest

This command downloads the latest version of the nginx image from the Docker Hub registry. If you want to download a specific version of the image, you can specify the version using the TAG argument:docker pull nginx:1.19.10

This command downloads version 1.19.10 of the nginx image. If you want to download all available versions of the image, you can use the -a option:docker pull –all-tags nginx

This command downloads all available versions of the nginx image.

3. docker ps

The docker ps command is used to list all running Docker containers on your system. It shows information about each container, such as the container ID, name, image, status, and ports.

The basic syntax for the docker ps command is as follows:docker ps [OPTIONS]

Here, OPTIONS are any additional options that you want to pass to the Docker engine while listing the containers.

Here are some common options that you can pass to the docker ps command:

  • -a or –all: This option shows all containers, including those that are not running.
  • -q or –quiet: This option shows only the container IDs, which can be useful for scripting.
  • –format: This option allows you to format the output of the docker ps command using a Go template.

Here’s an example docker ps command:docker ps

This command lists all running containers on your system. If you want to list all containers, including those that are not running, you can use the -a option:docker ps -a

This command lists all containers, whether they are running or stopped. If you want to see only the container IDs, you can use the -q option:docker ps -q

This command lists only the container IDs, which can be useful for scripting. If you want to customize the output format of the docker ps command, you can use the –format option:docker ps –format “{{.ID}}: {{.Names}} – {{.Image}}”

This command lists all running containers in a custom format that includes only the container ID, name, and image. The output might look something like this:8eb30b23dcb2: my_container – nginx:latest



4. docker images

The docker images command is used to list all Docker images that are available on your system. It shows information about each image, such as the image ID, repository, tag, and size.

The basic syntax for thedocker images command is as follows:docker images [OPTIONS] [REPOSITORY[:TAG]]

Here, OPTIONS are any additional options that you want to pass to the Docker engine while listing the images, and REPOSITORY and TAG are optional filters that you can use to narrow down the list of images.

Here are some common options that you can pass to the docker images command:

  • -a or –all: This option shows all images, including intermediate images that are used to build other images.
  • –digests: This option shows the image digest for each image, which is a unique identifier that can be used to verify the image.
  • –format: This option allows you to format the output of the docker images command using a Go template.

Here’s an example docker images command:docker images

This command lists all Docker images that are available on your system. If you want to see only a specific image, you can specify the image name using the REPOSITORY argument:docker images nginx

This command lists all images that have the nginx repository name. If you want to see only a specific version of the image, you can specify the image name and tag using the REPOSITORY:TAG syntax:docker images nginx:1.19.10

This command lists only the nginx image with the 1.19.10 tag. If you want to customize the output format of the docker images command, you can use the –format option:docker images –format “{{.Repository}}:{{.Tag}} – {{.Size}}”

This command lists all images in a custom format that includes only the repository name, tag, and size. The output might look something like this:nginx:latest – 133MB

5. docker stop

The docker stop command is used to stop a running Docker container. It sends a SIGTERM signal to the container process, giving it a chance to clean up before shutting down.

The basic syntax for the docker stop command is as follows:docker stop [OPTIONS] CONTAINER [CONTAINER…]

Here, OPTIONS are any additional options that you want to pass to the Docker engine while stopping the container, and CONTAINER is the name or ID of the container that you want to stop.

Here are some common options that you can pass to the docker stop command:

  • -t or –time: This option sets the timeout period for stopping the container, after which Docker will send a SIGKILL signal to force the container to stop.
  • -f or –force: This option forces the container to stop immediately by sending a SIGKILL signal, without waiting for the SIGTERM signal to be processed.

Here’s an example docker stop command:docker stop my_container

This command stops the Docker container with the name my_container. If you want to specify a timeout period for stopping the container, you can use the -t option:docker stop -t 30 my_container

This command stops the Docker container with the name my_container after 30 seconds, allowing it to perform any necessary cleanup tasks before shutting down. If you want to force the container to stop immediately, you can use the -f option:docker stop -f my_container

This command forces the Docker container with the name my_container to stop immediately, without waiting for the SIGTERM signal to be processed.

6. docker rm

The docker rm command is used to remove one or more Docker containers from your system. When you remove a container, Docker also removes any resources associated with that container, such as its network interfaces, volumes, and configuration settings.

The basic syntax for the docker rm command is as follows:docker rm [OPTIONS] CONTAINER [CONTAINER…]

Here, OPTIONS are any additional options that you want to pass to the Docker engine while removing the container, and CONTAINER is the name or ID of the container that you want to remove.

Here are some common options that you can pass to the docker rm command:

  • -f or –force: This option forces Docker to remove the container, even if it is running or has other resources attached to it.
  • -v or –volumes: This option also removes any volumes that are associated with the container.

Here’s an example docker rm command:docker rm my_container

This command removes the Docker container with the name my_container. If you want to force the removal of the container, even if it is running, you can use the -f option:docker rm -f my_container

This command forces the removal of the Docker container with the namemy_container, even if it is currently running. If you also want to remove any volumes that are associated with the container, you can use the -v option:docker rm -v my_container

This command removes the Docker container with the name my_container and any volumes that are associated with it.

7. docker rmi

The docker rmi command is used to remove one or more Docker images from your system. When you remove an image, Docker also removes any containers that were created from that image.

The basic syntax for the docker rmi command is as follows:docker rmi [OPTIONS] IMAGE [IMAGE…]

Here, OPTIONS are any additional options that you want to pass to the Docker engine while removing the image, and IMAGE is the name or ID of the image that you want to remove.

Here are some common options that you can pass to the docker rmi command:

  • -f or –force: This option forces Docker to remove the image, even if it is currently in use by a container.
  • -a or –all: This option removes all images from your system, not just the specified images.

Here’s an example docker rmi command:docker rmi my_image

This command removes the Docker image with the namemy_image. If you want to force the removal of the image, even if it is currently in use by a container, you can use the -f option:docker rmi -f my_image

This command forces the removal of the Docker image with the name my_image, even if it is currently in use by a container. If you want to remove all images from your system, you can use the -a option:docker rmi -a

This command removes all images from your system. However, be careful when using this command, as it will remove all images, including those that are currently in use by running containers.

These are just some of the basic commands for working with Docker. By mastering these commands, you can get started with Docker and begin running and managing your own containers.

Leave a comment