Skip to content

Instantly share code, notes, and snippets.

@fomvasss
Last active September 23, 2024 11:14
Show Gist options
  • Save fomvasss/647e0bccb242197cc386711d22365eea to your computer and use it in GitHub Desktop.
Save fomvasss/647e0bccb242197cc386711d22365eea to your computer and use it in GitHub Desktop.
Use Docker, docker-compose, Laradoc

Docker, Docker-compose, Laradoc

Install using the repository (Ubuntu)

(https://docs.docker.com/engine/install/ubuntu/)

Set up the repository

sudo apt-get update


sudo apt-get install \
    ca-certificates \
    curl \
    gnupg \
    lsb-release

curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg


echo \
  "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu \
  $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null    

Install Docker Engine

sudo apt-get update
sudo apt-get install docker-ce docker-ce-cli containerd.io

Install Docker Compose

sudo curl -L "https://github.com/docker/compose/releases/download/1.29.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose

Check

sudo usermod -aG docker $USER
sudo reboot
(?) sudo systemctl restart docker.service
docker -v
docker run hello-world

Laradoc Installation

(https://laradock.io/getting-started/)

Setup for Multiple Projects

Example: Your projects located in ~/Work/projects/...!

cd ~/Work
git clone https://github.com/laradock/laradock.git
cd laradock
cp .env.example .env

Replace {username} - your user name!

Edit .env:

APP_CODE_PATH_CONTAINER=/home/{username}/Work
PHP_FPM_INSTALL_EXIF=true
PHP_VERSION=8.0

Add New project (Example name: shop)

Files project places in dir shop - Work/projects/shop

Open dir nginx/sites/:

cp laravel.conf.example shop.conf

Edit shop.conf: ... server { server_name shop.test; root /home/{username}/Work/projects/shop/public; } ...

Add the domains to the /etc/hosts file. Add line:

127.0.0.1 adminer.test
127.0.0.1 shop.test

Run after change:

docker-compose down 
docker-compose up -d --build workspace php-fpm adminer nginx mysql

After add new project:

docker-compose restart nginx

Other workspace:

docker-compose up -d workspace-73
docker-compose exec --user=laradock workspace-73 bash

Open container (ssh)

docker-compose exec --user=laradock workspace bash

In Laravel project set .env:

DB_CONNECTION=mysql
DB_HOST=mysql
DB_PORT=3306
DB_DATABASE=shop
DB_USERNAME=root
DB_PASSWORD=root

Adminer http://127.0.0.1:8081/?server=mysql&username=root

Elasticsearch

docker compose build --no-cache elasticsearch
docker compose up --force-recreate --no-deps -d elasticsearch
docker compose ps

https://laradock.io/documentation/#use-elasticsearch https://dev.to/dendihandian/elasticsearch-in-laradock-nm4

Fix memory

sysctl -w vm.max_map_count=262144

https://stackoverflow.com/questions/51445846/elasticsearch-max-virtual-memory-areas-vm-max-map-count-65530-is-too-low-inc

Add plugins

add to elasticsearch/Dockerfile https://i.imgur.com/oUfr1ZC.png:

RUN elasticsearch-plugin install analysis-ukrainian
RUN elasticsearch-plugin install analysis-icu

run commands:

docker compose down elasticsearch
docker compose build --no-cache elasticsearch
docker compose up -d elasticsearch

Fix bugs

Edit php-fpm/opcache.ini set opcache.enable=0

docker-compose up --force-recreate --no-deps -d workspace
docker-compose build --no-cache workspace php-fpm adminer nginx mysql
docker-compose build php-fpm
docker-compose up -d --force-recreate php-fpm
docker-compose build --no-cache
docker-compose build workspace-73
docker-compose build nginx
docker-compose build php-fpm-73 
docker-compose rm nginx
docker-compose up -d nginx
docker-compose up -d php-fpm-73 
docker-compose up -d workspace-73

Composet AUTH - laradock/laradock#3190

WebSockets - laradock/laradock#2002

NGINX Error - https://stackoverflow.com/questions/50665028/docker-error-for-nginx-cannot-start-service-nginx-driver-failed-programming-ex

Cache - docker-compose build --no-cache workspace

Fix MySQL access

docker compose exec -it mysql bash
mysql

ALTER USER 'root'@'%' IDENTIFIED BY 'root';
flush privileges;

CREATE USER 'root'@'%' IDENTIFIED BY 'root'; 
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' WITH GRANT OPTION;
flush privileges;

docker compose restart mysql

GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' WITH GRANT OPTION;

Fix Mysql 8.4 (SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: Temporary failure)

В mysql/Dockerfile якщо є 1) то замінити на 2), якщо немає 1) - то просто вставити 2) (в кінець файлу): 1)

RUN if [ ${MYSQL_VERSION} > '8.4.0-0.000' ]; then \
  echo 'mysql_native_password=on' >> /etc/mysql/conf.d/my.cnf \
else \
  echo 'default-authentication-plugin=mysql_native_password' >> /etc/mysql/conf.d/my.cnf \
;fi
RUN version_gt() { \
  test "$(printf '%s\n' "$@" | sort -V | head -n 1)" != "$1"; \
  }; \
  mysql_version_trimmed=$(echo "$MYSQL_VERSION" | grep -oE '^[0-9]+\.[0-9]+'); \
  if version_gt "$mysql_version_trimmed" "8.3"; then \
  echo 'mysql_native_password=on' >> /etc/mysql/conf.d/my.cnf ; \
  else \
  echo 'default-authentication-plugin=mysql_native_password' >> /etc/mysql/conf.d/my.cnf ; \
  fi

В laradock mysql/my.cnf закоментувати (видалити) ряд: default-authentication-plugin=mysql_native_password

Add PHP versions

add next to nginx/Dockerfile

# Set upstream conf and remove the default conf
 ....
 
RUN echo "upstream php-upstream-80 { server php-fpm-80:9000; }" >> /etc/nginx/conf.d/upstream.conf
RUN echo "upstream php-upstream-70 { server php-fpm-70:9000; }" >> /etc/nginx/conf.d/upstream.conf
RUN echo "upstream php-upstream-73 { server php-fpm-73:9000; }" >> /etc/nginx/conf.d/upstream.conf

Add conf in docker-compose.yml (for php 8.0):

workspace-80 (remove section `ports`)
...
php-fpm-80

in added conf (workspace-80, php-fpm-80) not use options:

      depends_on:
        - workspace

Set upstream in nginx/sites:

server {
//...
    location ~ \.php$ {
        //...
        fastcgi_pass php-upstream-80;
        //...
    }
}

Run:

docker compose build --no-cache workspace-73 php-fpm-73 nginx
docker compose restart nginx
docker compose up -d workspace-73 php-fpm-73 adminer nginx mysql
docker compose exec --user=laradock workspace-73 bash

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment