官方提供的 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?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522168994567316782427435997%2522%252C%2522scm%2522%253A%252220140713.130102334.pc%255Fblog.%2522%257D&request_id=168994567316782427435997&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~blog~first_rank_ecpm_v1~times_rank-11-113655140-null-null.268%5Ev1%5Ekoosearch&utm_term=docker%E3%80%81wordpress%E3%80%81wordpress%E5%BB%BA%E7%AB%99%E3%80%81wordpress%E4%B8%BB%E9%A2%98%E3%80%81%E5%AE%B9%E5%99%A8%E9%95%9C%E5%83%8F%E3%80%81