It's new, but I had to create the title environment on Mac, so I built it. I will write it as a memorandum.
Machine macOS Catalina 10.15.7
The structure of files and directories is roughly as follows.
Arbitrary directory
│
├── docker-compose.yml
│
├── html/
│ └── index.html
│
├── mysql/
│ ├── Dockerfile
│ ├── data/
│ ├── init/
│ │ └── init.sql
│ └── my.cnf
│
├── php-apahce/
├── Dockerfile
└── php.ini
docker-compose.yml and Dockerfile are important parts of this build.
file name | Explanation |
---|---|
docker-compose.yml | It defines the configuration of the container. You can also define inter-container communication. |
Dockerfile | Definition for docker image. |
As far as I research and recognize it, it is as follows
name | meaning |
---|---|
build | Specify where the Dockerfile is |
volumus | Locally mounted location:Where to mount in the container |
ports | Port forwarding |
container_name | Container name docker container ls name |
environment | Environment variable? I somehow understand what the variable name means |
As far as I research and recognize it, it is as follows
name | meaning |
---|---|
FROM | What in what version |
EXPOSE | Which port to use |
ADD | Add local file to file in container |
CMD | The one that runs when the container starts |
COPY | Copy local files to a directory inside the container |
RUN | The one called when creating an image. So write the package installation etc. |
The main story starts here.
First, check the status of the container
ksk@ksknoMacBook-Pro work % docker container ls -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
There is nothing
./docker-compose.yml
docker-compose.yml is a file that defines the configuration of the container.
docker-compose.yml
version: '3'
services:
php-apache:
build: ./php-apache/
volumes:
- ./html:/var/www/html
ports:
- 8080:80
container_name: web
mysql:
build: ./mysql/
volumes:
- "./mysql/data:/var/lib/mysql"
- "./mysql/init:/docker-entrypoint-initdb.d"
environment:
- MYSQL_ROOT_PASSWORD=docker
- MYSQL_DATABASE=mydb
- MYSQL_USER=appuser
- MYSQL_PASSWORD=appuser1
container_name: db
ports:
- 3306:3306
Attributes written in yml? So, write what you understand in the table below (Please let me know if you have a kind person who can point out what is wrong)
./html/index.php Used to check the operation. For the time being, it would be nice if the DB data could be displayed. By the definition of volumes, html is the document root.
index.php
<?php
try {
$db = sprintf('mysql:host=%s:3306;dbname=%s', 'mysql', 'mydb');
$dbh = new PDO($db, 'appuser', 'appuser1');
foreach($dbh->query('select * from users') as $row) {
echo $row['name'] . '<br>';
}
$dbh = null;
} catch (PDOException $e) {
echo $e;
die();
}
$dbh = null;
The host specification says "'mysql'", but I understand this as the service name in docker-compose.yml.
docker-compose.yml
mysql:← This! !! !!
build: ./mysql/
volumes:
./mysql/Dockerfile
Dockerfile on mysql side A Dockerfile is a file that writes the definition of a Docker image.
FROM mysql:8.0
EXPOSE 3306
ADD ./my.cnf /etc/mysql/conf.d/my.cnf
CMD ["mysqld"]
./mysql/init/init.sql Make a description that packs appropriate tables and data
use mydb;
CREATE TABLE users (
id int(10) unsigned not null auto_increment,
name varchar(255) not null,
primary key (id)
);
insert into users(name) values('hogeo');
insert into users(name) values('fugao');
./mysql/my.cnf
my.cnf
[mysqld]
character-set-server=utf8
default_authentication_plugin=mysql_native_password
[mysql]
default-character-set=utf8
[client]
default-character-set=utf8
Since the default authentication plugin for mysql has changed, I am changing the default authentication plugin. I am creating a user (appuser) with the environment variable of mysql, but it is set by the authentication plugin called mysql_native_password. If left at the default, php will fail to access mysql.
./php-apache/Dockerfile
I looked at various sites and used them as a reference.
FROM php:7.4-apache
COPY ./php.ini /usr/local/etc/php/
RUN apt-get update
RUN apt-get install -y zip unzip vim libpng-dev libpq-dev
RUN docker-php-ext-install pdo_mysql
./php-apache/php.ini
I looked at various sites and used them as a reference.
[Date]
date.timezone = "Asia/Tokyo"
[mbstring]
mbstring.internal_encoding = "UTF-8"
mbstring.language = "Japanese"
docker-compose build
After creating the above, execute the following command in the same hierarchy as docker-compose.yml
ksk@ksknoMacBook-Pro work % docker-compose build
~~~ Omitted ~~~
Successfully built e5b1c15e825e
Successfully tagged work_php-apache:latest
Building mysql
Step 1/4 : FROM mysql:8.0
---> d4c3cafb11d5
Step 2/4 : EXPOSE 3306
---> Using cache
---> e11d55e213ab
Step 3/4 : ADD ./my.cnf /etc/mysql/conf.d/my.cnf
---> Using cache
---> 6dd70a6b7ecd
Step 4/4 : CMD ["mysqld"]
---> Using cache
---> c22190479ddf
Successfully built c22190479ddf
Successfully tagged work_mysql:latest
ksk@ksknoMacBook-Pro work % docker-compose up -d
Creating web ... done
Creating db ... done
Checking the status of the container
ksk@ksknoMacBook-Pro work % docker container ls -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b416b97d3f7b work_mysql "docker-entrypoint.s…" 2 seconds ago Up 2 seconds 0.0.0.0:3306->3306/tcp, 33060/tcp db
9985abb2eb90 work_php-apache "docker-php-entrypoi…" 2 seconds ago Up 2 seconds 0.0.0.0:8080->80/tcp web
I was able to confirm the container
http://localhost:8080 Access here
that's all!
Recommended Posts