Piping docker run container ID to docker exec - bash

In my development, I find myself issuing a docker run command followed by a docker exec command on the resulting container ID quite frequently. It's a little annoying to have to copy/paste the container ID between commands, so I was trying to pipe the container ID into my docker exec command.
Here's my example command.
docker run -itd image | xargs -i docker exec -it {} bash
This starts the container, but then I get the following error.
the input device is not a TTY
Does anyone have any idea how to get around this?
Edit: I also forgot to mention I have an ENTRYPOINT defined and cannot override that.

Do this instead:
ID=$(docker run -itd image) && docker exec -it $ID bash
Because xargs executes it arguments without allocating a new tty.

If you just want to "bash"-into the container you do not have to pass the container-id around. You can simply run
docker run -it --rm <image> /bin/bash
For example, if we take the ubuntu base image
docker run -it --rm ubuntu /bin/bash
root#f80f83eec0d4:/#
from the documentation
-t : Allocate a pseudo-tty
-i : Keep STDIN open even if not attached
--rm : Automatically remove the container when it exits
The command /bin/bash overwrites the default command that is specified with the CMD instruction in the Dockerfile.

Related

OCI runtime exec failed: exec failed: container_linux.go:349: starting container process caused "exec:

I have created image with our application, after running the image i can see the docker containers are also created, when I am trying to getinto the docker container i am getting the below error, can you please help me here.
""""OCI runtime exec failed: exec failed: container_linux.go:349: starting container process caused "exec: "bash": executable file not found in $PATH": unknown""""
I have tried to execute all the below commands
docker run -it exec 962f5d99458c
docker run -it 962f5d99458c
docker exec -it 962f5d99458c /bin
docker exec -it 75f6cb44f9e1
docker run --rm -ti 962f5d99458c sh
docker run --rm -ti 962f5d99458c /bin
docker exec --rm -ti 962f5d99458c /bin
docker exec -ti 962f5d99458c /bin
docker exec -ti 962f5d99458c \bin
docker exec -it 75f6cb44f9e1 bash
but no luck..... can you please help me here.
What the error says is that the startup command is invalid because the image has no (or it's not on $PATH) certain executable in it (bash in this case). The absence of bash is adequate for certain images (e.g. based on Alpine Linux or scratch) but if there is any shell at all, you can use sh:
# create a container from image and get straight into shell
docker run -it <image> sh
# or start a container in background, then get into shell
docker run -d <image>
docker exec -it <container> sh
If the image has no shell in it, then the only way to launch it, is by using the binary of the application the image supposed to run. Normally it works out of the box, unless you've overridden ENTRYPOINT and/or CMD in Dockerfile or by run arguments. The default startup arguments can be found with docker inspect:
docker image inspect nginx | jq '.[0].Config.Entrypoint'
docker image inspect nginx | jq '.[0].Config.Cmd'
In case you absolutely sure that the executable is there but still get the error, using an absolute path might help (e.g. /bin/bash instead of simply bash).

command in docker run and inside docker not behaving similarly

I have script that load a database into RAM and print the adress of the first data into a file (db_REGISTER), and I want to run it inside a docker. This script works fine when it is launched inside a bash after launching the docker with -it
$ docker run -it --env-file $FILE -v $wkp:/app dev_machine
$$ /app/scripts/loadBase.sh
db_REGISTER
<some random number>
However when I launch the same script with docker run directly, the script works but the adress printed is always 0, and I cannot use the database afterward.
$ docker run -it --env-file $FILE -v $wkp:/app dev_machine /app/scripts/loadBase.sh
db_REGISTER
0
Does that mean that the second command does not have access to a persistant adress in the RAM ? What should I do to correct that ?
EDIT : After some advice, I tried to tweak the --ipc setting. Using --ipc="host" made it work. I guess this was a problem of shared RAM
You could try to mount your file inside the Docker container before executing the command:
docker run -it --env-file $FILE -v $PWD/scripts:/t -w /t dev_machine loadBase.sh

Dockerfile CMD for taking bash commands from host

I've created a dockerfile with various compile and build tools. The goal of the dockerimage is to standardize our development tools, and make it easy and consistent for developing.
Everything is installed.
What I am stuck on, is how to make the docker container keep running, and be able to have a bash shell to that container so that I can run, for example, make etc. ?
If I use ENTRYPOINT /bin/bash my container exits immediately. How to keep the container running?
You should use the command at run time. You run your Docker container in interatice mode (-i) and set the command to "/bin/bash":
docker run -it myDockerImage myCommandToExecuteInteractively
For instance:
docker run -it myDocker /bin/bash
Here is a real life example:
a) Pulling the most basic image
docker pull debian:jessie-slim
b) Let's have a bash there:
docker run -it debian:jessie-slim /bin/bash
c) Enjoy:
A docker container will run as long as the CMD/Entrypoint from your Dockerfile takes.
You can run your Docker container in interactive mode using switch i
sudo docker run -it --entrypoint=/bin/bash <imagename>
Example : docker run -it --entrypoint=/bin/bash ubuntu:14.04
This will start an interactive shell in your container. Your container will exit as soon as you exit that shell.

How can I run docker container without entering into container

I have Dockefile
FROM centos:7
So I have no entrypoint in dockerfile.
Then I build it to image
sudo docker build -t my_container .
Then I start it.
sudo docker run -t my_container
And I get open tty to container
root#my_container_id/
If I start it without -t it stopped immidiately after start.
How can I run docker container without start tty and without entrypoint?
You can start your container in a detached mode:
docker run -it -d my_container
The -d option here means your container will run in "detached" mode, in the background.
If you want to attach the container and drop to a shell, you can use:
docker exec -it my_container /bin/bash
Note, if your container is based on an alpine image, you need to use sh, i.e.:
docker exec -it my_container /bin/sh
You can't do that. Your container lives if its main process is running, so you have to have a main process which is the process with PID 1 inside your container, and your container will be up if that process is running.

DockerFile : how to get bash command line after start?

This question is not duplicated, because I want to obtain an interactive shell without running with -it flags.
I'm moving first steps into Docker to create images only for internal use.
I start from this envirornment_full.df:
FROM ubuntu:16.04
ENTRYPOINT ["/bin/bash"]
I then build
docker rmi environment:full
docker build -t environment:full -f environment.df .
Then run
docker run environment:full
Running docker images -am I see my image
REPOSITORY TAG IMAGE ID CREATED SIZE
environment full aa91bbd39167 4 seconds ago 129 MB
So I run it
docker run environment:full
I see nothing happening ....
$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
5847c0a18f30 environment:full "/bin/bash" 21 seconds ago Exited (0) 20 seconds ago admiring_mirzakhani
Also
$ docker run environment:full -ti
bash: cannot set terminal process group (-1): Inappropriate ioctl for device
bash: no job control in this shell
root#aa768a585f33:/# exit
I'd like to have the ubuntu prompt, like if I was in a SSH connection. And this without user must enter -i or -tty flags.
How can I realize this?
bash won't run at all if stdin is closed. If you don't provide the -i flag, bash will simply exit immediately. So when you...
docker run environment:full
...bash exits immediately, and so your container exits. You would see it if you ran docker ps -a, which shows container that have stopped.
bash won't give you an interactive prompt if it's not attached to a tty. So if you were to run...
coerk run -i environment:full
...you would get a bash shell, but with no prompt, or job control, or other features. You need to provide -t for Docker to allocate a tty device.
You can't get what you want without providing both the -i and -t options on the command line.
An alternative would be to set up an image that runs an ssh daemon, and have people ssh into the container. Instead of behaving "like if I was in a SSH connection", it would actually be an ssh session.
Also, note that this:
docker run environment:full -ti
Is not the same as this:
docker run -it environment:full
The former will run bash -ti inside a container, while the latter passes the -i and -t options to docker run.

Resources