前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Docker构建

Docker构建

作者头像
张琳兮
发布2019-03-14 11:54:06
6590
发布2019-03-14 11:54:06
举报
文章被收录于专栏:首富手记首富手记

Docker构建之旅

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

代码语言:javascript
复制
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文件

代码语言:javascript
复制
[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文件

代码语言:javascript
复制
[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的主配置文件

代码语言:javascript
复制
```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;
}
代码语言:javascript
复制
**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的配置文件

代码语言:javascript
复制
```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;
}
}
代码语言:javascript
复制
**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

代码语言:javascript
复制
**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

代码语言:javascript
复制
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';

在客户端测试,自行测试

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018-09-23 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Docker构建之旅
    • 6,php的相关配置文件
      • 运行docker镜像,测试结果
        • 在客户端测试,自行测试
        相关产品与服务
        容器镜像服务
        容器镜像服务(Tencent Container Registry,TCR)为您提供安全独享、高性能的容器镜像托管分发服务。您可同时在全球多个地域创建独享实例,以实现容器镜像的就近拉取,降低拉取时间,节约带宽成本。TCR 提供细颗粒度的权限管理及访问控制,保障您的数据安全。
        领券
        问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档