玩一玩docker脚本

根据目录下的dockerfile构建镜像

#!/bin/bash
path=`pwd`
cd $path/dockerfiles/mysql && docker build -t mysqltest:mysqltest .
cd $path/dockerfiles/php && docker build -t phptest:phptest .
cd $path/dockerfiles/nginx && docker build -t nginxtest:nginxtest .

将镜像保存到本地

#!/bin/bash
path=`pwd`
docker save -o $path/images/phptest.tar phptest:phptest
docker save -o $path/images/nginxtest.tar nginxtest:nginxtest
docker save -o $path/images/mysqltest.tar mysqltest:mysqltest

部署镜像(新环境没有构建镜像时使用)

#!/bin/bash
path=`pwd`
docker load -i $path/images/phptest.tar
docker load -i $path/images/nginxtest.tar
docker load -i $path/images/mysqltest.tar
docker load -i $path/images/neo4jtest.tar

首次运行(由镜像生成容器,相当于做系统)

#!/bin/bash

# 安装顺序不能变(指定了hosts --link)

path=`pwd`
# 安装 mysql
docker run -it --name=mysqltest \
-e MYSQL_ROOT_PASSWORD='123456' \
-v $path/data/mysql:/var/lib/mysql:rw \
-v $path/log/mysql:/var/log/mysql:rw \
-v $path/conf/mysql:/etc/mysql/:rw \
-p 13306:3306 \
-d mysqltest:mysqltest

# 安装 neo4j
docker run -it --name=neo4jtest \
-v $path/data/neo4j:/var/lib/neo4j/data:rw \
-v $path/log/neo4j:/var/lib/neo4j/logs:rw \
-v $path/conf/neo4j:/var/lib/neo4j/conf/:rw \
--publish=7474:7474 --publish=7687:7687 \
-d neo4jtest:neo4jtest
# php下 --link 方式
# --publish=7474:7474 --publish=7687:7687 \

# 安装 php
docker run -it --name=phptest \
-v $path/conf/php:/usr/local/etc \
-v $path/log/php:/var/log/php \
-v $path/wwwroot:/usr/share/nginx/html:rw \
--link mysqltest:mysqltest \
--link neo4jtest:neo4jtest \
-d phptest:phptest

# 安装 nginx
docker run -it --name=nginxtest \
-v $path/conf/nginx:/etc/nginx \
-v $path/log/nginx:/var/log/nginx \
-v $path/wwwroot:/usr/share/nginx/html:rw \
-p 90:80 \
--link phptest:phptest \
-d nginxtest:nginxtest

容器的管理(启停等,相当于虚拟机开关机)

#!/bin/bash
docker restart mysqltest
docker restart neo4jtest
docker restart phptest
docker restart nginxtest

容器内执行

#!/bin/bash
# 前台执行
docker exec phptest /usr/local/bin/php /usr/share/nginx/html/test/public/index.php  api/index/test
# 后台执行
docker exec -d phptest /usr/local/bin/php /usr/share/nginx/html/test/public/index.php  api/index/test