Setup Docker with Vagrant
Get the vagrant-based docker env from DockerRoot Vagrant Box, which is tiny (only 12 MB) and contains the lastest version of docker (Linux kernel v4.1.6 and GLIBC, Docker v1.8.1). The direct download address is https://atlas.hashicorp.com/ailispaw/boxes/docker-root/versions/1.0.0/providers/virtualbox.box).
P.S. Some mini linux disto: Tiny Core Linux(on which boot2docker is based), yoctoproject, BusyBox, Buildroot.
Data-Only Container (Container-as-Volumn Pattern)
Tiny Docker Pieces, Loosely Joined:
Since I’ve spelled it out in such exhaustive detail, this may all seem like much ado about nothing…but, now that I finally grok the idea, I can see how data-only containers are so useful.
- It’s the simplest way to share data among multiple containers.
- It makes it easy to upgrade the application (or process) that operates on the data. For example, say we wanted to generate PDF files in addition to HTML files. All we need to do to accomplish that is to create an updated pandoc-convert Docker image.
- Adding additional processes that operate on the data is equally easy. What if you wanted to push the HTML files to an Amazon S3, in addition to serving them locally? Just create a new s3sync Docker image.
Build the LAMP Stack
1. mysql data container
Build mysql-data image and run (as container):
docker run --name cont-db-data mariadb echo "Data-only container for mariadb"
Note: not need to expose volume (-v /var/lib/mysql
), see http://container-solutions.com/understanding-volumes-docker/ for explaination.
To maint:
SHX="bash --rcfile <(echo 'PS1=\"\n\e[1;34m[\t \u@\h \W]\e[m\n\$ \"'; echo 'alias ll=\"ls -ltra --color=auto\"')"
docker exec -it cont-mariadb bash -c "$SHX"
Note: the $SHX
here initializes the bash shell with --rcfile
option (set the PS1
shell variable and ll
alias).
Or build with Dockerfile:
cat >> $APPROOOT/dockerfiles/dbdata/Dockerfile <<EOF
FROM stackbrew/busybox:latest
MAINTAINER fouding zheng <fouding.zheng@gmail.com>
VOLUME /var/lib/mysql
CMD ["true"]
EOF
# KEY: volume is /var/lib/mysql, NOT arbitrary path!
sudo docker build -t fouding/docker-mysql-data $APPROOOT/dockerfiles/dbdata
sudo docker run --name cont-db-data fouding/docker-mysql-data true
2. cont-app-data container
HTMLROOT=/usr/share/nginx/html
docker run --name cont-app-data -v $HTMLROOT stackbrew/busybox:latest echo "app data-only container"
To maint the cont-app-data:
docker run --rm --volumes-from cont-app-data -v /vagrant-dockerroot/:/app -it fouding/richarvey-nginx-php:addXdebug bash -c "$SHX"
docker run --rm --volumes-from cont-app-data -v /vagrant-dockerroot/:/app -it stackbrew/busybox:latest /bin/sh
Note: not docker exec -it cont-app-data bash -c "$SHX"
, as the cont is not in running state.
Then:
cp -r /app/iWshop /usr/share/nginx/html/
3. db: mariadb(mysql) container
docker run --name cont-mariadb --volumes-from cont-db-data -e MYSQL_USER=iwshop -e MYSQL_PASSWORD=1wsh0p -e MYSQL_DATABASE=iwshop -e MYSQL_ROOT_PASSWORD=my-secret-pw -d mariadb
To maint:
docker exec -it cont-mariadb bash -c "$SHX"
OR
docker run --rm --link cont-mariadb -it mariadb bash -c "$SHX"
If “TERM environment variable not set” error: https://github.com/dockerfile/mariadb/issues/3 :
docker exec -it mariadb bash
mysql
produces the following error message: TERM environment variable not set.Unsure why since
echo $TERM
returnsdumb
and issuingexport TERM=dumb
resolves issue.If the environment variable is explicitly issued in the dockerfile, the error message never appears, i.e. adding
ENV TERM dumb
4. web
nginx-php-fpm container:
docker run --name cont-webserver -p 8000:80 --volumes-from cont-app-data --link cont-mariadb:mariadb -d fouding/richarvey-nginx-php
To maint:
docker run -it --rm fouding/richarvey-nginx-php bash -c "$SHX"
docker run -it --rm fouding/richarvey-nginx-php bash -c "bash --rcfile <(echo 'PS1=\"\n\e[1;34m[\t \u@ws \W]\e[m\n\$ \"'; echo 'alias ll=\"ls -ltra --color=auto\"')"
5. test
Create a simple php file:
cat >> ~/app/php/index.php << EOF
<?php
echo test php;
phpinfo();
?>
EOF
Open in browser: http://localhost:8000, or use cmdline: curl -i http://localhost:8000
.
6. access
docker exec -it cont-webserver bash
Misc
- consider using the small phusion/baseimage image instead of the huge “ubuntu:14:04” image.
Docker Usage Summary
2 ways to communicate b/w 2 containers:
- Via volumes:
cont-mariadb --volumes-from cont-db-data
then the server can access the FILES in the dataCont => use as DATA STORAGE.
- Via container linking:
cont-webserver: --link cont-mariadb:mariadb
then the server can ping/connect to the dataCont => use as mysql server.
KEY: dataCont: -v guestVolume
to expose the vol, and server side: --volume-from dataCont
[RI: not need to specify the volume here, which has been predefined by the docker run -v /guestVolume dataCont
. The vol is: dataCont:guestVolume ]
2 types:
docker run -d imgName
docker run -ti imgName bash
2 ways to start a container:
docker run -i -t imgName cmd
docker run -d imgName
[no cmd appended]