본문 바로가기

카테고리 없음

Docker 사용해보기

Docker에 대한 자세한 설명을 아래 링크 슬라이드쉐어에서 보면 좋겠지만 스터디를 위해 간단히 정리 해본다. 

참고 : http://www.slideshare.net/pyrasis/docker-fordummies-44424016


Docker ? 


기존 가상화와 다르게 컨테이너를 사용하여 가상화하는 방식. 


기존 : 호스트 OS위에 게스트 OS를 올리는 방식. 게스트 OS만큼 용량이 많이 들고 하이퍼바이저 기술을 사용하지만 가상화되지 않은 실제 장비의 성능을 보여주지 못했다.




Docker : Docker를 사용하면 게스트 OS를 설치하지 않고 Host OS위에 서버를 위한 라이브러리와 프로그램만을 격리하여 설치 가능하다.

OS자원 (시스템 콜)을 호스트와 공유하기 때문에 성능 면에서 실제 물리 서버와 비슷.

Docker 성능 


아래와 같이 실제 호스트와 docker가 성능면에서 큰 차이를 보이지 않는다. 


참고 : http://www.pyrasis.com/book/DockerForTheReallyImpatient/Chapter01/01 




Docker Image 


베이스 이미지에 필요한 프로그램과 라이브러리를 설치 후 파일 하나로 만든 것. 

베이스 이미지에는 기본 OS도 있지만 이 위에 Redis등을 설치한 것도 있다. 









Docker Container ?


이미지를 실행한 상태로 위에서 말한 이미지로 같은 Container를 여러개 실행할 수 있다. 

이미지가 실행 파일이라면, 컨테이너는 실행된 프로세스이고, 프로그램으로 보면 이미지가 클래스이고 컨테이너는 인스턴스라 볼 수도 있다. 



Run Docker 


그렇다면 일단 써보자. 우리는 docker로 elasticsearch를 돌려 볼 것이다.

docker를 설치하고 아래와 같이 docker run elasticsearch를 입력하면 아래처럼 elasticsearch docker 바로 실행되는 것을 알 수 있다. 


bash-3.2$ docker run  elasticsearch                     --> docker로 elasticsearch 실행

Unable to find image 'elasticsearch:latest' locally   --> 로컬에 이미지가 있는지 확인 

latest: Pulling from elasticsearch                             --> 로컬에 없기 때문에 docker hub에서 다운로드

5679b9b90e09: Pull complete                                --> 이미지가 설치되는 과정 

ea6bab360f56: Pull complete 

0f062bc85662: Pull complete 

b33c416af6e1: Pull complete 

12ae388f310c: Pull complete 

28cdaecf1dbc: Pull complete 

ff99b0ef3456: Pull complete 

e0196564fc05: Pull complete 

cd062d9edf2f: Pull complete 

a49047b426da: Pull complete 

e6994c3ff4f3: Pull complete 

38d24504f6df: Pull complete 

99e074c389b7: Pull complete 

1e508a983834: Pull complete 

0f94979e18de: Pull complete 

5635ea125a97: Pull complete 

6923d6860e06: Pull complete 

9e41281f402c: Pull complete 

af82a4cc248b: Pull complete 

b9f6abdd7af1: Pull complete 

1712762ec177: Pull complete 

27e00713b488: Pull complete 

916d0eb031b3: Pull complete 

1f4f28424080: Pull complete 

10c23bbdf505: Pull complete 

80e7648f067a: Pull complete 

786f0d41c4c1: Pull complete 

ca64bf0173a0: Pull complete 

Digest: sha256:10235684ae3b71088e403f9a47961732cd8f93af4c9f957de3cdcf944bdc97e0

Status: Downloaded newer image for elasticsearch:latest                  --> 설치가 완료되었으며 아래에 elasticsearch실행 결과가 나온다. 

[2015-11-23 15:15:40,163][INFO ][node                     ] [The Wink] version[2.0.0], pid[1], build[de54438/2015-10-22T08:09:48Z]

[2015-11-23 15:15:40,164][INFO ][node                     ] [The Wink] initializing ...

[2015-11-23 15:15:40,213][INFO ][plugins                  ] [The Wink] loaded [], sites []

[2015-11-23 15:15:40,316][INFO ][env                      ] [The Wink] using [1] data paths, mounts [[/usr/share/elasticsearch/data (/dev/sda1)]], net usable_space [15.5gb], net total_space [18.1gb], spins? [possibly], types [ext4]

[2015-11-23 15:15:42,243][INFO ][node                     ] [The Wink] initialized

[2015-11-23 15:15:42,243][INFO ][node                     ] [The Wink] starting ...

[2015-11-23 15:15:42,369][WARN ][common.network           ] [The Wink] publish address: {0.0.0.0} is a wildcard address, falling back to first non-loopback: {172.17.0.3}

[2015-11-23 15:15:42,370][INFO ][transport                ] [The Wink] publish_address {172.17.0.3:9300}, bound_addresses {[::]:9300}

[2015-11-23 15:15:42,383][INFO ][discovery                ] [The Wink] elasticsearch/m6apL_-2TfmX-Y4IVW44Mg

[2015-11-23 15:15:45,478][INFO ][cluster.service          ] [The Wink] new_master {The Wink}{m6apL_-2TfmX-Y4IVW44Mg}{172.17.0.3}{172.17.0.3:9300}, reason: zen-disco-join(elected_as_master, [0] joins received)

[2015-11-23 15:15:45,526][WARN ][common.network           ] [The Wink] publish address: {0.0.0.0} is a wildcard address, falling back to first non-loopback: {172.17.0.3}

[2015-11-23 15:15:45,526][INFO ][http                     ] [The Wink] publish_address {172.17.0.3:9200}, bound_addresses {[::]:9200}

[2015-11-23 15:15:45,527][INFO ][node                     ] [The Wink] started

[2015-11-23 15:15:45,584][INFO ][gateway                  ] [The Wink] recovered [0] indices into cluster_state



Jump into docker container 


다른 창에서 docker ps를 입력하면 docker에 elasticsearch 이미지를 사용한 container가 d1265a24c869라는 containter id로 실행된 것을 확인 가능하다. 

bash-3.2$ docker ps

CONTAINER ID        IMAGE                   COMMAND                CREATED                    STATUS                           PORTS                 NAMES

d1265a24c869        elasticsearch       "/docker-entrypoint.   About a minute ago   Up About a minute   9200/tcp, 9300/tcp   thirsty_stallman    

bash-3.2$ docker exec -it d1265a24c869 bash

root@d1265a24c869:/# 


docker exec -it <container-id> <command>를 사용해서 원하는 container-id에 command를 실행한 결과를 바로 확인할 수 있는데 위에 실행한 것은 

d1265a24c869 컨테이너에 bash를 실행하고 -it옵션을 이용하여 i (input을 docker로 전달) /  t (tty를 연결)을 통해 입력 및 출력을 볼 수 있게 해달라는 것이다. 

그 결과로 붉은 글씨로 보이는 elasticsearch docker 내부에 들어온 것을 확인할 수 있다. 


그럼 실제 curl을 사용해서 elasticsearch server가 동작하는지 확인해보자. 

root@d1265a24c869:/# curl localhost:9200

{

  "name" : "The Wink",

  "cluster_name" : "elasticsearch",

  "version" : {

    "number" : "2.0.0",

    "build_hash" : "de54438d6af8f9340d50c5c786151783ce7d6be5",

    "build_timestamp" : "2015-10-22T08:09:48Z",

    "build_snapshot" : false,

    "lucene_version" : "5.2.1"

  },

  "tagline" : "You Know, for Search"

}

root@d1265a24c869:/# 


성공이다. 


이런식으로 쉽게 redis, mongodb등 원하는 어플리케이션을 띄울 수 있다. 


Docker로 redis 실행 

Unable to find image 'redis:latest' locally

latest: Pulling from redis

b014c4494ea5: Pull complete 

2f329595e406: Pull complete 

2d34e9ec4a65: Pull complete 

8fc6e86792e4: Pull complete 

867386488f31: Pull complete 

5e4fbefc8043: Pull complete 

3894d23d1769: Pull complete 

552c9d871496: Pull complete 

1585b92a2e81: Pull complete 

f52677abfb05: Pull complete 

5228fb0bad0a: Pull complete 

c7e038aa4e47: Pull complete 

179d219ba215: Pull complete 

4a2fca62f85e: Pull complete 

57dc1fc95252: Pull complete 

a8b4e95f4dae: Pull complete 

82ca9f96ee05: Pull complete 

redis:latest: The image you are pulling has been verified. Important: image verification is a tech preview feature and should not be relied on to provide security.

Digest: sha256:4f80d4304d2e83fdc72fc23a457a901e0b8fe1a4aca163d52ca67a02b06b07af

Status: Downloaded newer image for redis:latest

1:C 23 Nov 16:48:03.600 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf

                _._                                                  

           _.-``__ ''-._                                             

      _.-``    `.  `_.  ''-._           Redis 3.0.5 (00000000/0) 64 bit

  .-`` .-```.  ```\/    _.,_ ''-._                                   

 (    '      ,       .-`  | `,    )     Running in standalone mode

 |`-._`-...-` __...-.``-._|'` _.-'|     Port: 6379

 |    `-._   `._    /     _.-'    |     PID: 1

  `-._    `-._  `-./  _.-'    _.-'                                   

 |`-._`-._    `-.__.-'    _.-'_.-'|                                  

 |    `-._`-._        _.-'_.-'    |           http://redis.io        

  `-._    `-._`-.__.-'_.-'    _.-'                                   

 |`-._`-._    `-.__.-'    _.-'_.-'|                                  

 |    `-._`-._        _.-'_.-'    |                                  

  `-._    `-._`-.__.-'_.-'    _.-'                                   

      `-._    `-.__.-'    _.-'                                       

          `-._        _.-'                                           

              `-.__.-'                                               


1:M 23 Nov 16:48:03.603 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.

1:M 23 Nov 16:48:03.603 # Server started, Redis version 3.0.5

1:M 23 Nov 16:48:03.603 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.

1:M 23 Nov 16:48:03.604 # WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled.

1:M 23 Nov 16:48:03.604 * The server is now ready to accept connections on port 6379



Docker Hub


docker hub는 위와 같은 docker 이미지들을 검색할 수 있도록 해주며 docker 명령어중 docker search는 실제로 커맨드 라인에서 docker hub에서 검색한 결과를 보여주는 것이다. 

Docker hub page : https://hub.docker.com/


그럼 아까 사용했던 elasticsearch를 찾아 보자. 위의 주소로 가보면 오른쪽 위에 Search박스가 있는데 여기에 원하는 이미지를 검색하면 된다. 




여기에 elasticsearch를 검색하면 아래와 같이 elasticsearch에 대한 이미지들이 검색된다. 


맨 위와 같이 offical이라고 써있거고 /가 없는 것이 각 오픈소스 진영이나 벤더가 내놓은 공식 이미지 이다. 

아래두개는 각각 mesoscloud와 barnybug라는 아이디를 가진쪽에서 만든 이미지임을 나타낸다. 

공식의 경우 아래와 같이 실행했으나. 

docker run elasticsearch

비공식의 이미지의 경우도 아래와 같이 쉽게 실행가능하다. 

docker run mesoscloud/elasticsearch


 

다음에는 docker 실행에 대해서 더 복잡한 예제를 다뤄보기로한다.