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 ๋ก ์คํํด์ผ ํจ
- ์๋ฅผ ๋ค์ด ubuntu image ๋ฅผ
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 |
---|
๋๊ธ