Docker
Docker mariadb 로컬에 디비 연동하기
realforceman
2021. 12. 25. 23:41
728x90
개발 환경
맥북, M1, Monterey
Docker desktop
도커에 mariadb 설치후 데이터베이스는 로컬(맥 os)과 연동하도록 하자.
컨테이너를 종료시켜도 디비 자체는 없어지지 않도록 로컬에 연동하는 것이 포인트
#1 docker maria image 가져오기
docker pull mariadb
Using default tag: latest
latest: Pulling from library/mariadb
a39c84e173f0: Pull complete
19c05479159a: Pull complete
7a3fae4be7ce: Pull complete
c6f314de44c1: Pull complete
37a2529e55ed: Pull complete
0e027baf10a6: Pull complete
bba14cc653d8: Pull complete
9c2ef25d84c3: Pull complete
1bed92b0fe89: Pull complete
3f22a51b4300: Pull complete
Digest: sha256:0f04ae6f30c5a3295fb7cc9be5780c15ff21d6028f999b19f5803114c1e8559e
Status: Downloaded newer image for mariadb:latest
docker.io/library/mariadb:latest
➜ / docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mariadb latest 7eda4c38372f 6 weeks ago 394MB
#2 docker 컨테이너 실행
➜ ~ docker container run -d -p 13306:3306 -e MYSQL_ROOT_PASSWORD=1234qwer -v /Users/diaz/database/mariadb:/var/lib/mysql --name mariadb_local mariadb
0123ba5d5d17a2519f04b3b70f811abdcb31c4c90a30d2300748b3f18eb8e363
➜ ~ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
0123ba5d5d17 mariadb "docker-entrypoint.s…" 3 seconds ago Up 2 seconds 0.0.0.0:13306->3306/tcp mariadb_local
docker container run -d -p 13306:3306 \
-e MYSQL_ROOT_PASSWORD=1234qwer \
-v /Users/diaz/database/mariadb:/var/lib/mysql \
--name mariadb_local mariadb
/Users/diaz 에 database 디렉터리 생성 후 해당 경로에 -v 옵션을 통해 디비 연동을 하였다.
#3 docker 연동된 데이터베이스 확인
➜ database pwd
/Users/diaz/database
➜ database ls -la
total 0
drwxr-xr-x 3 diaz staff 96 12 25 23:00 .
drwxr-x---+ 38 diaz staff 1216 12 25 23:00 ..
drwxr-xr-x@ 2 diaz staff 64 12 25 23:00 mariadb
➜ database cd mariadb
➜ mariadb ls -la
total 0
drwxr-xr-x@ 2 diaz staff 64 12 25 23:00 .
drwxr-xr-x 3 diaz staff 96 12 25 23:00 ..
➜ mariadb ls -la
total 25520
drwxr-xr-x@ 13 diaz staff 416 12 25 23:04 .
drwxr-xr-x 3 diaz staff 96 12 25 23:00 ..
-rw-r----- 1 diaz staff 409600 12 25 23:04 aria_log.00000001
-rw-r----- 1 diaz staff 52 12 25 23:04 aria_log_control
-rw-r----- 1 diaz staff 16384 12 25 23:04 ddl_recovery.log
-rw-r----- 1 diaz staff 942 12 25 23:04 ib_buffer_pool
-rw-r----- 1 diaz staff 100663296 12 25 23:04 ib_logfile0
-rw-r----- 1 diaz staff 12582912 12 25 23:04 ibdata1
-rw-r----- 1 diaz staff 12582912 12 25 23:04 ibtmp1
-rw-r----- 1 diaz staff 0 12 25 23:04 multi-master.info
drwx------ 90 diaz staff 2880 12 25 23:04 mysql
drwx------ 3 diaz staff 96 12 25 23:03 performance_schema
drwx------ 106 diaz staff 3392 12 25 23:04 sys
➜ mariadb
mariadb 경로에 아무것도 없다가 도커가 실행된 후 디비 관련 파일이 생성된 것을 확인 할 수 있다.
#4 docker 디비 접속 확인
sequal pro를 통해 디비 접속을 진행
#5 docker 컨테이너 접속
➜ ~ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
0123ba5d5d17 mariadb "docker-entrypoint.s…" 34 minutes ago Up 34 minutes 0.0.0.0:13306->3306/tcp mariadb_local
➜ ~ docker exec -it 0123ba5d5d17 /bin/bash
root@0123ba5d5d17:/# mysql -u root -p
Enter password:
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 21
Server version: 10.6.5-MariaDB-1:10.6.5+maria~focal mariadb.org binary distribution
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 |
+--------------------+
| apb_local |
| information_schema |
| mysql |
| performance_schema |
| sys |
| test |
+--------------------+
6 rows in set (0.007 sec)
MariaDB [(none)]>
1. docker ps 명령어로 현재 실행중인 컨테이너 목록을 확인합니다.
2. container id를 획득
3. docker exec -it 0123ba5d5d17 /bin/bash
-it 과 /bin/bash 옵션을 통해 컨테이너 내부 배쉬 쉘 로 진입
4. 디비 접속
mysql -u root 입력 후 패스워드 입력(나의 경우에 1234qwer)
5. show databases; 통해 데이터베이스 목록 확인
728x90