下一篇:如何用shell快速建立自己的docker-nginx-php-mysql開發環境
感恩陳瑩光老師指導!!
0.在VirtualBox 安裝 CentOS 7.8,並開啟 22 port
(01)開啟VirtualBox 22 port
(02)ssh 連線1.在CentOS 安裝 docker
(01)解除安裝其他舊版本
$ sudo yum remove docker \
docker-client \
docker-client-latest \
docker-common \
docker-latest \
docker-latest-logrotate \
docker-logrotate \
docker-engine
(02)建立REPOSITORY
$ sudo yum install -y yum-utils
$ sudo yum-config-manager \
--add-repo \
https://download.docker.com/linux/centos/docker-ce.repo
(03)安裝docker
$ sudo yum install docker-ce docker-ce-cli containerd.io
(04)啟動docker
$ sudo systemctl start docker
現在我們將其寫成shell
檔案名稱:dockerInstall.sh
檔案內容:
#!/bin/bash
yum remove docker \
docker-client \
docker-client-latest \
docker-common \
docker-latest \
docker-latest-logrotate \
docker-logrotate \
docker-engine
yum install -y yum-utils
yum-config-manager \
--add-repo \
https://download.docker.com/linux/centos/docker-ce.repo
yum install docker-ce docker-ce-cli containerd.io
systemctl start docker
(01)安裝指令
$sudo curl -L "https://github.com/docker/compose/releases/download/1.27.4/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
(02)變更權限
sudo chmod +x /usr/local/bin/docker-compose
(03)版本
$sudo docker-compose version
現在我們將其寫成shell
檔案名稱:dockerComposeInstall.sh
檔案內容:
#!/bin/bash
curl -L "https://github.com/docker/compose/releases/download/1.27.4/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
chmod +x /usr/local/bin/docker-compose
docker-compose version
使用陳螢光老師的範例
(01)安裝git
$sudo yum install git
(02)建立 test 目錄並進入test目錄
$sudo mkdir test
$cd test
(03)git clone 陳螢光老師的範例
$sudo git clone https://github.com/hamichen/docker-nginx-php-mysql.git
(04)到docker-nginx-php-mysql目錄下
$cd docker-nginx-php-mysql
(05)修改 docker-compose.yml
$sudo vi docker-compose.yml
修改內容如下
version: '3'
services:
web:
image: nginx:alpine
volumes:
- "./etc/nginx/default.conf:/etc/nginx/conf.d/default.conf"
- "./etc/ssl:/etc/ssl"
- "./web:/var/www/html"
- "./etc/nginx/default.template.conf:/etc/nginx/conf.d/default.template"
ports:
- "8000:80" 改為 - "80:80"
- "3000:443" 改為 - "443:443"
environment:
- NGINX_HOST=${NGINX_HOST}
command: /bin/sh -c "envsubst '$$NGINX_HOST' < /etc/nginx/conf.d/default.template > /etc/nginx/conf.d/default.conf && nginx -g 'daemon off;'"
restart: always
depends_on:
- php
- mysqldb
php:
build:
context: ./fpm
restart: always
volumes:
- "./etc/php/php.ini:/usr/local/etc/php/conf.d/php.ini"
- "./web:/var/www/html"
後來可改寫,不需重新build
php:
image: hamichen/php-fpm-7.3.14
restart: always
volumes:
- "./etc/php/php.ini:/usr/local/etc/php/conf.d/php.ini"
- "./web:/var/www/html"
memcached:
image: memcached
restart: always
mongodb:
image: library/mongo:3.4.9
volumes:
- ./data/mongodb:/data/db
- /etc/localtime:/etc/localtime:ro
ports:
- "27117:27017" 改為 - "27017:27017"
restart: always
privileged: true
myadmin:
image: phpmyadmin/phpmyadmin
container_name: phpmyadmin
ports:
- "8080:80"
environment:
- PMA_ARBITRARY=1
- PMA_HOST=${MYSQL_HOST}
restart: always
depends_on:
- mysqldb
mysqldb:
image: mysql:${MYSQL_VERSION}
container_name: ${MYSQL_HOST}
restart: always
env_file:
- ".env"
environment:
- MYSQL_DATABASE=${MYSQL_DATABASE}
- MYSQL_ROOT_PASSWORD=${MYSQL_ROOT_PASSWORD}
- MYSQL_USER=${MYSQL_USER}
- MYSQL_PASSWORD=${MYSQL_PASSWORD}
ports:
- "8989:3306" 改為 - "3306:3306"
command: mysqld --sql_mode="" 停用mysql 嚴格模式
volumes:
- "./data/db/mysql:/var/lib/mysql"
(06)修改.env
$sudo vi .env
修改內容如下:
# See https://docs.docker.com/compose/environment-variables/#the-env-file
# Nginx
NGINX_HOST=localhost
# PHP
# See https://hub.docker.com/r/nanoninja/php-fpm/tags/
PHP_VERSION=latest
# MySQL
MYSQL_VERSION=5.7.22
MYSQL_HOST=mysql
MYSQL_DATABASE=test
MYSQL_ROOT_USER=root
MYSQL_ROOT_PASSWORD=root 改為root的密碼 aa123456
MYSQL_USER=dev
MYSQL_PASSWORD=dev docker 改為dev的密碼 aa123456
(07)依據 docker-compose.yml 內的定義,依序啟動多個 container ,以及建立它們之間的關連。
$sudo docker-compose up -d
注意事項:若網路連接時超時,中斷。可再下 $sudo docker-compose up -d,直到完成。
(08)檢查多個container 狀態
$sudo docker-compose ps
(09)開啟VirtualBox 80、8088 port 與網頁瀏覽http://127.0.0.1 與http://127.0.0.1:8080 (10)關閉、重啟、啟動 docker 內的mysql 服務
$sudo docker stop mysql
$sudo docker restart mysql
$sudo docker start mysql
(11)在docker 內加入redis服務到此複製
貼到docker-compose.yml,其內容如下,並修改:version: '3'
services:
web:
image: nginx:alpine
volumes:
- "./etc/nginx/default.conf:/etc/nginx/conf.d/default.conf"
- "./etc/ssl:/etc/ssl"
- "./web:/var/www/html"
- "./etc/nginx/default.template.conf:/etc/nginx/conf.d/default.template"
ports:
- "80:80"
- "443:443"
environment:
- NGINX_HOST=${NGINX_HOST}
command: /bin/sh -c "envsubst '$$NGINX_HOST' < /etc/nginx/conf.d/default.template > /etc/nginx/conf.d/default.conf && nginx -g 'daemon off;'"
restart: always
depends_on:
- php
- mysqldb
php:
image: hamichen/php-fpm-7.3.14
restart: always
volumes:
- "./etc/php/php.ini:/usr/local/etc/php/conf.d/php.ini"
- "./web:/var/www/html"
memcached:
image: memcached
restart: always
mongodb:
image: library/mongo:3.4.9
volumes:
- ./data/mongodb:/data/db
- /etc/localtime:/etc/localtime:ro
ports:
- "27017:27017"
restart: always
privileged: true
myadmin:
image: phpmyadmin/phpmyadmin
container_name: phpmyadmin
ports:
- "8080:80"
environment:
- PMA_ARBITRARY=1
- PMA_HOST=${MYSQL_HOST}
restart: always
depends_on:
- mysqldb
mysqldb:
image: mysql:${MYSQL_VERSION}
container_name: ${MYSQL_HOST}
restart: always
env_file:
- ".env"
environment:
- MYSQL_DATABASE=${MYSQL_DATABASE}
- MYSQL_ROOT_PASSWORD=${MYSQL_ROOT_PASSWORD}
- MYSQL_USER=${MYSQL_USER}
- MYSQL_PASSWORD=${MYSQL_PASSWORD}
ports:
- "3306:3306"
command: mysqld --sql_mode=""
volumes:
- "./data/db/mysql:/var/lib/mysql"
redis:
image: redis:4.0.8-alpine
container_name: db-redis
restart: always
ports:
- "6379:6379"
networks: 刪除
- net_db 刪除
volumes:
- ./redis/config:/docker/config
- ./redis/data:/data
command: redis-server /docker/config/redis.conf (14)刪除
(12)利用docker-compose up -d --build ,將redis加入docker 服務,並啟動。
$sudo docker-compose up -d --build
$sudo docker-compose ps
(13)檢查狀態
$sudo docker logs db-redis
發現無法打開 /docker/config/redis.conf檢查無此檔案
所以在docker-compose.yml刪除 command: redis-server /docker/config/redis.conf 重新下指令
$sudo docker-compose up -d --build
看所有container 是否被執行起來
$sudo docker-compose ps
將所有服務停止
$sudo docker-compose down
5.Mongodb 教學
(1)執行docker 內的mongodb
docker exec -it docker-nginx-php-mysql_mongodb_1 mongo
(2)秀出資料庫
>show dbs;
(3)建立資料庫test
>use test;
(4)使用 insert 指令將一些資料輸入到 restaurants 這個 collection
>db.restaurants.insert(
{
"address" : {
"street" : "2 Avenue",
"zipcode" : "10075",
"building" : "1480",
"coord" : [ -73.9557413, 40.7720266 ],
},
"borough" : "Manhattan",
"cuisine" : "Italian",
"grades" : [
{
"date" : ISODate("2014-10-01T00:00:00Z"),
"grade" : "A",
"score" : 11
},
{
"date" : ISODate("2014-01-16T00:00:00Z"),
"grade" : "B",
"score" : 17
}
],
"name" : "Vella",
"restaurant_id" : "41704620"
}
)
(5)查詢資料,指定資料庫test,使用 find指令>db.restaurants.find();
(6)使用大於($gt)、小於($lt)指定查詢條件>db.restaurants.find( { "grades.score": { $gt: 30 } } )
>db.restaurants.find( { "grades.score": { $lt: 30 } } )
(7)使用多個條件,$and 與 $or
>db.restaurants.find( { $or: [ { "cuisine": "Italian" }, { "address.zipcode": "10075" } ] } );
>db.restaurants.find( { $and: [ { "cuisine": "Italian" }, { "address.zipcode": "10075" } ] } );
(8)查詢結果排序
遞增排序,1 代表遞增
>db.restaurants.find().sort( { "borough": 1 } )
遞減排序,-1 代表遞減
>db.restaurants.find().sort( { "borough": -1 } )
先以"borough"條件遞增,後以"address.zipcode"條件遞增
db.restaurants.find().sort( { "borough": 1,"address.zipcode": 1 } )
(9)列出所有的 collections,共三種方法
>show collections;
>show tables;
>db.getCollectionNames();
(10)查詢整個 MongoDB 中所儲存的資料量大小
db.stats();
6.rodo 3t gui 工具 (安裝windows版本),安裝的機器IP為192.168.1.155
連接的port為 27017。所以設定為192.168.1.155:27017
(1)下載並安裝
接下來,就是[下一步]到安裝結束。
(2)對程式捷徑點兩下,就可以開始設定
設定完成,就可看到mongodb的資料庫架構
(3)mongodb的資料庫操作
(4)對 collection "restaurants",再insert 一筆資料。
db.restaurants.insert(
{
"address" : {
"street" : "2333 Avenue",
"zipcode" : "100759",
"building" : "1480",
"coord" : [ -73.9557413, 40.7720266 ],
},
"borough" : "Manha333ttan",
"cuisine" : "Italian",
"grades" : [
{
"date" : ISODate("2014-10-01T00:00:00Z"),
"grade" : "A",
"score" : 11
},
{
"date" : ISODate("2014-01-16T00:00:00Z"),
"grade" : "B",
"score" : 17
}
],
"name" : "Vella",
"restaurant_id" : "41704620"
}
);
(5)下指令
>db.getCollection('restaurants').find();
資料來源:
1.安裝 docker
2.安裝 docker-composer
3.Docker 教學
4.MySQL 與 MongoDB
5.Mongodb 教學
6.rodo 3t gui 工具
7.使用 docker-compose.yml 定義多容器應用程式
1.安裝 docker
2.安裝 docker-composer
3.Docker 教學
4.MySQL 與 MongoDB
5.Mongodb 教學
6.rodo 3t gui 工具
7.使用 docker-compose.yml 定義多容器應用程式
沒有留言:
張貼留言