嘗試使用 docker 部署 Laravel 專案

jaak發表於2019-08-24

環境: ubnutu18.04 虛擬機器上透過docker部署,首先需要安裝好docker環境
安裝docker

docker 映象要求

  • 基於Alpine Linux的容器。
  • Nginx Web伺服器。
  • PHP 7.0或更高版本。
  • OpenSSL PHP擴充套件。
  • PDO PHP擴充套件。
  • Mbstring PHP擴充套件。
  • Tokenizer PHP擴充套件。
  • XML PHP擴充套件。

1. 建立laradocker目錄

mkdir laradocker

2. 進入目錄cd laradocker 建立 Dockerfile 檔案 vim Dockerfile

FROM nginx:mainline-alpine
LABEL maintainer="Jaak zhang <xxxx@163.come>" 

COPY start.sh /start.sh
COPY nginx.conf /etc/nginx/nginx.conf
COPY supervisord.conf /etc/supervisord.conf
COPY site.conf /etc/nginx/sites-available/default.conf

RUN echo "#aliyun" > /etc/apk/repositories
RUN echo "https://mirrors.aliyun.com/alpine/v3.10/main/" >> /etc/apk/repositories
RUN echo "https://mirrors.aliyun.com/alpine/v3.10/community/" >> /etc/apk/repositories
RUN apk update

RUN apk add --update curl  php7-fpm php7  php7-dev  php7-apcu  php7-bcmath  php7-xmlwriter  php7-ctype  php7-curl  php7-exif  php7-iconv  php7-intl  php7-json  php7-mbstring php7-opcache  php7-openssl  php7-pcntl  php7-pdo  php7-mysqlnd  php7-mysqli  php7-pdo_mysql  php7-pdo_pgsql  php7-phar  php7-posix  php7-session  php7-xml  php7-simplexml  php7-mcrypt  php7-xsl  php7-zip  php7-zlib  php7-dom  php7-redis php7-tokenizer  php7-gd  php7-fileinfo  php7-zmq  php7-memcached  php7-xmlreader 

RUN curl -sS https://getcomposer.org/installer | \
php -- --install-dir=/usr/bin/ --filename=composer

RUN apk add --update bash vim openssh-client supervisor

RUN mkdir -p /etc/nginx && \
mkdir -p /etc/nginx/sites-available && \
mkdir -p /etc/nginx/sites-enabled && \
mkdir -p /run/nginx && \
ln -s /etc/nginx/sites-available/default.conf /etc/nginx/sites-enabled/default.conf && \
mkdir -p /var/log/supervisor && \
rm -Rf /var/www/* && \
chmod 755 /start.sh

RUN sed -i -e "s/;cgi.fix_pathinfo=1/cgi.fix_pathinfo=0/g" \
-e "s/variables_order = \"GPCS\"/variables_order = \"EGPCS\"/g" \
/etc/php7/php.ini && \
sed -i -e "s/;daemonize\s*=\s*yes/daemonize = no/g" \
-e "s/;catch_workers_output\s*=\s*yes/catch_workers_output = yes/g" \
-e "s/user = nobody/user = nginx/g" \
-e "s/group = nobody/group = nginx/g" \
-e "s/;listen.mode = 0660/listen.mode = 0666/g" \
-e "s/;listen.owner = nobody/listen.owner = nginx/g" \
-e "s/;listen.group = nobody/listen.group = nginx/g" \
-e "s/listen = 127.0.0.1:9000/listen = \/var\/run\/php-fpm.sock/g" \
-e "s/^;clear_env = no$/clear_env = no/" \
/etc/php7/php-fpm.d/www.conf

EXPOSE 443 80
WORKDIR /var/www

CMD ["/start.sh"]

3. laradocker目錄下建立start.sh 檔案; vim start.sh

#!/bin/bash

# ----------------------------------------------------------------------
# Create the .env file if it does not exist.
# ----------------------------------------------------------------------
if [[ ! -f "/var/www/.env" ]] && [[ -f "/var/www/.env.example" ]];
then
    cp /var/www/.env.example /var/www/.env
fi

# ----------------------------------------------------------------------
# Run Composer
# ----------------------------------------------------------------------
if [[ ! -d "/var/www/vendor" ]];
then
    cd /var/www
    composer update
    composer dump-autoload -o
fi

# ----------------------------------------------------------------------
# Start supervisord
# ----------------------------------------------------------------------
exec /usr/bin/supervisord -n -c /etc/supervisord.conf

4. laradocker目錄下建立nginx.conf 檔案; vim nginx.conf

user nginx;
worker_processes 1;

error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;

events {
    worker_connections 1024;
}

http {
    include /etc/nginx/mime.types;
    default_type application/octet-stream;
    access_log off;
    sendfile on;
    #tcp_nopush on;
    keepalive_timeout 65;
    #gzip on;
    include /etc/nginx/sites-enabled/*.conf;
}

5. 同樣建立site.conf檔案 vim site.conf

server {
    listen 80;

    root /var/www/public;
    index index.php index.html;

    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }

    location ~ /\. {
        deny all;
    }

    location ~ \.php$ {
        try_files $uri = 404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass unix:/var/run/php-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
}

6.建立supervisord.conf檔案


[unix_http_server]
file=/dev/shm/supervisor.sock

[supervisord]
logfile=/tmp/supervisord.log
logfile_maxbytes=50MB
logfile_backups=10
loglevel=warn
pidfile=/tmp/supervisord.pid
nodaemon=false
minfds=1024
minprocs=200
user=root

[rpcinterface:supervisor]
supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface

[supervisorctl]
serverurl=unix:///dev/shm/supervisor.sock

[program:php-fpm7]
command = /usr/sbin/php-fpm7 --nodaemonize --fpm-config /etc/php7/php-fpm.d/www.conf
autostart=true
autorestart=true
priority=5
stdout_logfile=/dev/stdout
stdout_logfile_maxbytes=0
stderr_logfile=/dev/stderr
stderr_logfile_maxbytes=0

[program:nginx]
command=/usr/sbin/nginx -g "daemon off;"
autostart=true
autorestart=true
priority=10
stdout_logfile=/dev/stdout
stdout_logfile_maxbytes=0
stderr_logfile=/dev/stderr
stderr_logfile_maxbytes=0

7. laradocker目錄下建立src資料夾 把你的laravel專案程式碼git clone下來 或者cpoy進去; laradocker 執行ls應該是下面幾個目錄檔案

root@localhost:~/laraDocker# ls
Dockerfile  nginx.conf  site.conf  src  start.sh  supervisord.conf
root@localhost:~/laraDocker# cd src/
root@localhost:~/laraDocker/src# ls
blog
# 這裡我clone下來的是個blog專案

8. 構建docker映象

$ docker build -t  my-image .

9.容器執行, 終端上執行以下命令:

# 將`$PWD/src`目錄掛載到`/var/www`容器中。`$PWD`將返回到當前工作目錄的路徑。
$ docker run -d -p 8000:80 -v $PWD/src/blog:/var/www --name="container-lara" my-image

10. 訪問你的虛擬機器IP+埠號8000, 我的是http://192.168.8.225:8000

出現錯誤 是laravel專案的key沒有,執行命令 docker exec -it container-lara bash進入容器
這裡其實也可以在start.sh檔案中先加入php artisan key:generate命令

root@localhost:~/laraDocker# docker exec -it container-lara bash
bash-5.0# ls
LICENSE            bootstrap          database           phpcs.sh           routes             vendor
README.md          composer.json      frontend           phpunit.xml        server.php         webpack.mix.js
app                composer.lock      package-lock.json  public             storage
artisan            config             package.json       resources          tests
bash-5.0# pwd
/var/www
bash-5.0# php artisan key:generate
Application key [base64:Br4OjMwsByCHcL797l/eyTu9V+gYTWnL+YgShopHTzI=] set successfully.
bash-5.0# 

依然報錯是因為mysql問題 沒有安裝mysql
使用docker拉取mysql映象

  • 執行命令docker pull mysql:5.7
  • 執行mysql容器 執行命令
    docker run -d -p 3306:3306 --name db-mysql -e MYSQL_ROOT_PASSWORD=123456 -e MYSQL_DATABASE=blog -d mysql:5.7
  • 修改src目錄下blog專案目錄下.env檔案中mysql相關配置即可

專案有用到redis,docker拉取redis映象

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

相關文章