Windows Boot2Docker leaving a container and returning to B2D - terminal

If I am running Boot2Docker on windows 7, and I connect to a container by running a command like docker run -it ubuntu:14.04 /bin/bash, how can I "disconnect" from the container and return the prompt to the boot2docker images command prompt?
Ideally I'd like to know both how to kill the container, and how to push the container into the background and then return it to the foreground later.

What you seem to be looking for is the -d option for the docker run command which starts the container on background. You can then access the container easily with command like this (found from Timur Fayzrakhmanov's answer on AskUbuntu)
sudo docker exec -i -t <your_id> bash
where you replace the your_id with something like 665b4a1e17b6.

Related

Is there a way to reintroduce yourself to a docker container running bash?

Let's say I have one docker container that when run it finishes with bash. So after docker run I have a bash terminal and I can put commands.
If for some reason I lost contact to this terminal (it can happen) and then in another terminal, I do docker ps, I can see the container running. However I am not "inside" the bash of this docker
Right now what I do is to delete this and then run docker run again but is there a way I can rejoin the terminal of this running container?
You can "detach" from a container without closing the program by Ctrl-P + Ctrl-Q
docker run -it -d --entrypoint sh busybox
docker attach $container_id
/ # #inside container, use Ctrl-p Ctrl-q to detach

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.

docker run ubuntu on mac and nothing happens

i'm on mac running:
docker is configured to use the default machine with IP 192.168.99.100
For help getting started, check out the docs at https://docs.docker.com
my-mac:mydir$ docker run ubuntu /bin/bash
my-mac:mydir$
am i doing something wrong? shouldn't I get into the ubuntu shell?
By running docker run ubuntu /bin/bash, docker create a randomly-named container from the image ubuntu and runs a bash without stdin, stdout nor stderr then bash exits (right after being started).
Try at least to set a tty and interactive mode (aka foreground mode):
docker ps -a
# if not exited, stop it first
docker stop <container_id>
# remove the container which cannot be used
docker rm <container_id>
# let's try again
docker run -it --rm --name=test ubuntu bash
As commented by physincubus:
'-it' is the bit that makes it interactive,
'--rm' removes the container when you exit (so if you want to be able to exit for detach and reattach later, do not do this), and
'--name' allows you to name the container more explicitly in case you want to run multiple instances of the same container
Run it with following command
docker run -it ubuntu /bin/bash
Then you will get bash prompt of ubuntu container

how to modify files in a container using a script

I am trying to run a container and modify certain files in it. I am trying to do this using a script. If I use:
docker run -i -t <container> <image>, it is giving me
STDERR: cannot enable tty mode on non tty input
If I use:
docker run -d <container> <image> bash, the container is not starting.
Is there anyway to do this?
Thanks
Run the docker image in background using:
docker run -d <image>:<version>
Check running docker containers using:
docker ps
If there is only one container running you can use below command to attach to a running docker container and use bash to browser files/directories inside container:
docker exec -it $(docker ps -q) bash
You can then modify/edit any file you want and restart the container.
To stop a running container:
docker stop $(docker ps -q)
To run a stopped container:
docker start -ia $(docker ps -lq)
So to start off, the -i -t is for an interactive tty mode for interacting with the container. If you are invoking this in a script then it's likely that this won't work as you expect.
This is not really the way containers are meant to be used. If it is a permanent change, you should be rebuilding the image and using that for the container.
However, if you want to make changes to files that are reflected in the container, you could consider using volumes to mount directories from the host into the container. This would look something like:
docker run -v /some/host/dir:/some/container/dir -d container
At this point anything you change within /some/host/dir will be within the container at /some/container/dir. You can then make your changes with a script on the host, without having to invoke the docker cli.

Docker. How to get bash\ssh inside runned container (run -d)?

I want to ssh or bash into runned docker container. Please, see example:
$ sudo docker run -d webserver
webserver is clean image from ubuntu:14.04
$ sudo docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
665b4a1e17b6 webserver:latest /bin/bash ... ... 22/tcp, 80/tcp loving_heisenberg
now I want to get something like this (go into runned container):
$ sudo docker run -t -i webserver (or maybe 665b4a1e17b6 instead)
$ root#665b4a1e17b6:/#
Previously I used Vagrant so I want to get behavior similar to vagrant ssh. Please, could anyone help me?
After the release of Docker version 1.3, the correct way to get a shell or other process on a running container is using the docker exec command. For example, you would run the following to get a shell on a running container:
docker exec -it myContainer /bin/bash
You can find more information in the documentation.
The answer is docker attach command.
For information see: https://askubuntu.com/a/507009/159189

Resources