獲取鏡像php
docker pull mysql:5.7.17
相應目錄
~/docker/data/mysql 數據目錄
~/docker/log/mysql log目錄html
生成容器mysql
docker run --name mysql -p 3306:3306 -v ~/docker/data/mysql:/var/lib/mysql -v ~/docker/log/mysql:/var/log/mysql -e MYSQL_ROOT_PASSWORD=123456 -dit mysql:5.7.17
外部鏈接nginx
mysql -uroot -p123456 -h 127.0.0.1 or docker exec -it mysql mysql -uroot -p123456
獲取鏡像redis
docker pull redis:3.2
相應目錄 ~/docker/data/redis 數據sql
~/docker/conf/redis.conf 配置文件 生成容器docker
docker run --name redis -p 6379:6379 -v ~/docker/data/redis:/data -v ~/docker/conf/redis/redis.conf:/etc/redis.conf -v ~/docker/log/redis:/var/log/redis -dit redis:3.2 redis-server /etc/redis.conf
###php-fpm 獲取鏡像php-fpm
docker pull php:7.1.8-fpm
生成容器fetch
docker run --name php-fpm -p 9000:9000 -v ~/docker/conf/php/php.ini:/usr/local/etc/php/php.ini -v ~/www:/var/www/html -v ~/docker/log/php:/var/log/php --link mysql:mysql --link redis:redis --privileged=true -dit php:7.1.8-fpm
安裝擴展code
# redis pecl bundle -d /usr/src/php/ext redis rm /usr/src/php/ext/redis-*.tgz docker-php-ext-install redis # pdo_mysql docker-php-ext-install pdo_mysql
test info.php
<?php echo date("Y-m-d H:i:s")."<br />\n"; $redis = new Redis(); $redis->connect("redis","6379"); //php客戶端設置的ip及端口 //存儲一個 值 $redis->set("say","hello world"); echo $redis->get("say"); //應輸出hello world try { $conn = new PDO("mysql:host=mysql;port=3306;dbname=mysql;charset=utf8", 'root', '123456'); } catch (PDOException $e) { echo 'Connection failed: ' . $e->getMessage(); } //$conn->exec('set names utf8'); $sql = "SELECT * FROM `user` WHERE 1"; $result = $conn->query($sql); while($rows = $result->fetch(PDO::FETCH_ASSOC)) { echo $rows['Host'] . ' ' . $rows['User']."<br />\n"; } echo phpinfo();
獲取鏡像
docker pull nginx
生成容器
docker run -p 80:80 --name nginx --link php-fpm:php-fpm -v ~/www:/var/www/html -v ~/docker/conf/nginx/conf.d:/etc/nginx/conf.d -v ~/docker/log/nginx:/var/log/nginx --privileged=true -d nginx
conf.d/default.conf 默認配置文件
server { listen 80 ; server_name localhost; root /var/www/html/; location / { index index.php index.html index.htm; autoindex off; if (!-e $request_filename) { rewrite ^.*$ /index.php; } } location ~ \.php$ { fastcgi_pass php-fpm:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } }