mysql : add various scripts

all   : fix terminal ENV context and add doc nav
This commit is contained in:
a-sansara 2016-08-01 04:22:06 +02:00
parent fc8be7c6f4
commit e185c732dc
15 changed files with 394 additions and 69 deletions

133
DOCKER.md Normal file
View File

@ -0,0 +1,133 @@
## Docker
- [index][1]
- [image pluie/alpine][2]
- [image pluie/alpine-mysql][3]
- [image pluie/alpine-apache][4]
### Networking
#### create network
```
docker network create \
--subnet=172.22.0.0/16 \
--gateway=172.22.0.1 \
-o "com.docker.network.bridge.name"="home0" \
home0
```
#### inspect ip
```
docker inspect --format='{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' container
```
#### /etc/hosts
```
# > DOCKER - bridge home0
172.22.0.2 db.docker
172.22.0.3 gogs.docker
172.22.0.4 pma.docker
172.22.0.5 bo-payment.docker
172.22.0.6 wordpress.docker
# <
```
### Util
#### remove all container
```
docker rm $(docker ps -a -q)
```
rm -v to also remove volume
rm -f to force stop running container before removing
#### remove none images
```
docker rmi $(docker images | grep "^<none>" | awk "{print $3}")
```
#### remove all volumes
```
docker volume rm $(docker volume ls -qf dangling=true)
```
#### logs container
```
docker logs -f container
```
#### stats container
```
docker stats container
```
### Pluie container
#### map
[db.docker] (http://db.docker)
[pma.docker] (http://pma.docker)
[gogs.docker] (http://gogs.docker)
[bo-payment.docker] (http://bo-payment.docker)
[wordpress.docker] (http://wordpress.docker)
#### Mysql
```
cd /home/dev/docker
docker run --name mysql --restart=always \
--net home0 -h db.docker --ip 172.22.0.2 \
-v $(pwd)/db/mysql:/var/lib/mysql \
-v $(pwd)/db/dump:/dump \
-e MYSQL_DATABASE=bo-payment \
-e MYSQL_USER=dev \
-e MYSQL_PASSWORD=mysql \
-d pluie/alpine-mysql
```
#### Gogs
```
cd /home/dev/docker
docker run --name gogs --restart=always \
--net home0 -h gogs.docker --ip 172.22.0.3 --link mysql:db \
-v $(pwd)/gogs:/data \
gogs/gogs
```
#### Phpmyadmin
```
docker run --name pma --restart=always \
--net home0 -h pma.docker --ip 172.22.0.4 --link mysql:db \
-d phpmyadmin/phpmyadmin
```
#### Apache
```
cd /home/dev/docker
docker run --name apache --restart=always \
--net home0 -h bo-payment.docker --ip 172.22.0.5 --link mysql:db \
-v $(pwd)/repo/bo-payment:/app \
-e HTTP_SERVER_NAME=bo-payment.docker \
-d pluie/alpine-apache
```
#### Wordpress
```
cd /home/dev/docker
docker run --name wordpress --restart=always \
--net home0 -h wordpress.docker --ip 172.22.0.6 --link mysql:db \
-v $(pwd)/blog:/app \
-e HTTP_SERVER_NAME=wordpress.docker \
-d pluie/alpine-apache
```
[1]: https://github.com/pluie-org/docker-images
[2]: https://github.com/pluie-org/docker-images/tree/master/pluie/alpine
[3]: https://github.com/pluie-org/docker-images/tree/master/pluie/alpine-mysql
[4]: https://github.com/pluie-org/docker-images/tree/master/pluie/alpine-apache
[5]: https://github.com/pluie-org/docker-images/blob/master/DOCKER.md

View File

@ -1,12 +1,13 @@
# docker-images # docker-images
based images for Docker
various based images for Docker
## Available Images ## Available Images
- __pluie/alpine__ ( < 10 MB) Alpine/3.4 - [pluie/alpine][2] ( < 10 MB ) Alpine/3.4
- __pluie/alpine-apache__ ( ~ 50 MB) Apache/2.4.23 Php/5.6.24 - [pluie/alpine-apache][3] ( ~ 50 MB ) Apache/2.4.23 Php/5.6.24
- __pluie/alpine-mysql__ ( ~ 172 MB) Mysql/5.5.47 [MariaDB] - [pluie/alpine-mysql][4] ( ~172 MB ) Mysql/5.5.47 ( MariaDB )
- [docker tips][5]
## Base Image Structure ## Base Image Structure
@ -67,3 +68,9 @@ __build__ script archive project files in __files.tar__ then execute the __Docke
no need to worry about pwd, docker repository and image name depends on directory structure. no need to worry about pwd, docker repository and image name depends on directory structure.
you can keep same __build__ script in any project you can keep same __build__ script in any project
[1]: https://github.com/pluie-org/docker-images
[2]: https://github.com/pluie-org/docker-images/tree/master/pluie/alpine
[3]: https://github.com/pluie-org/docker-images/tree/master/pluie/alpine-mysql
[4]: https://github.com/pluie-org/docker-images/tree/master/pluie/alpine-apache
[5]: https://github.com/pluie-org/docker-images/blob/master/DOCKER.md

View File

@ -1,5 +1,10 @@
# pluie/alpine-apache # pluie/alpine-apache
- [index][1]
- [image pluie/alpine][2]
- [image pluie/alpine-mysql][3]
- [docker tips][5]
Extend pluie/alpine with __apache 2.4.23__ and __php 5.6.24__ Extend pluie/alpine with __apache 2.4.23__ and __php 5.6.24__
- error log are attached to stdout - error log are attached to stdout
@ -54,3 +59,9 @@ for more commands :
``` ```
$ docker exec -it apache "httpd -h" $ docker exec -it apache "httpd -h"
``` ```
[1]: https://github.com/pluie-org/docker-images
[2]: https://github.com/pluie-org/docker-images/tree/master/pluie/alpine
[3]: https://github.com/pluie-org/docker-images/tree/master/pluie/alpine-mysql
[4]: https://github.com/pluie-org/docker-images/tree/master/pluie/alpine-apache
[5]: https://github.com/pluie-org/docker-images/blob/master/DOCKER.md

Binary file not shown.

View File

@ -1,7 +1,18 @@
# pluie/alpine-mysql # pluie/alpine-mysql
Extend pluie/alpine with mysql (mariadb) 5.5.47 - [index][1]
- [image pluie/alpine][2]
- [image pluie/alpine-apache][4]
- [docker tips][5]
Extend pluie/alpine with mysql (mariadb) 5.5.47
Project comes with various scripts to execute basic tasks such as :
- dbcreate
- dbdump
- dbload
If database directory is empty a root user is created at startup.
Root access to database is only permit on localhost
## Image Size ## Image Size
@ -9,56 +20,112 @@ Extend pluie/alpine with mysql (mariadb) 5.5.47
## Image Volumes ## Image Volumes
__/var/lib/mysql__ : mysql database directory ```
__/dump__ : directory to store various mysql scripts /var/lib/mysql # mysql database directory
/dump # directory to store various mysql scripts
```
## ENV variables ## ENV variables
__MYSQL_DATABASE__ : create specified database at startup ```
__MYSQL_USER__ : create specified user at startup (and grant all rights to __MYSQL_DATABASE__) MYSQL_DATABASE # create specified database at startup
__MYSQL_PASSWORD__ MYSQL_USER # create specified user at startup (and grant all rights to MYSQL_DATABASE)
__MYSQL_ROOT_PASSWORD__ : don't really need it. a random root password is generated if none MYSQL_PASSWORD
MYSQL_ROOT_PASSWORD # don't really need it. a random root password is generated if none
```
### Inherit ENV variables
```
SHENV_CTX=LOCAL # LOCAL|INT|PROD change context bg color
SHENV_NAME=Mysql # container name
SHENV_COLOR=97 # ANSI EXTENDED COLOR CODE
```
## Image Usage ## Image Usage
for example to start a new project :
``` ```
$ docker run --name mysql -p 3306 -v /home/docker/db/mysql:/var/lib/mysql -v /home/docker/db/dump:/dump -e MYSQL_DATABASE=mybase -e MYSQL_USER=dev -e MYSQL_PASSWORD=mysql -it pluie/alpine-mysql $ cd /home/docker;
$ mkdir -p db/{mysql,dump}
$ docker run --name mysql \
-v $(pwd)/db/mysql:/var/lib/mysql \
-v $(pwd)/db/dump:/dump \
-e MYSQL_DATABASE=mybase \
-e MYSQL_USER=dev \
-e MYSQL_PASSWORD=mysql \
-it pluie/alpine-mysql
```
you don't need to expose your localhost mysql port.
for example :
```
$ docker run --name pma -p 8080:80 --link mysql:db -d phpmyadmin/phpmyadmin
``` ```
### Root Usage and phpmyadmin is accessible via http://localhost:8080/ and linked to your mysql container
a root user is created at startup (if database directory is empty)
root login is permit only on localhost ### Existing Scripts
to do things without files you can run :
create a new database with full rights to a user :
```
$ docker exec -it mysql /dbcreate mydbname myuser
```
load an export file in particular database :
```
$ docker exec -it mysql /dbload mydbname /dump/mydbname.init.sql
```
dump a database :
```
$ docker exec -it mysql /dbdump mydbname
```
### More specific actions as user
to connect to the mysql server as user from host :
```
mysql -h ipmysqlcontainer -udev -pmysql
```
to retriew container ip you can run first :
```
docker inspect --format='{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' mysql
```
to connect to the mysql server as user from container :
```
$ docker exec -it mysql "mysql -udev -pmysql"
```
### More specific actions as root
to connect to the mysql server as root :
``` ```
$ docker exec -it mysql "mysql -uroot" $ docker exec -it mysql "mysql -uroot"
``` ```
to do things with files you can log in to container : to connect to the mysql container as root :
``` ```
docker exec -it mysql bash docker exec -it mysql bash
``` ```
then then
``` ```
$ mysql -uroot mybase < /dump/mydump.sql $ mysql -uroot mybase < /dump/init.mybase.sql
$ mysqldump -uroot mybase > /dump/last.mybase.dump.sql
``` ```
etc. etc.
### User usage
locally (on host) you can run (with appropriate user : pass) :
```
mysql -h ipmysqlcontainer -udev -pmysql
```
or if you want use the mysql client of the container :
```
docker exec -it mysql "mysql -h ipmysqlcontainer -udev -pmysql"
```
## Link Container ## Link Container
to link this container to another, use the default docker behavior : to link this container to another, use the default docker behavior
(as the example provide with phpmyadmin) :
``` ```
docker run --name mycontainer --link mysql:db ... docker run --name mycontainer --link mysql:db ...
``` ```
[1]: https://github.com/pluie-org/docker-images
[2]: https://github.com/pluie-org/docker-images/tree/master/pluie/alpine
[3]: https://github.com/pluie-org/docker-images/tree/master/pluie/alpine-mysql
[4]: https://github.com/pluie-org/docker-images/tree/master/pluie/alpine-apache
[5]: https://github.com/pluie-org/docker-images/blob/master/DOCKER.md

View File

@ -1,32 +0,0 @@
#!/bin/bash
# pluie/docker-images - a-Sansara (https://github.com/a-sansara)
dbname=${1:-''}
dbuser=${2:-''}
echo "dbname : $dbname"
echo "dbuser : $dbuser"
tfile=`mktemp`
if [ ! -f "$tfile" ]; then
return 1
fi
if [ ! -z "$dbname" ]; then
cat <<EOF > $tfile
CREATE DATABASE IF NOT EXISTS \`$dbname\` CHARACTER SET utf8 COLLATE utf8_general_ci;
EOF
if [ ! -z "$dbuser" ]; then
cat <<EOF >> $tfile
GRANT ALL ON \`$dbname\`.* to '$dbuser'@'localhost';
GRANT ALL ON \`$dbname\`.* to '$dbuser'@'%';
EOF
fi
echo "FLUSH PRIVILEGES;" >> $tfile
cat $tfile;
mysql -uroot < $tfile
rm -f $tfile
fi

View File

@ -0,0 +1,35 @@
#!/bin/bash
# pluie/docker-images - a-Sansara (https://github.com/a-sansara)
Ctitle="\033[1;48;5;30;1;38;5;15m"
Ctext="\033[1;38;5;30m"
Copt="\033[1;38;5;72m"
Cspe="\033[1;38;5;30m"
Cerr="\033[1;38;5;196m"
Cusa="\033[1;38;5;214m"
Coff="\033[m"
function echoTitle(){
echo -e "\n ${Ctitle} $shname ${Coff}"
}
function mktfile(){
tfile="$(mktemp)"
if [ ! -f "$tfile" ]; then
echoErr "can't make temp file" 1
exit 1
fi
}
function errEmptyDb(){
echoErr "database can't be empty"
}
function echoErr(){
echo -e "\n${Cerr} error : $1${Coff}"
if [ -z "$2" ]; then
usage
fi
echo
exit 1
}

42
pluie/alpine-mysql/dbcreate.sh Executable file
View File

@ -0,0 +1,42 @@
#!/bin/bash
# pluie/docker-images - a-Sansara (https://github.com/a-sansara)
. /scripts/dbcommon.sh
dbname=${1:-''}
dbuser=${2:-''}
shname=$(basename $0)
echoTitle
function usage(){
echo -e "\n ${Cusa}usage :${Coff}\n ${Ctext}$shname \t${Copt}DBNAME ${Cspe}[${Copt}USER${Cspe}]${Coff}"
}
mktfile
if [ ! -z "$dbname" ]; then
cat <<EOF > "$tfile"
CREATE DATABASE IF NOT EXISTS \`$dbname\` CHARACTER SET utf8 COLLATE utf8_general_ci;
EOF
if [ ! -z "$dbuser" ]; then
cat <<EOF >> "$tfile"
GRANT ALL ON \`$dbname\`.* to '$dbuser'@'localhost';
GRANT ALL ON \`$dbname\`.* to '$dbuser'@'%';
EOF
fi
echo "FLUSH PRIVILEGES;" >> "$tfile"
echo
cat "$tfile" | sed "s/^/ /";
echo
mysql -uroot < "$tfile"
if [ $? -eq 0 ]; then
echo -e "\n done"
fi
echo
rm -f "$tfile"
else
errEmptyDb
fi

23
pluie/alpine-mysql/dbdump.sh Executable file
View File

@ -0,0 +1,23 @@
#!/bin/bash
# pluie/docker-images - a-Sansara (https://github.com/a-sansara)
. /scripts/dbcommon.sh
dbname=${1:-''}
shname=$(basename $0)
echoTitle
function usage(){
echo -e "\n ${Cusa}usage :${Coff}\n ${Ctext}$shname \t${Copt}DBNAME USER${Coff}"
}
if [ ! -z "$1" ]; then
mysqldump -uroot "$dbname" > "/dump/"$(date +"%y%m%d-%H%I")".$dbname.sql"
if [ $? -eq 0 ]; then
echo -e "\n done"
fi
echo
else
errEmptyDb
fi

28
pluie/alpine-mysql/dbload.sh Executable file
View File

@ -0,0 +1,28 @@
#!/bin/bash
# pluie/docker-images - a-Sansara (https://github.com/a-sansara)
. /scripts/dbcommon.sh
dbname=${1:-''}
filename=${2}
shname=$(basename $0)
echoTitle
function usage(){
echo -e "\n ${Cusa}usage :${Coff}\n ${Ctext}$shname \t${Copt}DBNAME FILENAME${Coff}"
}
if [ ! -f "$filename" ]; then
echoErr "unknow file '$filename'"
fi
if [ ! -z "$1" ]; then
mysql -uroot "$dbname" < "$filename"
if [ $? -eq 0 ]; then
echo -e "\n done"
fi
echo
else
errEmptyDb
fi

Binary file not shown.

View File

@ -0,0 +1,3 @@
mv /scripts/dbdump.sh /dbdump
mv /scripts/dbcreate.sh /dbcreate
mv /scripts/dbload.sh /dbload

View File

@ -1,5 +1,10 @@
# pluie/alpine # pluie/alpine
- [index][1]
- [image pluie/alpine-mysql][3]
- [image pluie/alpine-apache][4]
- [docker tips][5]
This Image provide a Linux Alpine distribution with : This Image provide a Linux Alpine distribution with :
- fully functionnal & colorized terminal - fully functionnal & colorized terminal
- bash - bash
@ -79,5 +84,8 @@ EXPOSE 80
VOLUME /app VOLUME /app
``` ```
[1]: https://github.com/pluie-org/docker-images
[2]: https://github.com/pluie-org/docker-images/tree/master/pluie/alpine
[3]: https://github.com/pluie-org/docker-images/tree/master/pluie/alpine-mysql
[4]: https://github.com/pluie-org/docker-images/tree/master/pluie/alpine-apache
[5]: https://github.com/pluie-org/docker-images/blob/master/DOCKER.md

Binary file not shown.

View File

@ -2,14 +2,14 @@
# pluie/docker-images - a-Sansara (https://github.com/a-sansara) # pluie/docker-images - a-Sansara (https://github.com/a-sansara)
function bash_prompt() { function bash_prompt() {
local Cenv="243" local Cenvcode="243"
if [ "$SHENV_CTX" = "INT" ]; then if [ "$SHENV_CTX" = "INT" ]; then
local Cenv="202" local Cenvcode="202"
elif [ "$SHENV_CTX" = "PROD" ]; then elif [ "$SHENV_CTX" = "PROD" ]; then
local Cenv="160" local Cenvcode="160"
fi fi
local Cdate="\[\033[1;33m\]" local Cdate="\[\033[1;33m\]"
local Cctx="\[\033[1;48;5;243m\]" local Cctx="\[\033[1;48;5;${Cenvcode}m\]"
local Cname="\[\033[1;48;5;${SHENV_COLOR}m\]" local Cname="\[\033[1;48;5;${SHENV_COLOR}m\]"
local Cpath="\[\033[1;38;5;36m\]" local Cpath="\[\033[1;38;5;36m\]"
local Cwhite="\[\033[1;38;5;15m\]" local Cwhite="\[\033[1;38;5;15m\]"