[Docker] 컨테이너 익히기

아현·2022년 12월 23일
0

Kubernetes

목록 보기
3/9

1. 컨테이너 첫걸음


1) hello-world 컨테이너 돌려보기


hello-world는 도커 컨테이너의 동작을 설명하기 위해 만들어진 공식 이미지다.

docker run hello-world
docker run hello-world

> Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
2db29710123e: Pull complete
Digest: sha256:c77be1d3a47d0caf71a82dd893ee61ce01f32fc758031a6ec4cf1389248bb833
Status: Downloaded newer image for hello-world:latest

Hello from Docker!
This message shows that your installation appears to be working correctly.

To generate this message, Docker took the following steps:
 1. The Docker client contacted the Docker daemon.
 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
    (amd64)
 3. The Docker daemon created a new container from that image which runs the
    executable that produces the output you are currently reading.
 4. The Docker daemon streamed that output to the Docker client, which sent it
    to your terminal.

To try something more ambitious, you can run an Ubuntu container with:
 $ docker run -it ubuntu bash

Share images, automate workflows, and more with a free Docker ID:
 https://hub.docker.com/

For more examples and ideas, visit:
 https://docs.docker.com/get-started/



  1. 터미널 혹은 커맨드 프롬프트에서 docker run hello-world를 실행하면 도커 데몬에 접속한다.

    • 데몬 = 엔진
  2. 커맨드 중 hello-world는 레포지터리의 이름이다.

    • 도커 엔진은 도커 허브의 리포지터리에서부터 컨테이너를 위한 이미지를 로컬에 다운로드한다.
  3. 도커 엔진이 이미지로부터 컨테이너를 생성한다.

    • 컨테이너상의 프로세스가 메시지를 표준 출력에 쓰기 시작한다.
  4. 도커 엔진은 컨테이너의 표준 출력을 도커 커맨드에 보내고 터미널에 표시된다.



주요 용어


  • 도커의 이미지는 운영체제와 소프트웨어를 담고 있는 컨테이너 실행 이전의 상태다.

    • 각 이미지는 '리포지터리:태그'로 식별된다.
  • 도커의 리포지터리는 이미지 보관소를 말한다.

    • 리포지터리의 이름에 버전 등을 의미하는 태그를 붙여서 각각의 이미지를 구별하여 보관할 수 있다.

    • 태그를 생략하면 최신을 의미하는 latest가 사용된다.

    • 클라우드 서비스의 문서 등에서는 리포지터리 대신에 레지스트리란 표현이 쓰이기도 한다.

  • 레지스트리는 Windows의 설정 정보 데이터베이스를 말하기도 하지만, 도커에서는 리포지터리의 집합체로서 리포지터리를 제공하는 서버를 말한다.



2) 컨테이너의 생명 주기와 도커 커맨드


  • 컨테이너는 이미지, 실행, 정지의 세 가지 상태로 있을 수 있다.

    • 이미지: 컨테이너의 모현이 되는 것으로 실행되기 전의 상태다.

    • 실행: 컨테이너 위에서 프로세스가 실행 중인 상태를 의미한다.

    • 정지: 프로세스의 종료 코드, 로그가 보존된 채 정지한 상태다.

출처



이미지 다운로드


  • 원격의 리포지터리로부터 이미지를 다운로드 한다.

docker pull 리포지토리명[:태그]


docker pull centos:7
7: Pulling from library/centos
2d473b07cdd5: Pull complete
Digest: sha256:be65f488b7764ad3638f236b7b515b3678369a5124c47b8d32916d6487418ea4
Status: Downloaded newer image for centos:7
docker.io/library/centos:7


root@akim-virtual-machine:/home/akim# docker images
REPOSITORY         TAG       IMAGE ID       CREATED         SIZE
hello              latest    1bec3ca86604   3 days ago      829MB
hello-repository   latest    c1cd09d22c48   3 days ago      829MB
ubuntu             18.04     251b86c83674   2 weeks ago     63.1MB
registry           latest    81c944c2288b   6 weeks ago     24.1MB
hello-world        latest    feb5d9fea6a5   15 months ago   13.3kB
centos             7         eeb6ee3f44bd   15 months ago   204MB  #여기



컨테이너 실행


  • 지정한 이미지를 모형으로 컨테이너를 가동한다.

    • 만약 로컬에 없으면 원격의 리포지터리에서 이미지를 다운로드하고 컨테이너를 실행한다.
  • 기본값으로 설정된 원격 리포지터리는 도커 허브(Docker Hub)이지만, 클라우드에서 제공하는 프라이빗 레지스트리 서비스를 사용할 수 있다.


docker run [옵션] 리포지터리명:태그 [커맨드] [인자]

  • 옵션

    • -i: 키보드 입력을 컨테이너의 표준 입력에 연결하여 키보드 입력을 컨테이너의 셸 등에 보낸다.

    • -t: 터미널을 통해 대화형 조작이 가능하게 한다.

    • -d: 백그라운드로 컨테이너를 돌려 터미널과 연결하지 않는다.

    • --name: 컨테이너에 이름을 설정한다.

      • 시스템에서 유일한 이름이어야 하며, 옵션을 생략하면 자동으로 만들어지는 이름이 부여된다.
    • --rm: 컨테이너가 종료하면 종료 상태의 컨테이너를 자동으로 삭제한다.



 docker run -it --name test1 centos:7 bash
[root@a8a909ec7f6f /]#



컨테이너의 상태 출력


  • 실행 중이거나 정지 상태에 있는 컨테이너 목록을 출력한다.

    • 옵션을 생략한 경우에는 실행 중인 컨테이너만을 출력

docker ps [옵션]

  • 옵션

  • -a: 정지 상태인 컨테이너도 출력된다.


docker ps -a

CONTAINER ID   IMAGE              COMMAND                  CREATED         STATUS                        PORTS     NAMES
a8a909ec7f6f   centos:7           "bash"                   6 minutes ago   Exited (130) 14 seconds ago             test1
78b376607149   hello-world        "/hello"                 3 days ago      Exited (0) 3 days ago                   adoring_dewdney



로그 출력


  • 정지 상태인 컨테이너는 삭제될 때까지 남아 있으며, 실행 중 발생한 표준 출력과 표준 에러 출력을 간직하고 있다.

docker logs [옵션] 컨테이너ID | 컨테이너명

  • 옵션

    • -f를 사용하면 컨테이너가 실행 중인 상태에서 실시간으로 발생하는 로그를 볼 수 있다.

docker logs 78b376607149

Hello from Docker!
This message shows that your installation appears to be working correctly.

To generate this message, Docker took the following steps:
 1. The Docker client contacted the Docker daemon.
 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
    (amd64)
 3. The Docker daemon created a new container from that image which runs the
    executable that produces the output you are currently reading.
 4. The Docker daemon streamed that output to the Docker client, which sent it
    to your terminal.

To try something more ambitious, you can run an Ubuntu container with:
 $ docker run -it ubuntu bash

Share images, automate workflows, and more with a free Docker ID:
 https://hub.docker.com/

For more examples and ideas, visit:
 https://docs.docker.com/get-started/



컨테이너 정지


  • 실행 중인 컨테이너를 정지시키는 방법 세 가지

    1. 컨테이너의 PID=1인 프로세스가 종료한다.

      • 이미지를 별다른 커맨드 지정 없이 실행 시, 바로 종료되는 경우

      • PID=1인 셸이 종료된다.

    2. docker stop 컨테이너ID | 컨테이너명을 실행한다.

    3. docker kill 컨테이너ID | 컨테이너명을 실행한다.

      • 컨테이너 안의 PID=1인 프로세스를 강제 종료한다.

ctrl+c: 강제 종료
exit: 컨테이너 종료



컨테이너 재기동


  • 정지 상태인 컨테이너를 재기동할 수 있다.

docker start [옵션] 컨테이너ID | 컨테이너명

  • 옵션

    • -i: 컨테이너가 터미널의 입력을 받아 표준 출력과 표준 에러를 터미널에 표시하도록 기동하고 있다.



컨테이너의 변경 사항을 리포지터리에 저장


  • 가동한 컨테이너의 리눅스에서도 가상 서버에서처럼 필요한 패키지를 설치하거나 업데이트할 수 있다.

  • 현재 컨테이너의 상태를 이미지로 만들어 리포지터리에 보관할 수 있다.


docker commit [옵션] 컨테이너ID | 컨테이너명 리포지터리명[:태그]


docker run -it centos:7 bash
[root@1fff478e6913 /]# yum update -y
Loaded plugins: fastestmirror, ovl
Determining fastest mirrors
 * base: mirror.kakao.com
 * extras: mirror.kakao.com
 * updates: mirror.kakao.com
base                                                     | 3.6 kB     00:00
extras                                                   | 2.9 kB     00:00
updates                                                  | 2.9 kB     00:00
(1/4): base/7/x86_64/group_gz                              | 153 kB   00:00
(2/4): extras/7/x86_64/primary_db                          | 249 kB   00:00
(3/4): base/7/x86_64/primary_db                            | 6.1 MB   00:11
(4/4): updates/7/x86_64/primary_db                         |  19 MB   00:15
Resolving Dependencies

...


  • 위 예시는 컨테이너를 로컬의 리포지터리에 보관하고 있다.

    • 컨테이너 실행 중 이미지를 만들 수 있으나 기본적으로 이미지를 쓰는 동안은 컨테이너가 일시정지한다.

    • 보관할 이미지의 태그는 버전이나 기타 의미 있는 문자열을 사용하여 다른 이미지와 구별할 수 있다.


docker commit 1fff478e6913 centos:7-git
sha256:b5a02103e6f2303c7b157a95c0ea4b2580026781026e16323623a8d8175cf5be


docker images
REPOSITORY         TAG       IMAGE ID       CREATED          SIZE
centos             7-git     b5a02103e6f2   27 seconds ago   535MB



이미지를 원격 리포지터리에 보관


  • 이미지를 원격 리포지터리에 등록하는 것은 쿠버네티스에서 컨테이너를 돌리기 위해 반드시 해야하는 작업이다.


docker push 원격_리포지터리명:태그

  • 토커 허브 리포지터리를 사용하는 흐름

    1. 도커 허브에 가입, 도커 ID 취득

      • 하나의 계정에 여러 개의 리포지터리 소유 가능
    2. 명령어 docker login으로 도커 ID와 비밀번호 입력으로 로그인

    3. 명렁어 docker tag로 로컬의 이미지에 태그 부여

    4. 명령어 docker push를 사용하여 이미지를 원격 리포지터리에 업로드

    5. 도커 허브에 접속하여 등록된 것을 확인 및 설명 기재



docker login
Authenticating with existing credentials...
WARNING! Your password will be stored unencrypted in /root/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store

Login Succeeded
root@akim-virtual-machine:/home/akim# docker tag centos:7-git coronehi/centos:7-git
root@akim-virtual-machine:/home/akim# docker images

REPOSITORY         TAG       IMAGE ID       CREATED         SIZE
centos             7-git     b5a02103e6f2   5 minutes ago   535MB
coronehi/centos    7-git     b5a02103e6f2   5 minutes ago   535MB



종료한 컨테이너 제거


  • 컨테이너를 삭제한다.

    • 삭제되면 로그도 지워지고 더 이상 재기동할 수 없게 된다.

docker rm 컨테이너ID | 컨테이너명

docker rm 8ea1947b21ce
8ea1947b21ce



필요 없어진 이미지를 로컬 리포지터리에서 삭제


  • 필요 없어진 이미지를 로컬 리포지터리에서 삭제

docker rmi 이미지ID


docker rmi hello
Untagged: hello:latest
Deleted: sha256:1bec3ca8660408d588bc20026a485401ac5e0f7f9c2ae831a8648f69afef8a9b
Deleted: sha256:13a2529db07acffa12b1b09bcb3ef435d036f86a655af1b02800b6a83addde98
Deleted: sha256:4414996e33e92a695ffe128b8fe404c01e02bd511211c0405b8e2509549b9e41
Deleted: sha256:269768e22b3328f4674b236f14a7efbb14345d84dd2fad92cbfd6f203b8bee3b
Deleted: sha256:634298895be8e5548c149df8a566153b9b12c959c704260cf729d2a476f9fa4f
Deleted: sha256:ad2ef7c8953ad9d9175ed3905f2b412a966240384da3ce4f2f7ea8f7e5f82707
Deleted: sha256:daf104587498650228eaf8d47843c8bf10d4041373075aa60ce839938722149e



profile
For the sake of someone who studies computer science

0개의 댓글