Docker

Summary

  1. Install docker on VPS
  2. Run elasticsearch on Docker
  3. Dockerfile
  4. References and more docs

Install docker on VPS

VPS

Bạn có thể tạo một VPS theo hướng dẫn của bài viết này: Use Capistrano to deploy Rails app to VPS của tác giả 😉

Docker

Bạn có thể cài đặt Docker trên Ubuntu theo bài viết này Install Docker on Ubuntu.

Run docker on Ubuntu

Docker registry cũng giống như Github repo chứa các images.

[email protected]:~#  docker run ubuntu /bin/echo hello world

ubuntu là tên image từ docker registry.

/bin/echo hello world là lệnh bạn muốn thực hiện trên container kia.

Kết quả sẽ như thế này:

[email protected]:~#  docker run ubuntu /bin/echo hello world
Unable to find image 'ubuntu' locally
Pulling repository ubuntu
d0955f21bf24: Download complete
511136ea3c5a: Download complete
f3c84ac3a053: Download complete
a1a958a24818: Download complete
9fec74352904: Download complete
hello world

Mỗi dòng Download là một layer

Docker không ghi trực tiếp vào image mà nó tạo ra các layer nằm ở trên image. Khi chuyển đổi trạng thái Docker chỉ cần apply một hay nhiều layer vào trên cùng của image hiện tại giống như những bản vá.

Chạy một interactive shell:

[email protected]:~# docker run -i -t ubuntu /bin/bash

Để bật shell cần hai bước: download shell và khởi động shell. Vì chúng ta đã download Ubuntu image trước đó nên Docker đã cache nó, thời gian khởi động shell coi như bằng không do đó shell được bật lên ngay tức thì.

sudo apt-get install software-properties-common
sudo add-apt-repository ppa:webupd8team/java
sudo apt-get update
sudo apt-get install oracle-java8-installer
wget https://download.elasticsearch.org/elasticsearch/elasticsearch/elasticsearch-1.4.4.deb
sudo dpkg -i elasticsearch-1.4.4.deb
sudo /etc/init.d/elasticsearch start

Test

sudo apt-get install curl
curl http://localhost:9200

Trả về:

{
  "status" : 200,
  "name" : "Sergeant Fury",
  "cluster_name" : "elasticsearch",
  "version" : {
    "number" : "1.4.4",
    "build_hash" : "c88f77ffc81301dfa9dfd81ca2232f09588bd512",
    "build_timestamp" : "2015-02-19T13:05:36Z",
    "build_snapshot" : false,
    "lucene_version" : "4.10.3"
  },
  "tagline" : "You Know, for Search"
}

Make a commit Thoát khỏi interactive shell của Ubuntu

[email protected]:/# exit
exit
[email protected]:~# docker ps -l
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                            PORTS               NAMES
b13e5ac93389        ubuntu:latest       /bin/bash           30 minutes ago      Exited (127) About a minute ago                       insane_darwin

[email protected]:~# docker commit -m "install elasticsearch" b13e5ac93389 nhattan:elasticsearch
a760ec83b6393a7dc327839a1a8d811e66ca124251c7518bffd18c4c9650a885

Run container

[email protected]:~# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED              VIRTUAL SIZE
nhattan             elasticsearch       a760ec83b639        About a minute ago   841.1 MB
elasticsearch       latest              f57da6662fff        7 hours ago          363.6 MB
ubuntu              latest              d0955f21bf24        24 hours ago         188.3 MB

[email protected]:~# docker run a760ec83b639

[email protected]:~# docker ps -l
CONTAINER ID        IMAGE                   COMMAND             CREATED             STATUS                      PORTS               NAMES
6804e72df8db        nhattan:elasticsearch   /bin/bash           46 seconds ago      Exited (0) 45 seconds ago                       romantic_rosalind

Run elasticsearch in a container as a service

[email protected]:~# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
nhattan             elasticsearch       a760ec83b639        10 minutes ago      841.1 MB
elasticsearch       latest              f57da6662fff        7 hours ago         363.6 MB
ubuntu              latest              d0955f21bf24        24 hours ago        188.3 MB
[email protected]:~# docker run -p 80:80 a760ec83b639 /etc/init.d/elasticsearch start
 * Starting Elasticsearch Server
sysctl: setting key "vm.max_map_count": Read-only file system
   ...done.

Run elasticsearch on Docker

Cài đặt Elasticsearch trên Ubuntu image:

[email protected]:/# apt-get update
[email protected]:~# docker run elasticsearch
Unable to find image 'elasticsearch' locally
Pulling repository elasticsearch
f57da6662fff: Download complete
511136ea3c5a: Download complete
3b3a4796eef1: Download complete
50ec2d202fe8: Download complete
1073b544a1cb: Download complete
5e9b188bc82c: Download complete
45ff981e92b4: Download complete
42c3653e1b26: Download complete
44faa7b2809f: Download complete
3027412e8132: Download complete
5dd1542eedd6: Download complete
683ab114eceb: Download complete
92b2385a801c: Download complete
a2a7d7e64b4f: Download complete
2046e5c6261d: Download complete
5e9e422fe745: Download complete
29b49eb075bd: Download complete
[2015-03-21 03:07:34,224][INFO ][node                     ] [Gigantus] version[1.4.4], pid[1], build[c88f77f/2015-02-19T13:05:36Z]
[2015-03-21 03:07:34,226][INFO ][node                     ] [Gigantus] initializing ...
[2015-03-21 03:07:34,234][INFO ][plugins                  ] [Gigantus] loaded [], sites []
[2015-03-21 03:07:41,219][INFO ][node                     ] [Gigantus] initialized
[2015-03-21 03:07:41,223][INFO ][node                     ] [Gigantus] starting ...
[2015-03-21 03:07:41,516][INFO ][transport                ] [Gigantus] bound_address {inet[/0:0:0:0:0:0:0:0:9300]}, publish_address {inet[/172.17.0.4:9300]}
[2015-03-21 03:07:41,550][INFO ][discovery                ] [Gigantus] elasticsearch/rjUKRY5rSLCbXDcU7ruQXQ
[2015-03-21 03:07:45,363][INFO ][cluster.service          ] [Gigantus] new_master [Gigantus][rjUKRY5rSLCbXDcU7ruQXQ][8f49ac5faa3b][inet[/172.17.0.4:9300]], reason: zen-disco-join (elected_as_master)
[2015-03-21 03:07:45,426][INFO ][http                     ] [Gigantus] bound_address {inet[/0:0:0:0:0:0:0:0:9200]}, publish_address {inet[/172.17.0.4:9200]}
[2015-03-21 03:07:45,428][INFO ][node                     ] [Gigantus] started
[2015-03-21 03:07:45,463][INFO ][gateway                  ] [Gigantus] recovered [0] indices into cluster_state

Trong quá trình cài đặt nếu thiếu RAM dẫn tới việc cài đặt lâu bạn có thể tạo swapfile theo bài viết này create swapfile

Kiểm tra các images

[email protected]:~# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
elasticsearch       latest              f57da6662fff        4 hours ago         363.6 MB
ubuntu              latest              d0955f21bf24        21 hours ago        188.3 MB

Run elasticsearch:

[email protected]:~# docker run elasticsearch
[2015-03-21 03:47:20,264][INFO ][node                     ] [gen Harada] version[1.4.4], pid[1], build[c88f77f/2015-02-19T13:05:36Z]
[2015-03-21 03:47:20,265][INFO ][node                     ] [gen Harada] initializing ...
[2015-03-21 03:47:20,272][INFO ][plugins                  ] [gen Harada] loaded [], sites []
[2015-03-21 03:47:26,502][INFO ][node                     ] [gen Harada] initialized
[2015-03-21 03:47:26,503][INFO ][node                     ] [gen Harada] starting ...
[2015-03-21 03:47:26,712][INFO ][transport                ] [gen Harada] bound_address {inet[/0:0:0:0:0:0:0:0:9300]}, publish_address {inet[/172.17.0.5:9300]}
[2015-03-21 03:47:26,762][INFO ][discovery                ] [gen Harada] elasticsearch/xxEAFY1kRsKUzW8YIcHBvA
[2015-03-21 03:47:30,563][INFO ][cluster.service          ] [gen Harada] new_master [gen Harada][xxEAFY1kRsKUzW8YIcHBvA][2db666685840][inet[/172.17.0.5:9300]], reason: zen-disco-join (elected_as_master)
[2015-03-21 03:47:30,616][INFO ][http                     ] [gen Harada] bound_address {inet[/0:0:0:0:0:0:0:0:9200]}, publish_address {inet[/172.17.0.5:9200]}
[2015-03-21 03:47:30,620][INFO ][node                     ] [gen Harada] started
[2015-03-21 03:47:30,649][INFO ][gateway                  ] [gen Harada] recovered [0] indices into cluster_state
^C[2015-03-21 04:18:07,559][INFO ][node                     ] [gen Harada] stopping ...
[2015-03-21 04:18:07,616][INFO ][node                     ] [gen Harada] stopped
[2015-03-21 04:18:07,617][INFO ][node                     ] [gen Harada] closing ...
[2015-03-21 04:18:07,647][INFO ][node                     ] [gen Harada] closed

Run ở chế độ detached:

[email protected]:~# docker run -d elasticsearch
0f58f460bcdf5c8bf1adff22241bafc16b70ffc5c9af94267be6a838ef02b2b4

Liệt kê docker image đang chạy:

[email protected]:~# docker ps
CONTAINER ID        IMAGE                  COMMAND             CREATED             STATUS              PORTS                NAMES
0f58f460bcdf        elasticsearch:latest   elasticsearch       5 minutes ago       Up 5 minutes        9200/tcp, 9300/tcp   high_poincare

Stop running docker image:

[email protected]:~# docker stop cfc2e9bc19fb
cfc2e9bc19fb
[email protected]:~# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES

Dockerfile

Trước tiên vào https://hub.docker.com để tạo một automated build repo. Chọn repo ở github hoặc bitbucket có chứa file Dockerfile.

Ví dụ: https://github.com/nhattan/test

SSH vào server. gõ

docker build -t nhattan/test github.com/nhattan/test

References and more docs

  1. http://steveltn.me/blog/2014/03/15/deploy-rails-applications-using-docker/

  2. https://kitematic.com/

  3. http://dahernan.github.io/2013/08/04/docker-to-run-elasticsearch/