The Docker Doc says:
docker run = docker create + docker start
Consider the following command:
docker run -a stdin -a stdout image_id bash
I cannot think out the commands using docker create + docker start equivalent to docker run -a stdin -a stdout image_id bash.
docker create -a stdin -a stdout image_id bash is ok.
If I use docker start -a container_id, then all of stdin, stdout, and stderr will be attached. But I just want stdin and stdout to be attached.
If I use docker start container_id, then nothing will be attached.
Both cases are not what docker run -a stdin -a stdout image_id bash exactly did.
I just wonder, is it really true that docker run = docker create + docker start ?
Related
Any commands hang terminal inside docker container.
I login in container with docker exec -t php-zts /bin/bash
And then print any elementary command (date, ls, cd /, etc.)
Command hang
When I press ctrl+c I going back to host machine.
But, if I run any command without container - it's work normally
docker exec -t php-zts date
Wed Jan 26 00:04:38 UTC 2022
tty is enabled in docker-compose.yml
docker system prune and all cleanups can not help me.
I can't identify the problem and smashed my brain. Please help :(
The solution is to use the flag -i/--interactive with docker run. Here is a relevant section of the documentation:
--interactive , -i Keep STDIN open even if not attached
You can try to run your container using -i for interactive and -t for tty which will allow you to navigate and execute commands inside the container
docker run -it --rm alpine
In the other hand you can run the container with docker run then execute commands inside that container like so:
tail -f /dev/null will keep your container running.
-d will run the command in the background.
docker run --rm -d --name container1 alpine tail -f /dev/null
or
docker run --rm -itd --name container1 alpine sh # You can use -id or -td or -itd
This will allow you to run commands from inside the container.
you can choose sh, bash, or any other shell you prefer.
docker exec -it container1 alpine sh
From docker run --help:
-t, --tty Allocate a pseudo-TTY
Very nice. I use that to get color output automatically. The problem is, it also redirects stderr to stdout.
Without -t, stderr isn't redirected, which is what I want:
$ docker run ubuntu ls /aaaaaa
ls: cannot access '/aaaaaa': No such file or directory
$ docker run ubuntu ls /aaaaaa 2> /dev/null
With -t, the error messages are redirected to stdout:
$ docker run -t ubuntu ls /aaaaaa
ls: cannot access '/aaaaaa': No such file or directory
$ docker run -t ubuntu ls /aaaaaa 2> /dev/null
ls: cannot access '/aaaaaa': No such file or directory
$ docker run -t ubuntu ls /aaaaaa > /dev/null
Why is that? Is there a way to still allocate a pseudo-TTY and keep stderr separate?
e.g. I want to run something like this in CI:
docker run ... my-command > out.json
while the logger shows warnings and error messages in color.
You need to give your command a shell session so your container can have its own output redirection and not rely on what docker runtime has setup.
This will print error message as usual:
docker run -t --rm busybox sh -c "ls /aaa"
This will not print error message to your allocated tty:
docker run -t --rm busybox sh -c "ls /aaa 2>/dev/null"
This will print the directory listing of /etc:
docker run -t --rm busybox sh -c "ls /etc 2>/dev/null"
My goal is to query Haproxy Runtime API using dockerized socat.
Below command returns empty result (/var/run/haproxy.stat is haproxy socket located on the docker host)
echo "-h" | docker run -a stdin -a stderr alpine/socat stdio /var/run/haproxy.stat
I've tried to add haproxy socket via volume, but the result is still empty.
echo "-h" | docker run -a stdin -a stderr -v /var/run/haproxy.stat:/var/run/haproxy.stat alpine/socat stdio /var/run/haproxy.stat
Command that worked is:
echo "-h" | docker run -i -a stdin -a stderr -a stdout -v /var/run/haproxy.stat:/var/run/haproxy.stat alpine/socat stdio /var/run/haproxy.stat
Needed to add -a stdout and -i to docker run
Following suggestion by BMitch" tried below command and it worked as well
echo "-h" | docker run -i -v /var/run/haproxy.stat:/var/run/haproxy.stat alpine/socat stdio /var/run/haproxy.stat
Why can't I attach to docker container of mariadb?
$ docker run --name mariadbtest -e MYSQL_ROOT_PASSWORD=mypass -d mariadb/server:10.1
<SKIPPED>
78cadba14946919a3d62e1c616e39e76508107d24c6c1b93da534d3a3eb09e2d
$ docker attach 78cadba14946
<HANG>
How to see parameters of this container?
Also I can't ssh to the container
$ docker inspect -f "{{ .NetworkSettings.IPAddress }}" mariadbtest
172.17.0.2
$ ssh 172.17.0.2
<HANG>
docker attach <container> attaches your terminal stream to the container stdout/stderr. If the container sends nothing to these streams - you will see nothing after attaching to it. Try executing some statement in the database and see if anything appears.
As for ssh, normally containers do not have ssh in it. Use docker exec -it <container> sh instead.
Why when i run the command
docker run ubuntu
without option '-it' is not possible to interact with the created container even when running command start with the -a -i options
docker start -a -i CONTAINER_ID
or when i run
docker start CONTAINER_ID
simply the container has the status "Exit (0) 4 seconds ago"
But when i run
docker run -it ubuntu
i can use bash shell of ubuntu using 'docker start -a -i'
When you run docker run without -it it's still running the container but you've not given it a command, so it finishes and exits.
If you try:
docker run ubuntu /bin/bash -c "echo 'hello'";
It'll run ubunu, then the command, and then finish because there is no reason for it to be kept alive afterwards.
-i is saying keep it alive and work within in the terminal (allow it to be interactive), but if you type exit, you're done and the container stops.
-t is showing the terminal of within the docker container (see: What are pseudo terminals (pty/tty)?)
-it allows you to see the terminal in the docker instance and interact with it.
Additionally you can use -d to run it in the background and then get to it afterwards.
Ex:
docker run -it -d --name mydocker ubuntu;
docker exec -it mydocker /bin/bash;
TLDR is -it allows you connect a terminal to interactively connect to the container.
If you run docker run --help, you can find the details about docker run options.
$ docker run --help
Usage: docker run [OPTIONS] IMAGE [COMMAND] [ARG...]
Run a command in a new container
Options:
...
-i, --interactive Keep STDIN open even if not attached
...
-t, --tty Allocate a pseudo-TTY