简体   繁体   中英

Running ubuntu docker image in background

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?

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..

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

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