Docker containers have created new momentum in the virtualization and container area. Docker containers are processes running on the host operating system in a restricted environment.

Docker容器在虚拟化和容器领域创造了新的动力。 Docker容器是在受限环境中在主机操作系统上运行的进程。

Docker engine is used to manage the life cycle of the containers with various commands. We will look these sub-commands in this tutorial.

Docker引擎用于通过各种命令管理容器的生命周期。 我们将在本教程中查看这些子命令。

(Docker Container)

Now some terminology. Docker ecosystem provides a lot of different systems and terms about the work. A container is data and process that is running in a restricted way in a host as process. Generally working state is called container.

现在一些术语。 Docker生态系统为工作提供了许多不同的系统和术语。 容器是在主机中以受限方式运行的数据和进程。 通常,工作状态称为容器。

(Docker Image)

Docker image is the data files provided by different repositories. Docker image generally provides base operating system files and user spaces files related with the image.

Docker映像是由不同存储库提供的数据文件。 Docker映像通常提供与映像相关的基本操作系统文件和用户空间文件。

(Docker Hub)

Docker hub is popular and most used hub for providing docker images. As the creator and maintainer it is very popular and comes by default with Docker engine. Docker hub provides image for Ubuntu, Apache, CentOS, RabbitMQ, …

Docker枢纽是流行的也是使用最多的提供Docker映像的枢纽作为创建者和维护者,它非常流行,并且默认情况下与Docker引擎一起提供。 Docker中心为Ubuntu,Apache,CentOS,RabbitMQ等提供映像。

There are also alternative hubs those provides paid or more specialized Docker images. Also we can create some private Docker  hubs.

还有其他一些中心,它们提供付费或更专业的Docker映像。 我们还可以创建一些私有Docker集线器。

(Pull Container Image)

Docker Images are downloaded from repositories like Docker Hub. In order to start a container we need to get Image of the container. This is called pulling of the image. Pulling image will download related image files  from repository to the local Docker image location. Default docker image location is /var/lib/docker/image . We will use pull command. In the example we will download the Fedora image from Docker Hub.

Docker镜像是从Docker Hub等存储库下载的。 为了启动容器,我们需要获取容器的图像。 这称为图像拖拉。 提取映像会将相关的映像文件从存储库下载到本地Docker映像位置。 默认Docker映像位置为/var/lib/docker/image 。 我们将使用pull命令。 在示例中,我们将从Docker Hub下载Fedora映像。

$ sudo docker pull fedora

Linux 停止docker 容器 docker如何停止容器进程_python

Pull Container Image 拉容器图像

As we can see from screenshot while downloading an image just image name is not enough. We should also provide a tag which will specify exact image. But there is a shortcut provided by docker. If no tag is provided docker assumes tag latest by default.

正如我们从屏幕快照中看到的那样,下载图像时仅图像名称是不够的。 我们还应该提供一个标签,以指定确切的图像。 但是docker提供了一个快捷方式。 如果未提供标签,则docker默认采用latest标签。

(Create Container)

We have  downloaded image for Fedora in previous example. Now we can create a container without starting it. Actually a container can be started directly without creating it which will create automatically. Creating container will not start it. We will use create subcommand for this. In this example we will create container by giving name to it with --name option. The name of the new container will be myfed .

在前面的示例中,我们已经下载了Fedora图像。 现在,我们无需启动即可创建容器。 实际上,无需创建将自动创建的容器就可以直接启动它。 创建容器不会启动它。 我们将为此使用create子命令。 在此示例中,我们将通过使用--name选项为其命名来创建容器。 新容器的名称将为myfed 。

$ docker create fedora --name myfed

Linux 停止docker 容器 docker如何停止容器进程_java_02

Create Container 创建容器

After creation is completed a hash values is provided. This id the ID of the container and used for specify container in the various container related operations.

创建完成后,将提供哈希值。 此ID是容器的ID,用于在各种与容器相关的操作中指定容器。

LEARN MORE  How To Get Information About Running Containers, Images In Docker?

了解更多信息如何获取有关在Docker中运行容器和图像的信息?

(Start Docker Container)

In this step we will start container by specifying the base image name. Starting container will create new process in the host operating system according to the container parameters. We will use run command with various options where we look them in detail next examples.

在此步骤中,我们将通过指定基本图像名称来启动容器。 启动容器将根据容器参数在主机操作系统中创建新进程。 我们将在各种示例中使用run命令,并在下一个示例中对其进行详细介绍。

$ docker run fedora /bin/bash

After command operation completed the container will stop. This is the nature of the containers. They starts, runs and stops generally in limited times.

命令操作完成后,容器将停止。 这就是容器的本质。 它们通常在有限的时间内启动,运行和停止。

(Connect Container To Terminal)

Previous example we have started container but after the command complation the session end. If we need connect to the terminal of the container and want to issue commands we should provide -i and -t parameters like below.

在前面的示例中,我们已启动容器,但是在命令编译后会话结束。 如果我们需要连接到容器的终端并想要发出命令,则应提供如下的-i和-t参数。

$ docker run -t -i fedora /bin/bash

Linux 停止docker 容器 docker如何停止容器进程_python_03

Connect Container To Terminal 将容器连接到终端

The container is no stopped after command operation. We can issue commands here as root user of the container. If we do not provide a host name the host name is provided as random.

命令操作后,容器不会停止。 我们可以在这里以容器的root用户身份发出命令。 如果我们不提供主机名,则主机名是随机提供的。

(List Containers)

We may need to list currently running containers in a host. We will use ps command for this. For more information about the container status read following post.

我们可能需要列出主机中当前正在运行的容器。 我们将使用ps命令。 有关容器状态的更多信息,请阅读以下文章。

How To Get Information About Running Containers, Images In Docker?

如何获取有关在Docker中运行容器和图像的信息?

$ docker ps

Linux 停止docker 容器 docker如何停止容器进程_linux_04

List Containers 列出容器

重命名容器(Rename Container)

Currently running containers names can be changed. But keep in mind we do not change the ID we will change the name which is like a tag. We will use rename command with container id and new name. In the example the container id is f991c8869664 and new name will be set as mynewname .

当前正在运行的容器名称可以更改。 但是请记住,我们不会更改ID,而是会更改名称,就像标记一样。 我们将使用带有容器ID和新名称的rename命令。 在示例中,容器ID为f991c8869664,新名称将设置为mynewname 。

$ docker rename f991c8869664 mynewname

Linux 停止docker 容器 docker如何停止容器进程_docker_05

Rename Container 重命名容器

停止容器(Stop Container)

We can stop the Docker container with stop subcommand.

我们可以使用stop子命令停止Docker容器。

$ docker stop f991c8869664

Linux 停止docker 容器 docker如何停止容器进程_python_06

Stop Container 停止容器

删除和删除容器(Delete and Remove Container)

Containers are generally used for limited time. So after the use of container ends the container will be deleted and related resources will be freed. We can delete a container with rm command which is similar to Linux bash. In order to remove a container the container should be stopped as defined previous example. We will also provide container id for remove. In this example we remove the container with id f991c8869664

容器通常在有限的时间内使用。 因此,在结束使用容器后,将删除该容器并释放相关资源。 我们可以使用rm命令删除类似于Linux bash的容器。 为了卸下容器,应按照前面的示例中所述停止容器。 我们还将提供要删除的容器ID。 在此示例中,我们删除了ID为f991c8869664的容器

$ sudo docker rm f991c8869664
(Pause Container)

Pausing container maybe needed some times to cut down CPU usage. We can pause a container with pause sub-command by providing container id.

可能需要暂停容器以减少CPU使用率。 通过提供容器ID,我们可以使用pause子命令来pause容器。

$ docker pause a264a020b160

翻译自: https://www.poftut.com/create-start-stop-manage-docker-containers-linux/