Top 15 Docker Commands You Should Know

Become a Docker expert with the top 15 Docker commands you should know. Build, run, manage, and share containers efficiently with this comprehensive guide. This comprehensive guide covers everything from basic commands to more advanced topics like networking and debugging.

“Everything You Need To Know About Docker” is a series of articles (so far 6) that explains different parts of Docker** **in a very simple and straightforward way. Here are the parts so far:

We’ll look at 15 Docker CLI commands you should know

There are about a billion Docker commands (give or take a billion). The Docker docs are extensive, but overwhelming when you’re just getting started. In this article I’ll highlight the key commands for running vanilla Docker.

At risk of taking the food metaphor thread running through these articles too far, let’s use a fruit theme. Veggies provided sustenance in the article on slimming down our images. Now tasty fruits will give us nutrients as we learn our key Docker commands.

Overview

Recall that a Docker image is made of a **Dockerfile **+ any necessary dependencies. Also recall that a Docker container is a Docker image brought to life. To work with Docker commands, you first need to know whether you’re dealing with an image or a container.

  • A Docker image either exists or it doesn’t.
  • A Docker container either exists or it doesn’t.
  • A Docker container that exists is either running or it isn’t.

Once you know what you’re working with you can find the right command for the job.

Commmand Commonalities

Here are a few things to know about Docker commands:

  • Docker CLI management commands start with docker, then a space, then the management category, then a space, and then the command. For example, docker stops a container.
  • A command referring to a specific container or image requires the name or id of that container or image.

For example, docker is the command to build and run the container named my_app. I’ll use the name docker to refer to a generic container throughout the examples. Same goes for docker, docker, etc.

I’ll provide the command alone and then with common flags, if applicable. A flag with two dashes in front is the full name of the flag. A flag with one dash is a shortcut for the full flag name. For example, docker is short for the docker flag.

Flags provide options to commands

The goal is to help these commands and flags stick in your memory and for this guide to serve as a reference. This guide is current for Linux and Docker Engine Version 18.09.1 and API version 1.39.

First, we’ll look at commands for containers and then we’ll look at commands for images. Volumes will be covered in the next article. Here’s the list of 15 commands to know — plus 3 bonus commands!

Containers

Use docker

docker — Create a container from an image.

docker— Start an existing container.

docker — Create a new container and start it.

docker — List running* *containers.

docker — See lots of info about a container.

docker — Print logs.

docker — Gracefully stop running container.

docker —Stop main process in container abruptly.

docker— Delete a stopped container.

Images

Use docker

docker— Build an image.

docker — Push an image to a remote registry.

docker — List images.

docker — See intermediate image info.

docker — See lots of info about an image, including the layers.

docker — Delete an image.

Misc

docker — List info about your Docker Client and Server versions.

docker— Log in to a Docker registry.

docker — Delete all unused containers, unused networks, and dangling images.

Containers

Container Beginnings

The terms create, start, and run all have similar semantics in everyday life. But each is a separate Docker command that creates and/or starts a container. Let’s look at creating a container first.

docker — Create a container from an image.

I’ll shorten docker** **to docker for the rest of the article.

There are a lot of possible flags you could pass to docker.

docker

docker is short for docker. Attach the container to STDIN, STDOUT or STDERR.

Now that we’ve created a container let’s start it.

docker — Start an existing container.

Note that the container can be referred to by either the container’s ID or the container’s name.

docker

Now that you know how to create and start a container, let’s turn to what’s probably the most common Docker command. It combines both docker and docker into one command: docker.

docker** **Create a new container and start it. It also has a lot of options. Let’s look at a few.

docker

docker is short for docker. Keep STDIN open even if unattached.

dockeris short fordocker. Allocates a pseudo terminal that connects your terminal with the container’s STDIN and STDOUT.

You need to specify both docker and docker to then interact with the container through your terminal shell.

docker is short for docker. The port is the interface with the outside world.docker maps the Docker port 8000 to port 1000 on your machine. If you had an app that output something to the browser you could then navigate your browser to docker and see it.

docker Automatically delete the container when it stops running.

Let’s look at some more examples of docker.

docker

docker is a command you could specify at run time.docker will start a shell session inside your container that you can interact with through your terminal. docker is preferable to docker for Alpine images because Alpine images don’t come with docker installed. Type docker to end the interactive shell session.

Notice that we combined docker and docker into docker.

docker

docker is short for docker. Run the container in the background. Allows you to use the terminal for other commands while your container runs.

Checking Container Status

If you have running **Docker containers **and want to find out which one to interact with, then you need to list them.

docker container ls — List running* *containers. Also provides useful information about the containers.

docker container ls -a -s

docker is short for docker. List all containers (not just running ones).

docker is short for docker. List the size for each container.

docker — See lots of info about a container.

docker — Print a container’s logs.

Logs. Not sure how virtual logs are related. Maybe via reams of paper?

Container Endings

Sometimes you need to stop a running container.

docker — Stop one or more running containers gracefully. Gives a default of 10 seconds before container shutdown to finish any processes.

Or if you are impatient:

docker — Stop one or more running containers abruptly. It’s like pulling the plug on the TV. Prefer docker in most situations.

docker — Kill all running containers.

docker kill cockroach

Then you delete the container with:

docker— Delete one or more containers.

docker — Delete all containers that are not running.

Those are the eight essential commands for Docker containers.

To recap, you first create a container. Then, you start the container. Or combine those steps with docker. Then, your app runs. Yippee!

Then, you stop a container with docker. Eventually you delete the container with docker.

Now, let’s turn to the magical container-producing molds called images.

Docker Images

Here are seven commands for working with Docker images.

Developing Images

docker** .** — Build a **Docker image **named my_image from the **Dockerfile **located at the specified path or URL.

docker is short for tag. Tells docker to tag the image with the provided tag. In this case my_tag .

The docker (period) at the end of the command tells **Docker **to build the image according to the **Dockerfile **in the current working directory.

Build it

Once you have an image built you want to push it to a remote registry so it can be shared and pulled down as needed. Assuming you want to use Docker Hub, go there in your browser and create an account. It’s free. 😄

This next command isn’t an image command, but it’s useful to see here, so I’ll mention it.

docker — Log in to a Docker registry. Enter your username and password when prompted.

Push

docker — Push an image to a registry.

Once you have some images you might want to inspect them.

Inspecting Images

Inspection time

docker — List your images. Shows you the size of each image, too.

docker — Display an image’s intermediate images with sizes and how they were created.

docker — Show lots of details about your image, including the layers that make up the image.

Sometimes you’ll need to clean up your images.

Removing Images

docker — Delete the specified image. If the image is stored in a remote repository, the image will still be available there.

docker — Delete all images. Careful with this one! Note that images that have been pushed to a remote registry will be preserved — that’s one of the benefits of registries. 😃

Now you know most essential Docker image-related commands. We’ll cover data-related commands in the next article.

Commands are like fruit — nutritious and delicious. Err. Yeah.

Misc

docker — List info about your **Docker Client **and Server versions.

docker— Log in to a Docker registry. Enter your username and password when prompted.

docker makes an appearance in the next article.

docker —Delete all unused containers, unused networks, and dangling images.

docker

docker is short for docker. Delete unused images, not just dangling ones.

docker Remove unused volumes. We’ll talk more about volumes in the next article.

Wrap

If you are just getting started with Docker, these are the three most important commands:

docker — Create a new container and start it. You’ll probably want some flags here.

docker — Build an image.

docker — Push an image to a remote registry.

Here’s the larger list of essential Docker commands:

Docker Containers

Use docker

docker — Create a container from an image.

docker— Start an existing container.

docker — Create a new container and start it.

docker — List running* *containers.

docker — See lots of info about a container.

docker — Print logs.

docker — Gracefully stop running container.

docker —Stop main process in container abruptly.

docker— Delete a stopped container.

Images

Use docker

docker— Build an image.

docker — Push an image to a remote registry.

docker — List images.

docker — See intermediate image info.

docker — See lots of info about an image, including the layers.

docker — Delete an image.

Misc

docker — List info about your **Docker Client **and Server versions.

docker— Log in to a Docker registry.

docker — Delete all unused containers, unused networks, and dangling images.

To view the CLI reference when using Docker just enter the command docker in the command line. You can see the Docker docs here.

#docker #devops

Top 15 Docker Commands You Should Know
10.85 GEEK