이번 포스팅에서는 가장 중요한 부분중 하나인 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)]>
실습이 마무리 되었습니다 :)
고생하셨습니다.