Docker Compose 保持容器运行 [英] Docker Compose keep container running

查看:121
本文介绍了Docker Compose 保持容器运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用 docker-compose 启动一个服务并保持容器运行,这样我就可以通过docker inspect"获取它的 IP 地址.但是,容器总是在启动后立即退出.

I want to start a service with docker-compose and keep the container running so I can get its IP-address via 'docker inspect'. However, the container always exits right after starting up.

我尝试在 docker-compose.yml 中添加command: [sleep"、60"]"和其他内容,但是每当我添加带有command:..."的行时,我都无法调用docker-撰写",因为我将收到消息无法启动容器......系统错误:无效字符 'k' 正在寻找值的开头"

I tried to add "command: ["sleep", "60"]" and other things to the docker-compose.yml but whenever I add the line with "command:..." I cant call "docker-compose up" as I will get the message "Cannot start container ..... System error: invalid character 'k' looking for beginning of value"

我还尝试将CMD sleep 60"等添加到 Dockerfile 本身,但这些命令似乎没有被执行.

I also tried adding "CMD sleep 60" and whatnot to the Dockerfile itself but these commands do not seem to be executed.

是否有一种简单的方法可以让容器保持活动状态或解决我的一个问题?

Is there an easy way to keep the container alive or to fix one of my problems?

这是我要运行的 Compose 文件:

Here is the Compose file I want to run:

version: '2'
services:
  my-test:
    image: ubuntu
    command: bash -c "while true; do echo hello; sleep 2; done"

它工作正常如果我在 OS X 下使用 docker-compose 启动它,但如果我在 Ubuntu 16.04 下尝试同样的操作,它会给我上面的错误消息.

It's working fine If I start this with docker-compose under OS X, but if I try the same under Ubuntu 16.04 it gives me above error message.

如果我尝试使用 Dockerfile 的方法,Dockerfile 看起来像这样:

If I try the approach with the Dockerfile, the Dockerfile looks like this:

FROM ubuntu:latest
CMD ["sleep", "60"]

似乎什么都没做

编辑 2:我必须纠正自己,结果发现 Dockerfile 和 docker-compose.yml 是同样的问题:每次我向 Dockerfile 添加CMD ..."或向撰写文件添加command ..."时,我都会收到上述无效字符的错误.如果我删除这两个命令,它就可以完美运行.

EDIT 2: I have to correct myself, turned out it was the same problem with the Dockerfile and the docker-compose.yml: Each time I add either "CMD ..." to the Dockerfile OR add "command ..." to the compose file, I get above error with the invalid character. If I remove both commands, it works flawlessly.

推荐答案

要在使用 docker-compose 启动容器时保持容器运行,请使用以下命令

To keep a container running when you start it with docker-compose, use the following command

command: tail -F 任何东西

在上面的命令中,最后一部分 anything 应该包含在字面上,并且假设这样的文件不存在于容器中,而是带有 -F选项(大写 -F 不要与 -f 混淆,相反,如果找不到文件,它将立即终止)tail 命令将永远等待文件 anything 出现.一个永远等待的过程基本上就是我们所需要的.

In the above command the last part anything should be included literally, and the assumption is that such a file is not present in the container, but with the -F option (capital -F not to be confused with -f which in contrast will terminate immediateley if the file is not found) the tail command will wait forever for the file anything to appear. A forever waiting process is basically what we need.

所以你的 docker-compose.yml 变成了

So your docker-compose.yml becomes

version: '2'
services:
  my-test:
    image: ubuntu
    command: tail -F anything

并且您可以使用以下命令运行 shell 以进入容器

and you can run a shell to get into the container using the following command

docker exec -i -t composename_my-test_1 bash

其中 composenamedocker-compose 附加到您的容器的名称.

where composename is the name that docker-compose prepends to your containers.

这篇关于Docker Compose 保持容器运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆