Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I run bash in a new container of a docker image?

Tags:

bash

docker

I am able to run arbitrary shell commands in a container created from docker/whalesay image.

$ docker run docker/whalesay ls -l total 56 -rw-r--r-- 1 root root  931 May 25  2015 ChangeLog -rw-r--r-- 1 root root  385 May 25  2015 INSTALL -rw-r--r-- 1 root root 1116 May 25  2015 LICENSE -rw-r--r-- 1 root root  445 May 25  2015 MANIFEST -rw-r--r-- 1 root root 1610 May 25  2015 README -rw-r--r-- 1 root root  879 May 25  2015 Wrap.pm.diff drwxr-xr-x 2 root root 4096 May 25  2015 cows -rwxr-xr-x 1 root root 4129 May 25  2015 cowsay -rw-r--r-- 1 root root 4690 May 25  2015 cowsay.1 -rw-r--r-- 1 root root   54 May 25  2015 install.pl -rwxr-xr-x 1 root root 2046 May 25  2015 install.sh -rw-r--r-- 1 root root  631 May 25  2015 pgp_public_key.txt $ docker run docker/whalesay lsb_release -a No LSB modules are available. Distributor ID: Ubuntu Description:    Ubuntu 14.04.2 LTS Release:    14.04 Codename:   trusty 

However, I am unable to run a shell in a container created from this image.

$ docker run docker/whalesay bash $ docker ps CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES $ docker ps -a CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                         PORTS               NAMES 7ce600cc9904        docker/whalesay     "bash"                   5 seconds ago       Exited (0) 3 seconds ago                           loving_mayer 

Why did it not work? How can I make it work?

like image 465
Lone Learner Avatar asked Apr 09 '17 15:04

Lone Learner


People also ask

How do I start a bash container?

If you have a container in the exited state and you want to start a bash associated with that container, you can use the Docker start command along with the --attach and --interactive options. This will attach a new terminal and will allow you to interact with the container easily.

How do I run a command in docker container?

To use the docker exec command, you will need a running Docker container. If you don't already have a container, start a test container with the following docker run command: docker run -d --name container-name alpine watch "date >> /var/log/date. log"

What docker command is used to execute a command in a new container?

The docker exec command runs a new command in a running container. The command started using docker exec only runs while the container's primary process ( PID 1 ) is running, and it is not restarted if the container is restarted. COMMAND will run in the default directory of the container.

How do I run a docker container from an image?

To run an image inside of a container, we use the docker run command. The docker run command requires one parameter and that is the image name. Let's start our image and make sure it is running correctly. Execute the following command in your terminal.


1 Answers

If you docker run without attaching a tty, and only call bash, then bash finds nothing to do, and it exits. That's because by default, a container is non-interactive, and a shell that runs in non-interactive mode expects a script to run. Absent that, it will exit.

To run a disposable new container, you can simply attach a tty and standard input:

docker run --rm -it --entrypoint bash <image-name-or-id> 

Or to prevent the above container from being disposed, run it without --rm.

Or to enter a running container, use exec instead:

docker exec -it <container-name-or-id> bash 

In comments you asked

Do you know what is the difference between this and docker run -it --entrypoint bash docker/whalesay?

In the two commands above, you are specifying bash as the CMD. In this command, you are specifying bash as the ENTRYPOINT.

Every container is run using a combination of ENTRYPOINT and CMD. If you (or the image) does not specify ENTRYPOINT, the default entrypoint is /bin/sh -c.

So in the earlier two commands, if you run bash as the CMD, and the default ENTRYPOINT is used, then the container will be run using

/bin/sh -c bash 

If you specify --entrypoint bash, then instead it runs

bash <command> 

Where <command> is the CMD specified in the image (if any is specified).

like image 72
Dan Lowe Avatar answered Sep 19 '22 08:09

Dan Lowe