A complete guide to the Docker command line (18 things you have to know)

A complete guide to the Docker command line (18 things you have to know)

Preface

A Docker image consists of a Dockerfile and some necessary dependencies, and a Docker container is a dynamic Docker image. To use Docker commands, you first need to know whether you are dealing with an image or a container. Once you know whether you are dealing with an image or a container, you can find the correct command.

Commonality of commands

There are a few rules you need to know about Docker commands:

  • Docker CLI management commands begin with docker, followed by a space, followed by the management category, followed by a space, and then the command. For example, the command docker container stop stops the container.
  • Commands that reference a specific container or image require the name or ID of that container or image.

For example, docker container run my_app is the command to build and run a container named my_app. In this article, I will use the name my_container to refer to the generic container. Similarly, the same goes for my_image and my_tag.

I'll provide the commands and common flags separately. The sign with two dashes in front of it is the full name of the sign. Flags with one dash are abbreviations of the full flag name. For example, -p is short for the --port flag.

The goal of this article is to get these commands and flags firmly in your head, and hopefully you can use this guide as a reference when creating containers or building images. This guide is for Linux and Docker Engine version 18.09.1 ​​and API version 1.39.

Let's first understand the container commands and then look at the image commands.

Container Commands

Using docker container my_command

  • create — Create a container from an image
  • start — Start an existing container
  • run — creates a new container and starts it
  • ls — List running containers
  • inspect — View information about a container
  • logs — print logs
  • stop — gracefully stop a running container
  • kill — Immediately stop the main process in the container
  • rm — Remove a stopped container

Mirror Command

Use docker image my_command

  • build — build an image
  • push — Push the image to a remote image repository
  • ls — List images
  • history — View intermediate image information
  • inspect — View information about an image, including its layers
  • rm — remove an image

Containers & Images

  • docker version — Lists information about the Docker client and server versions
  • docker login — Log in to the Docker image repository
  • docker system prune — removes all unused containers, networks, and unnamed images (dangling images)

Container Commands Explained

Start the container

The terms "create", "start", and "run" all have similar semantics in everyday life, but each is a separate Docker command used to create and/or start a container. Let's first look at the command to create a container.

docker container create my_repo/my_image:my_tag — Create a container from an image

I will refer to my_repo/my_image:my_tag as my_image in the following text.

You can pass a number of flags to create.

docker container create -a STDIN my_image

-a is short for --attach, which means to connect the container to STDIN, STDOUT, or STDERR.

Now that we have created a container, let's start it.

docker container start my_container — Start an existing container

Note that a container can be referenced by either the container's ID or the container's name.

docker container start my_container

Now that you know how to create and start a container, let’s look at the most common Docker commands. It combines create and start into one command: run.

docker container run my_image — creates a new container and starts it. This command also has many options. Let’s look at a few of them.

docker container run -i -t -p 1000:8000 --rm my_image

-i is short for --interactive, which keeps STDIN open even if it is not connected; -t is short for --tty, which allocates a pseudo-terminal and connects the terminal with the container's STDIN and STDOUT.

You need to specify -i and -t to interact with the container through a terminal shell.

-p is the abbreviation of --port. A port is an interface to the outside world. 1000:8000 maps Docker port 8000 to port 1000 on your computer. If you have an app that outputs something to the browser, you can navigate your browser to localhost:1000 and see it.

--rm automatically removes stopped containers.

Let's look at a few more examples of run.

docker container run -it my_image my_command

sh is a command you can specify at runtime, and it will start a shell session inside the container, which you can interact with through the terminal. For Alpine images, sh is preferred over bash because Alpine images do not come installed with bash. Type exit to end the interactive shell session.

Note that we combined -i and -t into -it.

docker container run -d my_image

-d is short for --detach and means running the container in the background, allowing you to use the terminal for other commands while the container is running.

Check container status

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

docker container ls — Lists running containers and provides useful information about them.

docker container ls -a -s

-a is the abbreviation of --all, which lists all containers (not just the running ones)

-s is short for --size and lists the size of each container.

docker container inspect my_container — View information about a container

docker container logs my_container — List container logs

Terminating the container

Sometimes you need to stop a running container. You can use the following command:

docker container stop my_container — Gracefully stops one or more running containers. This provides a default of 10 seconds for any processes to complete before the container is shut down.

If you think 10 seconds is too long, you can use the following command:

docker container kill my_container — Immediately stops one or more running containers. It's like pulling the plug on your TV. However, in most cases, it is recommended to use the stop command.

docker container kill $(docker ps -q)— terminates all running containers

If you need to delete the container you can use the following command:

docker container rm my_container — Remove one or more containers

docker container rm $(docker ps -a -q) — remove all containers that are not running

The above are the key commands for Docker containers. Next, let's look at the mirroring commands.

Detailed explanation of mirror command

Here are the 7 commands used by the Docker image:

Build the image

docker image build -t my_repo/my_image:my_tag . Build a Docker image named my_image in the Dockerfile at the specified path or url.

-t is short for tag, and tells Docker to tag the image with the provided tag, in this case, my_tag.

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

Once you have built your image, you want to push it to a remote repository so that it can be shared and pulled when needed. Then the next command is very useful, although not a mirror command.

docker login — Log in to the Docker image repository and type your username and password when prompted

docker image push my_repo/my_image:my_tag — Pushes an image to a repository.

Once you have these images, you may want to check them out.

Check the image

docker image ls — List your images and the size of each image

docker image history my_image — displays the intermediate images of an image, including their sizes and how they were created

docker image inspect my_image — displays details about the image, including the layers that make up the image

Sometimes you also need to clean up your images.

Clean up the image

docker image rm my_image — removes the specified image. If the image is stored in an image repository, it will still be available there.

docker image rm $(docker images -a -q) — Remove all images. This command must be used with caution. Please note that images that have been pushed to the remote repository can still be saved, which is an advantage of the image repository.

These are most of the important commands related to Docker images. I hope it will be helpful for everyone’s study, and I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • Basic introduction and use of Docker container image related commands
  • Summary of essential Docker commands for developers
  • Summary and analysis of commonly used Docker commands and examples
  • Detailed explanation of common Docker commands
  • Summary of common docker commands
  • Summary of common docker commands (recommended)
  • Summary of learning Docker commands in one article
  • Introduction to common Docker commands

<<:  JavaScript canvas realizes colorful sun halo effect

>>:  MySQL 5.7.18 Installer installation download graphic tutorial

Recommend

Cross-browser development experience summary (I) HTML tags

Add a DOCTYPE to the page Since different browser...

Query process and optimization method of (JOIN/ORDER BY) statement in MySQL

The EXPLAIN statement is introduced in MySQL quer...

Detailed explanation of importing/exporting MySQL data in Docker container

Preface We all know that the import and export of...

Detailed installation and use of virtuoso database under Linux system

I've been researching some things about linke...

Implementing calculator functions with WeChat applet

This article is a simple calculator written using...

Mariadb remote login configuration and problem solving

Preface: The installation process will not be des...

Tutorial on installing MYSQL8.X on Centos

MySQL installation (4, 5, 6 can be omitted) State...

Web Theory: Don't make me think Reading Notes

Chapter 1 <br />The most important principl...

HTML6 implements folding menu and accordion menu example code

The main part of the page: <body> <ul id...

uniapp Sample code for implementing global sharing of WeChat mini-programs

Table of contents Create a global shared content ...

Introduction to HTML link anchor tags and their role in SEO

The <a> tag is mainly used to define links ...

How to import/save/load/delete images locally in Docker

1. Docker imports local images Sometimes we copy ...

Vue.js framework implements shopping cart function

This article shares the specific code of Vue.js f...

User experience analysis of facebook dating website design

<br />Related article: Analysis of Facebook&...