2017年5月14日 星期日

Docker 部署 Apache, Mysql

部屬 docker 於 centos 7 環境:



下載 Image

執行 centos 的 docker container,需要先下載 centos 的 image:
docker search centos //搜尋 centos 的 images
docker pull centos
下載 Lamp 的 Images,我自己測試這個 image 是 ok 的 (2017-05-15):

docker pull docker.io/andrewlau/centos-lamp

管理 Docker Container


啟動

啟動 Docker Container 之前,需要列出現在持有的 Image,並找到 hashcode:

docker images
列出後,啟動該 Image:

docker start [param] <hashcode>
[param] 說明:

  1.  -ti : 啟動後會直接跑進容器,卡住這個 tty 的執行緒
  2. -d : 背景執行
  3. -d -p [host_port:container_port] -p..... 背景執行或其他參數,加 -p 做連接埠映射
Example:

啟動剛才下載的 docker.io/andrewlau/centos-lamp 這個 Image:

docker run -d -p 80:80 -p 3306:3306 docker.io/andrewlau/centos-lamp


進入容器

進入容器之前,需要找到目前 container 運作狀態:

docker ps [param]
[param]說明:

  1. -a
  2. -l
找到容器的 hashcode 或是 name 之後,就可以進入容器:

docker exec -ti <name or hashcode> bash
bash 是要加的,代表進入容器的 bash。


重新啟動容器

docker restart <image hashcode>


強制停止容器

停止之前,需要先列出 container ,找到 hashcode:

docker ps -a
然後作停止:

docker kill <hashcode>...<hashcode>
可以一次停止多個,加上空白區分就好。

一般停止容器

docker stop <hashcode>...<hashcode>


Commit 容器

修改後的容器,可以做 commit:
docker commit -m "commit content" -a "author-name" <hashcode> <tag>:<v1>

匯出容器

docker export <hashcode> > name.tar

匯入容器

sudo docker import ./xxx.tar

Failed to get D-Bus connection: Operation not permitted

解法是啟動的時候使用:

docker run -d -p 80:80 -p 3306:3306 -v /sys/fs/cgroup:/sys/fs/cgroup:ro --name centos <image hashcode> /usr/sbin/init

打開後再檢查 systemctl status <services name> 來確認,用 systemctl start <services name> 開啟。


額外套件:
Rancher (網頁介面管理):
https://docs.rancher.com/rancher/v1.6/en/installing-rancher/installing-server/

Reference:
https://docs.docker.com/engine/reference/commandline/commit/#description
https://docs.docker.com/engine/reference/run/#operator-exclusive-options
https://www.linode.com/docs/applications/containers/how-to-install-docker-and-deploy-a-lamp-stack
https://www.gitbook.com/book/philipzheng/docker_practice
https://blog.hellosanta.com.tw/%E7%B6%B2%E7%AB%99%E8%A8%AD%E8%A8%88/%E4%BC%BA%E6%9C%8D%E5%99%A8/%E8%A6%96%E8%A6%BA%E5%8C%96%E7%AE%A1%E7%90%86%E7%9C%BE%E5%A4%9A-docker-%E5%AE%B9%E5%99%A8%E8%88%87%E9%83%A8%E7%BD%B2%E7%9A%84%E5%A5%BD%E5%B7%A5%E5%85%B7%EF%BC%9Arancher
http://dockone.io/question/729

沒有留言:

張貼留言

© Mac Taylor, 歡迎自由轉貼。
Background Email Pattern by Toby Elliott
Since 2014