Docker
在瀏覽器中服務 Laravel 的 Docker 容器
我正在嘗試為 laravel 應用程序啟動一個 docker 容器:-
編輯
Dockerfile.nginx
FROM nginx:alpine COPY ./nginx/my-site-config.conf /etc/nginx/conf.d/default.conf
nginx/my-site-config.conf
server { listen 8027; server_name localhost; # handle .php location ~ \.php$ { # 404 try_files $fastcgi_script_name =404; # default fastcgi_params include fastcgi_params; # fastcgi settings fastcgi_pass php-fpm-container:9000; fastcgi_index index.php; fastcgi_buffers 8 16k; fastcgi_buffer_size 32k; # fastcgi params fastcgi_param DOCUMENT_ROOT $realpath_root; fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name; fastcgi_param PHP_ADMIN_VALUE "open_basedir=$base/:/usr/lib/php/:/tmp/"; } }
碼頭工人-compose.yml
#Nginx Service <-- doesn't work # webserver: # build: # context: . # dockerfile: Dockerfile.nginx # container_name: webserver # restart: unless-stopped # tty: true # ports: # - "8027:80" # - "443:443" # networks: # - app-network # works webserver: image: nginx:alpine container_name: webserver restart: unless-stopped tty: true ports: - "8027:80" - "443:443" networks: - app-network
註釋掉的程式碼無法啟動:-
使用未註釋的程式碼並
nginx:alpine
作為圖像 - 伺服器託管 nginx 啟動螢幕。我的Dockerfile.nginx
出現了問題。
檢查你
nginx:alpine
的 nginx 配置docker run -it --rm nginx:alpine /bin/sh / # cd /etc/nginx /etc/nginx # ls -l total 44 drwxr-xr-x 2 root root 24 Dec 17 15:01 conf.d -rw-r--r-- 1 root root 1077 Dec 15 14:55 fastcgi.conf -rw-r--r-- 1 root root 1007 Dec 15 14:55 fastcgi_params -rw-r--r-- 1 root root 2837 Dec 15 14:55 koi-utf -rw-r--r-- 1 root root 2223 Dec 15 14:55 koi-win -rw-r--r-- 1 root root 5231 Dec 15 14:55 mime.types lrwxrwxrwx 1 root root 22 Dec 17 15:01 modules -> /usr/lib/nginx/modules -rw-r--r-- 1 root root 646 Dec 15 14:55 nginx.conf -rw-r--r-- 1 root root 636 Dec 15 14:55 scgi_params -rw-r--r-- 1 root root 664 Dec 15 14:55 uwsgi_params -rw-r--r-- 1 root root 3610 Dec 15 14:55 win-utf
這是預設的 nginx.conf
/etc/nginx # cat nginx.conf user nginx; worker_processes auto; 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; log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; access_log /var/log/nginx/access.log main; sendfile on; #tcp_nopush on; keepalive_timeout 65; #gzip on; include /etc/nginx/conf.d/*.conf; }
在文件夾裡面
conf.d
,有一個預設的伺服器配置/etc/nginx # ls -l conf.d/ total 4 -rw-r--r-- 1 root root 1093 Dec 15 14:55 default.conf
其中有一個非常基本的伺服器部分
/etc/nginx # grep -vE " *#|^ *$" conf.d/default.conf server { listen 80; server_name localhost; location / { root /usr/share/nginx/html; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root /usr/share/nginx/html; } }
如您所見,它僅配置為伺服器靜態內容,並且沒有將請求轉發到
*.php
支持的 php-fpm 的部分。通常你需要替換這個文件(或者如果你需要更多的控制,
nginx.conf
一起覆蓋)。至少您需要在伺服器配置中使用一個部分來將請求重定向到 PHP 到後端。
# handle .php location ~ \.php$ { # 404 try_files $fastcgi_script_name =404; # default fastcgi_params include fastcgi_params; # fastcgi settings fastcgi_pass php-fpm-container:9000; # <--- this is the PHP-FPM container fastcgi_index index.php; fastcgi_buffers 8 16k; fastcgi_buffer_size 32k; # fastcgi params fastcgi_param DOCUMENT_ROOT $realpath_root; fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name; fastcgi_param PHP_ADMIN_VALUE "open_basedir=$base/:/usr/lib/php/:/tmp/"; }
你可以使用DO 的 NGINX 配置工具來幫助你為你的應用程序創建一個好的起點,然後你可以在
nginx:apline
容器中覆蓋它。請注意,nginx 容器和 php 容器都需要訪問您的 PHP 應用程序文件。
編輯1
自定義 nginx 鏡像
FROM nginx:alpine # If you have a bundle of config created by DO config file, # you can copy them all to overwrite all the settings. # or use # to overwrite a single file # COPY my-site-config.conf /etc/nginx/conf.d/default.conf
更新您的 docker-compose 以使用您的自定義版本的 nginx 映像
version: '3' services: #PHP Service app: build: context: . dockerfile: Dockerfile image: digitalocean.com/php container_name: app restart: unless-stopped tty: true environment: SERVICE_NAME: app SERVICE_TAGS: dev working_dir: /var/www networks: - app-network #Nginx Service webserver: build: context: . dockerfile: Dockerfile.nginx container_name: webserver restart: unless-stopped tty: true ports: - "8027:80" - "443:443" networks: - app-network
編輯2
似乎您缺少 Docker 如何工作以及如何使用多容器環境的一些關鍵部分,對於初學者,我建議您從一個像這樣的工作模板開始(閱讀所有頁面以了解綁定掛載點的含義)正如您所看到的
app
(PHP 容器)和webserver
通過綁定掛載訪問文件的方式,此模板還為您提供了在不建構新容器的情況下覆蓋 php 配置和 nginx 的選項。version: '3' services: #PHP Service app: build: context: . dockerfile: Dockerfile image: digitalocean.com/php container_name: app restart: unless-stopped tty: true environment: SERVICE_NAME: app SERVICE_TAGS: dev working_dir: /var/www volumes: - ./:/var/www - ./php/local.ini:/usr/local/etc/php/conf.d/local.ini networks: - app-network #Nginx Service webserver: image: nginx:alpine container_name: webserver restart: unless-stopped tty: true ports: - "80:80" - "443:443" volumes: - ./:/var/www - ./nginx/conf.d/:/etc/nginx/conf.d/ networks: - app-network #MySQL Service db: image: mysql:5.7.22 container_name: db restart: unless-stopped tty: true ports: - "3306:3306" environment: MYSQL_DATABASE: laravel MYSQL_ROOT_PASSWORD: your_mysql_root_password SERVICE_TAGS: dev SERVICE_NAME: mysql volumes: - dbdata:/var/lib/mysql/ - ./mysql/my.cnf:/etc/mysql/my.cnf networks: - app-network #Docker Networks networks: app-network: driver: bridge #Volumes volumes: dbdata: driver: local