2

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?

2
  • I always fo docker run --rm -ti containername possibly followed by a command, possibly with mount or port options etc pp. Do docker images to see under what name your build succeeded. Commented Sep 18, 2019 at 20:10
  • For the edited question: docker run --rm -ti ubuntu:16.04 which will start bash by default. Commented Sep 18, 2019 at 20:21

3 Answers 3

11

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 (i.e. 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 (i.e. bash). And the combination of these flags allows you to have a normal interactive shell experience.

Sign up to request clarification or add additional context in comments.

Comments

2

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

Comments

1

that because you need to run it first before using exec

docker run -it mine bash

2 Comments

Yes. I changed the question to ask, "What's the simplest way to run an ubuntu 16.04 bash shell using docker?"
Oh, if I use -it (instead of -dti) for an interactive shell, this works. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.