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.
--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)
├── docker-compose.yml
├── nginx
│ └── default.conf
├── php
│ └── Dockerfile
└── www
└── html
└── [wordpress file]
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();
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.
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.
//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.
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 /
.
├── docker-compose.yml
├── nginx
│ └── default.conf
├── php
│ └── Dockerfile
└── www
└── html
└── [wordpress file]
When you access http: // localhost: 8080, you will see the screen below. Database registration. Installation. Enter site information and administrator user information. I registered a login user, so I logged in.
You have reached the wordpress management screen.
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
$ 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