Docker构建

Docker构建之旅

##构建三个docker,php、nginx、mysql三个镜像 ###1,先从docker仓库里面拉取centos镜像,和mysql镜像

docker pull docker.io/centos
docker pill docker.io/mysql

###2,创建一个网络,我们一会使用这个网络进行container之间的联系。 docker network create --subnet 172.16.1.0/24 testnetwork

###3,构建nginx的Dockerfile文件

[root@Docker docker_file]# vim Dockerfile_nginx 
FROM centos

VOLUME ["/code"]
COPY ./nginx.repo /etc/yum.repos.d/
RUN yum clean all && \
    yum -y install nginx  httpd-tools && \
    yum clean all && \
    htpasswd -b -c /etc/http_blog.pass zsf zsf && \
    groupadd -g 8888 www && useradd -u 8888 -g www -M -s /sbin/nologin www

COPY nginx.conf /etc/nginx/nginx.conf
COPY blog.sentinel.conf /etc/nginx/conf.d/
COPY edu.sentinel.conf /etc/nginx/conf.d/
COPY lt.sentinel.conf /etc/nginx/conf.d/

EXPOSE 80 81 82

CMD ["nginx","-g","daemon off;"]

###4,构建php的Dockerfile文件

[root@Docker docker_file]# vim php_file
FROM centos
RUN rpm -Uvh http://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm && \
    rpm -Uvh http://mirror.webtatic.com/yum/el7/webtatic-release.rpm && \
    yum -y install php71w php71w-cli php71w-common php71w-devel \
    php71w-embedded php71w-gd php71w-mcrypt php71w-mbstring \
    php71w-pdo php71w-xml php71w-fpm php71w-mysqlnd php71w-opcache \
    php71w-pecl-memcached php71w-pecl-redis php71w-pecl-mongodb httpd-tools && \
    yum clean all && groupadd -g 8888 www && useradd -u 8888 -g www -M -s /sbin/nologin www

COPY www.conf /etc/php-fpm.d/
COPY php-fpm.conf /etc/

EXPOSE 9000

CMD ["/usr/sbin/php-fpm"]

###5,在Nginx里面配置了三个web服务,分别是blog,edu,lt,构建对应的配置文件 nginx的主配置文件

```nginx.conf
[root@Docker docker_file]# cat nginx.conf 
user  www;
worker_processes  1;
error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;
events {
worker_connections  1024;
}
http {
include       /etc/nginx/mime.types;
default_type  application/octet-stream;
log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log  /var/log/nginx/access.log  main;

sendfile        on;
#tcp_nopush     on;
keepalive_timeout  65;
#gzip  on;
include /etc/nginx/vhost/*.conf;
include /etc/nginx/conf.d/*.conf;
}
**blog站点的配置文件**

```nginx.conf
[root@Docker docker_file]# cat blog.sentinel.conf
server {
    listen 80;
    server_name 10.0.0.107;
    root /code/wordpress;
    access_log /var/log/nginx/blog.access.log main;
    index index.php;
    client_max_body_size 300m;

    location  ~* \.php$ {
        root /code/wordpress;
        fastcgi_pass 172.16.1.100:9000;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }

    location ~* ^/wp-admin {
        auth_basic "please user password";
        auth_basic_user_file /etc/http_blog.pass;
        index index.php;
    }    
}

edusoho--nginx的配置文件

```nginx.conf
[root@Docker docker_file]# cat edu.sentinel.conf
server {
listen 81;
server_name 172.16.1.7;

client_max_body_size 300m;

root /code/edusoho/web;

access_log /var/log/nginx/edu.sentinel.org.log;
error_log /var/log/nginx/edu.sentinel.org.log;

location / {
    index app.php;
    try_files $uri @rewriteapp;
}

location @rewriteapp {
    rewrite ^(.*)$ /app.php/$1 last;
}

location ~ ^/udisk {
    internal;
    root /code/edusoho/app/data/;
}

location ~ ^/(app|app_dev)\.php(/|$) {
    fastcgi_pass   172.16.1.100:9000;
    fastcgi_split_path_info ^(.+\.php)(/.*)$;
    include fastcgi_params;
    fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;
    fastcgi_param  HTTPS              off;
    fastcgi_param HTTP_X-Sendfile-Type X-Accel-Redirect;
    fastcgi_param HTTP_X-Accel-Mapping /udisk=/code/edusoho/app/data/udisk;
    fastcgi_buffer_size 128k;
    fastcgi_buffers 8 128k;
}

location ~ ^/files/.*\.(php|php5)$ {
    deny all;
}

location ~ \.php$ {
    fastcgi_pass   172.16.1.100:9000;
    fastcgi_split_path_info ^(.+\.php)(/.*)$;
    include fastcgi_params;
    fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;
    fastcgi_param  HTTPS              off;
}
}
**lt的nginx配置文件**

```nginx
[root@Docker docker_file]# cat lt.sentinel.conf
server {
    listen 82;
    server_name 172.16.1.7;
    root /code/lt;
    index index.php;

    location ~* \.php$ {
        root /code/lt;
        fastcgi_pass 172.16.1.100:9000;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_index index.php;
        include fastcgi_params;
    }
}

6,php的相关配置文件

php的主配置文件

```nginx.conf [root@Docker docker_file]# cat php-fpm.conf daemonize = no

**www.conf的配置文件**

```nginx.conf
[root@Docker docker_file]# cat www.conf | grep -E "user|group|listen"
user = www
group = www
listen = 172.16.1.100:9000
listen.allowed_clients = 172.16.1.5

##然后开始构建镜像 构建nginx的镜像 [root@Docker docker_file]# docker build -f Dockerfile_nginx -t nginx/php:1.5 .

构建php代码 [root@Docker docker_file]# docker build -f php_file -t php:8.8 .

运行docker镜像,测试结果

构建一个nginx的容器container [root@Docker docker_file]# docker run -d --network testnetwork --ip 172.16.1.5 -p 10.0.0.250:80-82:80-82 -v /code:/code --name nginx nginx/php:1.5 构建完成之后执行命令 [root@Docker docker_file]# docker exec -it nginx chown -R www.www /code

构建一个PHP的容器container [root@Docker docker_file]# docker run -d --volumes-from nginx -p 9000:9000 --network testnetwork --ip 172.16.1.100 --name php php:8.8

构建mysql镜像 [root@Docker tmp]# docker run -it --network testnetwork --ip 172.16.1.10 --name mysql -v /data/db/mariadb:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 -d docker.io/mysql

root@6106cebefb38:/# mysql -uroot -p      
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 54
Server version: 8.0.12 MySQL Community Server - GPL

Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> CREATE USER 'zsf'@'172.16.1.100' IDENTIFIED BY '123123';
mysql> GRANT all ON *.* TO 'zsf'@'172.16.1.100';

在客户端测试,自行测试

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

扫码关注云+社区

领取腾讯云代金券