Docker 101 - These 10 Docker Commands Will Change Your Life

Docker 101: These 10 Docker Commands Will Change Your Life

Docker is a powerful tool that can help you to containerize your applications, making them portable and easy to deploy. However, if you’re new to Docker, it can be difficult to know where to start. That’s why I’ve put together this list of the 10 most essential Docker commands. These 10 Docker Commands Will Change Your Life and will give you the foundation you need to start using Docker to build and deploy your applications.

I’ve included a brief description of each command, as well as some examples of how you can use it. By the end of this post, you’ll be well on your way to mastering the basics of Docker. So what are you waiting for? Let’s get started!

These 10 Docker Commands Will Change Your Life:

  • docker version
  • docker search
  • docker pull
  • docker run
  • docker ps
  • docker stop
  • docker restart
  • docker kill
  • docker commit
  • docker push

docker version


The docker version command is used to show the version of Docker that is installed on your machine. It also shows the version of the Docker Engine and the Docker client.

The syntax for the docker version command is as follows:

docker version

For example, to show the version of Docker that is installed on your machine, you would run the following command:

docker version

This would output the following information (which may vary a bit from machine to machine):

Docker Version Command Output

docker search

Docker search is a command-line tool that allows you to search for Docker images on Docker Hub. You can use it to find images by name, description, or other criteria. The syntax for docker search is:

docker search <search_term>

For example, to search for images that contain the word “nginx”, you would use the following command:

docker search nginx
Docker Search Command Output

The docker search command will return a list of images that match your search criteria. The output of the command includes the following information for each image:

  • Name
  • Description
  • Stars (number of users who have starred the image)
  • Official (whether the image is an official Docker image)
  • Automated (whether the image was built using an automated build process)

You can use the docker search command to filter the results by using the following flags:

  • –filter=stars=<number>: This flag will only show images that have at least the specified number of stars.
  • –filter=is-automated=true: This flag will only show images that were built using an automated build process.
  • –filter=is-official=true: This flag will only show official Docker images.

You can also use the –format flag to specify the format of the output. For example, the following command will output the results of the search in a table format:

docker search --format "{{.Name}} | {{.Description}} | {{.Stars}}"

docker pull

The docker pull command downloads a Docker image from a registry. The syntax for docker pull is:

docker pull <image_name>[:<tag>]

For example, to pull the latest version of the nginx image, you would use the following command:

docker pull nginx
Docker Pull Command Output

You can also specify a tag to pull a specific version of the image. For example, to pull the 1.17.4 version of the nginx image, you would use the following command:

docker pull nginx:1.17.4
Docker Pull Command with tag Output

The docker pull command will download the image to your local machine. Once the image is downloaded, you can use it to create a container.

Here are some additional options that you can use with the docker pull command:

  • --all-tags: This option will download all tags for the specified image.
  • --disable-content-trust: This option will disable image verification.
  • --platform: This option will specify the platform for the image.
  • --quiet: This option will suppress verbose output.

Here are some examples of how to use the docker pull command:

# Pull the latest version of the nginx image
docker pull nginx

# Pull the 1.17.4 version of the nginx image
docker pull nginx:1.17.4

# Pull all tags for the nginx image
docker pull nginx --all-tags

# Disable image verification
docker pull nginx --disable-content-trust

# Specify the platform for the image
docker pull nginx --platform linux/amd64

# Suppress verbose output
docker pull nginx --quiet

docker run

The docker run command runs a Docker image as a container. The syntax for docker run is:

docker run [OPTIONS] IMAGE [COMMAND] [ARG...]

For example, to run the nginx image as a container, you would use the following command:

docker run nginx
Docker Run Command Output
Docker Nginx Localhost Output

This command will start a new container running the nginx image. The container will listen on port 80 by default. You can access the nginx web server by visiting http://localhost in your web browser.

You can also specify a command to run when the container starts. For example, to run the nginx image and serve a static website, you would use the following command:

docker run -p 80:80 nginx sh -c "echo 'Hello, world!' > /usr/share/nginx/html/index.html"

This command will start a new container running the nginx image. The container will listen on port 80 and serve the file index.html from the /usr/share/nginx/html directory.

Here are some additional options that you can use with the docker run command:

  • --name: This option will specify a name for the container.
  • --detach: This option will run the container in the background.
  • --publish: This option will publish a port from the container to the host machine.
  • --env: This option will set an environment variable in the container.
  • --volume: This option will mount a volume from the host machine to the container.

Here are some examples of how to use the docker run command:

# Run the nginx image
docker run nginx

# Run the nginx image and serve a static website
docker run -p 80:80 nginx sh -c "echo 'Hello, world!' > /usr/share/nginx/html/index.html"

# Run the nginx image in the background
docker run -d nginx

# Run the nginx image and name it "my-nginx"
docker run --name my-nginx nginx

# Publish port 80 from the container to the host machine
docker run -p 80:80 nginx

# Set the environment variable `FOO=bar` in the container
docker run --env FOO=bar nginx

# Mount the directory `/data` from the host machine to the container
docker run -v /data:/data nginx

docker ps

The docker ps command lists all the running containers on your Docker daemon. The syntax for docker ps is:

docker ps [OPTIONS]
Docker ps command output

The docker ps command will list all the running containers, including their names, IDs, image names, ports, and status. You can use the docker ps command to see a list of all the containers that you have running on your Docker daemon.

Here are some additional options that you can use with the docker ps command:

  • --all: This option will list all containers, including stopped containers.
  • --filter: This option will filter the list of containers based on a criteria. For example, to list all containers that are running the nginx image, you would use the following command:
docker ps --filter=image=nginx

--format: This option will format the output of the docker ps command. For example, to output the list of containers in a table format, you would use the following command:

docker ps --format "{{.Names}} | {{.Image}} | {{.Status}}"

Here are some examples of how to use the docker ps command:

# List all running containers
docker ps

# List all containers, including stopped containers
docker ps --all

# List all containers that are running the nginx image
docker ps --filter=image=nginx

# Output the list of containers in a table format
docker ps --format "{{.Names}} | {{.Image}} | {{.Status}}"

docker stop

The docker stop command stops a running container. The syntax for docker stop is:

docker stop [OPTIONS] CONTAINER [CONTAINER...]

For example, to stop the container with the ID 1234567890, you would use the following command:

docker stop 1234567890

You can also specify a name to stop a container. For example, to stop the container named my-nginx, you would use the following command:

docker stop my-nginx
Docker Stop Command Output

The docker stop command will send a SIGTERM signal to the container, which will give the container a chance to gracefully shut down. If the container does not shut down gracefully within a grace period, the docker stop command will send a SIGKILL signal to the container, which will forcefully stop the container.

Docker Stop Command SIGTERM Logs

Here are some additional options that you can use with the docker stop command:

  • --time: This option specifies the grace period in seconds. The default grace period is 10 seconds.
  • --force: This option forcefully stops the container without waiting for it to shut down gracefully.

Here are some examples of how to use the docker stop command:

# Stop the container with the ID `1234567890`
docker stop 1234567890

# Stop the container named `my-nginx`
docker stop my-nginx

# Stop the container with the ID `1234567890` with a grace period of 30 seconds
docker stop 1234567890 --time 30

# Forcefully stop the container with the ID `1234567890`
docker stop 1234567890 --force

docker restart

The docker restart command restarts a running container. The syntax for docker restart is:

docker restart [OPTIONS] CONTAINER [CONTAINER...]

For example, to restart the container with the ID 1234567890, you would use the following command:

docker restart 1234567890

You can also specify a name to restart a container. For example, to restart the container named my-nginx, you would use the following command:

docker restart my-nginx

The docker restart command will send a SIGHUP signal to the container, which will cause the container to reload its configuration and restart.

Docker Restart Command Output

Here are some additional options that you can use with the docker restart command:

  • --time: This option specifies the grace period in seconds. The default grace period is 10 seconds.
  • --force: This option forcefully restarts the container without waiting for it to shut down gracefully.

Here are some examples of how to use the docker restart command:

# Restart the container with the ID `1234567890`
docker restart 1234567890

# Restart the container named `my-nginx`
docker restart my-nginx

# Restart the container with the ID `1234567890` with a grace period of 30 seconds
docker restart 1234567890 --time 30

# Forcefully restart the container with the ID `1234567890`
docker restart 1234567890 --force

docker kill

The docker kill command sends a signal to a container to force it to stop. The syntax for docker kill is:

docker kill [OPTIONS] CONTAINER [CONTAINER...]

For example, to kill the container with the ID 1234567890, you would use the following command:

docker kill 1234567890

You can also specify a name to kill a container. For example, to kill the container named my-nginx, you would use the following command:

docker kill my-nginx

The docker kill command sends the SIGKILL signal to the container, which will forcefully stop the container. The SIGKILL signal cannot be ignored by the container, so it will always be effective.

Docker Kill Command Output

Here are some additional options that you can use with the docker kill command:

  • --signal: This option specifies the signal to send to the container. The default signal is SIGKILL.
  • --time: This option specifies the grace period in seconds. The default grace period is 0 seconds, which means that the container will be killed immediately.

Here are some examples of how to use the docker kill command:

# Kill the container with the ID `1234567890`
docker kill 1234567890

# Kill the container named `my-nginx`
docker kill my-nginx

# Kill the container with the ID `1234567890` with a grace period of 30 seconds
docker kill 1234567890 --time 30

# Send the SIGTERM signal to the container with the ID `1234567890`
docker kill 1234567890 --signal SIGTERM

docker commit

The docker commit command creates a new image from a running container. The syntax for docker commit is:

docker commit [OPTIONS] CONTAINER [REPOSITORY[:TAG]]

For example, to create a new image from a container named my-nginx, you would use the following command:

docker commit my-nginx my-nginx:latest

The docker commit command will create a new image that includes the changes that have been made to the container’s file system, environment variables, and configuration. The new image will be tagged with the repository and tag that you specified.

Docker Commit Command Output
Newly Created Image from the Docker Container

Here are some additional options that you can use with the docker commit command:

  • --author: This option specifies the author of the new image.
  • --message: This option specifies a message describing the changes that were made to the container.
  • --pause: This option pauses the container before the commit is made. This can be useful if you want to make sure that the container is in a consistent state before the commit is made.

Here are some examples of how to use the docker commit command:

# Create a new image from a container named `my-nginx` and tag it with `my-nginx:latest`
docker commit my-nginx my-nginx:latest

# Create a new image from a container named `my-nginx` and specify the author and message
docker commit -a "John Doe" -m "Added a new file" my-nginx my-nginx:latest

# Pause the container before the commit is made
docker commit --pause my-nginx my-nginx:latest

docker push

The docker push command pushes an image to a registry. The syntax for docker push is:

docker push [OPTIONS] IMAGE[:TAG]

For example, to push the image named my-nginx to the Docker Hub registry, you would use the following command:

docker push my-nginx
Docker Push Command Output

The docker push command will push the image to the registry that is specified by the docker daemon. If you are not logged in to a registry, the docker push command will prompt you to login.

Here are some additional options that you can use with the docker push command:

  • --force: This option forces the image to be pushed even if it has not been modified since the last push.
  • --tag: This option specifies the tag that you want to push the image with.
  • --digest: This option specifies the digest that you want to push the image with.

Here are some examples of how to use the docker push command:

# Push the image named `my-nginx` to the Docker Hub registry
docker push my-nginx

# Push the image named `my-nginx` with the tag `latest`
docker push my-nginx:latest

# Force the image to be pushed even if it has not been modified since the last push
docker push --force my-nginx

# Push the image named `my-nginx` with the digest `sha256:1234567890abcdef`
docker push --digest sha256:1234567890abcdef my-nginx

Verdict

Docker is a powerful tool that can help you build, run, and deploy applications. The 10 Docker commands in this article are essential for anyone who wants to get started with Docker. These commands will allow you to search for images, pull images, run containers, manage containers, and more.

Once you know these 10 commands, you’ll be well on your way to using Docker to build and deploy your applications. So what are you waiting for? Start learning these commands today!

Here are the 10 Docker commands that are covered in the article:

  • docker version
  • docker search
  • docker pull
  • docker run
  • docker ps
  • docker stop
  • docker restart
  • docker kill
  • docker commit
  • docker push

If you want to learn more about Docker, I recommend checking out the following resources:

Leave a Reply

Your email address will not be published. Required fields are marked *