Docker - How to create a custom database from docker mysql image
来源:互联网 发布:小沢健二 知乎 编辑:程序博客网 时间:2024/05/16 07:10
Problem:
I use the official mysql base image from Docker Hub to create mysql database. But by default it only creates one database. I want to create another database.
Solution:
mysql image actually provides a way to do this. Reference the official doc: https://github.com/docker-library/docs/tree/master/mysql Under "Initializing a fresh instance" section.
So we just need to create a .sql file with create database command, and mount this file as volume to docker-entrypoint-initdb.d folder inside mysql container.
This is my docker-compose.yml file:
version: '2'services: db: image: mysql # Pull mysql image from Docker Hub environment: # Set up mysql database name and password MYSQL_DATABASE: development MYSQL_ROOT_PASSWORD: password ports: # Set up ports exposed for other containers to connect to - "3306" volumes: # Mount relative path source folder on host to absolute path destination folder on docker container - ./docker-entrypoint-initdb.d:/docker-entrypoint-initdb.d web: build: . # Look in the current directory for a Dockerfile and build images using it depends_on: # Link web container to db container - db environment: # Set up environment variables MYSQL_DATABASE: development MYSQL_ROOT_PASSWORD: password RAILS_ENV: docker ports: - "3000:3000" # Forwards the exposed port 3000 on the container to port 3000 on the host machine
We create init.sql file inside docker-entrypoint-initdb.d folder, and use "volumes" to mount this folder to docker-entrypoint-initdb.d inside mysql container, so that mysql service will have access to all the files in docker-entrypoint-initdb.d folder on host. The content of init.sql file:
CREATE DATABASE test;
After we run
$docker-compose up
It will boot up db and web service. The db service will be a mysql container, which have two databases: develop and test.
- Docker - How to create a custom database from docker mysql image
- How to Interactively Create a Docker Container
- Docker: Create Image from Container
- How to list all tags of a docker image
- How to: Create a Custom List Definition
- How to: Create a Custom Action Specific to a List
- Using a Custom Action to Create a Database During Installation
- Using a Custom Action to Create a Database During Installation
- Using a Custom Action to Create a Database During Installation
- Using a Custom Action to Create a Database During Installation
- Using a Custom Action to Create a Database During Installation
- Using a Custom Action to Create a Database During Installation
- Using a Custom Action to Create a Database During Installation
- Using a Custom Action to Create a Database During Installation
- Using a Custom Action to Create a Database During Installation
- Using a Custom Action to Create a Database During Installation
- Using a Custom Action to Create a Database During Installation
- Using a Custom Action to Create a Database During Installation
- 网易2016研发工程师编程题
- 使用get 、post、soap方式访问webservice示例
- 滚动条的创建(1)
- Java开发学习日记2-ADT安装失败的解决方法
- CODEV 4248(NTT[Never Think about Transformation]-大数分解质数)
- Docker - How to create a custom database from docker mysql image
- opencv 识别网球 ,或者绿色的小球 输出重心坐标
- 打log分析安卓密钥
- 设计具有背景图的按钮(ImageButton的焦点及事件处理)
- 转行自学android,Day10-顺畅
- 关于linux上使用vi时,方向键出现ABCD的个人总结
- 在公共函数中区分上层调用者的模型
- 一个特别适合新手练习的Android小项目——每日一妹纸
- Android学习之GridLayout与GridView