Set up a Wordpress Docker environment without using the Worpdress image

I decided to create a Wordpress environment with Docker, but it was quite difficult to set it up without using an image, so I will leave it as a memorandum. It's just my record, so it may be easier or wrong.

environment

Ultimate purpose

Conditions you want to achieve

--nginx + php + mysql environment --You can access the wordpress administration screen by accessing http: // localhost: 8080 --Persistent database (delete container does not erase database contents)

Version of each container

file organization

├── docker-compose.yml
├── nginx
│   └── default.conf
├── php
│   └── Dockerfile
└── www
    └── html
        └── [wordpress file]

procedure

  1. Build nginx + php environment
  2. Add mysql
  3. Add wordpress, set

1. Build nginx + php environment

Create a file to create each container. The first file to build is:

├── docker-compose.yml
├── nginx
│   └── default.conf
├── php
│   └── Dockerfile
└── www
    └── html
        └── index.php

docker-compose.yml

docker-compose.yml


version: '3'
services:
 nginx:
  image: nginx:1.12.2
  ports:
   - 8080:80
  volumes:
   - ./www/html:/var/www/html
   - ./nginx/default.conf:/etc/nginx/conf.d/default.conf
 
 php:
  build: ./php
  volumes:
   - ./www/html:/var/www/html

nginx/default.conf Add a description so that index.php is also displayed when accessing http: // localhost: 8080.

nginx/default.conf


server {
    listen       80;
    server_name  localhost;
    root   /var/www/html;
    #Index here.Add php
    index  index.html index.htm index.php;

    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

    #Uncomment to make php work
    location ~ \.php$ {
        root           /var/www/html;
        fastcgi_pass   php:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include        fastcgi_params;
        fastcgi_intercept_errors on;
    }

}

php/Dockerfile You can specify it directly in docker-compose.yml with image, but I am creating a Dockerfile to add the description when adding mysql later.

php/Dockerfile


FROM php:7.2-fpm

www/html/index.php

www/html/index.php


<?php
  phpinfo();

Container startup

Use the cd command to change to the working directory and use the following command

// docker-compose.Install the container described in yml
$ docker-compose up -d

//Check if the container is created / started(-By adding a, it is possible to display containers that have not started.)
$ docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                  NAMES
0aed3ada6a01        docker_wp_php       "docker-php-entrypoi…"   19 seconds ago      Up 17 seconds       9000/tcp               task4_wp
b698e1032798        nginx:1.12.2        "nginx -g 'daemon of…"   19 seconds ago      Up 17 seconds       0.0.0.0:8080->80/tcp   docker_wp_nginx_1

If you access http: // localhost: 8080 and phpinfo is displayed, it is successful.

2. Add mysql

There are no additions or changes to the file structure, but there are additions to php / Dockerfile and docker-compose.yml when adding mysql.

php/Dockerfile

php/Dockerfile



FROM php:7.2-fpm

//Add the following 2 lines
RUN apt-get update
RUN docker-php-ext-install pdo_mysql mysqli

docker-compose.yml

docker-compose.yml


version: '3'
services:
 nginx:
  image: nginx:1.12.2
  ports:
   - 8080:80
  volumes:
   - ./www/html:/var/www/html
   - ./nginx/default.conf:/etc/nginx/conf.d/default.conf
 
 php:
  build: ./php
  container_name: "task4_wp"
  volumes:
   - ./www/html:/var/www/html
 
 //Add the description of mysql below
 db:
  image: mysql:5.6
  ports:
   - 3306:3306
  environment:
   MYSQL_DATABASE: wordpress
   MYSQL_ROOT_PASSWORD: root
   MYSQL_USER: user
   MYSQL_PASSWORD: password
  volumes:
   - wp_db:/var/lib/mysql

volumes:
 wp_db:

MYSQL_USER and MYSQL_PASSWORD set the user to use when logging in to mysql later. MYSQL_DATABASE is the database used when wordpress is installed. Also, the contents of the database are not mounted like www / html, but are placed outside the file as volume.

Restart the container

//Stop and delete the container you just created
$ docker-compose down

// docker-compose.Start the container described in yml
$ docker-compose up -d

//Check if the container is created / started
$ docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                    NAMES
9e9d13e9441f        nginx:1.12.2        "nginx -g 'daemon of…"   3 minutes ago       Up 3 minutes        0.0.0.0:8080->80/tcp     docker_wp_nginx_1
19451d8dfd82        docker_wp_php       "docker-php-entrypoi…"   3 minutes ago       Up 3 minutes        9000/tcp                 docker_wp_php_1
7f37f92bed6a        mysql:5.6           "docker-entrypoint.s…"   3 minutes ago       Up 3 minutes        0.0.0.0:3306->3306/tcp   docker_wp_db_1

If you access http: // localhost: 8080, you should see phpinfo as before. It doesn't change on the surface, but if STATUS is UP, the startup is successful.

3. Add wordpress, set

Finally, put wordpress. You can download wordpress from the following page. https://ja.wordpress.org/download/ Delete /www/html/index.php, then unzip and copy the contents ofwordpress /and copy it towww / html /.

file organization

├── docker-compose.yml
├── nginx
│   └── default.conf
├── php
│   └── Dockerfile
└── www
    └── html
        └── [wordpress file]

When you access http: // localhost: 8080, you will see the screen below. スクリーンショット 2020-09-29 20.53.06.png Database registration. https___qiita-image-store.s3.ap-northeast-1.amazonaws.com_0_481714_d321077c-de34-778b-145c-429722abf7c6.png Installation. スクリーンショット 2020-09-29 21.32.26.png Enter site information and administrator user information. スクリーンショット 2020-09-28 14.26.59.png I registered a login user, so I logged in. スクリーンショット 2020-09-29 21.42.47.png

You have reached the wordpress management screen. スクリーンショット 2020-09-29 21.53.48.png

Where I got caught

Understanding volumes

The code I wrote this time also has descriptions both inside and outside the container. I understood that the internal volumes are for sharing files locally with the container, but the external ones have a slightly different description method, and isn't the folder location specified in the first place? Isn't it because of the mount ...? I was wondering if I shouldn't use it. Reflection. This article was easy to understand. Docker seriously investigated Volume

Frequently used commands

$ docker-compose up -d
//Docker directly under the current folder-compose.Run yml in the background

$ docker-compose stop
// docker-compose.Stop the container written in yml

$ docker-compose start
//Start the container

$ docker-compose down
//Stop container → delete
$ docker-compose down --rmi all -v
// docker-compose.All the containers, images, and created volumes described in yml disappear.

$ docker exec -it [Container ID] /bin/bash
//Log in to the container (container ID is`docker ps`Can be confirmed at)

$ docker ps
//List running containers
$ docker ps -a
//Show all including unstarted containers

$ docker images
//List images

$ docker rm [Container ID]
//Delete the container. I get an error when trying to delete a running container
$ docker rm [Container ID] -f
//Delete the container. Forcibly delete even if it is moving

$ docker rmi [Image ID]
//Delete the image. I get an error when there is a container
$ docker rmi [Image ID] -f
//Delete the image. Forcibly delete even if there is a container

Recommended Posts

Set up a Wordpress Docker environment without using the Worpdress image
Build a browser test environment using Capybara in the Docker development environment
Create a web environment quickly using Docker
Build a Wordpress development environment with Docker
[Swift] How to set an image in the background without using UIImageView.
Create a Privoxy + Tor environment instantly using Docker
Build a WordPress development environment quickly with Docker
Push the image to docker hub using Jib
Up to the point of launching a Docker container built using RedHat Quarkus
Find the remainder divided by 3 without using a number
A command that definitely cleans the local docker environment
[Docker] Build a Wordpress environment locally (Win, Mac) quickly
Try to build a Java development environment using Docker
[2021] Build a Docker + Vagrant environment for using React / TypeScript
The story of migrating a stray batch without an owner from EC2 to a Docker environment
Create a Docker image with the Oracle JDK installed (yum
I tried to build the environment little by little using docker
Set up GitLab with docker
What Docker beginners did before setting up the nginx learning environment
Easily set up a Jenkins server on AWS using Bitnami's AMI
Install Docker on Ubuntu and set up remote connection using tls
How to set an image in the drawable Left / Right of a button using an icon font (Iconics)
Create a Vue3 environment with Docker!
Build a Node.js environment with Docker
Create a lightweight STNS Docker image
Write a null case using the Optional type without using an if statement
How to build a Ruby on Rails environment using Docker (for Docker beginners)
Minimal steps to set up a Ruby environment with rbenv on Ubuntu 20.04
Set up a Java GUI in a separate thread to keep the main