简体   繁体   中英

Running a ubuntu container in background using docker compose

I am able to run a docker container using following docker command:

docker run -it  ubuntu /bin/bash

Now I am trying to do it by using 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.

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. ( I am using docker on windows , linux version)

This is normal.

You are starting an ubuntu container with bash as the command ( thus the root process ). The thing is to keep bash alive you need to attach it with a terminal. This is why when you want to get a bash in a container, you're using -ti with your command :

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.
Below is an example with sleep infinity as your main process

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

With this example, you container will stay running indefinitely.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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