diff --git a/DOCKER.md b/DOCKER.md new file mode 100644 index 0000000..3fc62ce --- /dev/null +++ b/DOCKER.md @@ -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 "^" | 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 diff --git a/README.md b/README.md index 4dc7f7e..6ac9130 100644 --- a/README.md +++ b/README.md @@ -1,12 +1,13 @@ # docker-images -based images for Docker +various based images for Docker ## Available Images -- __pluie/alpine__ ( < 10 MB) Alpine/3.4 -- __pluie/alpine-apache__ ( ~ 50 MB) Apache/2.4.23 Php/5.6.24 -- __pluie/alpine-mysql__ ( ~ 172 MB) Mysql/5.5.47 [MariaDB] +- [pluie/alpine][2] ( < 10 MB ) Alpine/3.4 +- [pluie/alpine-apache][3] ( ~ 50 MB ) Apache/2.4.23 Php/5.6.24 +- [pluie/alpine-mysql][4] ( ~172 MB ) Mysql/5.5.47 ( MariaDB ) +- [docker tips][5] ## 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. 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 diff --git a/pluie/alpine-apache/README.md b/pluie/alpine-apache/README.md index 36d481b..958ce70 100644 --- a/pluie/alpine-apache/README.md +++ b/pluie/alpine-apache/README.md @@ -1,5 +1,10 @@ # 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__ - error log are attached to stdout @@ -54,3 +59,9 @@ for more commands : ``` $ 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 diff --git a/pluie/alpine-apache/files.tar b/pluie/alpine-apache/files.tar index 3032c29..cc7b253 100644 Binary files a/pluie/alpine-apache/files.tar and b/pluie/alpine-apache/files.tar differ diff --git a/pluie/alpine-mysql/README.md b/pluie/alpine-mysql/README.md index 639b5a7..f47c6ef 100644 --- a/pluie/alpine-mysql/README.md +++ b/pluie/alpine-mysql/README.md @@ -1,7 +1,18 @@ # 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 @@ -9,56 +20,112 @@ Extend pluie/alpine with mysql (mariadb) 5.5.47 ## 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 -__MYSQL_DATABASE__ : create specified database at startup -__MYSQL_USER__ : create specified user at startup (and grant all rights to __MYSQL_DATABASE__) -__MYSQL_PASSWORD__ -__MYSQL_ROOT_PASSWORD__ : don't really need it. a random root password is generated if none +``` + MYSQL_DATABASE # create specified database at startup + MYSQL_USER # create specified user at startup (and grant all rights to MYSQL_DATABASE) + 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 +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 -to do things without files you can run : + +### Existing Scripts + +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" ``` -to do things with files you can log in to container : +to connect to the mysql container as root : ``` docker exec -it mysql bash ``` 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 -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 ... ``` + + + [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 diff --git a/pluie/alpine-mysql/createdb.sh b/pluie/alpine-mysql/createdb.sh deleted file mode 100644 index 1656e6f..0000000 --- a/pluie/alpine-mysql/createdb.sh +++ /dev/null @@ -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 < $tfile -CREATE DATABASE IF NOT EXISTS \`$dbname\` CHARACTER SET utf8 COLLATE utf8_general_ci; -EOF - - if [ ! -z "$dbuser" ]; then - cat <> $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 - diff --git a/pluie/alpine-mysql/dbcommon.sh b/pluie/alpine-mysql/dbcommon.sh new file mode 100644 index 0000000..59a50b9 --- /dev/null +++ b/pluie/alpine-mysql/dbcommon.sh @@ -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 +} diff --git a/pluie/alpine-mysql/dbcreate.sh b/pluie/alpine-mysql/dbcreate.sh new file mode 100755 index 0000000..7dca600 --- /dev/null +++ b/pluie/alpine-mysql/dbcreate.sh @@ -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 < "$tfile" +CREATE DATABASE IF NOT EXISTS \`$dbname\` CHARACTER SET utf8 COLLATE utf8_general_ci; +EOF + + if [ ! -z "$dbuser" ]; then + cat <> "$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 diff --git a/pluie/alpine-mysql/dbdump.sh b/pluie/alpine-mysql/dbdump.sh new file mode 100755 index 0000000..201f061 --- /dev/null +++ b/pluie/alpine-mysql/dbdump.sh @@ -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 diff --git a/pluie/alpine-mysql/dbload.sh b/pluie/alpine-mysql/dbload.sh new file mode 100755 index 0000000..046e16e --- /dev/null +++ b/pluie/alpine-mysql/dbload.sh @@ -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 + diff --git a/pluie/alpine-mysql/files.tar b/pluie/alpine-mysql/files.tar index 70d6ea6..41e357f 100644 Binary files a/pluie/alpine-mysql/files.tar and b/pluie/alpine-mysql/files.tar differ diff --git a/pluie/alpine-mysql/install.d/40-scripts-fix.sh b/pluie/alpine-mysql/install.d/40-scripts-fix.sh new file mode 100644 index 0000000..a192567 --- /dev/null +++ b/pluie/alpine-mysql/install.d/40-scripts-fix.sh @@ -0,0 +1,3 @@ +mv /scripts/dbdump.sh /dbdump +mv /scripts/dbcreate.sh /dbcreate +mv /scripts/dbload.sh /dbload diff --git a/pluie/alpine/README.md b/pluie/alpine/README.md index fe0fe20..4127f55 100644 --- a/pluie/alpine/README.md +++ b/pluie/alpine/README.md @@ -1,5 +1,10 @@ # 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 : - fully functionnal & colorized terminal - bash @@ -79,5 +84,8 @@ EXPOSE 80 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 diff --git a/pluie/alpine/files.tar b/pluie/alpine/files.tar index 9c9e5ff..0dcd9d4 100644 Binary files a/pluie/alpine/files.tar and b/pluie/alpine/files.tar differ diff --git a/pluie/alpine/util.sh b/pluie/alpine/util.sh index 3ec1c70..b96dd1b 100755 --- a/pluie/alpine/util.sh +++ b/pluie/alpine/util.sh @@ -2,14 +2,14 @@ # pluie/docker-images - a-Sansara (https://github.com/a-sansara) function bash_prompt() { - local Cenv="243" + local Cenvcode="243" if [ "$SHENV_CTX" = "INT" ]; then - local Cenv="202" + local Cenvcode="202" elif [ "$SHENV_CTX" = "PROD" ]; then - local Cenv="160" + local Cenvcode="160" fi 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 Cpath="\[\033[1;38;5;36m\]" local Cwhite="\[\033[1;38;5;15m\]"