0x00 前言

docker学的太菜了,诟病很久了,之前学过会一些基本命令,但是真正搭一个web应用一直不会终于成功了记录一下。趁没课花了一早上学了一波终于成功了。

docker一些基本命令

查看已有镜像:

docker images

删除镜像

docker rmi [Image ID]
删全部:docker rmi $(docker images -q)

查看运行中的容器:

docker ps

查看所有容器:

docker ps -a

删除容器:

docker rm [container ID]
删全部:docker rm $(docker ps -a -q)

停止容器运转:

docker stop [container ID]

运行一个镜像:

docker run [OPTIONS] IMAGE [COMMAND] [ARG...]

常用[option]

-p  主机端口号:容器端口

-v  将主机目录挂载到容器目录

--name  xxx 将容器命名为xxx

-d  后台运行

查看容器日志

docker logs [容器名]

进入一个容器的shell

docker exec -it IMAGE /bin/sh

下载一个镜像

docker pull [image name]:[版本号]

0x01 搭建一个nginx+php-fpm环境

apache还没做成,再摸摸鱼,先记录一下nginx,参考了一下使用Docker快速搭建Nginx+PHP-FPM环境

首先搭建nginx作为web应用服务器,php-fpm作用为解析php,否则访问php就是下载php文件

下载nginx官方镜像和php-fpm镜像

docker pull nginx

docker pull bitnami/php-fpm

然后用镜像加载容器

php-fpm: docker run -d --name myFpm  -v /var/www/html:/usr/share/nginx/html bitnami/php-fpm

nginx:  docker run -d --name myNginx -p 8888:80 -v /var/www/html:/usr/share/nginx/html nginx
-d : 该参数为后台运行之意
-v : 指定宿主机与容器的映射关系。/var/www/html为宿主机的项目目录(自定的),/usr/share/nginx/html为nginx服务器项目默认的路径。

看到都是UP说明开启着,然后将nginx配置文件copy到本地:

docker cp myNginx:/etc/nginx/conf.d/default.conf   ./default.conf

到本地修改nginx容器里/etc/nginx/conf.d/default.conf文件,为

“`
location ~ .php$ {
root html;
fastcgi_pass 172.17.0.3:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /usr/share/nginx/html$fastcgi_script_name;
include fastcgi_params;
}

“`

fastcgi_pass可以通过docker inspect myFpm | grep "IPAddress"查看并修改,

root@tinmin-virtual-machine:~# docker inspect myFpm | grep "IPAddress"
            "SecondaryIPAddresses": null,
            "IPAddress": "172.17.0.3",
                    "IPAddress": "172.17.0.3",

再复制回去

docker cp ./default.conf    myNginx:/etc/nginx/conf.d/default.conf

进入nginx容器的shell:

docker exec -it myNginx /bin/sh

再让nginx重新加载一下配置文件:

service nginx reload

千万能不能service nginx restart,这样容器就停止了

然后CTRL+D退出

在浏览器输入127.0.0.1:8888/1.php

img

成功!

使用Dockerfile+docker-compose布置(方便)

参考:http://geekyplatypus.com/dockerise-your-php-application-with-nginx-and-php7-fpm/
创建一个nginx+fpm文件夹

目录结构为:
tree

docker-compose.yml

version: '3'
services:

  my-fpm:
    image: bitnami/php-fpm
    restart: always
    volumes:
      - "./nginx/1.php:/usr/share/nginx/html/index.php"
  nginx:
    image: nginx
    restart: always
    volumes:
      - "./nginx/conf.d:/etc/nginx/conf.d"
    ports:
      - "8080:8080"
    links:
      - "db"
      - "my-fpm"

  db:
    image: mysql:5.7
    restart: always
    environment:
      - MYSQL_ROOT_PASSWORD=tinmin
      - MYSQL_DATABASE=test

1.php

<?php

$host = "db";
$user = "root";
$passwd = "tinmin";
$db = "test";

$conn = new mysqli($host,$user,$passwd,$db);
var_dump($conn);

./conf.d/default.conf

server {
    listen       8080;
    server_name  localhost;

    location / {
        root   /usr/share/nginx/html;
        index   index.htm index.php;
    }
    location ~ \.php$ {
        root           html;
        fastcgi_pass   my-fpm:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  /usr/share/nginx/html$fastcgi_script_name;
        include        fastcgi_params;
    }
  }

最后docker-compose up -d启动就行了
curl

0x02 搭建一个apache2环境

官方文档有php-apache集合镜像,能快速搭建php+apache2环境,

比nginx简单多了2333

img

拉取镜像:

docker pull php:7.0-apache

运行容器,目录结构和linux的差不多,web根目录都是/var/www/html,直接:

docker run --name test -p 800:80 -v /var/www/html:/var/www/html -d php:7.0-apache

然后访问http://127.0.0.1:800

img

坑点就是php.ini没法设置,好多php模块用不了2333……

Categories: web

1 Comment

nicolialia pizzeria · March 8, 2019 at 5:19 am

I love your blog.. very nice colors & theme.
Did you make this website yourself or did you hire someone to do it for you?
Plz respond as I’m looking to create my own blog and would like to know where u got this from.
cheers

Leave a Reply

Your email address will not be published. Required fields are marked *