๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Docker

04 Docker ์ปจํ…Œ์ด๋„ˆ ๊ด€๋ฆฌ

by The Future Engineer, Lucy 2024. 10. 22.
728x90
๋ฐ˜์‘ํ˜•

โ€ป ์ด ๊ธ€์€ '๋„์ปค ๊ต๊ณผ์„œ' ์ฑ…๊ณผ ๊ต์œก์„ ํ†ตํ•ด ๋“ค์€ ๋‚ด์šฉ, ๊ณต์‹ ๋ฌธ์„œ๋ฅผ ํ† ๋Œ€๋กœ ์ •๋ฆฌํ–ˆ์Šต๋‹ˆ๋‹ค.

์ปจํ…Œ์ด๋„ˆ ์‹คํ–‰ํ•˜๊ธฐ

docker container run [OPTIONS] IMAGE [COMMAND] [ARG...]
docker run

docker run ๋ช…๋ น์–ด๋Š” ์ปจํ…Œ์ด๋„ˆ ์‹œ์ž‘ ์‹œ ํ•„์š”ํ•˜๋‹ค๋ฉด ์ด๋ฏธ์ง€๋ฅผ pullํ•˜๊ณ  ๋ช…๋ น์–ด๋ฅผ ์ˆ˜ํ–‰ํ•œ๋‹ค.

์ฃผ์š” ์˜ต์…˜

Option Description
--name ์ปจํ…Œ์ด๋„ˆ์˜ ์ด๋ฆ„์„ ์ง€์ •.
-d, --detach ์ปจํ…Œ์ด๋„ˆ๋ฅผ ๋ฐฑ๊ทธ๋ผ์šด๋“œ์—์„œ ์‹คํ–‰.
-e, --env ํ™˜๊ฒฝ ๋ณ€์ˆ˜ ์„ค์ •.
--env-file ํ™˜๊ฒฝ ๋ณ€์ˆ˜๋ฅผ ์ €์žฅํ•œ ํŒŒ์ผ๋กœ ์„ค์ •.
--expose ํฌํŠธ ๋˜๋Š” ํฌํŠธ ๋ฒ”์œ„๋ฅผ ๋…ธ์ถœ.
-p, --publish ํ˜ธ์ŠคํŠธ์—๊ฒŒ ์ปจํ…Œ์ด๋„ˆ ํฌํŠธ๋ฅผ ๊ณต๊ฐœ.
--rm ์ปจํ…Œ์ด๋„ˆ๊ฐ€ ์ข…๋ฃŒ๋˜๋ฉด ์ž๋™์œผ๋กœ ์‚ญ์ œ.
-i, --interactive STDIN์„ ํ™œ์„ฑํ™”.
-t, --tty pseudo-TTY๋ฅผ ํ• ๋‹น.
-v, --volume ๋ณผ๋ฅจ ์„ค์ •.

์ž์„ธํ•œ ์˜ต์…˜์€ docker run | Docker Docs๋ฅผ ์ฐธ๊ณ ํ•˜์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค.

Expose์™€ Publish ์ฐจ์ด์ 

  • ๋‘˜ ๋‹ค ์ปจํ…Œ์ด๋„ˆ ์™ธ๋ถ€ ์ ‘๊ทผ์„ ํ—ˆ์šฉํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉ.
  • expose๋Š” ๋™์ผํ•œ network๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ์ปจํ…Œ์ด๋„ˆ์—์„œ๋งŒ ์ ‘๊ทผ์ด ๊ฐ€๋Šฅ.
  • publish๋Š” host์˜ ํฌํŠธ์™€ ๋ฐ”์ธ๋“œํ•˜์—ฌ, host ๋˜๋Š” ์™ธ๋ถ€์—์„œ๋„ ์ ‘๊ทผ์ด ๊ฐ€๋Šฅ.

์ฃผ์˜์‚ฌํ•ญ

  • [COMMAND]๋Š” Entrypoint์˜ ์ถ”๊ฐ€ ์ธ์ž๋กœ ํ™œ์šฉ๋ฉ๋‹ˆ๋‹ค.

COMMAND ์˜ˆ์‹œ

$ docker run ubuntu:22.04 /bin/bash -c "whoami && date"
Unable to find image 'ubuntu:22.04' locally
22.04: Pulling from library/ubuntu
6414378b6477: Pull complete
Digest: sha256:0e5e4a57c2499249aafc3b40fcd541e9a456aab7296681a3994d631587203f97
Status: Downloaded newer image for ubuntu:22.04
root # whoami ๋ช…๋ น์–ด
Mon Oct 21 13:48:18 UTC 2024 # date ๋ช…๋ น์–ด

--publish ์˜ˆ์‹œ

$ docker run -d -p 81:80 nginx:latest # 81์ด ์™ธ๋ถ€๋กœ ๋…ธ์ถœ๋˜๋Š” ํฌํŠธ
Unable to find image 'nginx:latest' locally
latest: Pulling from library/nginx
a480a496ba95: Pull complete
f3ace1b8ce45: Pull complete
11d6fdd0e8a7: Pull complete
f1091da6fd5c: Pull complete
40eea07b53d8: Pull complete
6476794e50f4: Pull complete
70850b3ec6b2: Pull complete
Digest: sha256:28402db69fec7c17e179ea87882667f1e054391138f77ffaf0c3eb388efc3ffb
Status: Downloaded newer image for nginx:latest
6737921148d546597b43ed1b4bc7c8157174be13bf942b1c99994cba4d17bf6b

$ curl localhost:81 # powershell ์‚ฌ์šฉ ์‹œ ๋ช…๋ น์–ด ์ถฉ๋Œ๋กœ ์ธํ•œ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•˜๋ฏ€๋กœ curl.exe๋กœ ์‹คํ–‰.
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>

localhost:81๋กœ ์ ‘์†ํ•œ ๊ฒฐ๊ณผ

์ปจํ…Œ์ด๋„ˆ ๋ชฉ๋ก ๋ณด๊ธฐ

# ์ „์ฒด
docker container ls [OPTIONS]
# ์ถ•์•ฝํ˜•
docker container list
docker container ps
docker ps

์ปจํ…Œ์ด๋„ˆ ๋ชฉ๋ก์„ ๋ณด๋Š” ๋ช…๋ น์–ด์ž…๋‹ˆ๋‹ค.

์ฃผ์š” ์˜ต์…˜

Option Description
-a, --all ์ƒํƒœ์™€ ๊ด€๊ณ„์—†์ด ๋ชจ๋“  ์ปจํ…Œ์ด๋„ˆ๋ฅผ ํ‘œ์‹œ.
-f, --filter ์ง€์ •๋œ ์กฐ๊ฑด์— ๋งž๋Š” ์ปจํ…Œ์ด๋„ˆ๋งŒ ํ‘œ์‹œ.
-s, --size ์ „์ฒด ํŒŒ์ผ ์‚ฌ์ด์ฆˆ๋„ ํ‘œ๊ธฐ.

์ž์„ธํ•œ ์˜ต์…˜์€ docker ps | Docker Docs์„ ์ฐธ๊ณ ํ•˜์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค.

์ปจํ…Œ์ด๋„ˆ ๋กœ๊ทธ ํ™•์ธํ•˜๊ธฐ

docker container logs [OPTIONS] CONTAINER
docker logs

docker logs๋Š” ์‹คํ–‰ ์‹œ ์กด์žฌํ•˜๋Š” ๋กœ๊ทธ๋ฅผ ์ผ๊ด„ ๊ฒ€์ƒ‰ํ•˜๋Š” ๋ช…๋ น์–ด์ž…๋‹ˆ๋‹ค.

์ฃผ์š” ์˜ต์…˜

Option Default Description
-f, --follow   ๊ณ„์† ๋กœ๊ทธ ํ‘œ์‹œ.
-n, --tail all ๋งˆ์ง€๋ง‰ n๊ฐœ์˜ ๋กœ๊ทธ๋ฅผ ํ‘œ์‹œ.
-t, --timestamps   timestamp๋ฅผ ํ‘œ๊ธฐ.
--since   ํŠน์ • ์‹œ๊ฐ„ ์ดํ›„์— ๋ฐœ์ƒํ•œ ๋กœ๊ทธ๋งŒ ํ‘œ์‹œ.
--until   ํŠน์ • ์‹œ๊ฐ„ ์ด์ „์— ๋ฐœ์ƒํ•œ ๋กœ๊ทธ๋งŒ ํ‘œ์‹œ.

์˜ˆ์‹œ

$ docker logs was
/docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
/docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
/docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
10-listen-on-ipv6-by-default.sh: info: Getting the checksum of /etc/nginx/conf.d/default.conf
10-listen-on-ipv6-by-default.sh: info: Enabled listen on IPv6 in /etc/nginx/conf.d/default.conf
/docker-entrypoint.sh: Sourcing /docker-entrypoint.d/15-local-resolvers.envsh
/docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
/docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh
/docker-entrypoint.sh: Configuration complete; ready for start up
2024/10/21 14:24:01 [notice] 1#1: using the "epoll" event method
2024/10/21 14:24:01 [notice] 1#1: nginx/1.27.2
2024/10/21 14:24:01 [notice] 1#1: built by gcc 12.2.0 (Debian 12.2.0-14)
2024/10/21 14:24:01 [notice] 1#1: OS: Linux 5.15.153.1-microsoft-standard-WSL2
2024/10/21 14:24:01 [notice] 1#1: getrlimit(RLIMIT_NOFILE): 1048576:1048576
2024/10/21 14:24:01 [notice] 1#1: start worker processes
2024/10/21 14:24:01 [notice] 1#1: start worker process 30
2024/10/21 14:24:01 [notice] 1#1: start worker process 31
2024/10/21 14:24:01 [notice] 1#1: start worker process 32
2024/10/21 14:24:01 [notice] 1#1: start worker process 33

์ปจํ…Œ์ด๋„ˆ ์ ‘์† ๋ฐ ๋ช…๋ น์–ด ์‹คํ–‰

Attach

docker container attach [OPTIONS] CONTAINER
docker attach

ํ˜„์žฌ ์‹คํ–‰ ์ค‘์ธ ์ปจํ…Œ์ด๋„ˆ์˜ ํ„ฐ๋ฏธ๋„์˜ STDIN, STDOUT, STDERR์— ์ ‘๊ทผํ•ฉ๋‹ˆ๋‹ค.

ํ•ด๋‹น ๋ช…๋ น์–ด์˜ ์˜ต์…˜์€ docker container attach | Docker Docs๋ฅผ ์ฐธ๊ณ ํ•˜์‹œ๊ธธ ๋ฐ”๋ž๋‹ˆ๋‹ค.

์ฃผ์˜์‚ฌํ•ญ

  • ctrl+c๋ฅผ ํ†ตํ•ด ํ”„๋กœ์„ธ์Šค๋ฅผ ์ข…๋ฃŒํ•˜๊ฒŒ ๋˜๋ฉด ์ปจํ…Œ์ด๋„ˆ๋„ ์ข…๋ฃŒ๋จ.
  • ์ปจํ…Œ์ด๋„ˆ๋ฅผ ์ข…๋ฃŒํ•˜์ง€ ์•Š๊ณ  ์ ‘์†์„ ์ข…๋ฃŒํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ctrl+p+q๋ฅผ ์‚ฌ์šฉํ•ด์•ผ ํ•จ.

Exec

docker container exec [OPTIONS] CONTAINER COMMAND [ARG...]
docker exec

docker exec๋Š” ์‹คํ–‰ ์ค‘์ธ ์ปจํ…Œ์ด๋„ˆ์—์„œ ์ƒˆ ๋ช…๋ น์–ด๋ฅผ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ๋‹ค.

์ฃผ์˜์‚ฌํ•ญ

  • ๋ฉ”์ธ ํ”„๋กœ์„ธ์Šค๊ฐ€ ์‹คํ–‰๋˜๋Š” ๋™์•ˆ์—๋งŒ ๋ช…๋ น์–ด ์‹คํ–‰ ๊ฐ€๋Šฅ.
  • exec๋กœ ์‹คํ–‰ํ•œ ๋ช…๋ น์€ ์ปจํ…Œ์ด๋„ˆ ์žฌ์‹คํ–‰ ์‹œ ์‹คํ–‰๋˜์ง€ ์•Š์Œ.
  • ์ผ๋ถ€ ์ปจํ…Œ์ด๋„ˆ๋Š” bash, sh์™€ ๊ฐ™์€ shell์ด ์—†์„ ์ˆ˜ ์žˆ์Œ. ex) Scratch ์ด๋ฏธ์ง€, ...
  • -it ์˜ต์…˜์ด ์—†๋‹ค๋ฉด ์›๊ฒฉ์œผ๋กœ ๋ช…๋ น์–ด๋ฅผ ์‹คํ–‰ํ•˜๊ณ  ํ”„๋กœ์„ธ์Šค๊ฐ€ ์ข…๋ฃŒ๋จ.
  • exec๋กœ ์‹คํ–‰ํ•œ ๋ช…๋ น์–ด๋Š” working Directory๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์‹คํ–‰๋˜๋ฏ€๋กœ ํŒŒ์ผ ์‚ฌ์šฉ ์‹œ ์ฃผ์˜ ํ•„์š”.
$ docker exec was sh -c "pwd"
/
$ docker exec --workdir /bin was sh -c "pwd"
/bin

Attach์™€ Exec ์ฐจ์ด์ 

  Attach Exec
์ ‘๊ทผ ํ„ฐ๋ฏธ๋„ ์ปจํ…Œ์ด๋„ˆ์˜ STDIN, STDOUT, STDERR ์ƒˆ๋กœ์šด process์˜ ํ„ฐ๋ฏธ๋„
์ ‘์† ์ข…๋ฃŒ ๋ฐฉ๋ฒ• ctrl + p + q ctrl + c or ctrl + d(ctrl+c๋กœ ๋ฐฐ์› ์ง€๋งŒ ctrl+d๋งŒ ๋์Œ)
PID 1 (๋ฉ”์ธ ํ”„๋กœ์„ธ์Šค) != 1

์ปจํ…Œ์ด๋„ˆ ์ข…๋ฃŒ & ์‹œ์ž‘

# ์ข…๋ฃŒ
docker container stop [OPTIONS] CONTAINER [CONTAINER...]
docker stop
# ์‹œ์ž‘
docker container start [OPTIONS] CONTAINER [CONTAINER...]
docker start

docker stop๊ณผ docker start์— ๋Œ€ํ•œ ์ž์„ธํ•œ ๋‚ด์šฉ์€ ์•„๋ž˜ ๋งํฌ๋ฅผ ์ฐธ๊ณ ํ•˜์‹œ๊ธธ ๋ฐ”๋ž๋‹ˆ๋‹ค.

docker container stop | Docker Docs

docker container start | Docker Docs

์ฃผ์š”์‚ฌํ•ญ

  • Dockerfile์„ ํ†ตํ•ด ์‹คํ–‰๋˜์ง€ ์•Š์€ ํ”„๋กœ์„ธ์Šค๋“ค์€ ์ข…๋ฃŒ ํ›„ ์žฌ์‹œ์ž‘ํ•  ๊ฒฝ์šฐ ์‚ด์•„๋‚˜์ง€ ์•Š์Œ.

์ปจํ…Œ์ด๋„ˆ ์ค‘์ง€ & ์žฌ์‹คํ–‰

# ์ค‘์ง€
docker container pause CONTAINER [CONTAINER...]
docker pause
# ์žฌ์‹คํ–‰
docker container unpause CONTAINER [CONTAINER...]
docker unpause

์ฃผ์š”์‚ฌํ•ญ

  • ์žฌ์‹คํ–‰ํ•˜๋Š” ๊ฒฝ์šฐ ์ค‘์ง€๋˜์–ด์žˆ๋˜ ๋ชจ๋“  ํ”„๋กœ์„ธ์Šค๊ฐ€ ์žฌ์‹คํ–‰๋จ.

์ปจํ…Œ์ด๋„ˆ ์‚ญ์ œ

docker container rm [OPTIONS] CONTAINER [CONTAINER...]
docker container remove
docker rm

ํ•˜๋‚˜ ์ด์ƒ์˜ ์ปจํ…Œ์ด๋„ˆ๋ฅผ ์‚ญ์ œํ•ฉ๋‹ˆ๋‹ค.

docker rm $(docker ps -a -q -f status=exited)

์œ„ ๋ช…๋ น์–ด๋Š” ์ข…๋ฃŒ๋œ ๋ชจ๋“  ์ปจํ…Œ์ด๋„ˆ๋ฅผ ์‚ญ์ œํ•  ๋•Œ ์‚ฌ์šฉํ•˜๋ฉด ๋œ๋‹ค.

์ฃผ์š”์˜ต์…˜

Option Description
-f, --force SIGKILL์‹œ๊ทธ๋„์„ ์‚ฌ์šฉํ•˜์—ฌ ์‹คํ–‰ ์ค‘์ธ ์ปจํ…Œ์ด๋„ˆ๋ฅผ ์‚ญ์ œ.
-v, --volumes ์ปจํ…Œ์ด๋„ˆ์˜ anonymous volumes๋„ ํ•จ๊ป˜ ์‚ญ์ œ

์˜ˆ์‹œ

$ docker stop was
was
$ docker ps -a
CONTAINER ID   IMAGE                  COMMAND                  CREATED          STATUS                     PORTS                NAMES
5556aad7335a   nginx:latest           "/docker-entrypoint.…"   39 minutes ago   Exited (0) 2 seconds ago                        was
$ docker rm was
was
$ docker ps -a
CONTAINER ID   IMAGE                  COMMAND                  CREATED        STATUS          PORTS                 NAME

์ปจํ…Œ์ด๋„ˆ ๋ฆฌ์†Œ์Šค ์‚ฌ์šฉ๋Ÿ‰ ์กฐํšŒ

docker container stats [OPTIONS] [CONTAINER...]
docker stats

docker stats๋Š” ์‹คํ–‰ ์ค‘์ธ ์ปจํ…Œ์ด๋„ˆ์— ๋Œ€ํ•œ ์‹ค์‹œ๊ฐ„ ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

๋ฐ์ดํ„ฐ๋ฅผ ํ•˜๋‚˜ ์ด์ƒ์˜ ํŠน์ • ์ปจํ…Œ์ด๋„ˆ๋กœ ์ œํ•œํ•˜๋ ค๋ฉด ๊ณต๋ฐฑ์œผ๋กœ ๊ตฌ๋ถ„๋œ ์ปจํ…Œ์ด๋„ˆ ์ด๋ฆ„ ๋˜๋Š” ID ๋ชฉ๋ก์„ ์ง€์ •ํ•ฉ๋‹ˆ๋‹ค.

์ค‘์ง€๋œ ์ปจํ…Œ์ด๋„ˆ๋ฅผ ์ง€์ •ํ•  ์ˆ˜๋Š” ์žˆ์ง€๋งŒ ์ค‘์ง€๋œ ์ปจํ…Œ์ด๋„ˆ๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ˜ํ™˜ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.

์ฃผ์š”์˜ต์…˜

Option Description
-a, --all ๋ชจ๋“  ์ปจํ…Œ์ด๋„ˆ๋ฅผ ํ‘œ์‹œ.
--no-stream  ๊ฒฐ๊ณ ๋ฅผ ์ง€์†์ ์œผ๋กœ ์—…๋ฐ์ดํŠธํ•˜์ง€ ์•Š์Œ.

 

์ž์„ธํ•œ ๋‚ด์šฉ์€ docker container stats | Docker Docs๋ฅผ ์ฐธ๊ณ ํ•˜์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค.


์ด์ƒ์œผ๋กœ ๋„์ปค ์ปจํ…Œ์ด๋„ˆ ๊ด€๋ฆฌ์— ๋Œ€ํ•œ ๊ธฐ๋ณธ์ ์ธ ๋ช…๋ น์–ด ์ •๋ฆฌ๋ฅผ ๋งˆ์น˜๊ฒ ์Šต๋‹ˆ๋‹ค.

๋‹ค์Œ์€ ๋„์ปค ์ด๋ฏธ์ง€ ์ƒ์„ฑ ๋ฐ ์—…๋กœ๋“œ์— ๋Œ€ํ•ด ์„ค๋ช…ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

728x90
๋ฐ˜์‘ํ˜•

'Docker' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

03 Docker ์ด๋ฏธ์ง€ ๊ด€๋ฆฌ  (1) 2024.09.28
02 Docker๋ž€?  (1) 2024.09.28
01 Docker ์„ค์น˜ํ•˜๊ธฐ  (1) 2024.09.28