0

I was trying to run ubuntu docker image in background. So I tried below command

sudo docker container run -d --name my-ubuntu-container ubuntu:latest

But this command do not run the container in background. In fact the status becomes "EXITED" on checking using

docker container ls -a

But if I add "-it" flag in above command. Then the container runs in background.

sudo docker container run -itd --name my-ubuntu-container ubuntu:latest

Now on checking in docker container ls -a. We see the status now in "UP" and it runs in background.

Anyone can please advise why adding "-it" flag in above command along with "-d" runs the ubuntu docker image in background ?

Tarun Lalwani
  • 142,312
  • 9
  • 204
  • 265
nayak0765
  • 175
  • 2
  • 15
  • The standard `ubuntu` image on its own doesn't really do anything. Is there a reason you want it running? Do you have a more complete Dockerfile for a custom image that you're trying to run? – David Maze Mar 11 '21 at 16:03

2 Answers2

2

You didn't specify a command so the container runs with the default command bash. If you run bash interactively (-ti) it stays open, but otherwise it quits immediately. If you want to run a container detached in the backgroud you can change the command from bash to a command that blocks - for example tail -f /dev/null.

You can run the following command and the container won't exit immediately.

sudo docker container run -d --name my-ubuntu-container ubuntu:latest tail -f /dev/null
DerMaddi
  • 649
  • 2
  • 12
0

First of all, you should avoid runing docker command with "sudo". Instead, add your current user to the docker group in your linux OS.

Also, you could do docker container logs <container_id> to see if there were eventual errors.

As stated in that other post answer : https://stackoverflow.com/a/48368594/2409641

-it is short for --interactive + --tty when you docker run with this command.. it would take you straight inside of the container,, where -d is short for --detach which means you just run the container and then detach from it so basically you run container in the background.. edit : so if you run docker container with-itd it would run the-it options and detach you from the container, so your container still running in the background even without any default app to run..

Dharman
  • 30,962
  • 25
  • 85
  • 135
Flo
  • 936
  • 1
  • 8
  • 19
  • Since you can very easily use `docker run` to compromise the host (bind-mount the host's `/etc` directory and edit `/host/etc/shadow` as root inside the container, say) it's appropriate to require `sudo` access for it. – David Maze Mar 11 '21 at 16:02