Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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)

like image 760
Simsons Avatar asked Feb 17 '20 10:02

Simsons


People also ask

How do I keep docker containers running in the background?

The simplest way to keep the container running is to pass a command that never ends. We can use never-ending commands in any of the following ways: ENTRYPOINT or CMD directive in the Dockerfile. Overriding ENTRYPOINT or CMD in the docker run command.

How do I run a docker container in the background Ubuntu?

To run a docker container in the background or the detached mode from the terminal, you can use the docker run command followed by the -d flag (or detached flag) and followed by the name of the docker image you need to use in the terminal.

How do I run a docker container in the background using docker compose?

You can start Docker Compose in the background using the command docker-compose up -d . If using this method, you'll need to run docker-compose stop to shut it down.

Does docker compose up run in background?

The docker compose up command aggregates the output of each container (like docker compose logs --follow does). When the command exits, all containers are stopped. Running docker compose up --detach starts the containers in the background and leaves them running.


1 Answers

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.

like image 85
Marc ABOUCHACRA Avatar answered Oct 19 '22 06:10

Marc ABOUCHACRA