dockerfite创建镜像---INMP+wordpress

搭建dockerfile---lnmp

在192.168.10.201

使用 Docker 构建 LNMP 环境并运行 Wordpress 网站平台

[root@docker1 opt]# mkdir nginx mysql php

[root@docker1 opt]# ls

#分别拖入四个包:

nginx-1.22.0.tar.gz

mysql-boost-5.7.20.tar.gz

php-7.1.10.tar.bz2

wordpress-6.4.2-zh_CN.zip

路径

vim /opt/nginx/Dockerfile


FROM centos:7

RUN yum -y install gcc pcre-devel openssl-devel zlib-devel openssl openssl-devel

ADD nginx-1.22.0.tar.gz /usr/local/src/

RUN useradd -M -s /sbin/nologin nginx

WORKDIR /usr/local/src/nginx-1.22.0

RUN ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_stub_status_module --with-http_gzip_static_module --with-pcre --with-stream --with-stream_ssl_module --with-stream_realip_module && make -j 4 && make install

ENV PATH /usr/local/nginx/sbin:$PATH

COPY nginx.conf /usr/local/nginx/conf/

ADD wordpress-6.4.2-zh_CN.tar.gz /usr/local/nginx/html

RUN chmod -R 777 /usr/local/nginx/html

EXPOSE 80

VOLUME ["/usr/local/nginx/html/"]

CMD ["/usr/local/nginx/sbin/nginx","-g","daemon off;"]


位置

vim /opt/nginx/nginx.conf


worker_processes 1;

events {

worker_connections 1024;

}

http {

include mime.types;

default_type application/octet-stream;

sendfile on;

keepalive_timeout 65;

server {

listen 80;

server_name localhost;

charset utf-8;

location / {

root html;

index index.html index.php;

}

error_page 500 502 503 504 /50x.html;

location = /50x.html {

root html;

}

location ~ \.php$ {

root html;

fastcgi_pass 172.111.0.30:9000;

fastcgi_index index.php;

fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html$fastcgi_script_name;

include fastcgi_params;

}

}

}


1、创建nginx镜像

docker build -t nginx1:lnmp .

2、创建自定义网络

docker network create --subnet=172.111.0.0/16 --opt "com.docker.network.bridge.name"="docker1" mynetwork

3、创建并启动容器

docker run -itd --name nginx1 -p 80:80 -v /opt/nginx:/opt/nginxlogs --net mynetwork --ip 172.111.0.10 nginx:lnmp

vim /opt/mysql/Dockerfile


FROM centos:7

RUN yum -y install ncurses ncurses-devel bison cmake pcre-devel zlib-devel gcc gcc-c++ make && useradd -M -s /sbin/nologin mysql

ADD mysql-boost-5.7.20.tar.gz /usr/local/src/

WORKDIR /usr/local/src/mysql-5.7.20/

RUN cmake \

-DCMAKE_INSTALL_PREFIX=/usr/local/mysql \

-DMYSQL_UNIX_ADDR=/usr/local/mysql/mysql.sock \

-DSYSCONFDIR=/etc \

-DSYSTEMD_PID_DIR=/usr/local/mysql \

-DDEFAULT_CHARSET=utf8 \

-DDEFAULT_COLLATION=utf8_general_ci \

-DWITH_EXTRA_CHARSETS=all \

-DWITH_INNOBASE_STORAGE_ENGINE=1 \

-DWITH_ARCHIVE_STORAGE_ENGINE=1 \

-DWITH_BLACKHOLE_STORAGE_ENGINE=1 \

-DWITH_PERFSCHEMA_STORAGE_ENGINE=1 \

-DMYSQL_DATADIR=/usr/local/mysql/data \

-DWITH_BOOST=boost \

-DWITH_SYSTEMD=1 && make -j 6 && make install

COPY my.cnf /etc/my.cnf

EXPOSE 3306

RUN chown -R mysql:mysql /usr/local/mysql && chown mysql:mysql /etc/my.cnf

WORKDIR /usr/local/mysql/bin/

RUN ./mysqld \

--initialize-insecure \

--user=mysql \

--basedir=/usr/local/mysql \

--datadir=/usr/local/mysql/data && cp /usr/local/mysql/usr/lib/systemd/system/mysqld.service /usr/lib/systemd/system/ && systemctl enable mysqld

ENV PATH=/usr/local/mysql/bin:/usr/local/mysql/lib:$PATH

VOLUME ["/usr/local/mysql"]

ENTRYPOINT ["/usr/sbin/init"]


vim /opt/mysql/my.cnf


[client]

port = 3306

socket=/usr/local/mysql/mysql.sock

[mysqld]

user = mysql

basedir=/usr/local/mysql

datadir=/usr/local/mysql/data

port = 3306

character-set-server=utf8

pid-file = /usr/local/mysql/mysqld.pid

socket=/usr/local/mysql/mysql.sock

bind-address = 0.0.0.0

skip-name-resolve

max_connections=2048

default-storage-engine=INNODB

max_allowed_packet=16M

server-id = 1

general_log=ON

general_log_file=/usr/local/mysql/data/mysql_general.log

sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_AUTO_VALUE_ON_ZERO,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,PIPES_AS_CONCAT,ANSI_QUOTES


1、创建mysql镜像

docker build -t mysql:lnmp .

2、创建并启动容器

docker run -itd --name mysql -p 3306:3306 --privileged -v /opt/mysql1:/opt/mysql --net mynetwork --ip 172.111.0.20 mysql:lnmp

3、进入数据库

mysql -u root -p

4、创建数据库

create database wordpress;

5、赋权

grant all privileges on wordpress.* to 'wordpress'@'%' identified by '123456';

grant all privileges on *.* to 'root'@'%' identified by '123456';

flush privileges;

vim /opt/php/Dockerfile


FROM centos:7

RUN yum -y install gd \

libjpeg libjpeg-devel \

libpng libpng-devel \

freetype freetype-devel \

libxml2 libxml2-devel \

zlib zlib-devel \

curl curl-devel \

openssl openssl-devel \

gcc gcc-c++ make pcre-devel && useradd -M -s /sbin/nologin nginx

ADD php-7.1.10.tar.bz2 /usr/local/src

WORKDIR /usr/local/src/php-7.1.10

RUN ./configure \

--prefix=/usr/local/php \

--with-mysql-sock=/usr/local/mysql/mysql.sock \

--with-mysqli \

--with-zlib \

--with-curl \

--with-gd \

--with-jpeg-dir \

--with-png-dir \

--with-freetype-dir \

--with-openssl \

--enable-fpm \

--enable-mbstring \

--enable-xml \

--enable-session \

--enable-ftp \

--enable-pdo \

--enable-tokenizer \

--enable-zip && make -j 4 && make install

ENV PATH /usr/local/php/bin:/usr/local/php/sbin:$PATH

COPY php.ini /usr/local/php/lib

COPY php-fpm.conf /usr/local/php/etc/

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

EXPOSE 9000

ENTRYPOINT ["/usr/local/php/sbin/php-fpm","-F"]


三个配置文件 php.ini php-fpm.conf www.conf拖到php的目录中。

vim /opt/php/php-fpm.conf


;;;;;;;;;;;;;;;;;;;;;

; FPM Configuration ;

;;;;;;;;;;;;;;;;;;;;;

; All relative paths in this configuration file are relative to PHP's install

; prefix (/usr/local/php). This prefix can be dynamically changed by using the

; '-p' argument from the command line.

;;;;;;;;;;;;;;;;;;

; Global Options ;

;;;;;;;;;;;;;;;;;;

[global]

; Pid file

; Note: the default prefix is /usr/local/php/var

; Default Value: none

pid = run/php-fpm.pid

; Error log file

; If it's set to "syslog", log is sent to syslogd instead of being written

; into a local file.

; Note: the default prefix is /usr/local/php/var

; Default Value: log/php-fpm.log

;error_log = log/php-fpm.log

; syslog_facility is used to specify what type of program is logging the

; message. This lets syslogd specify that messages from different facilities

; will be handled differently.

; See syslog(3) for possible values (ex daemon equiv LOG_DAEMON)

; Default Value: daemon

;syslog.facility = daemon

; syslog_ident is prepended to every message. If you have multiple FPM

; instances running on the same server, you can change the default value

; which must suit common needs.

; Default Value: php-fpm

;syslog.ident = php-fpm

; Log level

; Possible Values: alert, error, warning, notice, debug

; Default Value: notice

;log_level = notice

; If this number of child processes exit with SIGSEGV or SIGBUS within the time

; interval set by emergency_restart_interval then FPM will restart. A value

; of '0' means 'Off'.

; Default Value: 0

;emergency_restart_threshold = 0

; Interval of time used by emergency_restart_interval to determine when

; a graceful restart will be initiated. This can be useful to work around

; accidental corruptions in an accelerator's shared memory.

; Available Units: s(econds), m(inutes), h(ours), or d(ays)

; Default Unit: seconds

; Default Value: 0

;emergency_restart_interval = 0

; Time limit for child processes to wait for a reaction on signals from master.

; Available units: s(econds), m(inutes), h(ours), or d(ays)

; Default Unit: seconds

; Default Value: 0

;process_control_timeout = 0

; The maximum number of processes FPM will fork. This has been designed to control

; the global number of processes when using dynamic PM within a lot of pools.

; Use it with caution.

; Note: A value of 0 indicates no limit

; Default Value: 0

; process.max = 128

; Specify the nice(2) priority to apply to the master process (only if set)

; The value can vary from -19 (highest priority) to 20 (lowest priority)

; Note: - It will only work if the FPM master process is launched as root

; - The pool process will inherit the master process priority

; unless specified otherwise

; Default Value: no set

; process.priority = -19

; Send FPM to background. Set to 'no' to keep FPM in foreground for debugging.

; Default Value: yes

;daemonize = yes

; Set open file descriptor rlimit for the master process.

; Default Value: system defined value

;rlimit_files = 1024

; Set max core size rlimit for the master process.

; Possible Values: 'unlimited' or an integer greater or equal to 0

; Default Value: system defined value

;rlimit_core = 0

; Specify the event mechanism FPM will use. The following is available:

; - select (any POSIX os)

; - poll (any POSIX os)

; - epoll (linux >= 2.5.44)

; - kqueue (FreeBSD >= 4.1, OpenBSD >= 2.9, NetBSD >= 2.0)

; - /dev/poll (Solaris >= 7)

; Set max core size rlimit for the master process.

; Possible Values: 'unlimited' or an integer greater or equal to 0

; Default Value: system defined value

;rlimit_core = 0

; Specify the event mechanism FPM will use. The following is available:

; - select (any POSIX os)

; - poll (any POSIX os)

; - epoll (linux >= 2.5.44)

; - kqueue (FreeBSD >= 4.1, OpenBSD >= 2.9, NetBSD >= 2.0)

; - /dev/poll (Solaris >= 7)

; - port (Solaris >= 10)

; Default Value: not set (auto detection)

;events.mechanism = epoll

; When FPM is built with systemd integration, specify the interval,

; in seconds, between health report notification to systemd.

; Set to 0 to disable.

; Available Units: s(econds), m(inutes), h(ours)

; Default Unit: seconds

; Default value: 10

;systemd_interval = 10

;;;;;;;;;;;;;;;;;;;;

; Pool Definitions ;

;;;;;;;;;;;;;;;;;;;;

; Multiple pools of child processes may be started with different listening

; ports and different management options. The name of the pool will be

; used in logs and stats. There is no limitation on the number of pools which

; FPM can handle. Your system will tell you anyway :)

; Include one or more files. If glob(3) exists, it is used to include a bunch of

; files from a glob(3) pattern. This directive can be used everywhere in the

; file.

; Relative path can also be used. They will be prefixed by:

; - the global prefix if it's been set (-p argument)

; - /usr/local/php otherwise

include=/usr/local/php/etc/php-fpm.d/*.con

vim /opt/php/php.ini


; reattach to the shared memory (for Windows only). Explicitly enabled file

; cache is required.

;opcache.file_cache_fallback=1

; Enables or disables copying of PHP code (text segment) into HUGE PAGES.

; This should improve performance, but requires appropriate OS configuration.

;opcache.huge_code_pages=0

; Validate cached file permissions.

;opcache.validate_permission=0

; Prevent name collisions in chroot'ed environment.

;opcache.validate_root=0

[curl]

; A default value for the CURLOPT_CAINFO option. This is required to be an

; absolute path.

;curl.cainfo =

[openssl]

; The location of a Certificate Authority (CA) file on the local filesystem

; to use when verifying the identity of SSL/TLS peers. Most users should

; not specify a value for this directive as PHP will attempt to use the

; OS-managed cert stores in its absence. If specified, this value may still

; be overridden on a per-stream basis via the "cafile" SSL stream context

; option.

;openssl.cafile=

; If openssl.cafile is not specified or if the CA file is not found, the

; directory pointed to by openssl.capath is searched for a suitable

; certificate. This value must be a correctly hashed certificate directory.

; Most users should not specify a value for this directive as PHP will

; attempt to use the OS-managed cert stores in its absence. If specified,

; this value may still be overridden on a per-stream basis via the "capath"

; SSL stream context option.

;openssl.capath=

; Local Variables:

; tab-width: 4

; End:


vim /opt/php/www.conf


; prevent configuration mistakes on the web server side. You should only limit

; FPM to .php extensions to prevent malicious users to use other extensions to

; execute php code.

; Note: set an empty value to allow all extensions.

; Default Value: .php

;security.limit_extensions = .php .php3 .php4 .php5 .php7

; Pass environment variables like LD_LIBRARY_PATH. All $VARIABLEs are taken from

; the current environment.

; Default Value: clean env

;env[HOSTNAME] = $HOSTNAME

;env[PATH] = /usr/local/bin:/usr/bin:/bin

;env[TMP] = /tmp

;env[TMPDIR] = /tmp

;env[TEMP] = /tmp

; Additional php.ini defines, specific to this pool of workers. These settings

; overwrite the values previously defined in the php.ini. The directives are the

; same as the PHP SAPI:

; php_value/php_flag - you can set classic ini defines which can

; be overwritten from PHP call 'ini_set'.

; php_admin_value/php_admin_flag - these directives won't be overwritten by

; PHP call 'ini_set'

; For php_*flag, valid values are on, off, 1, 0, true, false, yes or no.

; Defining 'extension' will load the corresponding shared extension from

; extension_dir. Defining 'disable_functions' or 'disable_classes' will not

; overwrite previously defined php.ini values, but will append the new value

; instead.

; Note: path INI options can be relative and will be expanded with the prefix

; (pool, global or /usr/local/php)

; Default Value: nothing is defined by default except the values in php.ini and

; specified at startup with the -d argument

;php_admin_value[sendmail_path] = /usr/sbin/sendmail -t -i -f www@my.domain.com

;php_flag[display_errors] = off

;php_admin_value[error_log] = /var/log/fpm-php.www.log

;php_admin_flag[log_errors] = on

;php_admin_value[memory_limit] = 32M


docker build -t php:lnmp .

#进入php容器,使用nginx和php的挂载卷:

docker run -itd --name php --net mynetwork --ip 172.111.0.30 -p 9000:9000 --volumes-from nginx --volumes-from mysql php:lnmp

#查看php的状态:

[root@c0be93fcb04b mysql]# ps -aux

USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND

root 1 0.2 0.1 113028 9536 pts/0 Ss+ 12:48 0:00 php-fpm: master process (/usr/local/php/etc/php-fp

nobody 7 0.0 0.0 113028 5788 pts/0 S+ 12:48 0:00 php-fpm: pool www

nobody 8 0.0 0.0 113028 5788 pts/0 S+ 12:48 0:00 php-fpm: pool www

root 9 0.1 0.0 11828 1932 pts/1 Ss 12:49 0:00 bash

root 28 0.0 0.0 51732 1724 pts/1 R+ 12:50 0:00 ps -aux

安装wordpress:

在nginx的html目录中,复制配置文件:

vim wp-config.php

<?php

/**

* The base configuration for WordPress

*

* The wp-config.php creation script uses this file during the installation.

* You don't have to use the web site, you can copy this file to "wp-config.php"

* and fill in the values.

*

* This file contains the following configurations:

*

* * Database settings

* * Secret keys

* * Database table prefix

* * ABSPATH

*

* @link https://wordpress.org/documentation/article/editing-wp-config-php/

*

* @package WordPress

*/

// ** Database settings - You can get this info from your web host ** //

/** The name of the database for WordPress */

define( 'DB_NAME', 'wordpress' );

/** Database username */

define( 'DB_USER', 'wordpress' );

/** Database password */

define( 'DB_PASSWORD', '123456' );

/** Database hostname */

define( 'DB_HOST', '192.168.233.40' );

/** 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}.

*

* You can change these at any point in time to invalidate all existing cookies.

* This will force all users to have to log in again.

*

* @since 2.6.0

*/

define( 'AUTH_KEY', '`f=+t0xD8)51}mwA/1@VuP?)RH<T%Nx2*M@HXbiDFNhl7B$@ 5&[Ajq%,(i+)-r6' );

define( 'SECURE_AUTH_KEY', 'tq%RuJaNe-6vx.D{as9-Nou(]% 2;P7{vjGw=:w^}D%.d$fE3vyBkU%stXLq7&gs' );

define( 'LOGGED_IN_KEY', '{_=Vs<(b*eyqcyZJj]*P:?djcfs!?v1Z=!Mb8m*;QTrcIb`Ir:!j>6p7q*%(Sd|9' );

define( 'NONCE_KEY', 'SVun3/0H:B}|ckW-K,SQLQ{YQqvpAFIF4>X=coE68A^1tc)wFJ3*1X Wz*ct}?>s' );

define( 'AUTH_SALT', 'h6Y^f*jC|Xy=nXJ,i02kf;T8+pC9:IT$6+|]+d?p_cHeDCja@VQ;hfw5&?sLb3xC' );

define( 'SECURE_AUTH_SALT', 'l)ZyeGGmM]Kfi-k([`5Z=lnoGK+S)RPw;{bFVF0><7=#D`h#Ep7J6h1bQ1Y&%64j' );

define( 'LOGGED_IN_SALT', '#)W_HK!n4jqQhL5J}uap=TN1sz%#pGlo)7M]o(E@m:n!m}^})5l.:&,4GU(*6MZ5' );

define( 'NONCE_SALT', '&#nNc5J&3iT)$3}z-EgrEapg,Te(.jf6P@U=/nx`(_cUK HqT5){N%&zjc|HTt}A' );

/**#@-*/

/**

* 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/documentation/article/debugging-in-wordpress/

*/

define( 'WP_DEBUG', false );

/* Add any custom values between this line and the "stop editing" line. */

/* 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';

如果上传图片失败:进入数据库容器:

select * from wp_options where option_name = 'upload_path';

option_value修改为:

"wp-content/uploads"

进入nginx容器:

修改权限

cd /usr/local/nginx/html

chmod 777 -R wordpress/

相关推荐
意疏9 小时前
【Linux 篇】Docker 的容器之海与镜像之岛:于 Linux 系统内探索容器化的奇妙航行
linux·docker
墨鸦_Cormorant9 小时前
使用docker快速部署Nginx、Redis、MySQL、Tomcat以及制作镜像
redis·nginx·docker
Code_Artist9 小时前
Docker镜像加速解决方案:配置HTTP代理,让Docker学会科学上网!
docker·云原生·容器
wanmei00210 小时前
Dockerfile复制目录进入镜像里
docker
inter_peng10 小时前
[Docker-显示所有容器IP] 显示docker-compose.yml中所有容器IP的方法
tcp/ip·docker·eureka
Linux运维日记11 小时前
k8s1.31版本最新版本集群使用容器镜像仓库Harbor
linux·docker·云原生·容器·kubernetes
一名路过的小码农12 小时前
ceph 18.2.4二次开发,docker镜像制作
ceph·docker·容器
xiangshangdemayi15 小时前
Windows环境GeoServer打包Docker极速入门
windows·docker·容器·geoserver·打包·数据挂载
程序员JerrySUN16 小时前
熟悉的 Docker,陌生的 Podman
linux·docker·容器·系统架构·podman
gobeyye16 小时前
Docker 用法详解
运维·docker·容器