迹忆客 EN >

当前位置:主页 > 学无止境 > 操作系统 > Docker >

重启 Docker 容器

作者:迹忆客 最近更新:2023/04/17 浏览次数:

当之前在后台运行的容器停止时,docker 提供了一些我们可以用来重启容器的命令。

在本文中,我们将学习如何在后台运行 Nginx 服务器的情况下重启 docker 容器。 我们还将了解一些命令如何导致容器立即终止。


从 Docker Hub 拉取 Nginx 镜像

首先,我们需要从 docker hub 中拉取一个 Nginx 镜像来运行镜像。 但是,您可以使用 Dockerfile 创建自定义映像。

在您的计算机上打开终端窗口并使用以下命令来拉取图像。

~$ docker pull nginx:alpine

从镜像运行容器

在本节中,我们将在后台运行一个带有 Nginx 服务器的容器。 使用以下命令运行名为 web-app 的容器。

~$ docker run --name web-app -d -p 8000:80 nginx:alpine

在此命令中,我们使用 --name 标志为容器创建名称。 此外,我们还使用了标志 -d,它允许我们在后台运行容器。

最后,我们添加了 -p 标志以将容器的端口发布到主机。 您可以使用以下命令来验证容器是否已启动并正在运行。

~$ docker ps

输出:

CONTAINER ID   IMAGE          COMMAND                  CREATED         STATUS         PORTS                  NAMES
c18fa4102fb2   nginx:alpine   "/docker-entrypoint.…"   5 seconds ago   Up 3 seconds   0.0.0.0:8000->80/tcp   web-app

重启正在运行的容器

我们可以使用两种方法来重启容器。 第一种方法使用 docker start 命令,而第二种方法使用 docker restart 命令。

docker start 命令用于启动一个停止的容器,docker restart 命令用于重启一个正在运行的容器。

由于我们的容器已经在运行,使用下面的命令重启 web-app 容器。

~$ docker restart web-app

在重新启动容器后,使用以下命令验证容器是否已启动并正在运行。

~$ docker ps

输出:

CONTAINER ID   IMAGE          COMMAND                  CREATED          STATUS        PORTS                  NAMES
c18fa4102fb2   nginx:alpine   "/docker-entrypoint.…"   20 minutes ago   Up 1 second   0.0.0.0:8000->80/tcp   web-app

重启停止的容器

这种方法与前一种方法相同。 我们必须使用 docker stop 命令停止正在运行的容器。

容器停止后,使用 docker start 命令重新启动停止的容器。 执行以下命令以查看其在实践中的工作原理。

~$ docker stop web-app

输出:

web-app

如果运行 docker ps -a 命令列出所有正在运行和已停止的容器,可以看到 web-app 容器的状态为 exited,表示容器已停止。

~$ docker ps -a

输出结果:

CONTAINER ID   IMAGE            COMMAND                  CREATED          STATUS                          PORTS      NAMES
c18fa4102fb2   nginx:alpine     "/docker-entrypoint.…"   32 minutes ago   Exited (0) About a minute ago              web-app

最后,使用以下命令重启容器。

~$ docker start web-app && docker ps

输出:

web-app
CONTAINER ID   IMAGE          COMMAND                  CREATED          STATUS                  PORTS                  NAMES
c18fa4102fb2   nginx:alpine   "/docker-entrypoint.…"   37 minutes ago   Up Less than a second   0.0.0.0:8000->80/tcp   web-app

Docker容器立即退出

上一节中的容器并没有终止,因为容器有一个 Nginx 进程在后台运行。

在某些情况下,如果 docker 容器已完成其进程的执行,它会立即退出。 为了说明这一点,运行以下命令创建一个名为 temp-web-app 的新容器。

~$ docker run -w /com/web-app/ --name temp-web-app -p 8080:80  nginx:alpine pwd

输出:

/com/web-app

请注意 ,此命令在打印工作目录 /com/web-app 后立即终止。 这是因为一旦其主进程执行 pwd 命令,容器就会退出。

因此,像上面这样的命令应该用于测试目的,而不是用于运行带有服务器、数据库或应用程序进程的容器。 使用以下命令检查 temp-web-app 容器的状态。

CONTAINER ID   IMAGE            COMMAND                  CREATED             STATUS                      PORTS                  NAMES
fb051673c15f   nginx:alpine     "/docker-entrypoint.…"   14 minutes ago      Exited (0) 3 minutes ago                           temp-web-app

由于在初始启动期间设置了默认命令,因此任何重新启动容器的尝试都不会起作用。 这意味着在容器的主进程退出时执行 pwd 命令后,容器将始终终止。


总结

我们在本文中学习了如何在 docker 中重启容器。 我们已经介绍了如何启动正在运行和停止的容器。

我们已经了解了运行立即退出且后台没有进程运行的容器的目的。

转载请发邮件至 1244347461@qq.com 进行申请,经作者同意之后,转载请以链接形式注明出处

本文地址:

相关文章

Solution to incorrect access log time when deploying Nginx in Docker

发布时间:2025/03/26 浏览次数:165 分类:Docker

In the process of operating the website, I never took the logs too seriously. Although logging was turned on, I never analyzed the logs carefully. Today, when I looked at the logs on a whim, I found that the recorded time was 8 hours less t

Docker deploys nginx php application

发布时间:2025/03/26 浏览次数:131 分类:Docker

I'm learning docker recently. I'm learning by building an nginx+php development environment example. Here I record the build process. First, give a docker-compose.yml deployment configuration file version: '3' services: nginx: container_nam

How to use Docker to image a Node.js web application

发布时间:2025/03/26 浏览次数:107 分类:Docker

Docker is a containerization platform that simplifies the packaging and execution of applications. Containers run as independent processes with their own file systems, but share the kernel of their host machine. Docker has attracted much at

Start a Bash terminal in a new Docker container

发布时间:2025/03/26 浏览次数:97 分类:Docker

Docker containers are a standard unit for packaging all the dependencies of an application, allowing us to easily run them in any environment. Containers have become very popular recently, and most developers now rely heavily on containers

Passing environment variables to containers in Docker

发布时间:2025/03/26 浏览次数:124 分类:Docker

This article will introduce how to pass environment variables to containers in Docker. Passing environment variables to containers in Docker using the -e and tags -env We will first see how to create environment variables and pass them to t

Install Docker using Homebrew

发布时间:2025/03/26 浏览次数:202 分类:Docker

There is no doubt that Docker containers have revolutionized the way we develop and deploy applications. They provide developers with the ability to package applications and dependencies in an isolated environment. Recently, we've seen wide

Enforce clean build of images in Docker

发布时间:2025/03/26 浏览次数:87 分类:Docker

This article discusses and demonstrates how to enforce clean builds of images in Docker. Building images in Docker We will use a simple Flask application to demonstrate this concept. my-app Create a app.py simple application named in the ho

Running a Docker instance from a Dockerfile

发布时间:2025/03/26 浏览次数:140 分类:Docker

Docker containers have undoubtedly become the standard unit for managing software and dependencies in different environments. When using real applications, you must create a docker file before building the container image of the application

扫一扫阅读全部技术教程

社交账号
  • https://www.github.com/onmpw
  • qq:1244347461

最新推荐

教程更新

热门标签

扫码一下
查看教程更方便