does docker ignore the -ti flags if -d is used? - docker

I ran this command:
docker run -d -ti foo
it works, but I realized that I probably forgot to remote the -ti part.
I assume that docker ignores those flags if -d is used, does anyone know?
It seems like -ti and -d would contradict each other?

It still sets up the input filehandle, and allocates a pseudo tty for the container. If the app inside the container attempts to read from stdin, it will hang waiting on input rather than exit immediately or fail. Later on, you can attach to that process. E.g.
$ docker run -dit --name test-dit busybox sh
f0e057ce47e03eb227aacb42e3a358b14fa5d8b26ad490fcec7cbfe0cd3cce73
$ docker run -d --name test-d busybox sh
4f2583d3380953f328b702c88884fbe55f16c44bce13dbccc00c4bb81f3270f2
$ docker container ls -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
4f2583d33809 busybox "sh" 5 seconds ago Exited (0) 4 seconds ago test-d
f0e057ce47e0 busybox "sh" 14 seconds ago Up 13 seconds test-dit
$ docker container attach test-dit
/ #
/ # ls
bin dev etc home proc root sys tmp usr var
/ # exit
$ docker container ls -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
4f2583d33809 busybox "sh" 22 seconds ago Exited (0) 21 seconds ago test-d
f0e057ce47e0 busybox "sh" 31 seconds ago Exited (0) 2 seconds ago test-dit
In the first container ls command, you can see the shell without the -it option immediately exited, while the one with -it was available to connect and run commands.

It does not ignore -ti.
The -ti part means it enables direct user interaction, and the -d part means it detaches the container the moment it gets started. So, in order to actually interact with it, you'll have to do
?> docker attach foo
So, yes, it might not be very useful, but it neither causes an impossible situation, nor one you cannot get out of.

Related

Docker container ps or ls doesn't show running Containers

Here is an example on my CLI:
$ docker pull hello-world
$ docker run hello-world
It shows empty when ls/ps
$ docker container ls
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
$ docker container ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
It shows up only when I use -a but it'd suggest the containers are actually not actively running.
$ docker container ls -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
96c3e42ae83a hello-world "/hello" 11 seconds ago Exited (0) 8 seconds ago jovial_rosalind
dcaed0ba308f registry "/entrypoint.sh /etc…" 42 minutes ago Created 0.0.0.0:5000->5000/tcp registry
Have I missed something?
Looks like your container exited right away. Is it meant to be interactive? (like running bash, or needing any user interaction?) if it is, you should run it like this to attach a terminal to it:
docker run -ti hello-world
If not, what does your hello program do? If it is not something that will keep running, then the container will stop whenever it exits.
Also keep in mind that, unless you pass docker run the -d/--detach flag, it will only return after the container has stopped - so if it returns right away, that means your container has already stopped.
You may want to use one of these to get a bash shell in the container to debug your problem:
docker run -ti hello-world bash
docker run --entrypoint bash -ti hello-world
To understand the difference between them, you can read the documentation on ENTRYPOINT and COMMAND.

What does COMMAND in 'docker ps' mean?

docker ps or docker container ls returns an overview of all running containers. The meaning of all columns is clear to me, except one. What does the column 'COMMAND' mean?
This is the command which is passed to the container.
$ docker run -d busybox top
$docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
3eca7c034b21 busybox "top" 6 seconds ago Up 5 seconds recursing_dirac
If you check above, top is the command which has been passed to the busybox container and that's what it's showing in the docker ps -a.
It's the command passed to docker run <image> [command].
$ docker run -d ubuntu sleep 60
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS
f0c9cd92a938 ubuntu "sleep 60" 3 seconds ago Up 1 second
If no command was specified there then it's the CMD from the Dockerfile. In ubuntu's case that would be CMD ["/bin/bash"]:
$ docker run -di ubuntu
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS
9cd752ee86f4 ubuntu "/bin/bash" 4 seconds ago Up 2 seconds

Docker ssh, back to container showing unexpected results

I'm studying the Docker documentation, but I'm having a hard time understanding the concept of creating a container, ssh, and ssh back.
I created a container with
docker run -ti ubuntu /bin/bash
Then, it starts the container and I can run commands. docker ps gives me
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
0e37da213a37 ubuntu "/bin/bash" About a minute ago Up About a minute keen_sammet
The issue is after I exit the container I can't ssh back.
I tried docker attach that gives me Error: No such container and I tried docker exec -ti <container>/bin/bash that gives me the same message Error: No such container
How do I run and ssh back to the container?
When you exit the bash process, the container exits (in general, a container will exit when the foreground process exits). The error message you are seeing is accurately describing the situation (the container is no longer running).
If you want to be able to docker exec into a container, you will want to run some sort of persistent command. For example, if you were to run:
docker run -ti -d --name mycontainer ubuntu bash
This would start a "detached" container. That means you've started bash, but it's just hanging around doing nothing. You could use docker exec to start a new process in this container:
$ docker exec -it mycontainer ps -fe
UID PID PPID C STIME TTY TIME CMD
root 1 0 0 16:28 pts/0 00:00:00 bash
root 17 0 0 16:28 pts/1 00:00:00 ps -fe
Or:
$ docker exec -it mycontainer bash
There's really no reason to start bash as the main process in this case, since you're not interacting with it. You can just as easily run...
docker run -ti -d --name mycontainer ubuntu sleep inf
...and the behavior would be the same.
The most common use case for all of this is when your docker run command starts up some sort of persistent service (like a web server, or a database server, etc), and then you use docker exec to perform diagnostic or maintenance tasks.
The docker attach command will re-connect you with the primary console of a detached container. In other words, if we return to the initial example:
docker run -ti -d --name mycontainer ubuntu bash
You could connect to that bash process (instead of starting a new one) by running:
docker attach mycontainer
At this point, exit would cause the container to exit.
First, you don't ssh to a docker container (unless you have a sshd process in that container). But you can execute a command with docker exec -ti mycontainer bash -l
But you can exec a command only on running container. If the container exited already you must use another approach : create an image from the container and run a new one.
Here is an example. First I create a container and create a file within then I exit it.
$ docker run -ti debian:9-slim bash -l
root#09f889e80153:/# echo aaaaaaaaaa > /zzz
root#09f889e80153:/# cat /zzz
aaaaaaaaaa
root#09f889e80153:/# exit
logout
As you can see the container is exited (Exited (0) 24 seconds ago)
$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
09f889e80153 debian:9-slim "bash -l" 45 seconds ago Exited (0) 24 seconds ago thirsty_hodgkin
So I create a new image with docker commit
$ docker commit 09f889e80153 bla
sha256:6ceb88470326d2da4741099c144a11a00e7eb1f86310cfa745e8d3441ac9639e
So I can run a new container that contains previous container content.
$ docker run -ti bla bash -l
root#479a0af3d197:/# cat zzz
aaaaaaaaaa

Create new image based on standard one

I have installed Docker and have running some Ubuntu image with command:
sudo docker run ubuntu
I would like to create some text file on it and find it next time the same image will run. How to achieve that?
UPD.
Got problems with attaching to docker.
I have running docker
docker ps -a
aef01293fdc9 ubuntu "/bin/bash" 6 hours ago Up 6 hours priceless_ramanujan
Since it is Up mode, I suppose I don't need to execute command:
docker start priceless_ramanujan
So, I run command attach
docker attach priceless_ramanujan
And got nothing in output while command not returns.
Why I can't get to container's bash?
Simple example:
$ docker run -it ubuntu
root#4d5643e8c1a8:/# echo "test" > test.txt
root#4d5643e8c1a8:/# cat test.txt
test
root#4d5643e8c1a8:/# exit
exit
$ docker run -it ubuntu
root#cdb44750bffc:/# cat test.txt
cat: test.txt: No such file or directory
root#cdb44750bffc:/#
docker run image_name
This command creates and starts a new container based on the provided image_name. If a name is not set for the container, a random one is generated and assigned by docker. In the above example 2 containers were created based on ubuntu.
with docker ps -a we can see that modest_jennings and optimistic_leakey are the random names created:
$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
cdb44750bffc ubuntu "/bin/bash" About a minute ago Exited (1) 4 seconds ago optimistic_leakey
4d5643e8c1a8 ubuntu "/bin/bash" 2 minutes ago Exited (0) 2 minutes ago modest_jennings
cat test.txt failed the 2nd time because the file didn't exist. The container started from a "clean" ubuntu image.
Actually, we created test.txt inside modest_jennings only.
docker start container_name
This command starts a stopped container. So, in our case, the file is still there:
$ docker start modest_jennings
modest_jennings
$ docker attach modest_jennings
root#4d5643e8c1a8:/# cat test.txt
test
root#4d5643e8c1a8:/#
docker commit container_name image_name
This command is to create a new image, so that you can use it later and run containers based on that image. Continuing our example...
$ docker commit modest_jennings my_ubuntu
sha256:a4357f37153ac0b94e37315595f1a3b540538283adc3721df4d4e3b39bf8334f
$ docker run -it my_ubuntu
root#2e38616d532a:/# cat test.txt
test
root#2e38616d532a:/#
If you want a custom image, you can create a Dockerfile
`FROM ubuntu:16.04
ADD ./test.txt /tmp/`
after you can build it docker build -t ubuntu:custom .
and finally run your custom image docker run --name myubuntu ubuntu:custom sleep 3000
You can check your file with docker exec -it myubuntu /bin/bash and more /tmp/test.txt

How to start a docker container (ubuntu image)

How to stat a docker container. I had created it using
docker run -d -P -v /Users/bsr:/usr/local/users --name test ubuntu
I do have virtual box guest addition installed, and mounting works. But, I am not sure why I can't I keep the shell running.
bsr[~/tmp/web] $ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
cf620ff6c36a ubuntu:latest "/bin/bash" 2 hours ago Exited (0) 2 minutes ago test
8213c8d49842 nginx:latest "nginx" 3 hours ago Up About an hour 0.0.0.0:49154->80/tcp web
bsr[~/tmp/web] $ docker start test
test
bsr[~/tmp/web] $ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
cf620ff6c36a ubuntu:latest "/bin/bash" 2 hours ago Exited (0) 2 seconds ago test
8213c8d49842 nginx:latest "nginx" 3 hours ago Up About an hour 0.0.0.0:49154->80/tcp web
bsr[~/tmp/web] $
Edit:
it may be because the command (/bin/bash ??) finishes immediately. When I tried,
docker run -d -P -v /Users/bsr:/usr/local/users --name test5 ubuntu /bin/bash -c "while true; do echo Hello world; sleep 1; done"
I could get the terminal. But isn't there any way to just start a container and get to the terminal ??
If you want to run an interactive process, you should use the -i (keep stdin open in case you detach) and -t (allocate a pseudo-tty) flags:
docker run -it ubuntu
You can look at the docs for more information on those flags and their usage.
You can start by using simple command.
docker run "CONTAINER_ID"

Resources