12

When I start a container as following:

docker run --name test -itd busybox

I can attach to it with:

docker attach test

I can now execute commands, such as ls, and see the results. Great!

Now, I want to start my container via a docker-compose.yml instead. I tried with:

version: "3.7"
services:
  busybox:
    image: busybox
    tty: true

With this, when I attach to the container again with docker attach <container id>, I seem to be able to "connect" to it, but I don't see any output when executing a command (for example ls).

Why is that? How can I fix this?

3 Answers3

8

You have to replicate the -i and -t flags. with tty yo did the -t. For the -i flag, add stdin_open to your docker-compose.yaml.

stdin_open: true
tty: true
Burak Serdar
  • 46,455
  • 3
  • 40
  • 59
  • Oh, right! I wrongly assumed that `tty: true` was the equivalent of `-it`. I did not really realize that `-it` actually contains two flags. –  May 01 '20 at 21:57
  • Scratching my head for a while on this one, I kept going down the wrong rabbit holes, but this was the only answer that worked. Thanks! – geerlingguy Apr 12 '22 at 13:05
  • @geerlingguy, just in case: https://stackoverflow.com/a/40026942/5113030 (*Confused about Docker -t option to Allocate a pseudo-TTY*...); https://stackoverflow.com/q/35459652/5113030 (*When would I use --interactive without --tty in a Docker container?*...); https://stackoverflow.com/q/47753047/5113030 (*Docker - Detached and Interactive?*...) – Artfaith Sep 11 '22 at 11:35
1

Maybe try docker exec -it <container id> bash because docker attach "only" attaches your terminal’s standard input, output, and error.

Tony Stark
  • 2,318
  • 1
  • 22
  • 41
  • 1
    I don't want to start a new shell, I want to attach to the running process. –  May 01 '20 at 21:53
0

Another slightly more mnemonic option is to use docker compose logs --follow. You can specify or exclude specific services with --attach and --no-attach respectively.

This does not actually help with this specific problem, because it only allows to read program output and not send input, but since this is the first thing I found when searching for my problem and it's reasonably related, I'll still put this here in case someone else finds it useful.

Fynn
  • 303
  • 1
  • 11