Docker 입문하기

김태훈·2023년 5월 22일
0

1. Docker란?

컨테이너 기반의 오픈소스 가상화 플랫폼

A container is a standard unit of software that packages up code and all its dependencies so the application runs quickly and reliably from one computing environment to another. A Docker container image is a lightweight, standalone, executable package of software that includes everything needed to run an application: code, runtime, system tools, system libraries and settings

Container images become containers at runtime and in the case of Docker containers – images become containers when they run on Docker Engine. Available for both Linux and Windows-based applications, containerized software will always run the same, regardless of the infrastructure. Containers isolate software from its environment and ensure that it works uniformly despite differences for instance between development and staging.

  • 공식 문서 설명 ‘[https://www.docker.com/resources/what-container/]

컨테이너는 즉 코드들과 그리고 그것들의 모든 종속성들을 패키징한 단위이다. 이를 통해 다른 환경에서도 빠르게 실행시킬 수 있다.여기에서 Docker Container Image를 사용하는데, 실행에만 필요한 시스템 구성요소를 갖추었기 때문에 엄청나게 가볍다. 따라서 도커는, 별도의 guest OS의 설치 없이, image 단위로 서버운영에 필요한 시스템 구성요소를 격리해서 설치하므로 용량도 줄어서 배포하기에도 매우 편리한 상태가 된다. image들은 runtime에 docker engine에 의해 container화 된다.

또한 Docker Container는 Docker Engine에서 동작하는데, 컨테이너는 확실히 isolated 되어있으며, Host OS를 공유하기 때문에, 굉장히 가볍다. VM만 생각해도..

2. VM과의 차이

VM 종류 설명

-- Hardware Virtualization 설명 시작 --

위의 그림을 보면 결국에는 각 application이 실행되어질 때, Guest OS를 건드린다. Guest OS에서 다시 VMM (HyperVisor)에 접근하여, 실제 HW(Host OS) 에 접근하게 되는 식이다. (Guest OS는 가상의 HW 정보를 가지고 있다. 결국 필요한 것은 실제 HW이다. 따라서 VMM이 필요!)

이 때, 전가상화냐, 반가상화냐 에 따라서 방식이 나뉘는데,

전가상화일 경우에는 GuestOS에서 Hardware 직접 건드리게 냅두고, Exception이 발생되어 결국 HyperVisor(실제 Hardware를 관리)를 call하는 상황으로 dynamic하게 전환하는 방식으로써 상대적으로 느리다.

하지만 반가상화일 경우에는, GuestOS에서 Hardware를 call하는 명령을 HyperVisor를 call하는 방식이다. 따라서 속도가 훨씬 빠르다.

VM HyperVisor 종류

-- OS Virtualization 설명 시작 --

그렇다면 OS virtualization은?

비슷한 느낌인것 같다..?

JAIL에서 발전해왔다는 어디 글을 보면 어쨌거나 비슷한 종류인 것 같다.

차이점

3. image

An image is a read-only template with instructions for creating a Docker container. Often, an image is based on another image, with some additional customization. For example, you may build an image which is based on the ubuntu image, but installs the Apache web server and your application, as well as the configuration details needed to make your application run.

You might create your own images or you might only use those created by others and published in a registry. To build your own image, you create a Dockerfile with a simple syntax for defining the steps needed to create the image and run it. Each instruction in a Dockerfile creates a layer in the image. When you change the Dockerfile and rebuild the image, only those layers which have changed are rebuilt. This is part of what makes images so lightweight, small, and fast, when compared to other virtualization technologies.

이미지는 컨테이너를 만들기 위해 필요한 template이다.
이러한 이미지를 만들기 위해서 Dockerfile을 세팅했던 것이고, 이를 통해 최종적으로 격리된 컨테이너를 만드는 것이다.

Dockerfile에서의 각 명령어들은 image의 layer를 만든다.

이는 왜 필요한 작업일까?

(스택오버플로우 펌)

한마디로 정리하자면, image layer는 변화를 기록하는 수단인 것이다.

Dockerfile의 한줄 한줄 명령어를 실행할 때 마다, 만들어진 이미지를 변경시킨다. 그래서 결국 명령어를 한번 시행시킬 때 마다, layer를 하나더 쌓는 식이다.

예를들어 rails 라는 이미지로부터 시작하여새로운 이미지 파일을 만든다고 가정한다면 우리는 거기에 추가적인 명령어로 layer를 하나씩 더 쌓아 올린다.

이렇게 하면, image를 만드는게 훨씬 쉬워지고, 용량면에서도 새로운 이미지를 또 만들 필요가 없으니까 매우 훌륭하게 컨테이너까지 만들 수 있다.

또한, 컨테이너를 생성할 때에도, layer 방식을 사용하는데, 기존의 이미지 레이어 위에 읽기/쓰기read-write 레이어를 추가한다. 그리고 이미지 레이어를 그대로 사용하면서 컨테이너가 실행중에 생성하는 파일이나 변경된 내용은 읽기/쓰기 레이어에 저장되므로 여러개의 컨테이너를 생성해도 최소한의 용량만 사용합니다.

곧 다음과 같다.

추가로 컨테이너는 이미지를 실행한 상태, 추가되거나 변화되는 값은 컨테이너에 저장된다. 당연히 같은 이미지에서 여러개의 container instance를 만들어낼 수 있다!

실제

실제로 도커를 실행해보았는데,

윈도우에서 wsl을 이용하여 repository를 clone해왔다.

이때 두가지방법을 활용할 수 있다.

  • 직접 이미지 build 후 도커 run으로 실행하기
    docker build -t firstdocker0518 . 
    docker run -dp 3000:3000 firstdocker0518
  • 처음부터 dev-container를 이용하여 (extension) 왼쪽하단 >< 눌러서 reopen in container로 열기
profile
기록하고, 공유합시다

0개의 댓글