Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

hw1: add initial docker compose config #21

Open
wants to merge 2 commits into
base: APogorelov/main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
MYSQL_USER=
MYSQL_PASSWORD=
MYSQL_ROOT_PASSWORD=
REDIS_PASSWORD=
10 changes: 10 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
init: docker-down docker-build docker-up

docker-down:
docker compose down --remove-orphans

docker-build:
docker compose build

docker-up:
docker compose up -d
48 changes: 48 additions & 0 deletions code/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

echo "<pre>";

$redis = new Redis();
$redis->connect('redis', 6379);
$redis->auth(getenv("REDIS_PASSWORD"));

if ($redis->ping()) {
print_r('Redis is alive!!!!!!');
echo "<hr />";
}

$mc = new Memcached();
$mc->addServer("memcached", 11211);
$mc->set("foo", "Hello!");
$mc->set("bar", "Memcached...");
$data = [
$mc->get("foo"),
$mc->get("bar")
];

print_r($data);
echo "<hr />";

try {
$user = getenv("DB_USER");
$pass = getenv("DB_PASSWORD");
$opt = [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
PDO::ATTR_EMULATE_PREPARES => false,
];
$pdo = new PDO("mysql:host=mysql;dbname=mysite", $user, $pass, $opt);
$stmt = $pdo->query('SHOW DATABASES;');

while ($row = $stmt->fetch()) {
print_r($row);
}
} catch (\Throwable $e) {
print_r($e->getMessage());
}

echo "<hr />";

echo "</pre>";

phpinfo();
70 changes: 70 additions & 0 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
version: '3'

services:
nginx:
image: nginx:latest
container_name: mysite-nginx
depends_on:
- php-fpm
ports:
- "80:80"
volumes:
- ./code:/data/mysite.local
- ./nginx/hosts/mysite.local.conf:/etc/nginx/conf.d/mysite.local.conf
networks:
- app-network

php-fpm:
build:
context: ./fpm
dockerfile: Dockerfile
image: mysite/php-fpm
container_name: mysite-php-fpm
environment:
- REDIS_PASSWORD=${REDIS_PASSWORD}
- DB_USER=${MYSQL_USER}
- DB_PASSWORD=${MYSQL_PASSWORD}
depends_on:
- memcached
- redis
volumes:
- ./code:/data/mysite.local
networks:
- app-network

memcached:
image: memcached:latest
container_name: mysite-memcached
networks:
- app-network

redis:
image: redis:latest
container_name: mysite-redis
environment:
- REDIS_PASSWORD=${REDIS_PASSWORD}
command: sh -c 'exec redis-server --requirepass "$REDIS_PASSWORD"'
networks:
- app-network

mysql:
image: mysql:8.0
container_name: mysite-mysql
ports:
- "3306:3306"
environment:
MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD}
MYSQL_DATABASE: mysite
MYSQL_USER: ${MYSQL_USER}
MYSQL_PASSWORD: ${MYSQL_PASSWORD}
volumes:
- mysite-dbdata:/var/lib/mysql
networks:
- app-network

networks:
app-network:
driver: bridge

volumes:
mysite-dbdata:
36 changes: 36 additions & 0 deletions fpm/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
FROM php:8.2-fpm

# ставим необходимые для нормальной работы модули
RUN apt-get update \
&& apt-get install -y \
libfreetype6-dev \
libjpeg62-turbo-dev \
libpng-dev \
libonig-dev \
libzip-dev \
libmemcached-dev \
zlib1g-dev \
libssl-dev \
libz-dev \
libmcrypt-dev \
&& pecl install mcrypt-1.0.6 \
&& docker-php-ext-enable mcrypt \
&& docker-php-ext-install -j$(nproc) iconv mbstring mysqli pdo_mysql zip \
&& docker-php-ext-configure gd --with-freetype --with-jpeg \
&& docker-php-ext-install -j$(nproc) gd \
&& pecl install redis \
&& docker-php-ext-enable redis

RUN yes '' | pecl install -f memcached-3.2.0 \
&& docker-php-ext-enable memcached

# Устанавливаем Composer
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer

COPY ./php.ini /usr/local/etc/php/conf.d/php-custom.ini

WORKDIR /data

VOLUME /data

CMD ["php-fpm"]
2 changes: 2 additions & 0 deletions fpm/php.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
session.save_handler = memcache
session.save_path = "tcp://memcache:11211"
25 changes: 25 additions & 0 deletions nginx/hosts/mysite.local.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
server {
listen 80;
server_name mysite.local;
root /data/mysite.local;
index index.php index.html;

location ~* .(jpg|jpeg|gif|css|png|js|ico|html)$ {
access_log off;
expires max;
}

location / {
try_files $uri $uri/ /index.php?$query_string;
}

location ~* .php$ {
try_files $uri = 404;
fastcgi_split_path_info ^(.+.php)(/.+)$;
fastcgi_pass php-fpm:9000;
#fastcgi_pass unix:/var/run/php-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
Loading