ttyd icon indicating copy to clipboard operation
ttyd copied to clipboard

Keep docker container alive after browser window closed

Open raquelhortab opened this issue 4 years ago • 13 comments

Hi, I have read issues in which the problem was that the containers kept alive after the browser window was closed. However, I am having the opposite problem. If I run docker run -it --rm -p 7681:7681 tsl0922/ttyd and open localhost:7681 on the browser, when I close the window the container dies. Is this really the expected behaviour? It feels weird that the container itself does (obviously) not need a client to run but once the client disconnects it dies. I'd like to know if there is a workaround to that as I'd like to keep it alive and just kill it myself afterwards.

The logs are:

docker run -it --rm -p 7681:7681 tsl0922/ttyd
[2021/05/13 08:15:19:8307] N: ttyd 1.6.3-1720642 (libwebsockets 4.1.6-)
[2021/05/13 08:15:19:8308] N: tty configuration:
[2021/05/13 08:15:19:8309] N:   start command: bash
[2021/05/13 08:15:19:8309] N:   close signal: SIGHUP (1)
[2021/05/13 08:15:19:8309] N:   terminal type: xterm-256color
[2021/05/13 08:15:19:8310] N: LWS: 4.1.6-, loglevel 7
[2021/05/13 08:15:19:8310] N: NET SRV H1 WS IPV6-off
[2021/05/13 08:15:19:8627] N:  Using foreign event loop...
[2021/05/13 08:15:19:8631] N:  Listening on port: 7681
[2021/05/13 08:15:23:1349] N: HTTP /token - 172.17.0.1
[2021/05/13 08:15:23:2471] N: WS   /ws - 172.17.0.1, clients: 1
[2021/05/13 08:15:23:3212] N: started process, pid: 9
[2021/05/13 08:15:31:0896] N: WS closed from 172.17.0.1, clients: 0
[2021/05/13 08:15:31:0897] N: killing process, pid: 9
== uv_read failed with error -5: i/o error
[2021/05/13 08:15:31:0913] N: process killed with signal 1, pid: 9

Thanks!

raquelhortab avatar May 13 '21 08:05 raquelhortab

The flag --rm automatically remove the container when it exits. Remove this flag for your need. Refer docker run documentation here

tsadarsh avatar May 14 '21 09:05 tsadarsh

The flag --rm automatically remove the container when it exits. Remove this flag for your need. Refer docker run documentation here

The exact same thing happens without the flag

docker run -it -p 7681:7681 tsl0922/ttyd
[2021/05/14 09:58:55:3533] N: ttyd 1.6.3-1720642 (libwebsockets 4.1.6-)
[2021/05/14 09:58:55:3533] N: tty configuration:
[2021/05/14 09:58:55:3533] N:   start command: bash
[2021/05/14 09:58:55:3533] N:   close signal: SIGHUP (1)
[2021/05/14 09:58:55:3533] N:   terminal type: xterm-256color
[2021/05/14 09:58:55:3533] N: LWS: 4.1.6-, loglevel 7
[2021/05/14 09:58:55:3533] N: NET SRV H1 WS IPV6-off
[2021/05/14 09:58:55:3608] N:  Using foreign event loop...
[2021/05/14 09:58:55:3609] N:  Listening on port: 7681
[2021/05/14 09:59:01:8150] N: HTTP / - 172.17.0.1
[2021/05/14 09:59:02:2633] N: HTTP /token - 172.17.0.1
[2021/05/14 09:59:02:3236] N: WS   /ws - 172.17.0.1, clients: 1
[2021/05/14 09:59:02:3775] N: started process, pid: 9
[2021/05/14 09:59:07:5659] N: WS closed from 172.17.0.1, clients: 0
[2021/05/14 09:59:07:5660] N: killing process, pid: 9
== uv_read failed with error -5: i/o error
[2021/05/14 09:59:07:5665] N: process killed with signal 1, pid: 9

raquelhortab avatar May 14 '21 10:05 raquelhortab

Was the container removed? Did you check docker ps -a?

tsadarsh avatar May 14 '21 10:05 tsadarsh

https://user-images.githubusercontent.com/19463905/118256353-ad9cac00-b4ad-11eb-9a78-b105053b13fc.mp4

Yes it did

raquelhortab avatar May 14 '21 10:05 raquelhortab

oh wait, forgot the -a, there a lot of things there, just a sec

raquelhortab avatar May 14 '21 10:05 raquelhortab

the container is there but dead:

affa5af674bf   tsl0922/ttyd   "/usr/bin/tini -- tt…"   13 seconds ago   Exited (139) 2 seconds ago             hardcore_sammet

raquelhortab avatar May 14 '21 10:05 raquelhortab

the container is there but dead:

affa5af674bf   tsl0922/ttyd   "/usr/bin/tini -- tt…"   13 seconds ago   Exited (139) 2 seconds ago             hardcore_sammet

Yes, you can restart the container using docker start <container_name>. The container name here is hardcore_sammet.

tsadarsh avatar May 14 '21 11:05 tsadarsh

So you have to restart the docker again every time you want to connect to it? No workaround to keep it up? Feels weird that this is the intended behaviour. I have not checked the code closely but by searching where the logs come from I saw there's a line in protocol.c that checks if there are 0 clients and exits, but it doesn't seem like it even gets there.

Anyway, thanks for looking into it!

raquelhortab avatar May 14 '21 12:05 raquelhortab

One more try. Are you running docker ps -a after quitting ttyd? If yes, try running it in another shell or run ttyd in background. To run process in background, append &:

docker run -it -p 7681:7681 tsl0922/ttyd &

tsadarsh avatar May 14 '21 14:05 tsadarsh

Sorry for the delay, running it in background (or in another shell) does not change anything. Using docker start I can start it again but if I leave the page I have to start the docker again manually. It does feel like an unexpected behaviour, I'll have to find a workaround then, thank you very much for spending time trying to help though!!!

raquelhortab avatar May 17 '21 07:05 raquelhortab

Hi, I have the same issue recently. It was working on version 1.6.3 (tag name), container keeps on after closing the page. However, when I use newer commits, say 07ad8beeb7fe, the container just closes as the OP said.

[2021/05/27 10:16:45:3502] N: ttyd 1.6.3-07ad8be (libwebsockets 4.1.6-)
[2021/05/27 10:16:45:3503] N: tty configuration:
[2021/05/27 10:16:45:3503] N:   start command: lnav -f /lnav_config /debug
[2021/05/27 10:16:45:3504] N:   close signal: SIGHUP (1)
[2021/05/27 10:16:45:3504] N:   terminal type: xterm-256color
[2021/05/27 10:16:45:3504] N:   custom index.html: /customHtmlPath
[2021/05/27 10:16:45:3504] N: LWS: 4.1.6-, loglevel 7
[2021/05/27 10:16:45:3504] N: NET SRV H1 WS IPV6-off
[2021/05/27 10:16:45:3633] N:  Using foreign event loop...
[2021/05/27 10:16:45:3635] N:  Listening on port: 7681
[2021/05/27 10:16:45:9598] N: HTTP / - 10.255.253.43
[2021/05/27 10:16:47:0717] N: HTTP /token - 10.255.253.43
[2021/05/27 10:16:47:1233] N: WS   /ws - 10.255.253.43, clients: 1
[2021/05/27 10:16:47:1346] N: started process, pid: 8
[2021/05/27 10:17:00:3727] N: WS closed from 10.255.253.43, clients: 0
[2021/05/27 10:17:00:3728] N: killing process, pid: 8
== uv_read failed with error -5: i/o error
[2021/05/27 10:17:00:3809] N: process killed with signal 1, pid: 8

After bisecting commits, I found this issue happens after commit d6921b23.

git bisect start
# bad: [07ad8beeb7fed1467ec3ddc51fa08cb001738362] build(deps-dev): bump webpack-cli from 4.6.0 to 4.7.0 in /html (#668)
git bisect bad 07ad8beeb7fed1467ec3ddc51fa08cb001738362
# good: [3b174dac3b81072e7ac5259c844f300d5af2e955] Bump to 1.6.3
git bisect good 3b174dac3b81072e7ac5259c844f300d5af2e955
# bad: [52adaba9e10b0372b3664ccda35e6e89c4b81dec] pty: use uint16_t for resize params
git bisect bad 52adaba9e10b0372b3664ccda35e6e89c4b81dec
# good: [62c340c6a479efff29eceb986e918bdc14a7cb2a] build(deps-dev): bump webpack-cli from 4.4.0 to 4.5.0 in /html (#596)
git bisect good 62c340c6a479efff29eceb986e918bdc14a7cb2a
# good: [0b5fd5d5fa78303561f21c310c73cc3996e94c16] build(deps-dev): bump html-webpack-plugin from 4.5.1 to 4.5.2 in /html (#609)
git bisect good 0b5fd5d5fa78303561f21c310c73cc3996e94c16
# good: [4d51d580080144bd72647fa93a6c50dacabaac17] build(deps-dev): bump mini-css-extract-plugin in /html (#613)
git bisect good 4d51d580080144bd72647fa93a6c50dacabaac17
# good: [ffea419f02a7e49d93217c89ad8b98f5d6416cd6] build(deps-dev): bump husky from 5.1.1 to 5.1.2 in /html (#616)
git bisect good ffea419f02a7e49d93217c89ad8b98f5d6416cd6
# good: [231fe32f595d107a49d95338d16d3028d377dabb] build(deps-dev): bump css-loader from 5.1.0 to 5.1.1 in /html (#617)
git bisect good 231fe32f595d107a49d95338d16d3028d377dabb
# bad: [d6921b235d1b97a57664f04e694b646f91fda5e6] protocol: add conpty support
git bisect bad d6921b235d1b97a57664f04e694b646f91fda5e6
# first bad commit: [d6921b235d1b97a57664f04e694b646f91fda5e6] protocol: add conpty support

That bad commit looks like a rewrite on pty process. Maybe there is some logic missing during the rewite.

Should we take this behavioral change as regression?

solarispika avatar May 27 '21 11:05 solarispika

I also found that after that commit, I need to specifically run docker with environment variable TERM set, i.e., -e TERM=xterm. Or else, lnav, program I ran under ttyd, will complain: Error opening terminal: unknown.

solarispika avatar May 27 '21 12:05 solarispika

I also have the same issue , and I have to change the command like this docker run --restart=always -itd -h ttyd --name ttyd -p 7681:7681 tsl0922/ttyd

After I close the browser,the container will automatic restart QQ截图20210703093231

ddzzfg avatar Jul 03 '21 01:07 ddzzfg