๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿง‘๐Ÿป‍๐Ÿ’ป Develop/Docker

Docker Detach vs Attach mode (Background vs Foreground)

by dev-jaesoon 2023. 5. 8.

Docker ๋ฅผ ์‹คํ–‰ ์‹œ์—๋Š” ๋ชจ๋“œ๋ฅผ ์„ค์ •ํ•˜๋Š” ์˜ต์…˜์ด 2๊ฐ€์ง€ ์žˆ๋‹ค.

  • Detach Mode (Background ์‹คํ–‰)
    • docker command : start
    • docker option : --detach / -d
    • docker-compose options : -d
  • Attach Mode (Foreground ์‹คํ–‰)
    • docker command : run
    • docker option : --attach / -a
    • docker-compose options : ์—†์Œ

Background vs Foreground
์ฐธ๊ณ ์ž๋ฃŒ : https://www.baeldung.com/linux/foreground-background-process
A process that connects to the terminal is called a foreground job. A job is said to be in the foreground because it can communicate with the user via the screen and the keyboard.

On the other hand, a process that disconnects from the terminal and cannot communicate with the user is called a background job. If the background job requires interaction with the user, it will stop and wait until establishing a connection to the terminal.

We can place the jobs that do not require interaction from the user as they run (like sorting a large file) in the background. This allows the user to access the terminal and continue to work, instead of waiting for a long job to finish:

Detach Mode (container background)

  • Docker ๋ฅผ Service ํ˜•ํƒœ๋กœ ์‹คํ–‰ํ•  ๋•Œ ์‚ฌ์šฉ๋˜๋Š” ๋ฐฉ์‹์œผ๋กœ terminal ์„ ์ข…๋ฃŒํ•ด๋„ ์ง€์†์ ์œผ๋กœ process ์˜ ์‹คํ–‰์ด ์œ ์ง€๋œ๋‹ค.
  • container ๋‚ด์—์„œ program ์ด foreground ๋กœ ์‹คํ–‰ ๋˜์–ด ์žˆ์–ด์•ผ ํ•˜๊ณ , ๊ทธ๋ ‡์ง€ ์•Š์œผ๋ฉด container ๋Š” ์ž๋™์œผ๋กœ ์ข…๋ฃŒ๋œ๋‹ค.
    • ์˜ˆ๋ฅผ ๋“ค์–ด ubuntu image ๋ฅผ -d ์˜ต์…˜์œผ๋กœ ์‹คํ–‰ํ•  ๊ฒฝ์šฐ ๋ฐ”๋กœ ์ข…๋ฃŒ๋จ
    • ๋ณ„๋„์˜ Server application ๋“ฑ์„ foreground ๋กœ ์‹คํ–‰ํ•ด์•ผ ํ•จ

How to run

  • ์ƒ์„ฑ๋œ container ๋ฅผ docker start command ๋กœ ์‹คํ–‰ํ•  ๊ฒฝ์šฐ background ์—์„œ ์‹คํ–‰๋œ๋‹ค.
  • container ์ƒ์„ฑ ์‹œ ์‚ฌ์šฉํ•˜๋Š” docker run command ์— -d ์˜ต์…˜์„ ๋ถ™์ž„์œผ๋กœ์จ background ์—์„œ ์‹คํ–‰๋˜๊ฒŒ ํ•  ์ˆ˜ ์žˆ๋‹ค.

Container log

  • log ์˜ต์…˜์„ ์‚ฌ์šฉํ•˜์—ฌ ์ถœ๋ ฅ๋˜๋Š” log ๋ฅผ ํ™•์ธ ๊ฐ€๋Šฅํ•˜๋‹ค
      $ docker log {container_id or container_name}
  • ํ•˜์ง€๋งŒ, ๊ธฐ๋ณธ์ ์œผ๋กœ Standard stream ์ด ํ™œ์„ฑํ™”๋˜์ง€ ์•Š์€ ์ƒํƒœ๋กœ container ๋ฅผ ์‹คํ–‰ํ•˜๊ธฐ ๋•Œ๋ฌธ์—, user input ์ด ๋ถˆ๊ฐ€๋Šฅํ•˜๋‹ค.

Access to container

  • ์ด๋ฏธ Detach Mode ๋กœ ์‹คํ–‰๋˜์–ด ์žˆ๋Š” container ์˜ ๊ฒฝ์šฐ, -it ์˜ต์…˜์„ ํ†ตํ•ด Standard stream ์„ ํ™œ์„ฑํ™”ํ•˜์—ฌ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.
    $ docker exec -it {container_id or container_name} /bin/bash 
  • ํ˜น์€ ์‹คํ–‰ ์‹œ, -a ์˜ต์…˜์„ ์‚ฌ์šฉํ•˜๋ฉด Foreground ๋กœ ์‹คํ–‰๋˜๊ธฐ ๋•Œ๋ฌธ์—, terminal ์„ ํ†ตํ•œ Standard stream ์ด ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•˜๋‹ค.

Attach Mode (container foreground)

  • ํ˜„์žฌ ์‹คํ–‰ ์ค‘์ธ terminal ์— container ์˜ ๋™์ž‘ ์ƒํƒœ๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ, container ์˜ ๋™์ž‘์„ ๋ณด๊ธฐ ์‰ฝ๊ธฐ ๋•Œ๋ฌธ์— test, debug ์‹œ์— ์‚ฌ์šฉ๋œ๋‹ค.
  • terminal ๊ณผ process ๊ฐ€ ๋ถ€๋ชจ - ์ž์‹์œผ๋กœ ์—ฐ๊ฒฐ๋˜๊ธฐ ๋•Œ๋ฌธ์—, terminal ์˜ ์—ฐ๊ฒฐ์ด ์ข…๋ฃŒ๋˜๋ฉด container ๋„ ์ข…๋ฃŒ๋˜๊ฒŒ ๋œ๋‹ค.
  • foreground ๋กœ ์‹คํ–‰ํ•˜๋ฉด์„œ ์‹คํ–‰ ์ƒํƒœ๋ฅผ ์œ ์ง€ํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด, tmux ๋“ฑ์œผ๋กœ ๋”ฐ๋กœ terminal process ๋ฅผ ๊ด€๋ฆฌํ•  ํ•„์š”๊ฐ€ ์žˆ๋‹ค.
  • docker-compose ๋„ ๊ธฐ๋ณธ์ ์œผ๋กœ attach mode ๋กœ ์‹คํ–‰๋œ๋‹ค.

'๐Ÿง‘๐Ÿปโ€๐Ÿ’ป Develop > Docker' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

Docker RUN vs ENTRYPOINT vs CMD  (0) 2023.05.09

๋Œ“๊ธ€