简体   繁体   English

交互式启动 docker 容器

[英]start docker container interactively

I have a very simple dockerfile with only one row, namely "FROM ubuntu".我有一个非常简单的 dockerfile 只有一行,即“FROM ubuntu”。 I created an image from this dockerfile by the command docker build -t ubuntu_.我通过命令docker build -t ubuntu_.

I know that I can create a new docker container from this image an run it interactively with the command docker run -it my_new_container我知道我可以从此图像创建一个新的 docker 容器并使用命令docker run -it my_new_container交互方式运行它

I can later start this new container with the command start my_new container稍后我可以使用命令start my_new container启动这个新容器

As I understand it, I should also be able to use this container it interactively by start -i my_new container据我了解,我也应该能够通过start -i my_new container交互地使用这个容器

But, it does not work.但是,它不起作用。 It just runs and exits.它只是运行并退出。 I don't get to the container's command prompt as I do when I use run.我没有像使用运行时那样进入容器的命令提示符。 What am I doing wrong?我究竟做错了什么?

If i understood correctly, you want to see the logs from container in terminal, same as when you run the image with docker run .如果我理解正确,您想在终端中查看来自容器的日志,就像您使用docker run运行图像时一样。 If that's the case, then try with如果是这种情况,请尝试

docker start -a my_docker_container

You can enter a running container with:您可以使用以下命令进入正在运行的容器:

docker exec -it <container name> /bin/bash

example:例子:

docker exec -it my_new_container /bin/bash

you can replace bash with sh if bash is not available in the container.如果容器中没有bash ,可以用sh替换bash

and if you need to explicitly use a UID, like root = UID 0, you can specify this:如果您需要显式使用 UID,例如 root = UID 0,您可以指定:

docker exec -it -u 0 my_new_container /bin/bash

which will log you as root这将把你记录为root

Direct answer:直接回答:

To run an interactive shell for a non-running container, first find the image that the container is based on.要为非运行容器运行交互式 shell,首先要找到容器所基于的图像。

Then:然后:

docker container run -it [yourImage] bash

If your eventual container is based on an alpine image, replace bash with sh.如果您最终的容器是基于 alpine 图像,请将 bash 替换为 sh。

Technically, this will create a NEW container, but it gets the job done.从技术上讲,这将创建一个新容器,但它完成了工作。

EDIT [preferred method]:编辑[首选方法]:

An even better way is to give the container something irrelevant to do.更好的方法是让容器做一些无关紧要的事情。 A nice solution from the VSCode docs is to put the following command into your service definition of the docker-compose.yml file: VSCode 文档中的一个很好的解决方案是将以下命令放入 docker-compose.yml 文件的服务定义中:

services:
   my-app-service:
      command: ["sleep", "infinity"]
      # other relevant parts of your service def...

The idea here is that you're telling your container to sleep for some time (infinite amount of time).这里的想法是你告诉你的容器休眠一段时间(无限量的时间)。 Ironically, this your container will have to maintain this state, forcing the container to keep running.具有讽刺意味的是,你的容器将不得不维护这个 state,迫使容器继续运行。

This is how I run containers.这就是我运行容器的方式。 Best wishes to whomever needs this nugget of info.向需要这些信息的人致以最良好的祝愿。 We're all learning:)我们都在学习:)

You cannot get a shell into a container in its stopped state, or restart it directly with another entry point.您无法将 shell 放入其已停止的 state 中的容器中,或直接使用另一个入口点重新启动它。 If the container keeps exiting and you need to examine it, the only option I know of is to commit the container as a new image and then start a new container with such image, as per a related answer .如果容器不断退出并且您需要检查它,我知道的唯一选择是将容器作为新图像提交,然后根据相关答案使用该图像启动新容器。

If you don't need that container anymore and just want it to stay up, you should run it with a process that will not exit.如果您不再需要该容器并且只想让它保持运行,则应该使用不会退出的进程来运行它。 An example with an Ubuntu image would be (you don't need a Dockerfile for this):带有 Ubuntu 图像的示例是(为此您不需要Dockerfile ):

docker run -d ubuntu --name carrot tail -f /dev/null

You will see that this container stays up and you can now run bash on it, to access the CLI:您将看到此容器保持运行状态,您现在可以在其上运行bash以访问 CLI:

docker exec -ti carrot bash

If the container has stopped for whatever reason, such as a machine restart, you can bring it back up:如果容器由于某种原因停止,例如机器重启,您可以将其重新启动:

docker start carrot

And it will continue to stay up again.它将继续熬夜。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM