Allow image posting with [Docker + WordPress + MySQL]

Yes, it's Haru Usagi. This time I got a WordPress project at work and I got stuck, so I will write a memo. I think that people who usually touch WordPress know this.

Premise

Where I got stuck this time

I will look at them in order. First directory structure

wp-template
├── docker-compose.yml
├── wp-config.php
└── wp-content
    ├── index.php
    ├── languages
    ├── plugins
    ├── themes
    ├── upgrade
    └── uploads

At worst, it works if you have the docker-compose.yml and wp-content folders. If you do docker-compose up, the contents of wp-content will be automatically configured as the initial ones. wp-config.php is a required file for WordPress. Will it be automatically generated during installation? It looks like, but in my case it didn't do it, so official wp-config.php I changed it for reference.

docker-compose.yml


version: "3"

services:
  db:
    image: mysql:5.7
    volumes:
      - db_data:/var/lib/mysql
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: somewordpress #Any
      MYSQL_DATABASE: wordpress #Any
      MYSQL_USER: wordpress #Any
      MYSQL_PASSWORD: wordpress #Any

  wordpress:
    depends_on:
      - db
    image: wordpress:latest
    ports:
      - "8000:80"
    restart: always
    environment:
      WORDPRESS_DB_HOST: db:3306 
      WORDPRESS_DB_USER: wordpress #Any
      WORDPRESS_DB_PASSWORD: wordpress #Any
    volumes:
      - ./wp-content:/var/www/html/wp-content
      - ./wp-config.php:/var/www/html/wp-config.php
volumes:
  db_data:

I thought it would be easier to manage DB-related items in .env. Now when you access [http: // localhost: 8000 /](http: // localhost: 8000 /), you will see the familiar WordPress screen.

What you can do normally up to this point.

After building a WordPress environment with docker, I can't upload images with WordPress

I can't upload an image to post in WordPress! When I looked it up, it seems that I have to add a sentence in wp-config.php.

wp-config.php


define('DB_NAME', 'wordpress'); #docker-compose.What was set in yml
define('DB_USER', 'wordpress'); #docker-compose.What was set in yml
define('DB_PASSWORD', 'wordpress'); #docker-compose.What was set in yml

define('DB_HOST', 'db'); #docker-compose.mysql container name defined in yml
define('WP_DEBUG', true);
define('DB_CHARSET', 'utf8');
define('DB_COLLATE', '');

define('AUTH_KEY',         'put your unique phrase here');
define('SECURE_AUTH_KEY',  'put your unique phrase here');
define('LOGGED_IN_KEY',    'put your unique phrase here');
define('NONCE_KEY',        'put your unique phrase here');
define('AUTH_SALT',        'put your unique phrase here');
define('SECURE_AUTH_SALT', 'put your unique phrase here');
define('LOGGED_IN_SALT',   'put your unique phrase here');
define('NONCE_SALT',       'put your unique phrase here');
define('UPLOADS', 'wp-content/uploads' ); #Add this

$table_prefix = 'wp_';

if ( !defined('ABSPATH') )
	define('ABSPATH', dirname(__FILE__) . '/');

require_once(ABSPATH . 'wp-settings.php');

This is the final form of code. The code that needed to be added below.

define('UPLOADS', 'wp-content/uploads' );

The container does not start up in the first place

sed: cannot rename ./xxxxx Device or resource busy is displayed and the container does not start. When I looked it up, the following article came out. This is the solution. The wisdom of our ancestors is wonderful. https://qiita.com/suzukihi724/items/e56fa9516639c6c90a33

in conclusion

I've been using WordPress since I was a student, but it wasn't difficult because it was a MAMP environment. This time I built the environment with docker, so I thought that I could understand the deep part of WordPress. I think that it was normal content for those who usually touch it. .. ..

Recommended Posts

Allow image posting with [Docker + WordPress + MySQL]
[Docker] Connection with MySQL
Rails + MySQL environment construction with Docker
Run lambda with custom docker image
Read dump file with Docker MySQL
WordPress with Docker Compose on CentOS 8
[Environment construction with Docker] Rails 6 & MySQL 8
mysql doesn't start up with docker.
Restart apache with docker php-apache image
Update MySQL from 5.7 to 8.0 with Docker
Create Rails 6 + MySQL environment with Docker compose
Deploy to heroku with Docker (Rails 6, MySQL)
Edit Mysql with commands in Docker environment
Create a MySQL environment with Docker from 0-> 1
Proxy server with squid using docker image
Laravel + MySQL + phpMyadmin environment construction with Docker
Build a Wordpress development environment with Docker
Image flew when updating Docker with WSL2
Implement simple CRUD with Go + MySQL + Docker
I tried to verify AdoptOpenJDK 11 (11.0.2) with Docker image
Building an environment for WordPress, MySQL and phpMyAdmin with Docker Compose on EC2
Build a WordPress development environment quickly with Docker
Rails6 [API mode] + MySQL5.7 environment construction with Docker
Image posting function
How to give your image to someone with docker
Update container image with KUSANAGI Runs on Docker
Launch Docker image with initial data injected with docker-compose
If you have trouble uploading Wordpress image files with KUSANAGI Runs on Docker
React + Django + Nginx + MySQL environment construction with Docker
Wordpress local environment construction & development procedure with Docker
Until you try running Apache Kafka with docker image
Build Rails (API) x MySQL x Nuxt.js environment with Docker
Build an Android image for Orange Pi 4 with Docker
Build WordPress environment with Docker (Local) and AWS (Production)
Launch MariaDB with Docker
Rails deploy with Docker
Run Pico with docker
Explode Docker with WSL2
Use Puphpeteer with Docker
Operate Emby with Docker
Delete unused docker image
Try WildFly with Docker
Use ngrok with Docker
Run Payara with Docker
Php settings with Docker
[Rails] Development with MySQL
Getting Started with Docker
Disposable PHP with Docker
Install Composer with Docker
Maybe it works! Create an image with Docker and share it!
Create a Docker image with the Oracle JDK installed (yum
Register your own Docker image with ECR using AWS CLI
How to build [TypeScript + Vue + Express + MySQL] environment with Docker ~ Express ~
Easy environment construction of MySQL and Redis with Docker and Alfred
Rails application development environment construction with Docker [Docker, Rails, Puma, Nginx, MySQL]
How to use mysql with M1 mac Docker preview version
docker-compose.yml when you want to keep mysql running with docker
I tried running WordPress with docker preview on M1 Mac.
Measures for permissions when building MySQL with Docker on WSL2