简体   繁体   中英

Run ubuntu 16.04 bash shell in a docker image

I have this Dockerfile:

FROM ubuntu:16.04

I run

docker build -t mine .

It builds. Then I run

docker exec -it mine /bin/bash

and it says

Error: No such container: mine

I'm sure I'm missing something simple, but I've Googled for docker image name, I've run docker images and it shows the "CONTAINER ID" "mine" (with tag "latest"), I've looked at stackoverflow. This is so basic it's hard to find the answer.

I'm running docker 19.03.2, build 6a30dfc, on OS X.

EDIT : If I run docker run mine it runs and immediately exits (of course, since that's what the Dockerfile says). Then there's a container (not running). Then if I exec that container by id, it says, "Container..hex.. is not running," which makes sense.

I guess I want the simplest way to run a bash shell in Ubuntu 16.04.

Advice?

Docker exec command is for executing a command inside of a running container. You simply need to run your container using docker run -it mine /bin/bash .

If your ultimate goal is to run Ubuntu's bash on itself, you can skip the build phase and just do docker run -it ubuntu:16.04 /bin/bash .

Notice the -i and -t flags. The first one indicates that your containerized program (ie bash) may receive some user input. Hence, docker will keep its stdin open. The second flag is to use a Linux pseudoterminal (PTY) as a controlling terminal of the command (ie bash). And the combination of these flags allows you to have a normal interactive shell experience.

The reason behinde this docker run mine command is, It will run the container but it will be terminated because it will not allocate pseudo-tty

-t              : Allocate a pseudo-tty
-i              : Keep STDIN open even if not attached

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 as you'll see in later examples. Specifying -t is forbidden when the client is receiving its standard input from a pipe, as in:

echo test | docker run -i busybox cat

so in your case you should allocate pseudo-tty for the ubuntu container.

docker run --name my_ubuntu_container -it ubuntu bash

Then you can run

--tty , -t      Allocate a pseudo-TTY
docker exec -it my_ubuntu_container bash

that because you need to run it first before using exec

docker run -it mine 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