Created as a memorandum of self-learning that Docker is not very good at.
When building a Web server in the host environment, do you want to build it in the local environment with the same OS as host? ?? This time, as a host environment, I tried ubutu (20.04), reverse proxy with nginx, and application framework with laravel. I will share the contents.
--host side
Laravel ##PJ root directory
├── docker
│ ├── Dockerfile
│ ├── docker-compose.yml
│ ├── entrypoint.sh
│ └── etc
│ ├── nginx
│ │ └── conf.d
│ │ └── laravel_sample.conf
│ └── php
│ └── 7.4
│ └── fpm
│ └── pool.d
│ └── www.conf
└── phpsample ##laravel project folder(Download sample project with composer)
$ brew install docker
$ brew install composer
--For the time being, create a Laravel sample project with composer as a sample this time. --Laravel version is the latest 8.0
$ composer create-project --prefer-dist laravel/laravel:^8.0 phpsample
--Set the following contents --Install the php package required to start laravel --Install nginx as a reverse proxy --Copy the customized conf file so that the contents of the customized nginx and php-fpm conf can be read when creating the container. --php-fpm socket file was not generated at build time, so start it with entrypoint --In addition to the above, start the nginx service at the entry point as well.
From ubuntu:20.04
LABEL Name=web_app_test
ENV TZ Asia/Tokyo
RUN apt-get update && apt-get install -y \
tzdata \
php-mbstring \
php-xml \
php-fpm \
php-zip \
php-common \
php-fpm \
php-cli \
unzip \
curl \
nginx \
&& mkdir /home/ubuntu \
&& rm -f /etc/nginx/sites-enabled/default
COPY /etc/php/7.4/fpm/pool.d/www.conf /etc/php/7.4/fpm/pool.d/www.conf
COPY ./entrypoint.sh /entrypoint.sh
COPY /etc/nginx/conf.d/laravel_sample.conf /etc/nginx/conf.d/laravel_sample.conf
ENTRYPOINT [ "/entrypoint.sh" ]
CMD [ "bash" ]
--Set the following contents
--Lastly, it is necessary to execute exec" $ @ "
as a magic (the situation where I forgot this and failed many times)
――The reason why this is necessary is to enable receiving unix signals as described in the official docker documentation. For more information, see Official Dokcer Reference
#!/bin/sh
service nginx start
service php7.4-fpm start
exec "$@"
--Set the following contents
--Since user and group of nginx are www-data
, listen.owner and listen.group are modified together.
[www]
user = www-data
group = www-data
listen = /run/php/php7.4-fpm.sock
listen.owner = www-data
listen.group = www-data
listen.mode = 0660
--Set the following contents
--Set root as / phpsample/public
to place laravel in/phpsample
--Set location php
--fastcgi_pass
is the socket path set in php-fpm above
server {
listen 80 default_server;
listen [::]:80 default_server;
root /phpsample/public;
# Add index.php to the list if you are using PHP
index index.html index.php index.htm index.nginx-debian.html;
server_name _;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404;
}
# pass PHP scripts to FastCGI server
#
location ~ \.php$ {
include snippets/fastcgi-php.conf;
# With php-fpm (or other unix sockets):
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
# With php-cgi (or other tcp sockets):
# fastcgi_pass 127.0.0.1:9000;
}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
--Set as follows
--Source code can be read from host using volumes
--As a precaution, the bash process should be init: true
to prevent it from being pid 1
.
version: '3.8'
services:
app:
build:
context: .
dockerfile: Dockerfile
container_name: "laravel-app"
volumes:
- ../phpsample:/phpsample/
ports:
- "80:80"
tty: true
init: true
Enter the command as follows to build the image and create the container
$ docker-compose build
$ docker-compose up -d
Confirm that you can see the sample Laravel app by typing localhost /
in your browser
Recommended Posts