简体   繁体   English

使用 docker compose 在后台运行 ubuntu 容器

[英]Running a ubuntu container in background using docker compose

I am able to run a docker container using following docker command:我可以使用以下 docker 命令运行 docker 容器:

docker run -it  ubuntu /bin/bash

Now I am trying to do it by using docker-compose:现在我正在尝试使用 docker-compose 来做到这一点:

version: "3"
services:
  ubuntu:
    container_name: ubuntu
    image: ubuntu
    restart: on-failure
    command: "/bin/bash"

Now when I do :现在当我这样做时:

 docker-compose up -d

Can see docker container starting and exiting immediately.可以看到docker容器立即启动和退出。

I tried looking at the logs :我试着查看日志:

docker logs b8 //b8 is container id

But there are no error logs.但是没有错误日志。

How do I keep ubuntu container running in background using docker.如何使用 docker 让 ubuntu 容器在后台运行。 ( I am using docker on windows , linux version) (我在 windows 上使用 docker,linux 版本)

This is normal.这是正常的。

You are starting an ubuntu container with bash as the command ( thus the root process ).您正在使用bash作为命令(因此是根进程)启动一个 ubuntu 容器。 The thing is to keep bash alive you need to attach it with a terminal.问题是要保持 bash活动,您需要将其与终端连接。 This is why when you want to get a bash in a container, you're using -ti with your command :这就是为什么当您想在容器中获取bash 时,您将-ti与您的命令一起使用:

docker container exec -ti [my_container_id] bash

So if you want to keep your ubuntu container alive and don't want to attach it to a terminal, you'll have to use a process that will stay alive for as long as you want.因此,如果您想让 ubuntu 容器保持活动状态并且不想将其附加到终端,则必须使用一个可以随心所欲地保持活动状态的进程。
Below is an example with sleep infinity as your main process下面是一个以sleep infinity为主要过程的例子

version: "3"
services:
  ubuntu:
    container_name: ubuntu
    image: ubuntu
    restart: on-failure
    command: ["sleep","infinity"]

With this example, you container will stay running indefinitely.在这个例子中,你的容器将无限期地保持运行。

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

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