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

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

问题描述

我想使用docker-compose启动服务并保持容器运行,以便我可以通过 docker inspect获取其IP地址。但是,容器始终在启动后立即退出。



我尝试向docker-添加 command:[ sleep, 60]等compose.yml,但是每当我使用 command:...添加行时,我都无法调用 docker-compose up,因为我会收到消息无法启动容器.....。系统错误:无效字符 k对于值的开始。



我还尝试向Dockerfile本身添加 CMD sleep 60和诸如此类的东西,但是这些命令似乎并未执行。



是否有一种简便的方法来保持容器的寿命或解决我的问题之一?



编辑:
此处是我要运行的撰写文件:

 版本: 2 
服务:
my-测试:
图像:ubuntu
命令:bash -c while true;回声问好;睡眠2;完成

工作正常,如果我在OS X下以docker-compose开头,但是如果在Ubuntu 16.04下尝试相同的操作,则会出现上述错误

如果我尝试使用Dockerfile,则Dockerfile如下所示:

 从ubuntu:最新
CMD [ sleep, 60]

似乎什么也没做



编辑2:
我必须纠正自己,原来这与Dockerfile和docker-compose.yml:
每次我将 CMD ...添加到Dockerfile或将 command ...添加到撰写文件时,都会收到无效字符以上的错误。如果我删除了这两个命令,它就可以正常工作。

解决方案

在使用 docker-compose ,使用以下命令



命令:tail -F any



因此您的docker-compose.yml变为

 版本:' 2'
服务:
我的测试:
图片:ubuntu
命令:tail -F any

,您可以使用以下命令运行Shell进入容器



docker exec -i -t composename_my-test_1 bash



其中 composename docker-compose 放在您的容器中。


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.

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"

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?

EDIT: 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"

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.

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

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

Which does not seem to do anything

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.

解决方案

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

command: tail -F anything

So your docker-compose.yml becomes

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

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

docker exec -i -t composename_my-test_1 bash

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

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

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