Environment construction with Docker (Ubuntu20.04) + Laravel + nginx

Created as a memorandum of self-learning that Docker is not very good at.

Overview

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.

System configuration

--host side

Folder structure

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)

PC (host) environment construction

  1. Install docker and composer from homebrew
$ brew install docker
$ brew install composer

Docker construction

Laravel construction

--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

Docker file creation

--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" ]
Create entrypoint.sh

--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 "$@"
Create php-fpm conf (/etc/php/7.4/fpm/pool.d/www.conf)

--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
Create nginx conf (laravel_sample.conf)

--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;
	#}
}

Create Docker composer

--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

build & container creation

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

スクリーンショット 2020-12-31 12.53.30.png

Recommended Posts

Environment construction with Docker (Ubuntu20.04) + Laravel + nginx
Laravel + MySQL + phpMyadmin environment construction with Docker
[Docker] Rails 5.2 environment construction with docker
React + Django + Nginx + MySQL environment construction with Docker
Laravel environment construction (Ubuntu 18.04 LTS)
React environment construction with Docker
Rails + MySQL environment construction with Docker
Node.js environment construction with Docker Compose
Environment construction with Docker for beginners
Laravel + Docker Laradock usage environment construction
Create Laravel environment with Docker (docker-compose)
[Environment construction with Docker] Rails 6 & MySQL 8
Build docker + laravel environment with laradock
Docker environment construction
[Portfolio] Manage site with laravel APP implementation (Docker environment construction)
Rails application development environment construction with Docker [Docker, Rails, Puma, Nginx, MySQL]
GPU environment construction with Docker [October 2020 version]
Rails environment construction with Docker (personal apocalypse)
Rails Docker environment construction
Spring Boot environment construction with Docker (January 2021 version)
Environment construction command memo with Docker on AWS
[Ubuntu 18.04] Environment construction for using PyTorch with RTX3090
Rails6 [API mode] + MySQL5.7 environment construction with Docker
Build mecab (NEologd dictionary) environment with Docker (ubuntu)
Wordpress local environment construction & development procedure with Docker
Pytorch execution environment with Docker
Deploy with EC2 / Docker / Laravel
virtulbox + vagrant + Docker + nginx + puma + MySQL Rails environment construction
MySQL 5.7 (Docker) environment construction memo
Redmine (Docker) environment construction memo
Build docker environment with WSL
Docker × Spring Boot environment construction
[Docker] postgres, pgadmin4 environment construction
Database environment construction with Docker in Spring boot (IntellJ)
Java + Spring development environment construction with VirtualBox + Ubuntu (Xfce4)
Easy environment construction of MySQL and Redis with Docker and Alfred
Case that gave up environment construction of react with ubuntu
[Windows] WSL2 + Ubuntu + Node.js environment construction
Create a Vue3 environment with Docker!
Build Couchbase local environment with Docker
Install java with Ubuntu 16.04 based Docker
Build a Node.js environment with Docker
Let's create Ubuntu environment with vmware
Build PlantUML environment with VSCode + Docker
Build environment with vue.js + rails + docker
General error: 1812 occurs with docker + laravel
Build Rails environment with Docker Compose
Create SolrCloud verification environment with Docker
Install laravel/Dusk in docker environment (laravel6)
Rails on Docker environment construction procedure
Laravel8 ubuntu20
Build a SPA for Laravel 6.2 / Vue.js / Nginx / Mysql / Redis with Docker
Ruby on Rails development environment construction with Docker + VSCode (Remote Container)
PostgreSQL environment construction with Docker (from setup to just before development)
Build a development environment for Django + MySQL + nginx with Docker Compose
"Rails 6 x MySQL 8" Docker environment construction procedure for sharing with teams
Alert slack with alert manager in Docker environment
SQL statement learning ~ Environment construction ~ Docker + MySQL
[Docker] Use environment variables in Nginx conf
Building Rails 6 and PostgreSQL environment with Docker
Sapper × Go (echo) × Docker development environment construction