[Docker系列·13]使用fig啟動容器

六翁發表於2017-01-04

fig的使用非常簡單,只需要配置fig.yml並在同目錄下執行fig up -d即可啟動容器。

配置fig.yml

node:
  image: feuyeux/ubuntu-node
  ports:
    - "8080:8080"
  links:
    - redis:node-redis
  mem_limit: 2g
  hostname: fig-node

redis:
  image: feuyeux/redis
  hostname: fig-redis

配置中的兩個映象的建立,請參考本系列前面的文章:

啟動容器

lu.hl@localhost:/opt/docker-room/fig-001$ fig up -d
Creating fig001_redis_1...
Creating fig001_node_1...

檢測容器

可以使用fig自己的命令檢測:

lu.hl@localhost:/opt/docker-room/fig-001$ fig ps
     Name                   Command               State               Ports
----------------------------------------------------------------------------------------
fig001_node_1    nodejs ./index.js                Up      22/tcp, 0.0.0.0:8080->8080/tcp
fig001_redis_1   redis-server /etc/redis/re ...   Up      22/tcp, 6379/tcp

也可以使用docker命令檢測:

lu.hl@localhost:/opt/docker-room/fig-001$ d ps
CONTAINER ID        IMAGE                        COMMAND                CREATED             STATUS              PORTS                            NAMES
c7f112d912b8        feuyeux/ubuntu-node:latest   "nodejs ./index.js"    28 minutes ago      Up 28 minutes       22/tcp, 0.0.0.0:8080->8080/tcp   fig001_node_1
394c9648c33a        feuyeux/redis:latest         "redis-server /etc/r   28 minutes ago      Up 28 minutes       22/tcp, 6379/tcp                 fig001_node_1/fig001_redis_1,fig001_node_1/node-redis,fig001_node_1/redis_1,fig001_redis_1

在另外一臺主機訪問docker的host,測試結果如下:

screenshot

雖然使用fig簡單,但小白還是有寫坑要踩。這裡列舉如下。

1.DOCKER_HOST

當系統沒有指定DOCKER_HOST引數時,執行fig命令會遇到如下錯誤。

Couldn`t connect to Docker daemon at http+unix://var/run/docker.sock - is it running?

If it`s at a non-standard location, specify the URL with the DOCKER_HOST environment variable.

解決的辦法是export該引數到當前終端或.bashrc

export DOCKER_HOST=tcp://localhost:4243
fig ps

2.DOCKER_OPTS

Ubuntu預設安裝的docker沒有啟動tcp監聽,因此這個約定俗成的埠4243也是要設定的。 可以在啟動docker時加引數,但如果是執行sudo service docker start的話,還是要設定一下DOCKER_OPTS:

配置/etc/default/docker

DOCKER_OPTS="-api-enable-cors=true -H tcp://0.0.0.0:4243 -H unix:///var/run/docker.sock --dns 8.8.8.8 --dns 8.8.4.4"
  • -api-enable-cors:這個引數的意義是允許ajax跨域資源共享。
  • tcp://0.0.0.0:4243:這個引數的意義是讓docker監聽所有4243埠的tcp請求(當然http是基於tcp之上的,remote api也是走這個埠) –

其他啟動相關的配置參見:/etc/init/docker.conf

3.cgroup_enable

Ubuntu預設是不允許使用cgroup做記憶體等資源的調配的,如果你在docker的配置檔案Dockerfile或者fig的配置檔案fig.yml中設定了容器的啟動記憶體,會遇到如下警告:

WARNING: Your kernel does not support cgroup swap limit. WARNING: Your kernel does not support swap limit capabilities. Limitation discarded.

解決的辦法是設定grub引數:

配置/etc/default/grub

GRUB_CMDLINE_LINUX="cgroup_enable=memory swapaccount=1"

然後執行sudo update-grub,重啟系統生效。

坑還不止於此,這是我踩過的。關於docker和fig的磚今天先拋到這裡。


相關文章