λ³Έλ¬Έ λ°”λ‘œκ°€κΈ°
πŸ§‘πŸ»‍πŸ’» 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

λŒ“κΈ€