用docker搭建最简单的应用
Posted givenchy_yzl
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了用docker搭建最简单的应用相关的知识,希望对你有一定的参考价值。
搭建Wordpress
下载项目包
#下载worldpress官网最新安装包
[root@docker1 opt]# wget https://wordpress.org/latest.zip
#解压压缩包
[root@docker1 opt]# unzip latest.zip
docker虚拟机上
nginx配置
#建立一个网桥
[root@docker1 ~]# docker network create lnmp
#创建三个文件夹
[root@docker1 ~]# mkdir {nginx,php,mysql}
#切换目录
[root@docker1 ~]# cd nginx/
#编辑nginx配置文件
[root@docker1 nginx]# vim nginx.conf
user www;
worker_processes auto;
error_log /var/log/nginx/error.log notice;
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/conf.d/*.conf;
}
#编辑worldpress站点目录文件
[root@docker1 nginx]# vim default.conf
server {
listen 80;
root /usr/share/nginx/html/;
index index.html index.htm index.php;
location / {
if ( -f $request_filename) {
break;
}
if (!-e $request_filename) {
rewrite ^/(.*)$ /index.php/$1 last;
break;
}
try_files $uri $uri/ /index.php$uri?$query_string;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
location ~ \\.php($|/) {
set $script $uri;
set $path_info "";
if ($uri ~ "^(.+\\.php)(/.+)") {
set $script $1;
set $path_info $2;
}
fastcgi_pass php:9000;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
fastcgi_param PATH_INFO $path_info;
fastcgi_param SCRIPT_NAME $script;
try_files $uri =404;
}
}
[root@docker1 nginx]# vim nginx.repo
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=0
enabled=1
module_hotfixes=true
#编辑nginx Dockerfile
[root@docker1 nginx]# vim Dockerfile
FROM centos:7
ADD default.conf /etc/nginx/conf.d/
ADD nginx.conf /etc/nginx
ADD nginx.repo /etc/yum.repos.d/nginx.repo
RUN useradd www
RUN yum install -y nginx
EXPOSE 80
WORKDIR /root
CMD nginx -g "daemon off;"
#利用Dockerfile 构建镜像
[root@docker1 nginx]# docker build -t nginx .
#开启ipv4地址转发
[root@docker1 ~]# vim /etc/sysctl.conf
net.ipv4.ip_forward = 1
#重启网络
[root@docker1 ~]# systemctl restart network
#查看ip转发是否配置成功
[root@docker1 ~]# sysctl net.ipv4.ip_forward
net.ipv4.ip_forward = 1
#启动容器(容器名为nginx,绑定端口映射,指定网桥,指定挂载目录,开辟一个窗口)
[root@docker1 opt]# docker run -it --name nginx -p 80:80 --network=lnmp -v /opt/wordpress/:/usr/share/nginx/html nginx:latest bash
PHP配置
#切换目录
[root@docker1 nginx]# cd …/php/
#编辑php配置文件
[root@docker1 php]# vim www.conf
[www]
user = www
group = www
listen = 9000
request_terminate_timeout = 0
pm = dynamic
pm.max_children = 50
pm.start_servers = 5
pm.min_spare_servers = 5
pm.max_spare_servers = 35
slowlog = /var/log/php-fpm/www-slow.log
php_admin_value[error_log] = /var/log/php-fpm/www-error.log
php_admin_flag[log_errors] = on
php_value[session.save_handler] = files
php_value[session.save_path] = /var/lib/php/session
php_value[soap.wsdl_cache_dir] = /var/lib/php/wsdlcache
#创建PHP.repo
[root@docker1 php]# vim php.repo
[php-webtatic]
name = PHP Repo
baseurl = http://us-east.repo.webtatic.com/yum/el7/x86_64/
gpgcheck = 0
enable = 1
#编辑php Dockerfile
[root@docker1 php]# vim Dockerfile
FROM centos:7
ADD www.conf /etc/php-fpm.d/www.conf
ADD php.repo /etc/yum.repos.d/php.repo
RUN useradd www
RUN yum install -y 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 php71w-bcmath --skip-broken
EXPOSE 9000
CMD ["php-fpm "-F""]
#构建php镜像
[root@docker1 php]# docker build -t php:v1 .
#启动容器(容器名为php,指定网桥,指定挂载目录,开辟一个窗口)
[root@docker1 php]# docker run -it --name php --network=lnmp -v /opt/wordpress/:/usr/share/nginx/html php:v1 bash
mysql配置
#启动容器(容器名为mysql,指定网桥,创建密码))
[root@docker1 opt]# docker run -d -e MYSQL_ROOT_PASSWORD=123 --network=lnmp --name mysql mysql:5.7
#进入mysql容器
[root@docker1 ~]# docker exec -it mysql bash
#进入数据库
root@9669bf1d0d99:/# mysql -uroot -p
Enter password: 123
#创建数据库
mysql> create database wordpress;
Query OK, 1 row affected (0.00 sec)
nginx容器内
#创建应用所需文件
[root@cdbddab2ad90 html]# cd/usr/share/nginx/html
[root@cdbddab2ad90 html]# vi wp-config.php
<?php
/**
* The base configuration for WordPress
*
* The wp-config.php creation script uses this file during the
*
* This file contains the following configurations:
*
* * MySQL settings
* * Secret keys
* * Database table prefix
* * ABSPATH
*
* @link https://wordpress.org/support/article/editing-wp-config-php/
*
* @package WordPress
*/
// ** MySQL settings - You can get this info from your web host ** //
/** The name of the database for WordPress */
define( 'DB_NAME', 'wordpress' );
/** MySQL database username */
define( 'DB_USER', 'root' );
/** MySQL database password */
define( 'DB_PASSWORD', '123' );
/** MySQL hostname */
define( 'DB_HOST', 'mysql' );
/** Database Charset to use in creating database tables. */
define( 'DB_CHARSET', 'utf8mb4' );
/** The Database Collate type. Don't change this if in doubt. */
define( 'DB_COLLATE', '' );
/**#@+
* Authentication Unique Keys and Salts.
*
* Change these to different unique phrases!
* You can generate these using the {@link https://api.wordpress.org/secret-key/1.1/salt/ WordPress.org secret-key service}
*
* @since 2.6.0
*/
define( 'AUTH_KEY', '|D-gm:iMGF5(HcH6:#N;nl<8n!lE.``P${q1p|/TM<6z~GK6*n9UyAPzu~a8, U}' );
define( 'SECURE_AUTH_KEY', '/+y}hVin#l0B;F+-juHh-E=5{$9ItrqVzqp!hI8X_2+-rI.[nZNcrfH!qZ*.<!Bf' );
define( 'LOGGED_IN_KEY', 'd@G|3N)R]c5TaeK,{9A?NI=AA]ps1HYhkBAFKJk9:Zn%c~$PQtki]]!}89a|^P6L' );
define( 'NONCE_KEY', 'giY3odJ_qn6^L4 T-xx@JISdy1l;eZn?}ZA~a+TE^gvc]0iU4s!Z-|Mk?n3iBKKu' );
define( 'AUTH_SALT', '=(2f8x;^-6fTEpg,X0_Wm16n@b$Zu]-Q$G7YtYj1.uD>+L S:8:mv [}1&7:Tqc)' );
define( 'SECURE_AUTH_SALT', 'as|%edpHdS5/ik/TV|Qoo$=Da/2fl{:r.`;dhAX-KL!*=%@W6{EW]M.oWO,E:9?X' );
define( 'LOGGED_IN_SALT', '#vBSzvD9JZ~4?UDhW.RVOl9}n1`Sm?ob~:j[&Hl@.]o)_M/TfE6.d2<Z3V_>tS2b' );
define( 'NONCE_SALT', 'f~MgE_(^s}rhjnGN1}XBP(4h0MS!EAR|*Z&OXk>#t@7=KKm4.PcY6~?J$Dq6_:ZE' );
/**#@-*/
/**
* WordPress Database Table prefix.
*
* You can have multiple installations in one database if you give each
* a unique prefix. Only numbers, letters, and underscores please!
*/
$table_prefix = 'wp_';
/**
* For developers: WordPress debugging mode.
*
* Change this to true to enable the display of notices during development.
* It is strongly recommended that plugin and theme developers use WP_DEBUG
* in their development environments.
*
* For information on other constants that can be used for debugging,
* visit the documentation.
*
* @link https://wordpress.org/support/article/debugging-in-wordpress/
*/
define( 'WP_DEBUG', false );
/* That's all, stop editing! Happy publishing. */
/** Absolute path to the WordPress directory. */
if ( ! defined( 'ABSPATH' ) ) {
define( 'ABSPATH', __DIR__ . '/' );
}
/** Sets up WordPress vars and included files. */
require_once ABSPATH . 'wp-settings.php';
以上是关于用docker搭建最简单的应用的主要内容,如果未能解决你的问题,请参考以下文章