官方提供的 wordpress 和 mysql 镜像,都是独立的,需要分别启动后 link 起来使用,可以拉取之后迅速完成部署,缺点是不利于容器迁移。
将 wordpress 网站打包到一个 docker 镜像中,可以方便地实现服务器迁移。
首先在当前目录下创建 Dockerfile 文件夹:
mkdir docker
cd docker
touch Dockerfile
vi Dockerfile
然后编辑 Dockerfile 文件
FROM mysql:5.7
ENV MYSQL_ALLOW_EMPTY_PASSWORD yes
WORKDIR /www
ADD ./mysql_init.sql /www/mysql_init.sql
RUN service mysql start \
&& sleep 5 \
&& mysql < /www/mysql_init.sql \
&& service mysql stop
RUN apt-get -q update \
&& apt-get -q -y install php7.0 php7.0-fpm php7.0-mysql \
&& apt-get -q -y install nginx \
&& apt-get -q -y install wget \
&& wget -q http://cn.wordpress.org/wordpress-4.7.4-zh_CN.tar.gz \
&& tar -xf wordpress-4.7.4-zh_CN.tar.gz \
&& chown -R www-data wordpress \
&& apt-get clean \
&& apt-get purge -y –auto-remove wget \
&& echo "" >> ~/.bashrc \
&& echo "service mysql start" >> ~/.bashrc \
&& echo "service php7.0-fpm start" >> ~/.bashrc \
&& echo "service nginx start" >> ~/.bashrc
ADD ./default /etc/nginx/sites-available/default
ADD ./db_init.sql /www/
WORKDIR /root
EXPOSE 80
ENTRYPOINT ["/bin/bash"]
添加 nginx 配置文件:default
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name _;
root /www/wordpress;
index index.php index.html index.htm;
location / {
try_files $uri $uri/ =404;
location ~ \.php$ {
include snippets/fastcgi-php.conf;
# With php7.0-fpm:
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
mysql 初始化文件: db_init.sql
CREATE DATABASE wordpress;
GRANT ALL PRIVILEGES ON wordpress.* to wordpress@localhost IDENTIFIED BY '12345678';
FLUSH PRIVILEGES;
编译 docker 镜像
sudo docker build -t wp:v1 .
启动 docker 容器
sudo docker run -dit –name demo -p 8000:80 wp:v1
sudo docker attach demo
连接 docker 容器后,初始化 mysql
mysql < /www/db_init.sql
原文链接:https://blog.csdn.net/weixin_39622398/article/details/113655140