Docker Command Line Getting Started of These 18 you have to know

  • 2021-06-28 09:53:26
  • OfStack

Preface

The Docker image consists of Dockerfile and some necessary dependencies, and the Docker container is a dynamic Docker image.To use the Docker command, you first need to know whether you are working with mirrors or containers.Once you know whether you are dealing with mirrors or containers, you will find the right command.

Common Commands

You need to know a few rules about the Docker command:

The Docker CLI administration command begins with docker, followed by a space, followed by an administrative category, followed by a space, and finally a command.For example, the command docker container stop stops the container. Commands that reference a specific container or image require the name of the container or image or ID.

For example, docker container run my_app is used to build and run a system named my_Command for the container of app.In this example, I'll use the name my_container references the generic container.Similarly, my_image, my_The same is true for tag.

I will provide commands and generic flags separately.The first two dashes mark the full name of the symbol.A flag with a dash is an abbreviation of the full flag name.For example, -p is the abbreviation for the--port flag.

The goal of this article is to keep these commands and tags in mind, and hopefully you can use this guide as a reference when creating containers or building mirrors.This guide applies to Linux and Docker Engine versions 18.09.1 and API versions 1.39.

We start with container commands and then look at mirror commands.

Container commands

Using docker container my_command

create - Create a container from the mirror start - Start an existing container run - Create a new container and start it ls - List running containers inspect - View information about containers logs - Print Log stop - Elegantly stop running containers kill - Stop the main process in the container immediately rm - Delete stopped containers

Mirror Command

Using docker image my_command

build - Build a mirror push - Push mirror into remote mirror warehouse ls - List Mirrors history - View intermediate mirror information inspect - View information about mirroring, including layers rm - Delete Mirror

container & image

docker version - Lists information about Docker client and server versions docker login - Log on to the Docker mirror repository docker system prune - Delete all unused containers, networks, and unnamed mirrors (virtual suspension mirrors)

Container command details

Start Container

The terms "create", "start" and "run" have similar semantics in everyday life, but each has a separate Docker command for creating and/or starting containers.Let's first look at the commands for creating containers.

docker container create my_repo/my_image:my_tag - Create a container from a mirror

I'll put my_belowrepo/my_image:my_tag abbreviated as my_image.

You can pass many flags to create.


docker container create -a STDIN my_image

-a is the abbreviation for - attach, referring to the connection of a 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 containers can be referenced either by the container's ID or by the container's name.


docker container start my_container

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

docker container run my_image - Create a new container and start it.The 1 command also has many options.Let's look at some of them.


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

-i is the abbreviation for interactive, keep STDIN open even if not connected;-t is the abbreviation for tty, which assigns a pseudo-terminal to connect the terminal to the container's STDIN and STDOUT.

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

-p is the abbreviation of port.Ports are interfaces to the outside world.1000:8000 maps Docker port 8000 to port 1000 on your computer.If you have an app that outputs some content to your browser, you can navigate the browser to localhost:1000 and view it.

--rm automatically deletes containers that have stopped running.

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


docker container run -it my_image my_command

sh is a command that you can specify at run time. It will start an shell session inside the container, and you can interact with it through the terminal.For Alpine mirrors, sh is superior to bash because Alpine mirrors are not installed with bash1.Type exit to end the interactive shell session.

Note that we combine -i with -t as -it.


docker container run -d my_image

-d is short for detach and refers to running a 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 running Docker containers and want to find which one to interact with, you need to list them.

docker container ls - Lists containers in operation while providing useful information about them.


docker container ls -a -s

-a is short for--all, listing all containers (not just those in operation)

-s is short for - size, listing the size of each container.

docker container inspect my_container - View information about containers

docker container logs my_container - List container logs

Terminate Container

Sometimes you need to stop a container that is running. You need to use the following commands:

docker container stop my_container - Elegantly stops one or more running containers.Provide a default of 10 seconds to complete any process before the container closes.

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

docker container kill my_container - Stops one or more running containers immediately.It's like unplugging TV plug 1.However, in most cases, the stop command is recommended.

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

You need to delete the container. You can use the following commands:

docker container rm my_container - Delete one or more containers

docker container rm $(docker ps-a-q) - Delete all containers that are not in operation

These are the key commands for the Docker container.Next, let's look at the commands for mirroring.

Mirror command details

Here are seven commands used by the Docker image

Build Mirror

docker image build-t my_repo/my_image:my_tag. Builds an my_in the Dockerfile of the specified path or urlDocker image of image.

-t is the abbreviation for tag and tells docker to mark the image with the provided tag, in this case my_tag.

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

Once you've built the mirror, you want to push it into a remote repository so that it can be shared and pulled when needed.A command of 10 is useful, though it's not a mirror command.

docker login - Log in to the Docker mirror repository and type your user name and password as prompted

docker image push my_repo/my_image:my_tag - Push a mirror to the warehouse.

Once you have these mirrors, you may want to examine them.

Check Mirror

docker image ls - List your mirrors and the size of each

docker image history my_image - An intermediate image showing the image, including size and how it was created

docker image inspect my_image - Shows details about the image, including the layers that make up the image

Sometimes you still need to clean up your mirror.

Clean Mirror

docker image rm my_image - Deletes the specified image.If a mirror is saved in a mirror warehouse, it is still available there.

docker image rm $(docker images - a - q) - Delete all mirrors.You must use this command with care.Note that mirrors that have been pushed to remote warehouses can still be saved, which is an advantage of mirror warehouses.


Related articles: