I made this post as my personal notes that might be useful for me or anyone who reads this.
By using docker containers you can ship your app to anyone or to any machine without worrying about versioning error, incompatible dependency, etc.
So, in this post I have a Laravel app with postgres as the database. We are using multiple databases in this case.
To make it easy, we will dockerize our Laravel app, database postgres, and web server Nginx.
Prerequisite
Before we get started, we need to add new folder in our laravel app.
...rest of laravel dirs and files
|- infra
|--- app
|--- nginx
|------ conf
|--- pgsql
|------ script
Let's create infra
folder to store our docker configuration. Within infra
, we have app
to store laravel & PHP Dockerfile
, nginx
for web server, and pgsql
for database.
PHP
We need these PHP dependencies to make our app run.
So, let's check the dependencies in our docker image we are going to use.
docker run php:7.4-fpm php -m
We will see that some of the dependencies don't exist in the PHP image we are going to use. So let's create a custom image.
Create Dockerfile
inside app
folder.
-> /infra/app/Dockerfile
FROM php:7.4-fpm
RUN apt-get update \
&& apt-get install -y \
git \
curl \
libpng-dev \
libonig-dev \
libxml2-dev \
zip \
unzip \
zlib1g-dev \
libpq-dev \
libzip-dev
RUN docker-php-ext-configure pgsql -with-pgsql=/usr/local/pgsql \
&& docker-php-ext-install pdo pdo_pgsql pgsql zip bcmath gd
COPY --from=composer:latest /usr/bin/composer /usr/bin/composer
EXPOSE 9000
Basically this Dockerfile will install PHP dependencies we needed also the composer, and then will expose our PHP app in port 9000.
You can customize the extension, if you need it you can install, if you don't need it, you can remove it. You can see here for the list of commands to install PHP extension.
Create docker compose
Then we back to root of the project and create docker-compose.yml
there.
version: "3.7"
networks:
app-network:
driver: bridge
services:
app:
container_name: app
build:
context: ./infra/app
dockerfile: Dockerfile
image: php-laravel-7.0
restart: unless-stopped
tty: true
working_dir: /var/www
volumes:
- ./:/var/www
networks:
- app-network
Basically you can copy this configuration.
We can name our container in container_app
and image name in image
as we want.
./:/var/www
in volumes
means that, we copy our laravel app from laravel root ./
to our container working directory /var/www
.
You can read further more about network here.
...Continue...
Discussion (0)