์ด๋ฒ ํฌ์คํ
์์๋ ๊ฐ์ฅ ์ค์ํ ๋ถ๋ถ์ค ํ๋์ธ DockerFile์ ๋ํด์ ๋ค๋ฃจ์ด๋ณด๋๋ก ํ๊ฒ ์ต๋๋ค!!!
์์ํ๊ธฐ์ ์, ๋์ปคํ์ผ์ด๋ ๋ญ๊น์? docs.docker.com/engine/reference/builder/
๊ฐ๋จํ๊ฒ "๋ ์ํผ"๋ผ๊ณ ๋ณผ์ ์์ต๋๋ค.
์๋ฆฌ๋ฅผ ๋ง๋ค๋ ์ด๋ป๊ฒ ๋ง๋ค์ด์ผ ๋ง์๋์ง ๋ ์ํผ๋ฅผ ์ ๋ฆฌํ์ฌ ์ ์๋ ๋ฐฉ์๋๋ก ์๋ฆฌ๋ฅผ ํ๋ฏ,
์ปจํ
์ด๋๋ ๋ง์ฐฌ๊ฐ์ง๋ก ๋์ปคํ์ผ์ ์ด์ฉํ์ฌ ์ด๋ป๊ฒ ๋ง๋ค๊ฒ์ธ์ง์ ๋ํด ์ ์ํฉ๋๋ค.
๋์ปคํ์ผ์ ์ฌ์ฉํ๋ฉด ํจํค์ง์ค์น, ํ๊ฒฝ๋ณ์ ๋ณ๊ฒฝ, ์ค์ ํ์ผ ๋ณ๊ฒฝ๋ฑ ๋ค์ํ ์์ ์ ํ๋ํ๋ ์ปจํ ์ด๋ ๋ง๋ค๊ณ ์ค์ ์ ์ ์ฉํ๊ณ ์ด๋ ๊ฒ ์ฌ๋ฌ๋ฒ ๋ฐ๋ณตํ ํ์ ์์ด, 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)]>
์ค์ต์ด ๋ง๋ฌด๋ฆฌ ๋์์ต๋๋ค :)
๊ณ ์ํ์ จ์ต๋๋ค.