製作 php Docker Image 紀錄
因為想將之前一個 php 專案改用 Docker 方式處理, 進行修改的紀錄
原本系統安裝方式
- OS : CentOS Linux release 7.5.1804 (Core) (PVE VM)
- 安裝可支援套件
yum install epel-release rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm yum install -y sysstat net-snmp freetds subversion php70w php70w-opcache php70w-mysql php70w-gd php70w-mbstring php70w-soap php70w-xml php70w-pdo_dblib
- 安裝 Composer
su - root php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');" php -r "if (hash_file('SHA384', 'composer-setup.php') === '93b54496392c062774670ac18b134c3b3a95e5a5e5c8f1a9f115f203b75bf9a129d5daa8ba6a13e2cc8a1da0806388a8') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;" php composer-setup.php php -r "unlink('composer-setup.php');"
- 安裝 Google Client Library
php composer.phar require google/apiclient:^2.0
- 安裝專案 php 程式碼
mkdir -p /root/googlesheet/ svn co https://192.168.1.30/repos/erptools/googlesheet/ /root/googlesheet/ cd /root/googlesheet/ ln -s ../vendor .
- 使用語法
php /root/googlesheet/db-googlesheet.php 1I8mYIdjXZKPIGVMmpre8cTqIPxxxxxxxxxxJNr9zehE
改成 Dockerfile 安裝方式
- 環境改變項目:
- php 改用 7.1 → docker hub php:7.1-cli (OS:debian 10)
- 改用 git repo
- 程式碼修改
- 要增加支援 PostgreSQL
- 將原本讀取 DB 設定檔改用環境變數方式處理
- 先使用
docker run -i -t php:7.1-cli bash
進行安裝驗證, 然後將驗證的語法寫成以下 Dockerfile 內容
FROM dockerhub/library/php:7.1-cli # Install OS tools RUN apt-get update && apt-get install --no-install-recommends -y \ wget \ vim \ git \ unzip \ gnupg # Set timezone ENV TZ=Asia/Taipei RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone # Add PostgreSQL repository RUN echo "deb http://apt.postgresql.org/pub/repos/apt/ buster-pgdg main" > /etc/apt/sources.list.d/pgdg.list \ && curl -k -s https://www.postgresql.org/media/keys/ACCC4CF8.asc | apt-key add - # Install PHP extensions deps RUN apt-get update && apt-get install --no-install-recommends -y \ postgresql-server-dev-10 \ unixodbc-dev \ freetds-bin \ freetds-dev \ libssl-dev \ openssl # Install PHP extensions RUN docker-php-ext-configure pdo_dblib --with-libdir=/lib/x86_64-linux-gnu \ && pecl install sqlsrv-4.1.6.1 \ && pecl install pdo_sqlsrv-4.1.6.1 \ && docker-php-ext-install \ mbstring \ pgsql \ mysqli \ pdo_pgsql \ pdo_mysql \ pdo_dblib \ && docker-php-ext-enable \ sqlsrv \ pdo_sqlsrv \ opcache # Clean repository RUN apt-get clean \ && rm -rf /var/lib/apt/lists/* # Install Composer RUN curl -sS https://getcomposer.org/installer | php -- \ --install-dir=/usr/local/bin \ --filename=composer # Install composer lib RUN mkdir -p /root/googlesheet/ \ && cd /root/googlesheet/ \ && composer require google/apiclient:^2.0 # install php repo # cd /root && git clone https://192.168.1.31/root/erptools_googlesheet.git # cp -f /root/erptools_googlesheet/app/* /root/googlesheet/ COPY app /root/googlesheet WORKDIR /root/googlesheet ENTRYPOINT ["php", "db-googlesheet.php"]
- db-googlesheet.php 內
- 增加依據環境變數 TZ 的值來設定時區
: require_once __DIR__ . '/vendor/autoload.php'; if (isset($_ENV["TZ"])) { date_default_timezone_set($_ENV["TZ"]); } :
- 依據環境變數來設定原本設定檔案的參數
: // 判別環境變數是否有定義 DB_ID / DB_SERVER / DB_NAME / DB_USER / DB_PASSWD $db_id = $_ENV["DB_ID"]; if ($db_id == $t_DB_ID) { $db_server = $_ENV["DB_SERVER"]; $db_port = $_ENV["DB_PORT"]; $db_name = $_ENV["DB_NAME"]; $db_user = $_ENV["DB_USER"]; $db_passwd = $_ENV["DB_PASSWD"]; } else { print("The source database connection information (DB_ID, DB_SERVER, DB_NAME, DB_USER, DB_PASSWD) should be defined in the environment variables!\n"); exit; } :
- 建立出的 docker image 為 tryweb/db-googlesheet:latest
- 使用語法
docker run \ -e DB_ID=dev3-redmine \ -e DB_SERVER=192.168.4.13 \ -e DB_PORT=5432 \ -e DB_NAME=redminedb \ -e DB_USER=redmine \ -e DB_PASSWD=hBp1wxxxxxxxxxxsxgBmLv7Em4Hx \ tryweb/db-googlesheet 1I8mYIdjXZKPIGVMmpre8cTqIPxxxxxxxxxxJNr9zehE