PhpStorm + Docker in Win10

跟主題無關的 murmur:來嘗試一種筆記方式,以「解決某個問題」或「達到某個目標」為主,記錄中間的試驗跟操作等過程。因為是過程記錄,路途中可能歪掉(?)、出現好像有關但最後跟解決方式無關的東西。


目標

目標是 Win10 下的 PhpStorm 可以用不同版本 PHP 執行程式。

一個方式是裝多個版本的 PHP (覺得把環境弄得很亂不蘇胡),既然 PhpStorm 的 CLI Interpreter 可以用 Docker,乾脆來玩一下 Docker~

雖然是在 Win10,但除了安裝 Docker 的版本不同,其他 Docker 操作基本在 Linux 應該是可以用的(畢竟這沒有牽涉到更細節的什麼 linux container、windows container 之類的,我也不確定那有沒有關係)。

Win10 Docker Installation

google it,裝完就忘了,大概是 Hyper-V 要開、Win10 要某個版本之後,然後去裝 Docker for Windows,可以參考這篇

Docker 的 Image & Container 極簡概念

  • image:類似 VM image 的東西,而且 image 可以一層層疊起來。
  • container:依據 image 開起來的 instance,container 的環境是互相隔離的。

利用 image tag 執行不同 PHP 版本的 container

1
2
$ docker run [OPTIONS] IMAGE[:TAG|@DIGEST] [COMMAND] [ARG...]
$ docker run php:<version>-cli

不加 :<version>-cli 就能執行最新版。這裡有 PHP image 支援的 tag

跑 phpunit image(需要 login Docker Hub)

$ docker run phpunit/phpunit

以 php container 執行本機的 php file

$ docker run -it --rm --name my-running-script -v D:\tmp:/usr/src/myapp -w /usr/src/myapp php php hello.php

  • -t : Allocate a pseudo-tty
  • -i : Keep STDIN open even if not attached
    • -i-t 可以合寫成 -it,需要 -it 才能跟 interactive process 互動。
  • -rm:預設上 container 的 file system 在 container 結束後仍會保留。-rm 可以在 container 結束後自動 clean up container 以及刪除其 file system。
  • --name 指定 container 的名字
  • -w 指定 container 中跑執行檔的 working directory,預設是 root directory (/)
  • -v 是做 shared volume
    • -v, --volume=[host-src:]container-dest[:<options>]: Bind mount a volume.
    • 最簡單的用法是 host 跟 container 的 file system mapping,將 host 的某個路徑 mapping 到 container 的某個路徑。更詳細的說明

docker run 可以指定 container 中要執行的指令,也就是上面指令的第二個 php(第一個 php 是 image 名稱),當然也可以不指定、直接執行 image 預設的指令。

總結一下,這個指令是把 host 的 D:\tmp mapping 到 container 的 /usr/src/myapp,並將 container working directory 改為 /usr/src/myapp,以 php 執行 hello.phphello.php 當然是放在 host 的 D:\tmp\)。

Docker Build Image

開個資料夾,在裡面建立檔案 Dockerfile,內容如下:

1
2
3
4
5
6
7
8
9
10
11
# Use an official PHP runtime as a parent image
FROM php

# Set the working directory to /app
WORKDIR /app

# Copy the current directory contents into the container at /app
COPY . /app

# Run app.py when the container launches
CMD ["php", "hello.php"]

新增 hello.php,裡面隨意寫點 PHP code。最後在 command line 切到該資料夾,執行:

$ docker build -t myhelloworld:first .

如此即建立一個名稱為 myhelloworld、tag 為 first(不加 tag 也可以)的 image,可以用 docker image ls 看到。也可以不指定名稱,每個 image 都有 id。

PhpStorm + Docker

在 PhpStorm 裡要先設定 Docker Server。

  1. Docker daemon 開啟 localhost:2375
  1. PhpStorm 設定 Docker server

用 Docker 當 CLI interpreter

  1. 進 PHP 設定,點 CLI Interpreter 右邊的 ...
  1. 增加 CLI Interpreter From Docker
  1. 選擇 Docker server 及 Image
  1. Load 到 PHP 相關資訊啦~
  1. 選擇剛剛新增的 interpreter

設定完就能用 Docker container 的 php 執行 code 啦~

需要多個 PHP 版本可以設定多個 interpreter 來切換,不會搞得 host 環境裝一堆不同版本 PHP 難以管理。

Build & Run Dockerfile

  1. 在 project 裡建立 Dockerfile,如上
  2. 編輯 Run configuration
  1. 增加 Dockerfile configuration
  1. 選擇 Dockerfile 檔案
  1. 執行!

執行一次會產生一個 image,它會先 build 一個 image 再去 run 它。

Build Docker image

也可以直接在 Dockerfile build image: