设为首页 收藏本站
查看: 1124|回复: 0

[经验分享] Docker构建

[复制链接]

尚未签到

发表于 2019-2-21 06:02:52 | 显示全部楼层 |阅读模式
个人博客转至: www.zhangshoufu.com

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';
在客户端测试,自行测试




运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-674954-1-1.html 上篇帖子: 关于搭建harbor企业级访问docker仓库 下篇帖子: 『中级篇』Docker Cloud自动构建 Docker image(55)
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表