2024年5月24日 星期五

Docker 開發筆記 - 使用 Dockerfile 透過 Ubuntu 18.04 建置 PHP7 + MySQL 5.7 + PHPMyAdmin 開發環境 @ macOS arm64

Ubuntu:18.04 + php7.2 + mysql5.7 + phpinfo();

此需求源自於舊網站的開發環境的建置,一般人不會碰到。

同事回報之前弄的 Docker 環境,因為從 Macbook Intel x86 架構,換到 Mac M2 ARM 環境後,出現問題。主因是網站使用很舊的 MySQL 5.6 環境,在 macos arm64 的 Docker 環境中,無法很便利的只用官方環境建置出來,預設只能直接升級到 MySQL 8 跑,接著還要面對 MySQL 5.6 跟 MySQL 5.7 以上的 SQL 語法變化。

在此的解法是放棄 MySQL 5.6 ,頂多弄出 MySQL 5.7 環境,並追蹤有哪些 SQL 語法需要修改,SQL語法方面,主要是面對 ONLY_FULL_GROUP_BY 模式的處理。此紀錄透過 Docker 弄個 MySQL 5.7 環境出來過度,且最終只需用 ubuntu:18.04 即可搞定大部分,以下是流水帳:

原先要用 Docker - Ubuntu 24.04 arm64 環境來使用,但追蹤後發現 Ubuntu 20.04, Ubuntu 22.04, Ubuntu 24.04 預設都只提供 MySQL 8 的環境,並且依照 MySQL APT Repository 的方式安裝 mysql-apt-config_0.8.30-1_all.deb 後,確認都沒 mysql-5.7 可選,而坊間其他教學文,主要是透過舊版 mysql-apt-config_0.8.12-1_all.deb 套件,在 Ubuntu 20.04 安裝了 mysql-5.7 ,但其過程是 mysql-apt-config 套件無法偵測出 Ubuntu 環境,透過人工選擇 Ubuntu 18.04 的環境裝到 mysql-5.7 而已,以下是個例子:

% docker run -it ubuntu:24.04 /bin/bash
root@2560222cf1ee:/# apt update && apt install -y wget lsb-release gnupg dialog
root@2560222cf1ee:/# wget https://dev.mysql.com/get/mysql-apt-config_0.8.30-1_all.deb
root@2560222cf1ee:/# dpkg -i ./mysql-apt-config_0.8.30-1_all.deb

MySQL APT Repo features MySQL Server along with a variety of MySQL components. You may select the appropriate product to choose the version that you
wish to receive.

Once you are satisfied with the configuration then select last option 'Ok' to save the configuration, then run 'apt-get update' to load package list.
Advanced users can always change the configurations later, depending on their own needs.

  1. MySQL Server & Cluster (Currently selected: mysql-8.4-lts)  3. MySQL Preview Packages (Currently selected: Disabled)
  2. MySQL Tools & Connectors (Currently selected: Enabled)      4. Ok

Which MySQL product do you wish to configure?
Which server version do you wish to receive?

來試試看裝舊版 mysql-apt-config_0.8.12-1_all.deb:

root@2560222cf1ee:/# dpkg -i ./mysql-apt-config_0.8.12-1_all.deb
root@bc33821f13de:/# apt update
W: GPG error: http://repo.mysql.com/apt/ubuntu cosmic InRelease: The following signatures couldn't be verified because the public key is not available: NO_PUBKEY B7B3B788A8D3785C
E: The repository 'http://repo.mysql.com/apt/ubuntu cosmic InRelease' is not signed.
N: Updating from such a repository can't be done securely, and is therefore disabled by default.
N: See apt-secure(8) manpage for repository creation and user configuration details.
root@bc33821f13de:/# apt-key adv --keyserver keyserver.ubuntu.com --recv-keys B7B3B788A8D3785C
Warning: apt-key is deprecated. Manage keyring files in trusted.gpg.d instead (see apt-key(8)).
Executing: /tmp/apt-key-gpghome.eheHF2erjp/gpg.1.sh --keyserver keyserver.ubuntu.com --recv-keys B7B3B788A8D3785C
gpg: key B7B3B788A8D3785C: public key "MySQL Release Engineering <mysql-build@oss.oracle.com>" imported
gpg: Total number processed: 1
gpg:               imported: 1
root@bc33821f13de:/# apt update
W: http://repo.mysql.com/apt/ubuntu/dists/cosmic/InRelease: Key is stored in legacy trusted.gpg keyring (/etc/apt/trusted.gpg), see the DEPRECATION section in apt-key(8) for details.
N: Skipping acquire of configured file 'mysql-tools/binary-arm64/Packages' as repository 'http://repo.mysql.com/apt/ubuntu cosmic InRelease' doesn't support architecture 'arm64'
N: Skipping acquire of configured file 'mysql-5.7/binary-arm64/Packages' as repository 'http://repo.mysql.com/apt/ubuntu cosmic InRelease' doesn't support architecture 'arm64'
N: Skipping acquire of configured file 'mysql-apt-config/binary-arm64/Packages' as repository 'http://repo.mysql.com/apt/ubuntu cosmic InRelease' doesn't support architecture 'arm64'

最後就輕鬆改用 ubuntu:18.04 ,再把 phpmyadmin 擺好即可,收工。

連續動作:

# Use the official Ubuntu 18.04 as the base image
FROM ubuntu:18.04

# Set non-interactive mode for APT
ENV DEBIAN_FRONTEND=noninteractive
ENV TZ=Asia/Taipei
ENV PHP_VERSION=7.2

# Update the package list and install necessary packages
RUN apt-get update && \
    apt-get install -y tzdata && \
    ln -fs /usr/share/zoneinfo/$TZ /etc/localtime && dpkg-reconfigure --frontend noninteractive tzdata && \
    apt-get install -y \
    htop \
    jq \
    vim \
    curl \
    wget \
    telnet \
    php7.2-cli \
    php7.2-curl \
    php7.2-fpm \
    php7.2-ldap \
    php7.2-mysql \
    php7.2-zip \
    phpunit \
    mysql-server-5.7 \
    ssl-cert \
    nginx \
    openssh-server && \
    apt-get clean && \
    rm -rf /var/lib/apt/lists/*

EXPOSE 22 80 443 8080 8443 3306

# Set up MySQL
RUN service mysql start && \
    mysql -e "CREATE USER 'developer'@'%' IDENTIFIED BY 'developer'; GRANT ALL PRIVILEGES ON *.* TO 'developer'@'%'; CREATE DATABASE developer; FLUSH PRIVILEGES;"

# Set up PHP-FPM Short Open Tag
RUN test -f /etc/php/$PHP_VERSION/fpm/php.ini && \ 
    sed -i 's/short_open_tag = Off/short_open_tag = On/' /etc/php/$PHP_VERSION/fpm/php.ini && \
    sed -i 's/max_execution_time = 30/max_execution_time = 300/' /etc/php/$PHP_VERSION/fpm/php.ini && \
    sed -i 's/upload_max_filesize = 2M/upload_max_filesize = 100M/' /etc/php/$PHP_VERSION/fpm/php.ini && \
    sed -i 's/post_max_size = 8M/post_max_size = 100M/' /etc/php/$PHP_VERSION/fpm/php.ini 

# Set up phpmyadmin
RUN cd /tmp && \
    wget https://www.phpmyadmin.net/downloads/phpMyAdmin-latest-all-languages.tar.gz && \
    tar xvzf phpMyAdmin-latest-all-languages.tar.gz && \
    mv phpMyAdmin-*-all-languages /usr/share/phpmyadmin && \
    mkdir -p /usr/share/phpmyadmin/tmp && \
    chown -R www-data:www-data /usr/share/phpmyadmin && \
    chmod 777 /usr/share/phpmyadmin/tmp

# Copy the entrypoint script
COPY nginx-php7.2-fpm-phpmyadmin.conf /etc/nginx/sites-enabled/phpmyadmin
COPY nginx-php7.2-fpm-website.conf /etc/nginx/sites-enabled/default 
COPY entrypoint.sh /usr/local/bin/
RUN chmod +x /usr/local/bin/entrypoint.sh

# Set the entrypoint
ENTRYPOINT ["/usr/local/bin/entrypoint.sh"]

# Start services in the foreground
CMD ["bash"]

# Define a volume for mounting the local directory
VOLUME ["/var/www/my-website"]

運行:

% ls
Dockerfile nginx-php7.2-fpm-phpmyadmin.conf
README.md nginx-php7.2-fpm-website.conf
entrypoint.sh
% docker build -t test .
% mkdir /tmp/php
% echo "<?php phpinfo();" > /tmp/php/index.php
% cat /tmp/php/index.php 
<?php phpinfo();
% docker run -p 80:80 -p 443:443 -p 8080:8080 -p 8443:8443 -v /tmp/php:/var/www/my-website -t test
 * Starting MySQL database server mysqld                                                           No directory, logging in with HOME=/
                                                                                            [ OK ]
 * Starting OpenBSD Secure Shell server sshd                                                [ OK ] 
 * Starting nginx nginx                                                                     [ OK ] 
Fri May 24 21:42:43 CST 2024

如此,就可以用 http://localhost, https://localhost/ 看到 phpinfo() 的資料,而用 http://localhost:8080, https://localhost:8443/ 則會看到 phpmyadmin 網頁服務

要關閉時,記得找一下 container id:

% docker ps
CONTAINER ID   IMAGE     COMMAND                   CREATED              STATUS              PORTS                                                                                                        NAMES
a1631b90d924   test      "/usr/local/bin/entr…"   About a minute ago   Up About a minute   0.0.0.0:80->80/tcp, 22/tcp, 0.0.0.0:443->443/tcp, 0.0.0.0:8080->8080/tcp, 3306/tcp, 0.0.0.0:8443->8443/tcp   thirsty_booth
% docker stop a1631b90d924
a1631b90d924

 其他資訊:changyy/docker-study/ubuntu18.04-php7.2-mysql5.7-phpmyadmin

沒有留言:

張貼留言