본문 바로가기

Docker

ubuntu에서 docker mysql 이미지 설치 및 실행

반응형
 
ubuntu로 mysql 이미지를 받아서 실행하는 예제입니다.
 
환경 : ubuntu 16.04
 
 
 

1.도커 이미지 검색

docker search mysql

NAME                              DESCRIPTION                                     STARS               OFFICIAL            AUTOMATED
mysql                             MySQL is a widely used, open-source relation…   9137                [OK]
mariadb                           MariaDB is a community-developed fork of MyS…   3235                [OK]
mysql/mysql-server                Optimized MySQL Server Docker images. Create…   676                                     [OK]
centos/mysql-57-centos7           MySQL 5.7 SQL database server                   68
mysql/mysql-cluster               Experimental MySQL Cluster Docker images. Cr…   62
centurylink/mysql                 Image containing mysql. Optimized to be link…   61                                      [OK]
deitch/mysql-backup               REPLACED! Please use http://hub.docker.com/r…   41                                      [OK]
bitnami/mysql                     Bitnami MySQL Docker Image                      35                                      [OK]
tutum/mysql                       Base docker image to run a MySQL database se…   34
schickling/mysql-backup-s3        Backup MySQL to S3 (supports periodic backup…   29                                      [OK]
prom/mysqld-exporter                                                              26                                      [OK]
linuxserver/mysql                 A Mysql container, brought to you by LinuxSe…   24
centos/mysql-56-centos7           MySQL 5.6 SQL database server                   19
circleci/mysql                    MySQL is a widely used, open-source relation…   18
mysql/mysql-router                MySQL Router provides transparent routing be…   14
arey/mysql-client                 Run a MySQL client from a docker container      13                                      [OK]
databack/mysql-backup             Back up mysql databases to... anywhere!         10
openshift/mysql-55-centos7        DEPRECATED: A Centos7 based MySQL v5.5 image…   6
fradelg/mysql-cron-backup         MySQL/MariaDB database backup using cron tas…   5                                       [OK]
genschsa/mysql-employees          MySQL Employee Sample Database                  4                                       [OK]
devilbox/mysql                    Retagged MySQL, MariaDB and PerconaDB offici…   2
ansibleplaybookbundle/mysql-apb   An APB which deploys RHSCL MySQL                2                                       [OK]
jelastic/mysql                    An image of the MySQL database server mainta…   1
monasca/mysql-init                A minimal decoupled init container for mysql    0
widdpim/mysql-client              Dockerized MySQL Client (5.7) including Curl…   0                                       [OK]
 
docker search를 통해 mysql 이미지를 찾는다. 가장 상단에 있는 mysql이 OFFICIAL한 버전이므로 mysql 이름을 가진 이미지를 다운받는다. 특정 버전을 선택하지 않고 최신버전 즉 latest 태그를 가진 이미지를 가져올 것이다.
 
 
 

2.docker image 받기

docker pull mysql:latest

619014d83c02: Pull complete
9ced578c3a5f: Pull complete
731f6e13d8ea: Pull complete
3c183de42679: Pull complete
6de69b5c2f3c: Pull complete
00f0a4086406: Pull complete
84d93aea836d: Pull complete
f18efbfd8d76: Pull complete
012b302865d1: Pull complete
fe16fd240f59: Pull complete
ca3e793e545e: Pull complete
51d0f2cb2610: Pull complete
Digest: sha256:6d0741319b6a2ae22c384a97f4bbee411b01e75f6284af0cce339fee83d7e314
Status: Downloaded newer image for mysql:latest
docker.io/library/mysql:latest
 
mysql:latest를 docker pull 하게되면 최신 버전의 mysql docker image를 다운로드 할 수 있다.
 
 
 

3. docker 이미지 목록 출력

docker images

REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
mysql               latest              791b6e40940c        2 weeks ago         465MB
 
docker images를 통해 pull 받은 docker  image들의 목록을 출력한다. latest 버전의 mysql이 다운로드 된 것을 확인할 수 있다.
 
 
 
 

4. docker mysql 실행

docker \
  run \
  --detach \
  --env MYSQL_ROOT_PASSWORD="root password" \
  --env MYSQL_USER="user name"  \
  --env MYSQL_PASSWORD="mysql password" \
  --env MYSQL_DATABASE="dbname" \
  --name "containername" \
  --publish 3306:3306 \
  mysql;
 
mysql docker image를 실행(run)하기 위해서는몇가지 설정 값들을 인자로 주어야 한다.
 
 
 
--env (-e) : 환경변수 값 
아래와 같은 환경 변수를 인자로 받는다.
  • MYSQL_ROOT_PASSWORD : mysql root 패스워드를 입력하는 환경 변수
  • MYSQL_USER : mysql user name을 입력하는 환경 변수
  • MYSQL_PASSWORD : mysql 패스워드를 입력하는 환경 변수
  • MYSQL_DATABASE : 생성할 데이터베이스 명
 
--name : docker container 이름
 
 
--publish (-p) : 호스트 포트 : 컨테이너 내부 포트
 
 
 
docker \
  run \
  --detach \
  --env MYSQL_ROOT_PASSWORD="root" \
  --env MYSQL_USER="limcode"  \
  --env MYSQL_PASSWORD="limcode" \
  --env MYSQL_DATABASE="test" \
  --name "mysql-db" \
  --publish 3306:3306 \
  mysql;
 
일단 위와 같이 구성했다. 이로서 docker image를 실행했다.
 
 
 

5. docker mysql 접속

docker exec -it mysql-db bash
 
root@vultr:~/docker# docker exec -it mysql-db bash

root@a8ac86e5ca20:/# mysql -u root -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.19 MySQL Community Server - GPL


Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.


Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.


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


mysql>
 
docker exec -it mysql-db bash
명령어를 통해 mysql-db 컨데이너에 bash 쉘로 접속했다.
해당 컨데이터 안에서 mysql -u root -p를 통해 mysql 어플리케이션이 실행된 것을 볼 수 있다.
 
이로서 mysql docker image 설치부터 실행까지 끝! 여기서 이제 데이터베이스를 가지고 놀면 된다.
 
 
반응형

'Docker' 카테고리의 다른 글

ubuntu에 docker compose설치하기  (1) 2020.03.09