[따배도] Docker container 사용하기 - 실습

조앵·2024년 5월 10일

Docker

목록 보기
8/11
post-thumbnail

Docker container 실습

1. 컨테이너 이미지 관리 명령어 사용

totoro@docker-ubuntu:~$ docker search nginx
NAME                                              DESCRIPTION                                      STARS     OFFICIAL
nginx                                             Official build of Nginx.                         19821     [OK]
unit                                              Official build of NGINX Unit: Universal Web …   28        [OK]
nginx/nginx-ingress                               NGINX and  NGINX Plus Ingress Controllers fo…   89        
nginxinc/nginx-unprivileged                       Unprivileged NGINX Dockerfiles                   144       
nginx/nginx-prometheus-exporter                   NGINX Prometheus Exporter for NGINX and NGIN…   40        
nginxinc/nginx-s3-gateway                         Authenticating and caching gateway based on …   6         
nginx/unit                                        This repository is retired, use the Docker o…   63        
nginx/nginx-ingress-operator                      NGINX Ingress Operator for NGINX and NGINX P…   2         
nginxinc/amplify-agent                            NGINX Amplify Agent docker repository            1         
nginx/nginx-quic-qns                              NGINX QUIC interop                               1         
nginxinc/ingress-demo                             Ingress Demo                                     4         
nginxproxy/nginx-proxy                            Automated nginx proxy for Docker containers …   136       
nginxproxy/acme-companion                         Automated ACME SSL certificate generation fo…   131       
bitnami/nginx                                     Bitnami container image for NGINX                186       
bitnami/nginx-ingress-controller                  Bitnami container image for NGINX Ingress Co…   33        
bitnami/nginx-exporter                            Bitnami container image for NGINX Exporter       5         
nginxproxy/docker-gen                             Generate files from docker container meta-da…   17        
nginxinc/mra-fakes3                                                                                0         
ubuntu/nginx                                      Nginx, a high-performance reverse proxy & we…   112       
rancher/nginx-ingress-controller                                                                   13        
kasmweb/nginx                                     An Nginx image based off nginx:alpine and in…   7         
nginxinc/ngx-rust-tool                                                                             0         
nginxinc/mra_python_base                                                                           0         
rancher/nginx-ingress-controller-defaultbackend 
# 오피셜 이미지만 사용하는 것이 좋다

# docker pull 명령어
totoro@docker-ubuntu:~$ docker pull nginx:1.14
1.14: Pulling from library/nginx
27833a3ba0a5: Pull complete 
0f23e58bd0b7: Pull complete 
8ca774778e85: Pull complete 
Digest: sha256:f7988fb6c02e0ce69257d9bd9cf37ae20a60f1df7563c3a2a6abe24160306b8d
Status: Downloaded newer image for nginx:1.14
docker.io/library/nginx:1.14

# docker images 
totoro@docker-ubuntu:~$ docker images
REPOSITORY             TAG       IMAGE ID       CREATED        SIZE
nginx                  1.14      295c7be07902   5 years ago    109MB

# images --no-trunc 옵션 -> 이미지 명을 풀네임으로 출력
totoro@docker-ubuntu:~$ docker images --no-trunc 
REPOSITORY             TAG       IMAGE ID                                                                  CREATED        SIZE

nginx                  1.14      sha256:295c7be079025306c4f1d65997fcf7adb411c88f139ad1d34b537164aa060369   5 years ago    109MB

2. 컨테이너 실행 및 운영하기

# docker create -> 기본이 백그라운드로 실행된다.
totoro@docker-ubuntu:~$ docker create --name webserver nginx:1.14
5ee4fffb5b2501499a2bc41c0647066b4679151fe80b6368d763f8d3d4d89757

# docker ps
totoro@docker-ubuntu:~$ docker ps -a
CONTAINER ID   IMAGE        COMMAND                   CREATED         STATUS    PORTS     NAMES
5ee4fffb5b25   nginx:1.14   "nginx -g 'daemon of…"   2 minutes ago   Created             webserver

# docker start
totoro@docker-ubuntu:~$ docker start webserver 
webserver
totoro@docker-ubuntu:~$ docker ps -a
CONTAINER ID   IMAGE        COMMAND                   CREATED         STATUS         PORTS     NAMES
5ee4fffb5b25   nginx:1.14   "nginx -g 'daemon of…"   3 minutes ago   Up 8 seconds   80/tcp    webserver

# docker inspect webserver
[
    {
        "Id": "5ee4fffb5b2501499a2bc41c0647066b4679151fe80b6368d763f8d3d4d89757",
        "Created": "2024-05-09T14:08:04.623071089Z",
        "Path": "nginx",
        "Args": [
            "-g",
            "daemon off;"
        ],
        "State": {
            "Status": "running",
            "Running": true,
            "Paused": false,
            "Restarting": false,
            "OOMKilled": false,
            "Dead": false,
            "Pid": 83980,
            "ExitCode": 0,
            "Error": "",
            "StartedAt": "2024-05-09T14:11:11.820904239Z",
            "FinishedAt": "0001-01-01T00:00:00Z"
        },
# 중략
# 레이어는 뭐를 쓰고 있는지 속도 및 볼륨 마운트 등 세부적인 내용 확인

# ip 주소만 확인
totoro@docker-ubuntu:~$ docker inspect --format '{{.NetworkSettings.IPAddress}}' webserver
172.17.0.2

alias 명령어로 컨테이너 ip주소 확인하는 명령어

# alias 명령어로 등록
totoro@docker-ubuntu:~$ alias cip="docker inspect --format '{{.NetworkSettings.IPAddress}'"
totoro@docker-ubuntu:~$ alias
alias alert='notify-send --urgency=low -i "$([ $? = 0 ] && echo terminal || echo error)" "$(history|tail -n1|sed -e '\''s/^\s*[0-9]\+\s*//;s/[;&|]\s*alert$//'\'')"'
alias cip='docker inspect --format '\''{{.NetworkSettings.IPAddress}}'\'''
alias egrep='egrep --color=auto'
alias fgrep='fgrep --color=auto'
alias grep='grep --color=auto'
alias l='ls -CF'
alias la='ls -A'
alias ll='ls -alF'
alias ls='ls --color=auto'

# alias로 등록한 명령어 사용
totoro@docker-ubuntu:~$ cip webserver
172.17.0.2

확인한 컨테이너 ip주소를 이용해 확인

totoro@docker-ubuntu:~$ curl 172.17.0.2
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    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>

log 확인

totoro@docker-ubuntu:~$ docker logs webserver 
172.17.0.1 - - [09/May/2024:14:23:34 +0000] "GET / HTTP/1.1" 200 612 "-" "curl/7.68.0" "-"
# 200번이면 정상

# 실시간 log 확인
totoro@docker-ubuntu:~$ docker logs -f webserver
172.17.0.1 - - [09/May/2024:14:23:34 +0000] "GET / HTTP/1.1" 200 612 "-" "curl/7.68.0" "-"
2024/05/10 10:07:52 [error] 6#6: *2 open() "/usr/share/nginx/html/b.html" failed (2: No such file or directory), client: 172.17.0.1, server: localhost, request: "GET /b.html HTTP/1.1", host: "172.17.0.2"
172.17.0.1 - - [10/May/2024:10:07:52 +0000] "GET /b.html HTTP/1.1" 404 169 "-" "curl/7.68.0" "-"

top 명령어로 프로세스 확인

totoro@docker-ubuntu:~$ docker top webserver
UID                 PID                 PPID                C                   STIME               TTY                 TIME                CMD
root                83980               83957               0                   5월09               ?                   00:00:00            nginx: master process nginx -g daemon off;
systemd+            83999               83980               0                   5월09               ?                   00:00:00            nginx: worker process

exec 명령어로 직접 연결

# -i:interactive / -t:terminal
totoro@docker-ubuntu:~$ docker exec -it webserver /bin/bash
root@5ee4fffb5b25:/# cd /usr/share/nginx/html/
root@5ee4fffb5b25:/usr/share/nginx/html# ls
50x.html  index.html
root@5ee4fffb5b25:/usr/share/nginx/html# cat index.html
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    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>

# index.html 변경해보기
root@5ee4fffb5b25:/usr/share/nginx/html# echo "totoro's HOMEPAGE" > index.html
root@5ee4fffb5b25:/usr/share/nginx/html# exit    
exit
totoro@docker-ubuntu:~$ curl 172.17.0.2
totoro's HOMEPAGE

3. 컨테이너 종료하기

# 컨테이너 중지
totoro@docker-ubuntu:~$ docker stop webserver
webserver
totoro@docker-ubuntu:~$ docker ps -a
CONTAINER ID   IMAGE        COMMAND                   CREATED        STATUS                     PORTS     NAMES
5ee4fffb5b25   nginx:1.14   "nginx -g 'daemon of…"   20 hours ago   Exited (0) 3 seconds ago             webserver
totoro@docker-ubuntu:~$ docker start webserver
webserver
totoro@docker-ubuntu:~$ docker ps -a
CONTAINER ID   IMAGE        COMMAND                   CREATED        STATUS         PORTS     NAMES
5ee4fffb5b25   nginx:1.14   "nginx -g 'daemon of…"   20 hours ago   Up 3 seconds   80/tcp    webserver
totoro@docker-ubuntu:~$ curl 172.17.0.2
totoro's HOMEPAGE

# 컨테이너 삭제
# docker remove
totoro@docker-ubuntu:~$ docker rm webserver
Error response from daemon: cannot remove container "/webserver": container is running: stop the container before removing or force remove

# 에러가 뜨는 이유는 컨테이너를 종료하고 삭제해야하는데 -f 옵션을 주면 강제로 삭제할 수 있다.
totoro@docker-ubuntu:~$ docker rm -f webserver
webserver
totoro@docker-ubuntu:~$ docker ps -a
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES

문제풀이

아래 조건대로 컨테이너를 실행하세요

  1. 아파치 웹서버 컨테이너 이미지를 검색하고 다운로드하세요
  2. 다운로드한 아파치 웹서버를 백그라운드로 실행(detach), 컨테이너 이름: web으로 동작 시키세요
  3. 동작중인 컨테이너 목록을 확인해서 web 컨테이너가 running중인지 확인하세요
  4. 실행중인 web 컨테이너의 IP Address를 확인해보세요
  5. curl 명령으로 접속시도 어떤 웹페이지가 표시되나요?
  6. web 컨테이너가 만들어내는 로그를 출력하세요.
  7. 실행중인 모든 컨테이너를 중지하고 삭제하세요.
  8. 다운로드 된 컨테이너 이미지를 삭제하세요
# 1
totoro@docker-ubuntu:~$ docker pull httpd
Using default tag: latest
latest: Pulling from library/httpd
Digest: sha256:36c8c79f900108f0f09fd4148ad35ade57cba0dc19d13f3d15be24ce94e6a639
Status: Image is up to date for httpd:latest
docker.io/library/httpd:latest

# 2
totoro@docker-ubuntu:~$ docker create --name web httpd:latest
11dad59fbcc2383d7c3e7b97f846aab1ce136858965031cc980f37b536eed9d6
totoro@docker-ubuntu:~$ docker ps -a
CONTAINER ID   IMAGE          COMMAND              CREATED         STATUS    PORTS     NAMES
11dad59fbcc2   httpd:latest   "httpd-foreground"   3 seconds ago   Created             web
totoro@docker-ubuntu:~$ docker start web
web

# 3
totoro@docker-ubuntu:~$ docker ps -a
CONTAINER ID   IMAGE          COMMAND              CREATED          STATUS         PORTS     NAMES
11dad59fbcc2   httpd:latest   "httpd-foreground"   11 seconds ago   Up 2 seconds   80/tcp    web

# 4 alias로 추가해준 명령어 사용
totoro@docker-ubuntu:~$ cip web
172.17.0.2

# 5
totoro@docker-ubuntu:~$ curl 172.17.0.2
<html><body><h1>It works!</h1></body></html>

# 6
totoro@docker-ubuntu:~$ docker logs web
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
[Fri May 10 10:32:45.059676 2024] [mpm_event:notice] [pid 1:tid 140263581190016] AH00489: Apache/2.4.59 (Unix) configured -- resuming normal operations
[Fri May 10 10:32:45.060146 2024] [core:notice] [pid 1:tid 140263581190016] AH00094: Command line: 'httpd -D FOREGROUND'
172.17.0.1 - - [10/May/2024:10:33:21 +0000] "GET / HTTP/1.1" 200 45

# 7
totoro@docker-ubuntu:~$ docker rm -f web
web
totoro@docker-ubuntu:~$ docker ps -a
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES

# 8
totoro@docker-ubuntu:~$ docker images
REPOSITORY             TAG       IMAGE ID       CREATED        SIZE
httpd                  latest    67c2fc9e3d84   4 weeks ago    147MB
totoro@docker-ubuntu:~$ docker rmi httpd
Untagged: httpd:latest
Untagged: httpd@sha256:36c8c79f900108f0f09fd4148ad35ade57cba0dc19d13f3d15be24ce94e6a639
totoro@docker-ubuntu:~$ docker images
REPOSITORY             TAG       IMAGE ID       CREATED        SIZE
profile
Viva La Vida

0개의 댓글