Inception Step 2: Make containers

James An·2022년 10월 5일
0

42SEOUL

목록 보기
10/11
post-custom-banner

요구사항

  • nginx container
  • wordpress + php-fpm container
  • mariadb container

개요

  • Step1에서 docker-compose로 Dockerfile을 읽어 nginx를 빌드하고 컨테이너를 생성하는 방법에 대해 알아보았다.
  • Step2에서는 요구사항에 맞춰 나머지 wordpress, mariadb 각각의 컨테이너를 생성해보자.

1. nginx container


2. wordpress + php-fpm container

Dockerfile

FROM	debian:buster

RUN		apt-get update -y && \
		apt-get upgrade -y && \
		apt-get -y install \
		mariadb-client \
		wget \
		curl \
		tar \
		php7.3-fpm \
		php-cli \
		php-mysql \
		vim \
		sudo

COPY	./tools/wordpress_setup.sh /tmp/wordpress_setup.sh
COPY	./conf/www.conf /etc/php/7.3/fpm/pool.d/www.conf
RUN		wget https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar && chmod +x wp-cli.phar && mv wp-cli.phar /usr/bin/wp

WORKDIR	/var/www/html/wordpress
RUN		mkdir -p /run/php

EXPOSE	9000

ENTRYPOINT ["sh", "/tmp/wordpress_setup.sh"]

wordpress.setup.sh

#!/bin/sh

chown -R www-data:www-data /var/www/

sudo -u www-data sh -c " \
wp core download --locale=$WORDPRESS_LANG && \
wp config create --dbname=$WORDPRESS_DB_HOST --dbuser=$WORDPRESS_DB_USER --dbpass=$WORDPRESS_DB_PASSWORD --dbhost=$WORDPRESS_DB_HOST --dbcharset="utf8"
wp core install --url=$DOMAIN_NAME --title=$WORDPRESS_TITLE --admin_user=$WORDPRESS_DB_ADMIN --admin_password=$WORDPRESS_DB_ADMIN_PASSWORD --admin_email=$WORDPRESS_DB_ADMIN_EMAIL --skip-email && \
wp user create $WORDPRESS_USER $WORDPRESS_EMAIL --role=author --user_pass=$WORDPRESS_PASSWORD && \
wp plugin update --all
"

exec /usr/sbin/php-fpm7.3 -F

www.conf

[www]

# nginx user & group
user = www-data
group = www-data

# default www.conf setting
pm = dynamic
pm.max_children = 5
pm.start_servers = 2
pm.min_spare_servers = 1
pm.max_spare_servers = 3

# added env for mariadb & wordpress
env[MYSQL_DATABASE] = $MYSQL_DATABASE
env[MYSQL_USER] = $MYSQL_USER
env[MYSQL_PASSWORD] = $MYSQL_PASSWORD

# changed listen ip:port
listen = 0.0.0.0:9000
  • user와 group을 nginx의 root에 맞춤
  • listen=0.0.0.0:9000으로 설정

3. mariadb container

// Dockerfile
FROM	debian:buster

RUN		apt-get update -y && \
		apt-get upgrade -y && \
		apt-get -y install \
		mariadb-server \
		mariadb-client

COPY	./tools/mariadb_setup.sh /tmp/mariadb_setup.sh
COPY	./conf/50-server.cnf /etc/mysql/mariadb.conf.d/

ENTRYPOINT ["sh", "/tmp/mariadb_setup.sh"]

EXPOSE 3306

mariadb_setup.sh

#!/bin/sh

service mysql start;

cat /var/lib/mysql/.setup 2> /dev/null

mysql -e "CREATE DATABASE IF NOT EXISTS $MYSQL_DATABASE";
mysql -e "CREATE USER IF NOT EXISTS '$MYSQL_USER'@'%' IDENTIFIED BY '$MYSQL_PASSWORD'";
mysql -e "GRANT ALL PRIVILEGES ON $MYSQL_DATABASE.* TO '$MYSQL_USER'@'%'";
mysql -e "FLUSH PRIVILEGES";
mysql -e "ALTER USER '$MYSQL_ROOT'@'localhost' IDENTIFIED BY '$MYSQL_ROOT_PASSWORD'";

mysql $MYSQL_DATABASE -u$MYSQL_ROOT -p$MYSQL_ROOT_PASSWORD
mysqladmin -u$MYSQL_ROOT -p$MYSQL_ROOT_PASSWORD shutdown

touch /var/lib/mysql/.setup

exec mysqld --console
profile
born 2 code :)
post-custom-banner

0개의 댓글