otong
otong

Reputation: 1545

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.

Upvotes: 4

Views: 8005

Answers (5)

Krzysztof Raciniewski
Krzysztof Raciniewski

Reputation: 4924

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):

enter image description here

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)

Upvotes: 7

grapes
grapes

Reputation: 8646

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.

Upvotes: 1

andnik
andnik

Reputation: 2804

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

Upvotes: 1

Truong Dang
Truong Dang

Reputation: 3427

This should be work:

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

Upvotes: 1

herm
herm

Reputation: 16405

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

Upvotes: 7

Related Questions