15

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)

4
  • There are no error logs because there are no errors. You start a container but as it has nothing to do (except running bash with no command) it just exists because it has finished its 'tasks'. If you just want ti to run in the back ground doing nothing modify its entry point to perform something like while true sleep Commented Feb 17, 2020 at 10:42
  • It’s pretty unusual to run the bare ubuntu image, or to have just a shell be the main container process. Generally you’d package up an application (or one piece like a database or a REST API) in a custom image, and have its default CMD in the Dockerfile run the application. Commented Feb 17, 2020 at 11:52
  • @DavidMaze, what if I want to run rsync on a cron job, on my win10 system ? Yes, I can create a docker file and add the apks, but wont need a command , just need it in the background. Commented Feb 19, 2020 at 19:12
  • It seems common enough to run the cron daemon as the main container process. As you say, you’d need a custom image to add in the program you need to run and install your crontab, and also to set the default CMD to run cron. Commented Feb 19, 2020 at 19:28

1 Answer 1

38

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.