본문 바로가기

클라우드/Docker

[docker] devops/컨테이너

 

 

 

for문으로 컨테이너 10개 한번에 생성하기

ubuntu@ip-172-31-0-83:~$ cat con.sh
for i in $(seq 1 10)

do

docker container create nginx
done

ubuntu@ip-172-31-0-83:~$
ubuntu@ip-172-31-0-83:~$
ubuntu@ip-172-31-0-83:~$ sh con.sh
375b6dd9fc1727fb8e3700de441b87fa6331c664e91bec2ed16640fab012cf67
d584d3a9c9beaa91d04ee9babaa9953a5984fcdb5f73b47eede96c64ce80662f
60e113963a0c16d61ebe3b4098da8a7d3dcc0d394c3a47de393cc868f99917d5
18ff15846e73c2fa71d1d33c1855de0d3835bb09c5d2cf7a018a2e4fc4f6914b
61065ae4923ca27810b06f436ee769c4feacce51080c3c49c210c891a9343701
492d92229447d4ef312c973e72fce15728399043b9097766fe2fb4a75852685d
fb3b9e1996e128e204ab9b7def0042d1519d12b8d6b78e4afd13333582f71c7e
810455e933f0126c34f3d224de2d62203e90a1ce69b31c9099ee5a8f2efe7ca8
918ee99df7ef08fccc3d939a20ba6746679ce8c72fa752fd6e83fe00165ca17e
046ec5eaa9442515b6e064ca48f02a01a9b5d52906aae1686e2125d6f334e9b3
ubuntu@ip-172-31-0-83:~$
ubuntu@ip-172-31-0-83:~$
ubuntu@ip-172-31-0-83:~$ docker ps
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES
ubuntu@ip-172-31-0-83:~$ ps -a
    PID TTY          TIME CMD
 114487 pts/2    00:00:00 ps
ubuntu@ip-172-31-0-83:~$ docker ps -a
CONTAINER ID   IMAGE     COMMAND                  CREATED          STATUS                       PORTS     NAMES
046ec5eaa944   nginx     "/docker-entrypoint.…"   25 seconds ago   Created                                charming_wiles
918ee99df7ef   nginx     "/docker-entrypoint.…"   25 seconds ago   Created                                awesome_perlman
810455e933f0   nginx     "/docker-entrypoint.…"   25 seconds ago   Created                                magical_poitras
fb3b9e1996e1   nginx     "/docker-entrypoint.…"   26 seconds ago   Created                                nervous_babbage
492d92229447   nginx     "/docker-entrypoint.…"   26 seconds ago   Created                                reverent_curran
61065ae4923c   nginx     "/docker-entrypoint.…"   26 seconds ago   Created                                unruffled_wright
18ff15846e73   nginx     "/docker-entrypoint.…"   26 seconds ago   Created                                blissful_agnesi
60e113963a0c   nginx     "/docker-entrypoint.…"   26 seconds ago   Created                                exciting_nash
d584d3a9c9be   nginx     "/docker-entrypoint.…"   26 seconds ago   Created                                heuristic_jang
375b6dd9fc17   nginx     "/docker-entrypoint.…"   26 seconds ago   Created                                wizardly_raman
7ca532bb772a   nginx     "/docker-entrypoint.…"   2 hours ago      Exited (137) 8 minutes ago             netnginx
f61bab4a6bac   nginx     "/docker-entrypoint.…"   4 hours ago      Exited (137) 5 minutes ago             mynginx

 

 

10개 컨테이너 한번에 삭제하기

ubuntu@ip-172-31-0-83:~$ docker rm -f $(docker ps -aq)
046ec5eaa944
918ee99df7ef
810455e933f0
fb3b9e1996e1
492d92229447
61065ae4923c
18ff15846e73
60e113963a0c
d584d3a9c9be
375b6dd9fc17
7ca532bb772a
f61bab4a6bac
ubuntu@ip-172-31-0-83:~$
ubuntu@ip-172-31-0-83:~$ docker ps -a
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES

 

 

ubuntu@ip-172-31-0-83:~$ docker run -d -p 80:80 --name mynginx jarrydms/nginx:v1.0
adb345c327586d455ce941bb50ec88f7a860b019dacc6aeb8abcac69a2ba2f2f
ubuntu@ip-172-31-0-83:~$ docker ps -a
CONTAINER ID   IMAGE                 COMMAND                  CREATED         STATUS         PORTS                               NAMES
adb345c32758   jarrydms/nginx:v1.0   "/docker-entrypoint.…"   5 seconds ago   Up 5 seconds   0.0.0.0:80->80/tcp, :::80->80/tcp   mynginx

 

 

ubuntu@ip-172-31-0-83:~$ docker pull  bokyoung71/ubuntu:test
test: Pulling from bokyoung71/ubuntu
9c704ecd0c69: Pull complete
//레이어 1개확인

Digest: sha256:86553d650703d1d74a306808fc1ef8f5d218ac5c75c23de63842733c8f420bfb
Status: Downloaded newer image for bokyoung71/ubuntu:test
docker.io/bokyoung71/ubuntu:test



ubuntu@ip-172-31-0-83:~$ docker pull  bokyoung71/ubuntu:v1
v1: Pulling from bokyoung71/ubuntu
3153aa388d02: Pull complete
74726e0bc6e7: Pull complete
9a55ce58bbdb: Pull complete
//레이어 3개 확인

 

 

ubuntu@ip-172-31-0-83:~$ sudo cat /var/lib/docker/image/overlay2/repositories.json
{"Repositories":{"bokyoung71/ubuntu":{"bokyoung71/ubuntu:test":"sha256:35a88802559dd2077e584394471ddaa1a2c5bfd16893b829ea57619301eb3908",
"bokyoung71/ubuntu:v1":"sha256:a663c6053c892f56a8af61e98b20a4cca7bc1b1a62f4846a87b42df9f558483c",
"bokyoung71/ubuntu@sha256:73007c8116dfcbd2a93408212e143da2db2264620deb7c53e05124c0bdf2a28b":"sha256:a663c6053c892f56a8af61e98b20a4cca7bc1b1a62f4846a87b42df9f558483c",
"bokyoung71/ubuntu@sha256:86553d650703d1d74a306808fc1ef8f5d218ac5c75c23de63842733c8f420bfb":"sha256:35a88802559dd2077e584394471ddaa1a2c5bfd16893b829ea57619301eb3908"},
"jarrydms/nginx":{"jarrydms/nginx:v1.0":"sha256:60c8a892f36faf6c9215464005ee6fb8cf0585f70b113c0b030f6cb497a41876","jarrydms/nginx@sha256:2ebf3d369d813bcc6a531ba43e1859bd91ad5c8217ae33b821f5ffada06a6cd4":"sha256:60c8a892f36faf6c9215464005ee6fb8cf0585f70b113c0b030f6cb497a41876"},"nginx":{"nginx:latest":"sha256:60c8a892f36faf6c9215464005ee6fb8cf0585f70b113c0b030f6cb497a41876","nginx:v1.0":"sha256:60c8a892f36faf6c9215464005ee6fb8cf0585f70b113c0b030f6cb497a41876","nginx@sha256:bc5eac5eafc581aeda3008b4b1f07ebba230de2f27d47767129a6a905c84f470":"sha256:60c8a892f36faf6c9215464005ee6fb8cf0585f70b113c0b030f6cb497a41876"}}}ubuntu@ip-172-31-0-83:~$

 

 

ubuntu@ip-172-31-0-83:~$ sudo ls /var/lib/docker/image/overlay2/layerdb
mounts  sha256  tmp
ubuntu@ip-172-31-0-83:~$ sudo ls /var/lib/docker/image/overlay2/layerdb/sha256
01c22c5216c94ae4a6285e21b0ccb6bb786d437aa7eb7d3e2de8a454115d17a8  a37c7cf211e461dc03b1550ae5d50d5af1d05297db6cfdfe06f79205ddc94411
47984982982b32672d3b0cc6ebc1016e70916a8347c79765dc2ba09ed9afc97c  c3548211b8264f8bfa47a6727043a64f1791b82ac965a284a7ea187e971a95e2
59c56aee1fb4dbaeb334aef06088b49902105d1ea0c15a9e5a2a9ce560fa4c5d  ceff183e9da02c76af52712096cbe7e26e01909f827f18141058afbf4f7e32db
9312ec104533f8bf3fa6c7272c8c40517614ad072cf86005efb384c685c980d4  d775439dbfb804d168b7ab8501c32013896d40d66b14944d2429778d995c7fe4
9a980991ece0116dad7650d5af48faa2f693f9277bfd99f4fb3c8c2ce0b4e27d  f8fffef24ebb396c3e1721168923665f594d6b0ec1270700f642155fb51179cb
a30a5965a4f7d9d5ff76a46eb8939f58e95be844de1ac4a4b452d5d31158fdea

 

 

nginx를 실행할때는 레이어 7개가 같은 이미지를 공유하고있다 (컨테이너의 레이어까지 같이 보여지고있다.)

 

 

 

ubuntu@ip-172-31-0-83:~$ sudo ls /var/lib/docker/containers
adb345c327586d455ce941bb50ec88f7a860b019dacc6aeb8abcac69a2ba2f2f

ubuntu@ip-172-31-0-83:~$ cd /home/ubuntu
ubuntu@ip-172-31-0-83:~$ ls -l
total 4
-rw-rw-r-- 1 ubuntu ubuntu 64 Nov 19 05:45 con.sh
ubuntu@ip-172-31-0-83:~$ sh con.sh
b6d393be237c5cc09a4c5deef09efae6450ac9ea680072977eea6985b351579c
9d32aa98693ec3800bb45ae1e231ffc28c0104fb72fee7c8fd063b687d42792b
97fc6d2edb668175b1940e0c28a575093cbc16a7c93f790155f6adf9fef99bd6
149020125d3c5cbf37462be7c7e3e5dfcee18533a09d368e41a2dcbac41cdd0a
5b2db1ac284d5a308ae369050d486cf9cba17fbcd42b9a6b75f98bac567f3391
ea66631fc9ab9df00fe982ed81b4e508e7807288c871da02ff99b45dc77a85cd
f72c55f8546bea1bf16de49dc6ef5a61faaadc6fe70496bc9f359453589f3097
6a3e4bf20c0b19155adbdb513ce647f81736a06a1c56bc214766a1c8e23726b6
17905f08d4f07f0a93a70f90771e3db35223e43eac0a6ae4c857d554618ef89c
66a4f5d52bec23c8517c216d73a3d681ff1de345817c7319087444b287bdd0e1
ubuntu@ip-172-31-0-83:~$
ubuntu@ip-172-31-0-83:~$
ubuntu@ip-172-31-0-83:~$ sudo ls /var/lib/docker/containers
149020125d3c5cbf37462be7c7e3e5dfcee18533a09d368e41a2dcbac41cdd0a  9d32aa98693ec3800bb45ae1e231ffc28c0104fb72fee7c8fd063b687d42792b
17905f08d4f07f0a93a70f90771e3db35223e43eac0a6ae4c857d554618ef89c  adb345c327586d455ce941bb50ec88f7a860b019dacc6aeb8abcac69a2ba2f2f
5b2db1ac284d5a308ae369050d486cf9cba17fbcd42b9a6b75f98bac567f3391  b6d393be237c5cc09a4c5deef09efae6450ac9ea680072977eea6985b351579c
66a4f5d52bec23c8517c216d73a3d681ff1de345817c7319087444b287bdd0e1  ea66631fc9ab9df00fe982ed81b4e508e7807288c871da02ff99b45dc77a85cd
6a3e4bf20c0b19155adbdb513ce647f81736a06a1c56bc214766a1c8e23726b6

 

ubuntu@ip-172-31-0-83:~$ echo $(docker ps -aq)
66a4f5d52bec 17905f08d4f0 6a3e4bf20c0b f72c55f8546b ea66631fc9ab 5b2db1ac284d 149020125d3c 97fc6d2edb66 9d32aa98693e b6d393be237c adb345c32758
ubuntu@ip-172-31-0-83:~$
ubuntu@ip-172-31-0-83:~$
ubuntu@ip-172-31-0-83:~$ echo $(docker ps -aq)
66a4f5d52bec 17905f08d4f0 6a3e4bf20c0b f72c55f8546b ea66631fc9ab 5b2db1ac284d 149020125d3c 97fc6d2edb66 9d32aa98693e b6d393be237c adb345c32758
ubuntu@ip-172-31-0-83:~$
ubuntu@ip-172-31-0-83:~$
ubuntu@ip-172-31-0-83:~$ echo $(docker ps -aq)
66a4f5d52bec 17905f08d4f0 6a3e4bf20c0b f72c55f8546b ea66631fc9ab 5b2db1ac284d 149020125d3c 97fc6d2edb66 9d32aa98693e b6d393be237c adb345c32758
ubuntu@ip-172-31-0-83:~$
ubuntu@ip-172-31-0-83:~$
ubuntu@ip-172-31-0-83:~$ docker ps
CONTAINER ID   IMAGE                 COMMAND                  CREATED          STATUS          PORTS                               NAMES
adb345c32758   jarrydms/nginx:v1.0   "/docker-entrypoint.…"   35 minutes ago   Up 35 minutes   0.0.0.0:80->80/tcp, :::80->80/tcp   mynginx
ubuntu@ip-172-31-0-83:~$ docker rm adb
Error response from daemon: You cannot remove a running container adb345c327586d455ce941bb50ec88f7a860b019dacc6aeb8abcac69a2ba2f2f. Stop the container before attempting removal or force remove
ubuntu@ip-172-31-0-83:~$ docker rm -f $(docker ps -aq)
66a4f5d52bec
17905f08d4f0
6a3e4bf20c0b
f72c55f8546b
ea66631fc9ab
5b2db1ac284d
149020125d3c
97fc6d2edb66
9d32aa98693e
b6d393be237c



adb345c32758
ubuntu@ip-172-31-0-83:~

 

 

 

ubuntu@ip-172-31-0-83:~$ docker pull rockylinux:8
8: Pulling from library/rockylinux
9088cdb84e39: Pull complete
Digest: sha256:9794037624aaa6212aeada1d28861ef5e0a935adaf93e4ef79837119f2a2d04c
Status: Downloaded newer image for rockylinux:8
docker.io/library/rockylinux:8

ubuntu@ip-172-31-0-83:~$ docker images
REPOSITORY          TAG       IMAGE ID       CREATED         SIZE
jarrydms/nginx      v1.0      60c8a892f36f   6 weeks ago     192MB
nginx               latest    60c8a892f36f   6 weeks ago     192MB
nginx               v1.0      60c8a892f36f   6 weeks ago     192MB
bokyoung71/ubuntu   test      35a88802559d   5 months ago    78.1MB
rockylinux          8         c79048e50f5f   12 months ago   198MB
bokyoung71/ubuntu   v1        a663c6053c89   16 months ago   178MB
ubuntu@ip-172-31-0-83:~$
ubuntu@ip-172-31-0-83:~$
ubuntu@ip-172-31-0-83:~$ docker inspect
"docker inspect" requires at least 1 argument.
See 'docker inspect --help'.

Usage:  docker inspect [OPTIONS] NAME|ID [NAME|ID...]

Return low-level information on Docker objects
ubuntu@ip-172-31-0-83:~$ docker inspect rockylinux:8
[
    {
        "Id": "sha256:c79048e50f5fce116723442952adf4f5258455a1665bbc64aa65469abe9ead90",
        "RepoTags": [
            "rockylinux:8"
        ],
        "RepoDigests": [
            "rockylinux@sha256:9794037624aaa6212aeada1d28861ef5e0a935adaf93e4ef79837119f2a2d04c"
        ],
        "Parent": "",
        "Comment": "buildkit.dockerfile.v0",
        "Created": "2023-11-24T18:22:37Z",
        "Container": "",
        "ContainerConfig": {
            "Hostname": "",
            "Domainname": "",
            "User": "",
            "AttachStdin": false,
            "AttachStdout": false,
            "AttachStderr": false,
            "Tty": false,
            "OpenStdin": false,
            "StdinOnce": false,
            "Env": null,
            "Cmd": null,
            "Image": "",
            "Volumes": null,
            "WorkingDir": "",
            "Entrypoint": null,
            "OnBuild": null,
            "Labels": null
        },
        "DockerVersion": "",
        "Author": "",
        "Config": {
            "Hostname": "",
            "Domainname": "",
            "User": "",
            "AttachStdin": false,
            "AttachStdout": false,
            "AttachStderr": false,
            "Tty": false,
            "OpenStdin": false,
            "StdinOnce": false,
            "Env": [
                "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
            ],
            "Cmd": [
                "/bin/bash"
            ],
            "ArgsEscaped": true,
            "Image": "",
            "Volumes": null,
            "WorkingDir": "/",
            "Entrypoint": null,
            "OnBuild": null,
            "Labels": null
        },
        "Architecture": "amd64",
        "Os": "linux",
        "Size": 197598868,
        "VirtualSize": 197598868,
        "GraphDriver": {
            "Data": {
                "MergedDir": "/var/lib/docker/overlay2/485e0dc261ae7680fec24ed1b677289bf5671c671e765c95bcdf17e6270cbe0e/merged",
                "UpperDir": "/var/lib/docker/overlay2/485e0dc261ae7680fec24ed1b677289bf5671c671e765c95bcdf17e6270cbe0e/diff",
                "WorkDir": "/var/lib/docker/overlay2/485e0dc261ae7680fec24ed1b677289bf5671c671e765c95bcdf17e6270cbe0e/work"
            },
            "Name": "overlay2"
        },
        "RootFS": {
            "Type": "layers",
            "Layers": [
                "sha256:c1827ee010dbe3d0e7aa85282da0a80f74f02da1c44d6e81313cccdf465e58c6"
            ]
        },
        "Metadata": {
            "LastTagTime": "0001-01-01T00:00:00Z"
        }
    }
]

 

 

 

ubuntu@ip-172-31-0-83:~$ docker ps -a
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES
ubuntu@ip-172-31-0-83:~$
ubuntu@ip-172-31-0-83:~$
ubuntu@ip-172-31-0-83:~$ docker run --name rokylinux rockylinux:8
ubuntu@ip-172-31-0-83:~$
ubuntu@ip-172-31-0-83:~$ docker ps -a
CONTAINER ID   IMAGE          COMMAND       CREATED         STATUS                     PORTS     NAMES
eb8eaef02d0d   rockylinux:8   "/bin/bash"   4 seconds ago   Exited (0) 2 seconds ago             rokylinux
ubuntu@ip-172-31-0-83:~$
ubuntu@ip-172-31-0-83:~$
ubuntu@ip-172-31-0-83:~$ docker run --name test rockylinux:8 /bin/cal
    November 2024
Su Mo Tu We Th Fr Sa
                1  2
 3  4  5  6  7  8  9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30

ubuntu@ip-172-31-0-83:~$ docker ps -a
CONTAINER ID   IMAGE          COMMAND       CREATED          STATUS                      PORTS     NAMES
98a1b595e6ee   rockylinux:8   "/bin/cal"    13 seconds ago   Exited (0) 12 seconds ago             test
eb8eaef02d0d   rockylinux:8   "/bin/bash"   44 seconds ago   Exited (0) 42 seconds ago             rokylinux
ubuntu@ip-172-31-0-83:~$

 

 

ubuntu@ip-172-31-0-83:~$ docker ps -a
CONTAINER ID   IMAGE          COMMAND       CREATED          STATUS                      PORTS     NAMES
98a1b595e6ee   rockylinux:8   "/bin/cal"    13 seconds ago   Exited (0) 12 seconds ago             test
eb8eaef02d0d   rockylinux:8   "/bin/bash"   44 seconds ago   Exited (0) 42 seconds ago             rokylinux
ubuntu@ip-172-31-0-83:~$
ubuntu@ip-172-31-0-83:~$
ubuntu@ip-172-31-0-83:~$
ubuntu@ip-172-31-0-83:~$ docker run -it --name test2 rockylinux:8

 

COMMAND정보에 "/bin/cal" , "/bin/bash" 으로 run 으로 실행함 

 

 

 

서버리스로 실행하고 끝나는 방법 docker run --rm --name 활용합니다.

ubuntu@ip-172-31-0-83:~$ docker run --rm --name test4 rockylinux:8 /bin/cal
    November 2024
Su Mo Tu We Th Fr Sa
                1  2
 3  4  5  6  7  8  9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30

ubuntu@ip-172-31-0-83:~$ docker images
REPOSITORY          TAG       IMAGE ID       CREATED         SIZE
jarrydms/nginx      v1.0      60c8a892f36f   6 weeks ago     192MB
nginx               latest    60c8a892f36f   6 weeks ago     192MB
nginx               v1.0      60c8a892f36f   6 weeks ago     192MB
bokyoung71/ubuntu   test      35a88802559d   5 months ago    78.1MB
rockylinux          8         c79048e50f5f   12 months ago   198MB
bokyoung71/ubuntu   v1        a663c6053c89   16 months ago   178MB
ubuntu@ip-172-31-0-83:~$ docker ps -a
CONTAINER ID   IMAGE          COMMAND       CREATED         STATUS                     PORTS     NAMES
4034e367f305   rockylinux:8   "/bin/bash"   4 minutes ago   Exited (0) 2 minutes ago             test2
98a1b595e6ee   rockylinux:8   "/bin/cal"    6 minutes ago   Exited (0) 6 minutes ago             test
eb8eaef02d0d   rockylinux:8   "/bin/bash"   7 minutes ago   Exited (0) 7 minutes ago             rokylinux

 

 

 

 

 

 

ubuntu@ip-172-31-0-83:~$ docker run -d -p 3307:3306 --name testdb2 -e MYSQL_ROOT_PASSWORD=1111 \
> -e MYSQL_DATABASE=testdb \
> -e MYSQL_USER=testuser \
> -e MYSQL_PASSWORD=1111 \
> mysql
15a9be8299b4164365ff14c4b09d19d05a5e4abaae87f56df052ee75c3e8ec44
ubuntu@ip-172-31-0-83:~$

CPU부하 발생으로 EC2가 죽었습니다.

 

읽기작업이 올라감 (프리티어라서 그런듯합니다)

반응형