๐ŸŒฑ Infra/Container_Docker

[Docker-Basic (10)] Docker File ์‚ฌ์šฉํ•˜๊ธฐ - ์‹ค์Šต2 MARIA DB

mini_world 2020. 9. 29. 16:32
๋ชฉ์ฐจ ์ ‘๊ธฐ


์ด๋ฒˆ ํฌ์ŠคํŒ…์—์„œ๋Š” ๊ฐ€์žฅ ์ค‘์š”ํ•œ ๋ถ€๋ถ„์ค‘ ํ•˜๋‚˜์ธ DockerFile์— ๋Œ€ํ•ด์„œ ๋‹ค๋ฃจ์–ด๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค!!!

 


์‹œ์ž‘ํ•˜๊ธฐ์ „์—, ๋„์ปคํŒŒ์ผ์ด๋ž€ ๋ญ˜๊นŒ์š”?  docs.docker.com/engine/reference/builder/

 

Dockerfile reference

Docker can build images automatically by reading the instructions from a Dockerfile. A Dockerfile is a text document that contains all the commands a user could call on the command...

docs.docker.com

๊ฐ„๋‹จํ•˜๊ฒŒ "๋ ˆ์‹œํ”ผ"๋ผ๊ณ  ๋ณผ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์š”๋ฆฌ๋ฅผ ๋งŒ๋“ค๋–„ ์–ด๋–ป๊ฒŒ ๋งŒ๋“ค์–ด์•ผ ๋ง›์žˆ๋Š”์ง€ ๋ ˆ์‹œํ”ผ๋ฅผ ์ •๋ฆฌํ•˜์—ฌ ์ •์˜๋œ ๋ฐฉ์‹๋Œ€๋กœ ์š”๋ฆฌ๋ฅผ ํ•˜๋“ฏ, 
์ปจํ…Œ์ด๋„ˆ๋„ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ๋„์ปคํŒŒ์ผ์„ ์ด์šฉํ•˜์—ฌ ์–ด๋–ป๊ฒŒ ๋งŒ๋“ค๊ฒƒ์ธ์ง€์— ๋Œ€ํ•ด ์ •์˜ํ•ฉ๋‹ˆ๋‹ค.

๋„์ปคํŒŒ์ผ์„ ์‚ฌ์šฉํ•˜๋ฉด ํŒจํ‚ค์ง€์„ค์น˜, ํ™˜๊ฒฝ๋ณ€์ˆ˜ ๋ณ€๊ฒฝ, ์„ค์ •ํŒŒ์ผ ๋ณ€๊ฒฝ๋“ฑ ๋‹ค์–‘ํ•œ ์ž‘์—…์„ ํ•˜๋‚˜ํ•˜๋‚˜ ์ปจํ…Œ์ด๋„ˆ ๋งŒ๋“ค๊ณ  ์„ค์ •์„ ์ ์šฉํ•˜๊ณ  ์ด๋ ‡๊ฒŒ ์—ฌ๋Ÿฌ๋ฒˆ ๋ฐ˜๋ณตํ•  ํ•„์š” ์—†์ด, Dockerfile์„ ์ด์šฉํ•˜์—ฌ ์ •์˜ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์ด์ œ๋ถ€ํ„ฐ, ๋„์ปคํŒŒ์ผ์„ ๊ฐ„๋‹จํ•˜๊ฒŒ ํ™œ์šฉํ•˜์—ฌ ์‹ค์Šตํ•ด๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

 

Docker File ์˜ˆ์ œ (MariaDB)

 

1. Dockerfile ์ž‘์„ฑ

์ƒˆ๋กœ์šด ๋””๋ ‰ํ„ฐ๋ฆฌ๋ฅผ ์ƒ์„ฑํ•˜๊ณ  ๊ทธ ๊ฒฝ๋กœ๋กœ ์ด๋™ํ•ฉ๋‹ˆ๋‹ค.

[root@docker-worker /]# mkdir dockerLAB && cd dockerLAB/

๋„์ปค ํŒŒ์ผ์„ ์ž‘์„ฑํ•ด๋ด…์‹œ๋‹ค :)
๋ช…๋ น์–ด ์•„๋ž˜ FROM ๋ถ€ํ„ฐ ๋ณต์‚ฌ ๋ถ™์—ฌ๋„ฃ๊ธฐ ํ•˜์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค!!

[root@docker-worker dockerLAB]# vim Dockerfile


FROM centos:centos7

MAINTAINER The CentOS Project <clouc-ops@centos.org>
LABEL Vendor="CentOS"
LABEL License=GPLv2
LABEL Version=5.5.41

LABEL Build docker build --rm --tag centos/mariadb .

RUN yum -y install --setopt=tsflags=nodocs epel-release&&\
    yum -y install --setopt=tsflags=nodocs mariadb-server bind-utils pwgen psmisc hostname&&\
    yum -y erase vim-minimal &&\
    yum -y update && yum clean all

COPY fix-permissions.sh ./
RUN ./fix-permissions.sh /var/lib/mysql/ &&\
    ./fix-permissions.sh /var/log/mariadb/ &&\
    ./fix-permissions.sh /var/run

COPY docker-entrypoint.sh ./

ENTRYPOINT ["/docker-entrypoint.sh"]

VOLUME /var/lib/mysql

USER 27
EXPOSE 3306
CMD ["mysqld_safe"]

๋„์ปคํŒŒ์ผ๊ณผ ์˜์กด๊ด€๊ณ„์ธ fix-permissions.sh ํŒŒ์ผ๋„ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค. ๋ณต์‚ฌ ๋ถ™์—ฌ๋„ฃ๊ธฐ ํ•˜์„ธ์š”!

[root@docker-worker dockerLAB]# cat fix-permissions.sh 

#/bin/bash
chgrp -R 0 $1
chmod -R g+rw $1
find $1 -type d -exec chmod g+x {} +

docker-entrypoint.sh ํŒŒ์ผ๋„ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค! ๋ณต์‚ฌ ๋ถ™์—ฌ๋„ฃ๊ธฐ ํ•˜์„ธ์š”!

[root@docker-worker dockerLAB]# cat docker-entrypoint.sh 

#!/bin/bash
set -e

if [ "${1:0:1}" = '-' ]; then
	set -- mysqld_safe "$@"
fi

if [ "$1" = 'mysqld_safe' ]; then
	DATADIR="/var/lib/mysql"
	
	if [ ! -d "$DATADIR/mysql" ]; then
		if [ -z "$MYSQL_ROOT_PASSWORD" -a -z "$MYSQL_ALLOW_EMPTY_PASSWORD" ]; then
			echo >&2 'error: database is uninitialized and MYSQL_ROOT_PASSWORD not set'
			echo >&2 '  Did you forget to add -e MYSQL_ROOT_PASSWORD=... ?'
			exit 1
		fi
		
		echo 'Running mysql_install_db ...'
		mysql_install_db --datadir="$DATADIR"
		echo 'Finished mysql_install_db'
		
		# These statements _must_ be on individual lines, and _must_ end with
		# semicolons (no line breaks or comments are permitted).
		# TODO proper SQL escaping on ALL the things D:
		
		tempSqlFile='/tmp/mysql-first-time.sql'
		cat > "$tempSqlFile" <<-EOSQL
			DELETE FROM mysql.user ;
			CREATE USER 'root'@'%' IDENTIFIED BY '${MYSQL_ROOT_PASSWORD}' ;
			GRANT ALL ON *.* TO 'root'@'%' WITH GRANT OPTION ;
			DROP DATABASE IF EXISTS test ;
		EOSQL
		
		if [ "$MYSQL_DATABASE" ]; then
			echo "CREATE DATABASE IF NOT EXISTS \`$MYSQL_DATABASE\` ;" >> "$tempSqlFile"
			if [ "$MYSQL_CHARSET" ]; then
				echo "ALTER DATABASE \`$MYSQL_DATABASE\` CHARACTER SET \`$MYSQL_CHARSET\` ;" >> "$tempSqlFile"
			fi
			
			if [ "$MYSQL_COLLATION" ]; then
				echo "ALTER DATABASE \`$MYSQL_DATABASE\` COLLATE \`$MYSQL_COLLATION\` ;" >> "$tempSqlFile"
			fi
		fi
		
		if [ "$MYSQL_USER" -a "$MYSQL_PASSWORD" ]; then
			echo "CREATE USER '$MYSQL_USER'@'%' IDENTIFIED BY '$MYSQL_PASSWORD' ;" >> "$tempSqlFile"
			
			if [ "$MYSQL_DATABASE" ]; then
				echo "GRANT ALL ON \`$MYSQL_DATABASE\`.* TO '$MYSQL_USER'@'%' ;" >> "$tempSqlFile"
			fi
		fi
		
		echo 'FLUSH PRIVILEGES ;' >> "$tempSqlFile"
		
		set -- "$@" --init-file="$tempSqlFile"
	fi
	
fi

exec "$@"

 

 

2. ๋„์ปค ์ด๋ฏธ์ง€ ๋นŒ๋“œ

์œ„์—์„œ ์ƒ์„ฑํ•œ Dockerfile๋กœ ์ด๋ฏธ์ง€๋ฅผ ๋งŒ๋“ค์–ด๋ด…์‹œ๋‹ค!
๋จผ์ € ํŒŒ์ผ ๊ถŒํ•œ์„ ๋ณ€๊ฒฝํ•ด์ค๋‹ˆ๋‹ค.

[root@docker-worker dockerLAB]# chmod 755 *.sh

Docker Build ๋ช…๋ น์–ด๋ฅผ ํ†ตํ•ด ๋„์ปค ์ด๋ฏธ์ง€๋ฅผ ๋งŒ๋“ค์–ด์ค๋‹ˆ๋‹ค.

docker build  <์˜ต์…˜> <๋„์ปคํŒŒ์ผ ๊ฒฝ๋กœ> 
-t(--tag) ์˜ต์…˜์€ ์ƒ์„ฑํ•  ์ด๋ฏธ์ง€ ์ด๋ฆ„์„ ์ง€์ •ํ•ฉ๋‹ˆ๋‹ค.
[root@docker-worker dockerLAB]# docker build -t centos/mariadb .

Sending build context to Docker daemon  6.144kB
Step 1/15 : FROM centos:centos7
centos7: Pulling from library/centos
75f829a71a1c: Pull complete 
Digest: sha256:19a79828ca2e505eaee0ff38c2f3fd9901f4826737295157cc5212b7a372cd2b
Status: Downloaded newer image for centos:centos7
 ---> 7e6257c9f8d8
Step 2/15 : MAINTAINER The CentOS Project <clouc-ops@centos.org>
 ---> Running in ce1ed3e3c1f8
Removing intermediate container ce1ed3e3c1f8
 ---> d040b4f72e4f
Step 3/15 : LABEL Vendor="CentOS"
 ---> Running in 95bf51ee5871
Removing intermediate container 95bf51ee5871
 ---> 8e431aadf810
Step 4/15 : LABEL License=GPLv2
 ---> Running in 7e7df0dc3c66
Removing intermediate container 7e7df0dc3c66
 ---> 822f51054ea7
Step 5/15 : LABEL Version=5.5.41
 ---> Running in bafb91dfbc8a
Removing intermediate container bafb91dfbc8a
 ---> 7b6a5ccf9393
Step 6/15 : LABEL Build docker build --rm --tag centos/mariadb .
 ---> Running in ba375663c48c
Removing intermediate container ba375663c48c
 ---> 9c75f7eb5bcd
Step 7/15 : RUN yum -y install --setopt=tsflags=nodocs epel-release&&    yum -y install --setopt=tsflags=nodocs mariadb-server bind-utils pwgen psmisc hostname&&    yum -y erase vim-minimal &&    yum -y update && yum clean all
 ---> Running in 01fae14f7e2c
Removing intermediate container 01fae14f7e2c
 ---> 93da11e7dd8e
Step 8/15 : COPY fix-permissions.sh ./
 ---> a8199c094883
Step 9/15 : RUN ./fix-permissions.sh /var/lib/mysql/ &&    ./fix-permissions.sh /var/log/mariadb/ &&    ./fix-permissions.sh /var/run
 ---> Running in 8c4085525252
Removing intermediate container 8c4085525252
 ---> bd7ccf963343
Step 10/15 : COPY docker-entrypoint.sh ./
 ---> 1f5fbc5cc811
Step 11/15 : ENTRYPOINT ["/docker-entrypoint.sh"]
 ---> Running in 5f2f626886c1
Removing intermediate container 5f2f626886c1
 ---> c8504fc87326
Step 12/15 : VOLUME /var/lib/mysql
 ---> Running in e3a450a0c280
Removing intermediate container e3a450a0c280
 ---> 63a6923b0ecd
Step 13/15 : USER 27
 ---> Running in 95266d9a68c4
Removing intermediate container 95266d9a68c4
 ---> 55e5d913b83c
Step 14/15 : EXPOSE 3306
 ---> Running in 108f7d22e645
Removing intermediate container 108f7d22e645
 ---> d41b0bcbbb8c
Step 15/15 : CMD ["mysqld_safe"]
 ---> Running in bc51c98627d5
Removing intermediate container bc51c98627d5
 ---> ac1247fbebdf
Successfully built ac1247fbebdf
Successfully tagged centos/mariadb:latest

์ด๋ฏธ์ง€๊ฐ€ ์ž˜ ์ƒ์„ฑ๋˜์—ˆ๋Š”์ง€ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค.

[root@docker-worker dockerLAB]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
centos/mariadb      latest              ac1247fbebdf        15 seconds ago      390MB
centos              centos7             7e6257c9f8d8        7 weeks ago         203MB
jcdemo/flaskapp     latest              4f7a2cc79052        23 months ago       88.7MB

 

 

3. ๋„์ปค ์ปจํ…Œ์ด๋„ˆ ์ƒ์„ฑ ๋ฐ ํ…Œ์ŠคํŠธ

์ด๋ฏธ์ง€๋ฅผ ๋นŒ๋“œํ–ˆ์œผ๋‹ˆ, ์ปจํ…Œ์ด๋„ˆ๋กœ ์‹คํ–‰ํ•ด์„œ ์ž˜ ๋™์ž‘๋˜๋Š”์ง€ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค.
๋จผ์ €, ์ด๋ฏธ์ง€๋ฅผ ์ด์šฉํ•ด์„œ ์ปจํ…Œ์ด๋„ˆ๋ฅผ ์‹คํ–‰ํ•ฉ๋‹ˆ๋‹ค.

[root@docker-worker centos]# docker run --name mariadb -d -p 3306:3306 -e MYSQL_ROOT_PASSWORD=P@ssw0rd centos/mariadb
4020b27879bb8a8447aa162ea28b62518193ce5376cb801bab455425d76d2a1d

์ปจํ…Œ์ด๋„ˆ ์ž˜ ๋™์ž‘ํ•˜๋Š”์ง€ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค.

[root@docker-worker centos]# docker container ls
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                    NAMES
4020b27879bb        centos/mariadb      "/docker-entrypoint.…"   3 seconds ago       Up 3 seconds        0.0.0.0:3306->3306/tcp   mariadb

์ปจํ…Œ์ด๋„ˆ์— ์ ‘์†ํ•ด์„œ mariaDB๋ฅผ ์‚ฌ์šฉํ•ด๋ด…์‹œ๋‹ค!

[root@docker-worker centos]# docker exec -it mariadb /bin/bash

bash-4.2$ mysql -uroot -p

Enter password: 
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 2
Server version: 5.5.65-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
+--------------------+
3 rows in set (0.00 sec)

MariaDB [(none)]> create database test;
Query OK, 1 row affected (0.00 sec)

MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
+--------------------+
4 rows in set (0.00 sec)

MariaDB [(none)]>  

 

์‹ค์Šต์ด ๋งˆ๋ฌด๋ฆฌ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค :)

๊ณ ์ƒํ•˜์…จ์Šต๋‹ˆ๋‹ค.

728x90