Docker - When "tty: true" is necessary in docker-compose.yml
Tags: docker, php, node
Published: 2022-04-10 14:27:03

## About "tty: true" in docker-compose.yml
In docker-compose.yml, I wasn't sure how "tty: true" meant to work, so I looked it up.  

**What is “tty: true” in docker-compose.yml? | by KeisukeKoshikawa | Medium**  
<https://kossy0701.medium.com/what-is-tty-true-in-docker-compose-yml-47a72891aee2>  

> If you write “tty: true” in the docker-compose.yml, you will be able to “keep the container running”.  

He said, when the containers are started by "docker-compose up -d", the containers terminate immediately.  
You need an option called "tty: true" to keep the containers running.  

"tty" seems to be the same as the Linux command.  

> What is tty?  
> A pseudo terminal (also known as a tty or a pts ) connects a user’s “terminal” with the stdin and stdout stream, commonly (but not necessarily) though a shell such as bash .  
> … In the case of docker, you’ll often use -t and -i together when you run processes in interactive mode, such as when starting a bash shell.  

This explanation is more simple.  

**tty【コマンド】とは｜「分かりそう」で「分からない」でも「分かった」気になれるIT用語辞典**
**What is tty?｜IT dictionary that makes you feel like you understand even if you don't**  
<https://wa3.i-3-i.info/word11668.html>  

> 「tty」コマンドはコマンドライン上から接続端末のデバイスファイル名を取得する際に使います。  
> (The "tty" command is used to get the device file name of the connected terminal from the command line.)  

I understand that it is necessary to keep the containers running, but the syntax is questionable, why "enable standard output" setting is used to made the containers run persistent.  
It would have been better to just use "permanent: true" or something like that.  

## Actual behavior: Some containers accessible without "tty: true"
"Then, to access the containers (so that they don't terminate immediately), write 'tty: true'."  
But, there are some containers that can actually be accessed without such a syntax in docker-compose.yml.  
 
For example:  

### docker-compose.yml
```yaml
version: "3"

services:
  php:
    image: php:8.0-fpm

  node:
    image: node:16-slims
```

running command  
```
docker-compose up -d
```

login command   
```
docker-compose exec php bash
docker-compose exec node bash
```

It can access php, but fails to access node.  
Both don't have "tty: true".
I thought if the above explanation is correct, both processes terminate as soon as the containers started, and both are inaccessible.  
But apparently not.

I mean, I remember that nginx, mysql, and redis don't have the syntax "tty: true".

## the case "tty: true" is required.
I looked into this and found this information.  

**docker-composeで tty: true にしてデーモンがいないコンテナを動かし続ける**
**Set "tty: true" in docker-compose to keep running docker containers without daemon.**  
<https://imagawa.hatenadiary.jp/entry/2019/07/31/065830>  

> そういえばmysqlとかredisみたいなデーモンで動かすプロセス作ってないから何も継続する仕事がなくて終わっちゃうんだろうなと思って調べると、tty: true という設定値をdocker-compose.ymlに書けるらしい。  
> (I haven't created a process that runs on a daemon like mysql or redis, so I thought it would terminate. Since there are no task left.
>  To avoid it, I found out about the setting value 'tty: true' in docker-compose.yml.)  

**docker-compose up したコンテナを起動させ続ける方法**
**How to keep running containers using 'docker-compose up'**  
<https://qiita.com/sekitaka_1214/items/2af73d5dc56c6af8a167>  

> ポート待受とかしていないコンテナをdocker-compose upで起動するとコンテナがすぐ終了してしまうと思います。  
> (If you run containers without waiting port using 'docker-compose up,' the containers will terminate immediately.)  

If the daemon is in the container, it keeps running even if you don't write anything special in docker-compose.yml.  
Then, I feel that "adding 'tty: true' to make it permanent" is a tricky approach, rather than a straightforward method.  

***If the container have daemon, "tty: true" is unnecessary in docker-compose.yml.***
***Otherwise, "tty: true" is necessary in docker-compose.yml.***  

So, the above docker-compose.yml requires "tty: true" for node.  

### docker-compose.yml
```yaml
version: "3"

services:
  php:
    image: php:8.0-fpm

  node:
    image: node:16-slims
    tty: true
```

By the way, "node:16-slims" is Node.js light package.  

If you need further information, please visit [official website](https://hub.docker.com/_/node).  

> node:<version>-slim  
> This image does not contain the common packages contained in the default tag and only contains the minimal packages needed to run node.Unless you are working in an environment where only the node image will be deployed and you have space constraints, we highly recommend using the default image of this repository.  

## Extra
I ran it with "tty: false".  
Here is the docker-compose.yml.  

### docker-compose.yml
```yaml
version: "3"

services:
  php:
    image: php:8.0-fpm
    tty: false

  node:
    image: node:16-slim
    tty: false
```

### Result
 * php - accessible
 * node - inaccessible


Even if "tty: false" is added, it does not seem that the access route is cut off in the case of the containers with daemons.  

I thought it natural behavior is "when the containers are started, the containers terminate immediately." or "The containers are running, but can't access."
But it wasn't.  

Docker is too mysterious for us.  


## Reference
Japanese version
https://kaki-note-02.netlify.app/2022/04/07/