使用 Docker 和 Laradock 進行 PHP 開發

garguntua發表於2017-09-22

為什麼要使用Docker

Docker能極大地減少環境配置和環境差異帶來的工作,能相當程度提高生產力.以往我們使用的Homestead等虛擬機器只能實現開發環境的共享,並不能用到生產環境中,無法減少運維的工作.

什麼是Docker?

Docker 是個劃時代的開源專案,它徹底釋放了計算虛擬化的威力,極大提高了應用的執行效率,降低了雲端計算資源供應的成本! 使用 Docker,可以讓應用的部署、測試和分發都變得前所未有的高效和輕鬆!

理解Docker

傳統的虛擬機器是虛擬一套硬體,再在這套硬體上跑系統,Docker是作業系統級別的虛擬化,寄宿於你計算機中原有的作業系統,共用一套硬體.相比傳統虛擬機器,Docker更像是一個沙箱,也更輕量高效.在雲端計算虛擬化方便應用非常廣泛.

核心概念

源(Registry)

類似各類Linux軟體源,下載映象的地方

倉庫(Repository)

各種版本映象的集合,例如Ubuntu是一個倉庫,這個倉庫裡放了各種版本的映象

映象(Image)

Docker 映象是一個特殊的檔案系統,除了提供容器執行時所需的程式、庫、資源、配置等檔案外,還包含了一些為執行時準備的一些配置引數(如匿名卷、環境變數、使用者等)。映象不包含任何動態資料,其內容在構建之後也不會被改變。概念上可以視作程式程式碼中 "類" 的概念.

容器(Container)

執行映象產生的就是容器,容器的實質是程式,容器之間是相互隔離的.概念上類比,類例項化產生的物件.

Docker常用命令

#映象
docker images #列出本地映象
docker rmi training/sinatra #刪除(在刪除映象之前要先用 docker rm 刪掉依賴於這個映象的所有容器)
docker run -t -i ubuntu:14.04 /bin/bash #
docker commit -m "Added json gem" -a "Docker Newbee" 0b2616b0e5a8 ouruser/sinatra:v2 #更新映象
docker tag 5db5f8471261 ouruser/sinatra:devel #修改標籤
docker build ${dockerfile_dir} #Dockerfile 構建
docker save -o ubuntu_14.04.tar ubuntu:14.04 #儲存
docker load --input ubuntu_14.04.tar #匯入
#容器
docker ps #檢視容器資訊
docker rm #刪掉容器(-f 刪除執行中)
docker inspect #檢視指定容器詳細資訊(可獲取ip,pid等資訊)
docker logs insane_babbage #檢視容器log
docker port CONTAINER [PRIVATE_PORT[/PROTO]] #檢視埠對映
docker start|stop|restart insane_babbage #啟動終止重啟
docker attach insane_babbage #進入後臺執行的容器 -d(推薦nsenter)
docker export 7691a814370e > ubuntu.tar #匯出快照
cat ubuntu.tar | sudo docker import - test/ubuntu:v1.0 #匯入快照
## docker hub 
docker search #搜尋映象
docker pull #下載
docker push #推送(需登入)

Laradock 是什麼?

Laradock是一個利用Docker實現的WEB環境配置工具,整合了大量WEB開發領域會用到的軟體,如Redis,Mysql,MongoDB等.為PHP/Laravel開發和部署提供了極大的便利

Laradock 常用命令

docker-compose:

  build              Build or rebuild services
  bundle             Generate a Docker bundle from the Compose file
  config             Validate and view the Compose file
  create             Create services
  down               Stop and remove containers, networks, images, and volumes
  events             Receive real time events from containers
  exec               Execute a command in a running container
  help               Get help on a command
  images             List images
  kill               Kill containers
  logs               View output from containers
  pause              Pause services
  port               Print the public port for a port binding
  ps                 List containers
  pull               Pull service images
  push               Push service images
  restart            Restart services
  rm                 Remove stopped containers
  run                Run a one-off command
  scale              Set number of containers for a service
  start              Start services
  stop               Stop services
  top                Display the running processes
  unpause            Unpause services
  up                 Create and start containers
  version            Show the Docker-Compose version information

Laradock官方提供了詳細的文件,非常贊
http://laradock.io/

一些經驗

  • 我們知道Docker的映象是不含資料的,所以我們的資料需要存到一個共享的資料捲上,Laradoc已經為我們配置好了
  • 要熟悉Laradoc,重點看專案目錄下的.env和docker-compose.yml,裡面有詳細的註釋和配置項
  • Docker的容器是相互隔離的,每個容器都有一套獨立的執行時環境,相當於執行在一個微型的Linux上面.注意雖然Laradock轉發了一些埠到宿主機127.0.0.1,但127.0.0.1對每個容器來說都是他自己,並不是宿主機.
  • 由於眾所周知的原因,Docker需要用國內映象加速
    也有一些開發者把映象裡的源都做了替換,如
    https://github.com/callect/laradock
    我個人並沒有用這個,不是很新,很多官方新出的映象沒有
    但如果用官網的版本,最要有高速科學上網的工具,不然就得手動改源了
  • win10需要專業版和企業版才能使用Hyper-V,這是Docker在windows上執行的基礎

國內使用者如何加速容器內的包安裝

推薦方法:為DockerFile設定http_proxy代理

docker-compose build --build-arg http_proxy=http://ip:port

參考資料:

Docker — 從入門到實踐(免費的電子書, star 5.6K +)

Laradock專案 star 3.7k+

本作品採用《CC 協議》,轉載必須註明作者和本文連結

相關文章