๐ŸŒฑ Infra/Container_Docker

[Docker-Basic (3)] ๋„์ปค ๊ธฐ๋ณธ ๋ช…๋ น์–ด ์ตํžˆ๊ธฐ :D

mini_world 2020. 9. 28. 13:16
๋ชฉ์ฐจ ์ ‘๊ธฐ


 

์ด๋ฒˆ ์‹ค์Šต์—์„œ Docker ์ปจํ…Œ์ด๋„ˆ์˜ ๊ธฐ๋ณธ ์‚ฌ์šฉ๋ฒ•์„ ์‹ค์Šตํ•ฉ๋‹ˆ๋‹ค. ๐Ÿ˜

Docker ์ปจํ…Œ์ด๋„ˆ์˜ Status์™€ ๋ผ์ดํ”„์‚ฌ์ดํด(=๋ช…๋ น์–ด)์„ ์•„๋ž˜ ์ฐธ๊ณ  ์‚ฌ์ง„์—์„œ ํ™•์ธํ•  ์ˆ˜์žˆ์Šต๋‹ˆ๋‹ค.
* Docker ์ปจํ…Œ์ด๋„ˆ์˜ 5๊ฐ€์ง€ Status : created, running, stopped, paused, deleted

์ด๋ฏธ์ง€ ์ถœ์ฒ˜ : https://github.com/jwilder/docker-gen

์ด์ œ ๊ฐ ๋ช…๋ น์–ด๋ฅผ ํ•˜๋‚˜์”ฉ ์‹คํ–‰ ํ•ด๋ณด๋ฉด์„œ ์‹ค์Šตํ•ด๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค :)


1. ์ปจํ…Œ์ด๋„ˆ ์ƒ์„ฑ ๋ฐ ํ™•์ธ

   1-1) Create ( pull + create )

Create ๋ช…๋ น์–ด๋Š” ์ด๋ฏธ์ง€๋ฅผ ๊ฐ€์ ธ์˜ค๊ณ , ์ปจํ…Œ์ด๋„ˆ๋ฅผ ์ƒ์„ฑํ•˜๋Š” ๋ช…๋ น์–ด ์ž…๋‹ˆ๋‹ค. 
Create ๋ช…๋ น์–ด๋ฅผ ์‚ฌ์šฉํ•  ๊ฒฝ์šฐ ์ปจํ…Œ์ด๋„ˆ๋Š” ์ƒ์„ฑ๋˜์ง€๋งŒ, Start๋ฅผ ๋ณ„๋„๋กœ ํ•ด์ฃผ์–ด์•ผ ์ปจํ…Œ์ด๋„ˆ๊ฐ€ ์‹คํ–‰๋ฉ๋‹ˆ๋‹ค.
# Docker container create ๋ช…๋ น์–ด๋ฅผ ํ†ตํ•ด httpd ์ปจํ…Œ์ด๋„ˆ๋ฅผ ์ƒ์„ฑํ•ด๋ด…์‹œ๋‹ค :)

[root@docker /]# docker container create --name webserver -p 80:80 httpd
Unable to find image 'httpd:latest' locally
latest: Pulling from library/httpd
d121f8d1c412: Pull complete 
9cd35c2006cf: Pull complete 
b6b9dec6e0f8: Pull complete 
fc3f9b55fcc2: Pull complete 
802357647f64: Pull complete 
Digest: sha256:5ce7c20e45b407607f30b8f8ba435671c2ff80440d12645527be670eb8ce1961
Status: Downloaded newer image for httpd:latest
64c1174446d8c95377f214c90d242442a29fa821708be29e42c49b0ad50828d7

์ด๋ฏธ์ง€๋ฅผ ํ™•์ธํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. 

[root@docker /]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
httpd               latest              417af7dc28bc        12 days ago         138MB

  #docker container ls -a ๋ช…๋ น์–ด๋ฅผ ํ†ตํ•ด ์ปจํ…Œ์ด๋„ˆ์˜ ํ˜„์žฌ ์ƒํƒœ๋ฅผ ํ™•์ธํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.
STATUS๋ฅผ ํ™•์ธํ•ด๋ณด๋ฉด Running ์ƒํƒœ๊ฐ€ ์•„๋‹Œ Created ์ƒํƒœ๋กœ ๋˜์–ด์žˆ๋Š”๊ฒƒ์„ ๋ณผ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
์ฆ‰, ์ปจํ…Œ์ด๋„ˆ๋ฅผ ์ƒ์„ฑํ–ˆ์ง€๋งŒ ์‹คํ–‰์ค‘์€ ์•„๋‹™๋‹ˆ๋‹ค.

[root@docker /]# docker container ls -a
CONTAINER ID        IMAGE               COMMAND              CREATED             STATUS              PORTS               NAMES
64c1174446d8        httpd               "httpd-foreground"   19 seconds ago      Created                                 webserver

์ปจํ…Œ์ด๋„ˆ๋ฅผ ์ด์ œ ์‹œ์ž‘ํ•ด๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

[root@docker /]# docker container start webserver
webserver

์ปจํ…Œ์ด๋„ˆ ์ƒํƒœ๋ฅผ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค. ์ด์ œ Status(์ƒํƒœ)๊ฐ€ UP์ž…๋‹ˆ๋‹ค. :)

[root@docker /]# docker container ls -a
CONTAINER ID        IMAGE               COMMAND              CREATED              STATUS              PORTS                NAMES
64c1174446d8        httpd               "httpd-foreground"   About a minute ago   Up 4 seconds        0.0.0.0:80->80/tcp   webserver

 

 1-2) run ( pull + create + start ) 

Run ๋ช…๋ น์–ด๋Š” ์ด๋ฏธ์ง€๋ฅผ pullํ•˜๊ณ  ์ปจํ…Œ์ด๋„ˆ ์ด๋ฏธ์ง€๋ฅผ createํ•˜๊ณ , startํ•ฉ๋‹ˆ๋‹ค :)
์œ„์˜ Create๋ช…๋ น์–ด์™€ ๋‹ฌ๋ฆฌ RUN ๋ช…๋ น์–ด๋Š” ๋ณ„๋„๋กœ ์ปจํ…Œ์ด๋„ˆ๋ฅผ ์‹คํ–‰์‹œํ‚ค์ง€ ์•Š์•„๋„ ์ปจํ…Œ์ด๋„ˆ๊ฐ€ ์‹คํ–‰๋ฉ๋‹ˆ๋‹ค.

์ž, ๋จผ์ € run ๋ช…๋ น์–ด๋กœ nginx๋ฅผ ์‹œ์ž‘ํ•ด๋ด…์‹œ๋‹ค.

[root@docker /]# docker container run --name nginx -d -p 8080:80 nginx
Unable to find image 'nginx:latest' locally
latest: Pulling from library/nginx
d121f8d1c412: Already exists 
ebd81fc8c071: Pull complete 
655316c160af: Pull complete 
d15953c0e0f8: Pull complete 
2ee525c5c3cc: Pull complete 
Digest: sha256:c628b67d21744fce822d22fdcc0389f6bd763daac23a6b77147d0712ea7102d0
Status: Downloaded newer image for nginx:latest
45580502d3b874b1c844d0c9b48bcf10b4ab93f989d70d28b573f70826229fa9

๋„์ปค ์ปจํ…Œ์ด๋„ˆ ๋ชฉ๋ก์„ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค.
์ด๋ฒˆ์—๋Š” ๋ณ„๋„๋กœ Start ํ•˜์ง€ ์•Š์•˜์ง€๋งŒ ์ปจํ…Œ์ด๋„ˆ๊ฐ€ ์‹คํ–‰๋˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.

[root@docker /]# docker container ls -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                  NAMES
45580502d3b8        nginx               "/docker-entrypoint.…"   6 seconds ago       Up 5 seconds        0.0.0.0:8080->80/tcp   nginx
64c1174446d8        httpd               "httpd-foreground"       5 minutes ago       Up 3 minutes        0.0.0.0:80->80/tcp     webserver

 

2. ์ปจํ…Œ์ดํ„ฐ ์ค‘์ง€ ๋ฐ ์žฌ์‹คํ–‰

์ด๋ฒˆ์—๋Š” ์ปจํ…Œ์ด๋„ˆ๋ฅผ ์ค‘์ง€ํ•˜๊ณ  ๋‹ค์‹œ ์‹œ์ž‘ํ•˜๋Š” ๋ช…๋ น์–ด๋ฅผ ์‚ฌ์šฉํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

์ž, ์ง€๊ธˆ ๋‘๊ฐœ์˜ ์ปจํ…Œ์ด๋„ˆ๊ฐ€ ์‹คํ–‰๋˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค :)

[root@docker /]# docker container ls -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                  NAMES
45580502d3b8        nginx               "/docker-entrypoint.…"   6 seconds ago       Up 5 seconds        0.0.0.0:8080->80/tcp   nginx
64c1174446d8        httpd               "httpd-foreground"       5 minutes ago       Up 3 minutes        0.0.0.0:80->80/tcp     webserver

์ด์ œ nginx ์ปจํ…Œ์ด๋„ˆ๋ฅผ ์ค‘์ง€ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

[root@docker /]# docker container stop nginx
nginx

์ปจํ…Œ์ด๋„ˆ ์ƒํƒœ๋ฅผ ๋‹ค์‹œํ•œ๋ฒˆ ํ™•์ธํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.
nginx ์ปจํ…Œ์ด๋„ˆ STATUS๊ฐ€ Exited ์ƒํƒœ์ž…๋‹ˆ๋‹ค.

[root@docker /]# docker container ls -a
CONTAINER ID        IMAGE               COMMAND                  CREATED              STATUS                     PORTS                NAMES
45580502d3b8        nginx               "/docker-entrypoint.…"   About a minute ago   Exited (0) 2 seconds ago                        nginx
64c1174446d8        httpd               "httpd-foreground"       7 minutes ago        Up 5 minutes               0.0.0.0:80->80/tcp   webserver

์ด์ œ nginx ์ปจํ…Œ์ด๋„ˆ๋ฅผ ๋‹ค์‹œ ์‹œ์ž‘ ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

[root@docker /]# docker container restart nginx
nginx

์ปจํ…Œ์ด๋„ˆ  ์ƒํƒœ๋ฅผ ๋‹ค์‹œํ•œ๋ฒˆ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค.
์ด์ œ nginx ์ปจํ…Œ์ด๋„ˆ์˜ Status๊ฐ€ UP์ƒํƒœ๊ฐ€ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.

[root@docker /]# docker container ls -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                  NAMES
45580502d3b8        nginx               "/docker-entrypoint.…"   2 minutes ago       Up 1 second         0.0.0.0:8080->80/tcp   nginx
64c1174446d8        httpd               "httpd-foreground"       7 minutes ago       Up 5 minutes        0.0.0.0:80->80/tcp     webserver

 

3. ์ปจํ…Œ์ด๋„ˆ ์ผ์‹œ์ค‘์ง€

์ด๋ฒˆ์—๋Š” nginx ์ปจํ…Œ์ด๋„ˆ๋ฅผ ์ผ์‹œ์ค‘์ง€(pause)ํ•ด๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

[root@docker /]# docker container pause nginx
nginx

nginx ์ปจํ…Œ์ด๋„ˆ์˜ Status๊ฐ€ UP (Paused)์ธ ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 

[root@docker /]# docker container ls -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                  PORTS                  NAMES
45580502d3b8        nginx               "/docker-entrypoint.…"   5 minutes ago       Up 3 minutes (Paused)   0.0.0.0:8080->80/tcp   nginx
64c1174446d8        httpd               "httpd-foreground"       10 minutes ago      Up 9 minutes            0.0.0.0:80->80/tcp     webserver

์ด๋ฒˆ์—๋Š” ์ผ์‹œ์ค‘์ง€๋ฅผ ํ’€๊ฒ ์Šต๋‹ˆ๋‹ค.

[root@docker /]# docker container unpause nginx
nginx

nginx ์ปจํ…Œ์ด๋„ˆ์˜ Status๊ฐ€ UP์ธ ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 

[root@docker /]# docker container ls -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                  NAMES
45580502d3b8        nginx               "/docker-entrypoint.…"   5 minutes ago       Up 3 minutes        0.0.0.0:8080->80/tcp   nginx
64c1174446d8        httpd               "httpd-foreground"       11 minutes ago      Up 9 minutes        0.0.0.0:80->80/tcp     webserver

 

 

4. ์ปจํ…Œ์ด๋„ˆ ๋‚ด๋ถ€ ์ •๋ณด ํ™•์ธํ•˜๊ธฐ

์ปจํ…Œ์ด๋„ˆ์˜ ๋‚ด๋ถ€์ •๋ณด๋ฅผ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋Š” ๋ช…๋ น์–ด๋ฅผ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค.

[root@docker /]# docker container inspect nginx
[
    {
        "Id": "45580502d3b874b1c844d0c9b48bcf10b4ab93f989d70d28b573f70826229fa9",
        "Created": "2020-09-28T04:15:34.48069232Z",
        "Path": "/docker-entrypoint.sh",
        "Args": [
            "nginx",
            "-g",
            "daemon off;"
        ],
        "State": {
            "Status": "running",
            "Running": true,
            "Paused": false,
            "Restarting": false,
            "OOMKilled": false,
            "Dead": false,
            "Pid": 4048,
            "ExitCode": 0,
            "Error": "",
            "StartedAt": "2020-09-28T04:17:38.822151268Z",
            "FinishedAt": "2020-09-28T04:17:15.43676131Z"
        },
        
        -- ์ƒ๋žต --
        
]

 

5. ์ปจํ…Œ์ด๋„ˆ ์ ‘์† ๋ฐ ํŒŒ์ผ ๋ณ€๊ฒฝํ•˜๊ธฐ (index.html)

์ปจํ…Œ์ด๋„ˆ์˜ ๋ชฉ๋ก์„ ํ™•์ธํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.
์ด๋ฒˆ์—๋Š” nginx ์ปจํ…Œ์ด๋„ˆ์— ์ ‘์†ํ•ด์„œ index.html ํŒŒ์ผ์˜ ๋‚ด์šฉ์„ ์ˆ˜์ •ํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

[root@docker /]# docker container ls -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                  NAMES
45580502d3b8        nginx               "/docker-entrypoint.…"   9 minutes ago       Up 7 minutes        0.0.0.0:8080->80/tcp   nginx
64c1174446d8        httpd               "httpd-foreground"       15 minutes ago      Up 13 minutes       0.0.0.0:80->80/tcp     webserver

nginx ์ปจํ…Œ์ด๋„ˆ์— ์ ‘์†ํ•ฉ๋‹ˆ๋‹ค.

[root@docker /]# docker container exec -it nginx /bin/bash

์ปจํ…Œ์ด๋„ˆ์— ์ ‘์† ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.
์ด์ œ index.html ํŒŒ์ผ์— ์ƒˆ๋กœ์šด ๋‚ด์šฉ์„ ์ž…๋ ฅํ•˜๊ณ  ์ปจํ…Œ์ด๋„ˆ์—์„œ ๋‚˜์˜ต๋‹ˆ๋‹ค.

root@45580502d3b8:/# echo 'Docker Container - test' > /usr/share/nginx/html/index.html 
root@45580502d3b8:/# exit
exit

nginx http ์ถœ๋ ฅ ๊ฒฐ๊ณผ๋ฅผ ํ™•์ธํ•ด๋ด…์‹œ๋‹ค :)

[root@docker /]# curl 127.0.0.1:8080/index.html
Docker Container - test

 

6.  ์ปจํ…Œ์ด๋„ˆ ๋กœ๊ทธ ์ •๋ณด ํ™•์ธ

์ปจํ…Œ์ด๋„ˆ ์‹คํ–‰์‹œ ์˜ค๋ฅ˜๊ฐ€ ๋‚˜์„œ ๋กœ๊ทธ๋ฅผ ํ™•์ธํ•ด๋ด์•ผํ• ๋•Œ, ์–ด๋–ป๊ฒŒ ๋ณผ์ˆ˜์žˆ์„๊นŒ์š” ?
์ปจํ…Œ์ด๋„ˆ ๋‚ด๋ถ€์˜ ๋กœ๊ทธ๋ฅผ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค.

์ปจํ…Œ์ด๋„ˆ์˜ ๋ชฉ๋ก์„ ํ™•์ธํ•ด๋ด…์‹œ๋‹ค.
๋‘๊ฐœ์˜ ์ปจํ…Œ์ด๋„ˆ๊ฐ€ ์šด์˜์ค‘์ด๋‹ˆ, ๋‘๊ฐœ ๋ชจ๋‘ ๋กœ๊ทธ๋ฅผ ํ™•์ธํ•ด๋ด…์‹œ๋‹ค!

[root@docker /]# docker container ls -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                  NAMES
45580502d3b8        nginx               "/docker-entrypoint.…"   9 minutes ago       Up 7 minutes        0.0.0.0:8080->80/tcp   nginx
64c1174446d8        httpd               "httpd-foreground"       15 minutes ago      Up 13 minutes       0.0.0.0:80->80/tcp     webserver

๋จผ์ € nginx ์ปจํ…Œ์ด๋„ˆ์˜ ๋กœ๊ทธ๋ฅผ ํ™•์ธํ•ด๋ด…์‹œ๋‹ค.
์œ„์—์„œ index.html ํŽ˜์ด์ง€ ์š”์ฒญํ•œ๊ฒƒ๋„ ๋กœ๊ทธ๋กœ ๋‚จ์•„์žˆ๋„ค์š” :)

[root@docker /]# docker container logs -f nginx
/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: Getting the checksum of /etc/nginx/conf.d/default.conf
10-listen-on-ipv6-by-default.sh: Enabled listen on IPv6 in /etc/nginx/conf.d/default.conf
/docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
/docker-entrypoint.sh: Configuration complete; ready for start up
/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: error: IPv6 listen already enabled
/docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
/docker-entrypoint.sh: Configuration complete; ready for start up
172.17.0.1 - - [28/Sep/2020:04:26:56 +0000] "GET /index.html HTTP/1.1" 200 24 "-" "curl/7.29.0" "-"

์ด๋ฒˆ์—๋Š” webserver(httpd)๋กœ๊ทธ๋ฅผ ํ™•์ธํ•ด๋ด…๋‹ˆ๋‹ค.

[root@docker /]# docker container logs -f webserver
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 172.17.0.2. Set the 'ServerName' directive globally to suppress this message
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 172.17.0.2. Set the 'ServerName' directive globally to suppress this message
[Mon Sep 28 04:11:59.349361 2020] [mpm_event:notice] [pid 1:tid 139753489142912] AH00489: Apache/2.4.46 (Unix) configured -- resuming normal operations
[Mon Sep 28 04:11:59.349535 2020] [core:notice] [pid 1:tid 139753489142912] AH00094: Command line: 'httpd -D FOREGROUND'
209.17.96.42 - - [28/Sep/2020:04:33:31 +0000] "GET / HTTP/1.1" 200 45

 

7. ์ปจํ…Œ์ด๋„ˆ ์ƒํƒœ ์ •๋ณด ํ™•์ธ

๊ทธ๋Ÿผ ์ปจํ…Œ์ด๋„ˆ์˜ CPU Memory ์‚ฌ์šฉ๋Ÿ‰ ๋“ฑ์˜ ์‹ค์‹œ๊ฐ„ ์ •๋ณด๋Š” ์–ด๋–ป๊ฒŒ ํ™•์ธํ• ๊นŒ์š”?
stats ๋ช…๋ น์–ด๋กœ ์ƒํƒœ๋ฅผ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค, ํ™•์ธ ํ•ด๋ด…์‹œ๋‹ค :)

[root@docker /]# docker container stats

CONTAINER ID        NAME                CPU %               MEM USAGE / LIMIT     MEM %               NET I/O             BLOCK I/O           PIDS
45580502d3b8        nginx               0.00%               1.406MiB / 7.392GiB   0.02%               1.39kB / 682B       0B / 0B             2
64c1174446d8        webserver           0.00%               17.09MiB / 7.392GiB   0.23%               2.82kB / 1.53kB     0B / 0B             109

 

8. ์ปจํ…Œ์ด๋„ˆ ํ”„๋กœ์„ธ์Šค ๋ฐ ํฌํŠธ ํ™•์ธ

์ปจํ…Œ์ด๋„ˆ ๋ชฉ๋ก์„ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค.

[root@docker /]# docker container ls -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                  NAMES
45580502d3b8        nginx               "/docker-entrypoint.…"   28 minutes ago      Up 26 minutes       0.0.0.0:8080->80/tcp   nginx
64c1174446d8        httpd               "httpd-foreground"       33 minutes ago      Up 31 minutes       0.0.0.0:80->80/tcp     webserver

nginx ์ปจํ…Œ์ด๋„ˆ ๋‚ด๋ถ€์— ์–ด๋–ค ํ”„๋กœ์„ธ์Šค๊ฐ€ ๊ตฌ๋™์ค‘์ธ์ง€ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
nginx ํ”„๋กœ์„ธ์Šค๋„ค์š” :)

<!-- ์ปจํ…Œ์ด๋„ˆ nginx์˜ ํ”„๋กœ์„ธ์Šค ํ™•์ธ -->
[root@docker /]# docker container top nginx
UID                 PID                 PPID                C                   STIME               TTY                 TIME                CMD
root                4048                4031                0                   04:17               ?                   00:00:00            nginx: master process nginx -g daemon off;
101                 4092                4048                0                   04:17               ?                   00:00:00            nginx: worker process

nginx ์ปจํ…Œ์ด๋„ˆ์—์„œ ์‚ฌ์šฉํ•˜๋Š” ํฌํŠธ๋ฅผ ํ™•์ธํ•ด๋ด…์‹œ๋‹ค.
์ปจํ…Œ์ด๋„ˆ 80ํฌํŠธ๊ฐ€ ํ˜ธ์ŠคํŠธ ์ปดํ“จํ„ฐ์˜ 8080ํฌํŠธ๋กœ ์—ฐ๊ฒฐ๋˜์–ด์žˆ๋„ค์š” :)

[root@docker /]# docker container port nginx
80/tcp -> 0.0.0.0:8080

์ด๋ฒˆ์—๋Š” webserver ์ปจํ…Œ์ด๋„ˆ๋ฅผ ํ™•์ธํ•ด๋ด…์‹œ๋‹ค!
์ง€๊ธˆ webserver ์ปจํ…Œ์ด๋„ˆ์—์„œ ์šด์˜๋˜๊ณ ์žˆ๋Š” ํ”„๋กœ์„ธ์Šค์ž…๋‹ˆ๋‹ค.

[root@docker /]# docker container top webserver
UID                 PID                 PPID                C                   STIME               TTY                 TIME                CMD
root                3662                3644                0                   04:11               ?                   00:00:00            httpd -DFOREGROUND
bin                 3689                3662                0                   04:11               ?                   00:00:00            httpd -DFOREGROUND
bin                 3690                3662                0                   04:11               ?                   00:00:00            httpd -DFOREGROUND
bin                 3691                3662                0                   04:11               ?                   00:00:00            httpd -DFOREGROUND
bin                 4244                3662                0                   04:41               ?                   00:00:00            httpd -DFOREGROUND

 

webserver ์ปจํ…Œ์ด๋„ˆ์—์„œ ์‚ฌ์šฉํ•˜๋Š” ํฌํŠธ๋Š” 80ํฌํŠธ์ด๋ฉฐ, ํ˜ธ์ŠคํŠธ ์ปดํ“จํ„ฐ์—๋„ 80 ํฌํŠธ์— ๋งคํ•‘๋˜์–ด์žˆ๋„ค์š” :)

[root@docker /]# docker container port webserver
80/tcp -> 0.0.0.0:80

 

9. ์ปจํ…Œ์ด๋„ˆ ์ด๋ฆ„ ๋ณ€๊ฒฝํ•˜๊ธฐ

์ปจํ…Œ์ด๋„ˆ์˜ ๋ชฉ๋ก์„ ํ™•์ธํ•ด๋ด…๋‹ˆ๋‹ค. webserver ์ปจํ…Œ์ด๋„ˆ์˜ ์ด๋ฆ„์„ ๋ฐ”๊ฟ”๋ณผ๊นŒ์š”?

[root@docker /]# docker container ls -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                  NAMES
45580502d3b8        nginx               "/docker-entrypoint.…"   31 minutes ago      Up 29 minutes       0.0.0.0:8080->80/tcp   nginx
64c1174446d8        httpd               "httpd-foreground"       36 minutes ago      Up 34 minutes       0.0.0.0:80->80/tcp     webserver

docker container rename <์ปจํ…Œ์ด๋„ˆ ์ง€๊ธˆ์“ฐ๋Š”์ด๋ฆ„> <์ปจํ…Œ์ด๋„ˆ ๋ฐ”๊ฟ€ ์ด๋ฆ„> ๋ช…๋ น์–ด๋ฅผ ํ†ตํ•ด ์ด๋ฆ„์„ ๋ณ€๊ฒฝํ•ฉ๋‹ˆ๋‹ค. 

[root@docker /]# docker container rename webserver apache_server

์ž˜ ๋ณ€๊ฒฝ ๋˜์—ˆ๋Š”์ง€ ํ™•์ธํ•ด๋ด…๋‹ˆ๋‹ค :)
apache_server๋กœ ์ž˜ ๋ณ€๊ฒฝ๋˜์—ˆ๋„ค์š”.

[root@docker /]# docker container ls -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                  NAMES
45580502d3b8        nginx               "/docker-entrypoint.…"   31 minutes ago      Up 29 minutes       0.0.0.0:8080->80/tcp   nginx
64c1174446d8        httpd               "httpd-foreground"       36 minutes ago      Up 35 minutes       0.0.0.0:80->80/tcp     apache_server

 

10. ์ปจํ…Œ์ด๋„ˆ์— ํŒŒ์ผ ๋ณต์‚ฌํ•˜๊ธฐ

์ด๋ฒˆ์—๋Š” ํ˜ธ์ŠคํŠธ ์ปดํ“จํ„ฐ์˜ index2.html ํŒŒ์ผ์„ ์ปจํ…Œ์ด๋„ˆ๋กœ ๋ณต์‚ฌ ๋ถ™์—ฌ๋„ฃ๊ธฐ ํ•ด๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. :)

์ปจํ…Œ์ด๋„ˆ ๋ชฉ๋ก์„ ํ™•์ธํ•ด๋ด…์‹œ๋‹ค.
์ด๋ฒˆ์—๋„ apache_server ์ปจํ…Œ์ด๋„ˆ๋ฅผ ์‚ฌ์šฉํ•ด๋ด…์‹œ๋‹ค!

[root@docker /]# docker container ls -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                  NAMES
45580502d3b8        nginx               "/docker-entrypoint.…"   34 minutes ago      Up 32 minutes       0.0.0.0:8080->80/tcp   nginx
64c1174446d8        httpd               "httpd-foreground"       39 minutes ago      Up 38 minutes       0.0.0.0:80->80/tcp     apache_server

์ž, ๋จผ์ € apache_server ์ปจํ…Œ์ด๋„ˆ์˜ ์›น์„œ๋ฒ„๊ฐ€ ์ž˜ ๋™์ž‘ํ•˜๊ณ  ์žˆ๋Š”์ง€ ํ™•์ธํ•ด๋ด…์‹œ๋‹ค.

[root@docker /]# curl 127.0.0.1:80/index.html
<html><body><h1>It works!</h1></body></html>

์ด์ œ index2.html ํŒŒ์ผ์„ ์ž‘์„ฑํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

[root@docker /]# echo 'This is Docker test - File COPY' > index2.html

์ด์ œ ์ด ํŒŒ์ผ์„ ์ปจํ…Œ์ด๋„ˆ์— ๋ณต์‚ฌํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.
๋ช…๋ น์–ด๋Š” docker container cp <์›๋ณธํŒŒ์ผ> <์ปจํ…Œ์ด๋„ˆ๋ช…>:<๊ฒฝ๋กœ> ์ž…๋‹ˆ๋‹ค :)

[root@docker /]# docker container cp index2.html apache_server:/usr/local/apache2/htdocs/

์ž ์ด์ œ ์ž˜ ๋ณต์‚ฌ๋˜์—ˆ๋‚˜ ํ™•์ธํ•ด๋ณผ๊นŒ์š”! 
index2.html ํŒŒ์ผ์ด ์ปจํ…Œ์ด๋„ˆ ๋‚ด๋ถ€๋กœ ๋ณต์‚ฌ ๋˜์—ˆ๊ณ , index2.html ์š”์ฒญ์— ์‘๋‹ต ์ž˜ ํ•˜๋„ค์š”!!

[root@docker /]# curl 127.0.0.1:80/index2.html
This is Docker test - File COPY

 

11. ์ปจํ…Œ์ด๋„ˆ ๋ณ€๊ฒฝ ์ •๋ณด ํ™•์ธํ•˜๊ธฐ

์œ„ ๋‹จ๊ณ„์—์„œ index2.html์ด๋ผ๋Š” ํŒŒ์ผ์„ ์ปจํ…Œ์ด๋„ˆ ๋‚ด๋ถ€์— ๋ณต์‚ฌํ•ด๋„ฃ์—ˆ์Šต๋‹ˆ๋‹ค.
diff ๋ช…๋ น์–ด๋กœ ๋„์ปค ์ปจํ…Œ์ด๋„ˆ ๋ณ€๊ฒฝ์ •๋ณด ํ™•์ธ ํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. index2.html์€ ์ƒˆ๋กœ ์ถ”๊ฐ€๋˜์–ด A ํƒœ๊ทธ๊ฐ€ ๋ถ™์–ด์žˆ๋„ค์š”!
* 'C'๋Š” Change, 'A'๋Š” Add, 'D'๋Š” Delete 

[root@docker /]# docker container diff apache_server
C /usr
C /usr/local
C /usr/local/apache2
C /usr/local/apache2/logs
A /usr/local/apache2/logs/httpd.pid
C /usr/local/apache2/htdocs
A /usr/local/apache2/htdocs/index2.html

 

12. ์ปจํ…Œ์ด๋„ˆ ์‚ญ์ œํ•˜๊ธฐ

์ด์ œ ๋งˆ์ง€๋ง‰ ๋‹จ๊ณ„์ž…๋‹ˆ๋‹ค.
์ปจํ…Œ์ด๋„ˆ๋ฅผ ์ƒ์„ฑํ•˜๊ณ  ์šด์˜ํ•ด๋ณด์•˜์œผ๋‹ˆ, ์ด์ œ ์‚ญ์ œํ•ด๋ณผ๊นŒ์š”!
์ปจํ…Œ์ด๋„ˆ์˜ ๋ชฉ๋ก์„ ํ™•์ธํ•ด๋ด…์‹œ๋‹ค!

[root@docker /]# docker container ls -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                  NAMES
45580502d3b8        nginx               "/docker-entrypoint.…"   39 minutes ago      Up 37 minutes       0.0.0.0:8080->80/tcp   nginx
64c1174446d8        httpd               "httpd-foreground"       45 minutes ago      Up 43 minutes       0.0.0.0:80->80/tcp     apache_server

์ปจํ…Œ์ด๋„ˆ remove(rm) ๋ช…๋ น์–ด๋ฅผ ์ด์šฉํ•ด์„œ ์ปจํ…Œ์ด๋„ˆ๋ฅผ ์‚ญ์ œ ํ•ด๋ณผ๊นŒ์š”?
์•—, ์—๋Ÿฌ๊ฐ€ ๋‚˜์˜ค๋„ค์š”. 
๊ทธ ์ด์œ ๋Š” ์‹คํ–‰์ค‘์ธ ์ปจํ…Œ์ด๋„ˆ๋Š” stop ์ดํ›„์— remove ํ• ์ˆ˜์žˆ๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค.

[root@docker /]# docker rm apache_server
Error response from daemon: You cannot remove a running container 64c1174446d8c95377f214c90d242442a29fa821708be29e42c49b0ad50828d7. Stop the container before attempting removal or force remove

์ด๋ฒˆ์—๋Š” f ์˜ต์…˜์„ ์ค˜์„œ ๊ฐ•์ œ๋กœ ์‚ญ์ œํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

[root@docker /]# docker rm apache_server -f
apache_server

์ž˜ ์ง€์›Œ์กŒ๋Š”์ง€ ๋ชฉ๋ก์„ ํ™•์ธํ•ด๋ด…๋‹ˆ๋‹ค.
์‚ญ์ œ๊ฐ€ ์ž˜ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค :)

[root@docker /]# docker container ls -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                  NAMES
45580502d3b8        nginx               "/docker-entrypoint.…"   40 minutes ago      Up 38 minutes       0.0.0.0:8080->80/tcp   nginx

Prune ๋ช…๋ น์–ด๋ฅผ ์‚ฌ์šฉํ•ด๋ณผ๊ฑด๋ฐ, ์ด ๋ช…๋ น์–ด๋Š” ์ค‘์ง€์ƒํƒœ์ธ ์ปจํ…Œ์ด๋„ˆ๋ฅผ ์ „์ฒด ์‚ญ์ œํ•˜๋Š” ๋ช…๋ น์–ด ์ž…๋‹ˆ๋‹ค.

[root@docker /]# docker container prune
WARNING! This will remove all stopped containers.
Are you sure you want to continue? [y/N] y
Total reclaimed space: 0B

์ง€๊ธˆ์€ ์ค‘์ง€์ƒํƒœ์ธ ์ปจํ…Œ์ด๋„ˆ๊ฐ€ ์—†์–ด์„œ, ์ƒํƒœ ๊ทธ๋Œ€๋กœ ์œ ์ง€๋ฉ๋‹ˆ๋‹ค. ๊ฒฐ๊ณผ ํ™•์ธํ•ด๋ณผ๊ฒŒ์š” :)

[root@docker /]# docker container ls -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                  NAMES
45580502d3b8        nginx               "/docker-entrypoint.…"   42 minutes ago      Up 39 minutes       0.0.0.0:8080->80/tcp   nginx

๊ทธ๋Ÿฐ๋ฐ ์ด๋ ‡๊ฒŒ ํ•˜๋‚˜ํ•˜๋‚˜ ์‚ญ์ œํ•˜๋Š”๊ฒŒ ๊ท€์ฐฎ์„๋–„ ํ•œ๋ฒˆ์— ๋‚ ๋ฆด์ˆ˜ ์žˆ๋Š” ๋ช…๋ น์–ด๊ฐ€ ์žˆ์„๊นŒ์š”?
์ „์ฒด ์‚ญ์ œ ํ•ด๋ด…์‹œ๋‹ค!

[root@docker /]# docker container rm -f $(docker ps -aq)
45580502d3b8

์ปจํ…Œ์ด๋„ˆ ๋ชฉ๋ก์„ ํ™•์ธํ•ด๋ด…์‹œ๋‹ค :)
์ „๋ถ€ ๋‹ค ์ž˜ ์‚ญ์ œ ๋˜์—ˆ๋„ค์š”.

[root@docker /]# docker container ls -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES

 

์ˆ˜๊ณ ํ•˜์…จ์Šต๋‹ˆ๋‹ค. ๐Ÿฅณ

728x90