Docker Community Forums

Share and learn in the Docker community.

Docker start with -a

Hi,

What is a use case of the “attach”, -a, option of docker container start ?

It says “Attach STDOUT/STDERR and forward signals”, but I can’t find a use case.

But I understand -i, for “Attach container’s STDIN”.

Can I have an example of using “-a” ?

I use docker on ubuntu.

Thanks

Anyone knows how I can use this option ?

Basicly -a is to attach a terminal device to the container.

Lets have some examples:

This will result in “test” beeing printed inside the container, but since stdout is not attached, you will not see anything in your terminal:

me@docker:~$ echo "test" | docker run --rm -i -a stdin alpine cat -
75f178eb4111829c78ada5b2ca0d5d7375ea19274caefb0cf7d73538bedfb8f4
me@docker:~$

This will result in “test” beeing printed inside the container to the attached stdout, and therefor be visible in your terminal

me@docker:~$ echo "test" | docker run --rm -i -a stdin -a stdout alpine cat -
test
me@docker:~$

… and even be passed to follow up pipes:

me@docker:~$ echo "test" | docker run --rm -i -a stdin -a stdout alpine cat - | grep test
test
me@docker:~$

This will result in an error of the ls command, because the test folder does not exist, but it will not be rendered, since errors are printed to stderr, which is not attached.

me@docker:~$ echo "test" | docker run --rm -i -a stdin -a stdout alpine ls $(cat -)
me@docker:~$

This will result in an error of the ls command, because the test folder does not exist, but this time stderr is attached and you will be able to see it in your terminal

me@docker:~$ echo "test" | docker run --rm -i -a stdin -a stdout -a stderr alpine ls $(cat -)
ls: test: No such file or directory
me@docker:~$

This finaly works, because the bin folder exists

me@docker:~$ echo "bin" | docker run --rm -i -a stdin -a stdout -a stderr alpine ls $(cat -)
arch
ash
base64
bbconfig
busybox
cat
chgrp
chmod
chown
conspy
cp
date
dd
df
dmesg
dnsdomainname
dumpkmap
echo
ed
egrep
false
fatattr
fdflush
fgrep
fsync
getopt
grep
gunzip
gzip
hostname
ionice
iostat
ipcalc
kbd_mode
kill
link
linux32
linux64
ln
login
ls
lzop
makemime
mkdir
mknod
mktemp
more
mount
mountpoint
mpstat
mv
netstat
nice
pidof
ping
ping6
pipe_progress
printenv
ps
pwd
reformime
rev
rm
rmdir
run-parts
sed
setpriv
setserial
sh
sleep
stat
stty
su
sync
tar
touch
true
umount
uname
usleep
watch
zcat
me@docker:~$

I hope this helps to clearify the usage.

2 Likes

Thank you !
Exactly the kind of examples what I was looking for !

Start one or more stopped containers

Usage

docker start [OPTIONS] CONTAINER [CONTAINER...]

For example uses of this command, refer to the examples section below.

Options

Name, shorthand Default Description
--attach , -a Attach STDOUT/STDERR and forward signals
--checkpoint experimental (daemon)
Restore from this checkpoint
--checkpoint-dir experimental (daemon)
Use a custom checkpoint storage directory
--detach-keys Override the key sequence for detaching a container
--interactive , -i Attach container’s STDIN