简体   繁体   中英

Docker run: how to run Ubuntu:16.04 without command?

I want to try out installing a program inside of Ubuntu in docker,

So I just run directly from the command prompt

docker run --name ubuntu_test ubuntu:16.04
docker exec -it ubuntu_test bash

but it doesn't work, it says container not running? how can I run the bash without setting up a dockerfile? (I tried using dockerfile, but it doesn't work because of interactive installer problem )

So I thought maybe directly install it from the bash could work.

The problem is that your command doesn't keep the proccess alive nor does it keep it in the background so the container finishes its work and stops running. Its like docker run hello-world which prints some stuff and exits.

docker run -it --name ubuntu_test ubuntu:16.04 will work for you. The documentation explains:

For interactive processes (like a shell), you must use -i -t together in order to allocate a tty for the container process. -i -t is often written -it

An alternative would be to run the container in detached mode (-d) and give it a long running command so that it doesn't exit immediately:

docker run --name ubuntu_test -d ubuntu:16.04 sleep 300 docker exec -it ubuntu_test bash

You are failing to start the container. Try this:

docker run -itd ubuntu:16.04 bash

-i, --interactive - Keep STDIN open even if not attached

-t, --tty - Allocate a pseudo-TTY

-d, --detach - Run container in background and print container ID

After this command list all your containers( docker ps ):

在此处输入图片说明

Now you can attach to your running container and do some stuff:

docker exec -it 82d0bb7754e7 /bin/bash

(in this case, to indicate the container I used the ID, you can also use container name)

When you run a container you can pass a command at the end of the command that should run, like:

docker run --name ubuntu_test -it ubuntu:16.04 bash

If there's an Entrypoint specified in Dockerfile then you need to override entrypoint:

docker run --name ubuntu_test -it --entrypoint bash ubuntu:16.04

I am not sure, you are on right way with installing into running image. It is not convenient because not automated - next time you need your image you will have to do all steps again and there is no way to record them.

I would suggest instead to try to fix the interactive installer problem, you are not the first one to face it. There are many ways, including yes program, which automatically outputs predefined answer to the installer's questions or you can simply use echo piping the result to installer.

This should be work:

docker run --rm -it --name ubuntu_test -d ubuntu:16.04 
docker exec -it ubuntu_test /bin/bash

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