Dockerize wordpress

2019-04-10 17:32发布

问题:

Trying to dockerise wordpress I figure out this scenenario:

2 data volume containers, one for the database (bbdd) and another for wordpress files (wordpress):

sudo docker create -v /var/lib/mysql --name bbdd ubuntu:trusty /bin/true
sudo docker create -v /var/www/html --name wordpress ubuntu:trusty /bin/true

Then I need a container for mysql so I use the official mysql image from docker hub and also the volume /var/lib/mysql from the first data container:

docker run --volumes-from bbdd --name mysql -e MYSQL_ROOT_PASSWORD="xxxx" -d mysql:5.6

Then I need a container for apache/php so I use official wordpress image from docker hub and also the volume /var/lib/mysql from the first data container:

docker run --volumes-from wordpress --name apache --link mysql:mysql -d -p 8080:80 wordpress:4.1.2-apache

What I understand from docker docs is that if I don't remove the data containers, I'll have persistance.
However if I stop and delete running containers (apache and mysql) and recreate them again with last commands, data get lost:

docker run --volumes-from bbdd --name mysql -e MYSQL_ROOT_PASSWORD="xxxx" -d mysql:5.6
docker run --volumes-from wordpress --name apache --link mysql:mysql -d -p 8080:80 wordpress:4.1.2-apache

However if I create the containers without data containers, it seems to work as I expected:

docker run -v /home/juanda/project/mysql:/var/lib/mysql --name mysql -e MYSQL_ROOT_PASSWORD="juanda" -d mysql:5.6
docker run -v /home/juanda/project/wordpress:/var/www/html --name apache --link mysql:mysql -d -p 8080:80 wordpress:4.1.2-apache

回答1:

You need to run the data container for once to make it persistent:

sudo docker run -v /var/lib/mysql --name bbdd ubuntu:trusty /bin/true
sudo docker run -v /var/www/html --name wordpress ubuntu:trusty /bin/true

This is an old bug of Docker described here. You may be affected if your Docker version is old.



回答2:

In a very simplified test case this appears to work as advertised and documented in Creating and mounting a Data Volume Container:

prologic@daisy
Thu Apr 30 08:18:45 
~
$ docker create -v /test --name data busybox /vin/true
Unable to find image 'busybox:latest' locally
latest: Pulling from busybox
cf2616975b4a: Pull complete 
6ce2e90b0bc7: Pull complete 
8c2e06607696: Already exists 
busybox:latest: The image you are pulling has been verified. Important: image verification is a tech preview feature and should not be relied on to provide security.
Digest: sha256:38a203e1986cf79639cfb9b2e1d6e773de84002feea2d4eb006b52004ee8502d
Status: Downloaded newer image for busybox:latest
6f5fc1d2e33654867cff8ffdb60c5765ced4b7128441ae2c6be24b68fb6454ef

prologic@daisy
Thu Apr 30 08:20:53 
~
$ docker run -i -t --rm --volumes-from data crux /bin/bash
bash-4.3# cd /test
bash-4.3# ls
bash-4.3# touch foo
bash-4.3# echo "Hello World" >> foo
bash-4.3# cat foo
Hello World
bash-4.3# exit

prologic@daisy
Thu Apr 30 08:21:20 
~
$ docker run -i -t --rm --volumes-from data crux /bin/bash
bash-4.3# cd /test
bash-4.3# ls
foo
bash-4.3# cat foo
Hello World
bash-4.3# exit

Note that I deleted the attached container to make sure the persistent data volume container's data was left in tact.

The data volume container and it's data would only disappear if you ran the following:

docker rm -v data

Note: the -v option to actually remove volumes.

See (specifically the -v/--volumes option):

$ docker rm -h

Usage: docker rm [OPTIONS] CONTAINER [CONTAINER...]

Remove one or more containers

-f, --force=false Force the removal of a running container (uses SIGKILL) --help=false Print usage -l, --link=false Remove the specified link -v, --volumes=false Remove the volumes associated with the container

For reference I am running:

prologic@daisy
Thu Apr 30 08:24:51 
~
$ docker version
Client version: 1.6.0
Client API version: 1.18
Go version (client): go1.3.3
Git commit (client): 47496519da
OS/Arch (client): linux/amd64
Server version: 1.6.0
Server API version: 1.18
Go version (server): go1.3.3
Git commit (server): 47496519da
OS/Arch (server): linux/amd64

Update: For a quick example (which you can use in production) of a Dockerized Wordpress setup with full hosting support see: https://gist.github.com/prologic/b5525a50bb4d867d84a2



回答3:

You can simply use docker-compose file as:

version: '3.3'

services:

   # https://hub.docker.com/_/nginx
   # Doesn't play well with wordpress fpm based images
#   nginx:
#       image: nginx:latest
#       container_name: "${PROJECT_NAME}_nginx"
#       ports:
#         - "${NGINX_HTTP_PORT}:80"
#       working_dir: /var/www/html
#       volumes:
#         - ./docker/etc/nginx:/etc/nginx/conf.d
#         - ./logs/nginx:/var/log/nginx
#         - ./app:/var/www/html
#       environment:
#          - NGINX_HOST=${NGINX_HOST}
#       #command: /bin/sh -c "envsubst '$$NGINX_HOST' < /etc/nginx/conf.d/wordpress.conf > /etc/nginx/conf.d/wordpress.conf && nginx -g 'daemon off;'"
#       links:
#         - wordpress
#       restart: always

   # https://hub.docker.com/r/jwilder/nginx-proxy
   nginx-proxy:
     image: jwilder/nginx-proxy
     container_name: "${PROJECT_NAME}_nginx-proxy"
     ports:
       - "80:80"
     volumes:
       - /var/run/docker.sock:/tmp/docker.sock:ro

   # https://hub.docker.com/_/mysql
   mysql:
     image: mysql:${MYSQL_TAG}

     # For MySQL 8.0
     #image: mysql:8
     #command: '--default-authentication-plugin=mysql_native_password'

     container_name: "${PROJECT_NAME}_mysql"
     ports:
           - "${MYSQL_PORT}:3306"
     volumes:
       - ./data/mysql:/var/lib/mysql
     environment:
       MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD}
       MYSQL_DATABASE: ${MYSQL_DATABASE}
       MYSQL_USER: ${MYSQL_USER}
       MYSQL_PASSWORD: ${MYSQL_PASSWORD}
     restart: always

   # https://hub.docker.com/_/wordpress
   wordpress:
     # -fpm or apache, unfortunately fpm doesn't work properly with nginx-proxy
     image: wordpress:${WP_VERSION}-php${PHP_VERSION}-apache
     container_name: "${PROJECT_NAME}_wordpress"
     environment:
       - VIRTUAL_HOST=${WP_HTTP_HOST}
       - WORDPRESS_DB_HOST=mysql:3306
       - WORDPRESS_DB_NAME=${MYSQL_DATABASE}
       - WORDPRESS_DB_USER=${MYSQL_USER}
       - WORDPRESS_DB_PASSWORD=${MYSQL_ROOT_PASSWORD}
     working_dir: /var/www/html
     volumes:
       - ./app:/var/www/html
       #- ./app/wp-content:/var/www/html/wp-content
       - ./docker/etc/php-fpm/custom.ini:/usr/local/etc/php/conf.d/999-custom.ini
     #depends_on:
     #  - mysql
     ports:
       - "${WP_HTTP_PORT}:80"
     expose:
       - ${WP_HTTP_PORT}
     links:
       - mysql
     restart: always

   # https://hub.docker.com/r/phpmyadmin/phpmyadmin
   phpmyadmin:
      image: phpmyadmin/phpmyadmin
      container_name: "${PROJECT_NAME}_phpmyadmin"
      ports:
          - "${PMA_PORT}:80"
      expose:
          - ${PMA_PORT}
      environment:
          VIRTUAL_HOST: ${PMA_HTTP_HOST}
          PMA_HOST: mysql
      depends_on:
        - mysql

   # @todo services
   # jwilder/nginx-proxy
   # https / letsencrypt
   # composer
   # mailhog
   # redis
   # phpredisadmin
   # blackfire

networks:
    default:
      external:
        name: nginx-proxy

SOURCE: https://github.com/MagePsycho/wordpress-dockerized