Build apache7.4 + mysql8 environment with Docker (with initial data) (your own memo)

It's new, but I had to create the title environment on Mac, so I built it. I will write it as a memorandum.

environment

Machine macOS Catalina 10.15.7

Constitution

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

An important part of this construction

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.

Elements of docker-compose.yml

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

Dockerfile elements

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.

Build

The main story starts here.

Verification

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

About default_authentication_plugin = mysql_native_password

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

Container creation & start

ksk@ksknoMacBook-Pro work % docker-compose up -d
Creating web ... done
Creating db  ... done

Verification

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

Screen check

http://localhost:8080 Access here

qiita_20200117_01.png

that's all!

Recommended Posts

Build apache7.4 + mysql8 environment with Docker (with initial data) (your own memo)
MySQL 5.7 (Docker) environment construction memo
Build docker environment with WSL
Build Rails (API) x MySQL x Nuxt.js environment with Docker
How to build [TypeScript + Vue + Express + MySQL] environment with Docker ~ Express ~
Rails + MySQL environment construction with Docker
Build Couchbase local environment with Docker
Build a Node.js environment with Docker
Build PlantUML environment with VSCode + Docker
Build environment with vue.js + rails + docker
Build Rails environment with Docker Compose
[Environment construction with Docker] Rails 6 & MySQL 8
Build docker + laravel environment with laradock
How to build [TypeScript + Vue + Express + MySQL] environment with Docker ~ MySQL edition ~
How to build Rails + Vue + MySQL environment with Docker [2020/09 latest version]
Build a development environment for Django + MySQL + nginx with Docker Compose
How to build [TypeScript + Vue + Express + MySQL] environment with Docker ~ Sequelize ~
Build a PureScript development environment with Docker
Create Rails 6 + MySQL environment with Docker compose
Edit Mysql with commands in Docker environment
Create a MySQL environment with Docker from 0-> 1
Laravel + MySQL + phpMyadmin environment construction with Docker
Build a Wordpress development environment with Docker
[Docker] Build Jupyter Lab execution environment with Docker
Build an environment with Docker on AWS
Build TensorFlow operation check environment with Docker
How to build Rails 6 environment with Docker
When I tried to build an environment of PHP7.4 + Apache + MySQL with Docker, I got stuck [Windows & Mac]
[Memo] Create a CentOS 8 environment easily with Docker
Start SQL Server with Docker & register initial data
Build a Laravel / Docker environment with VSCode devcontainer
Environment construction command memo with Docker on AWS
Build a WordPress development environment quickly with Docker
Rails6 [API mode] + MySQL5.7 environment construction with Docker
Launch Docker image with initial data injected with docker-compose
React + Django + Nginx + MySQL environment construction with Docker
Build mecab (NEologd dictionary) environment with Docker (ubuntu)
[Rails] How to build an environment with Docker
[First team development ②] Build an environment with Docker
Build Apache and Tomcat environment with Docker. By the way, Maven & Java cooperation
How to build an environment of [TypeScript + Vue + Express + MySQL] with Docker ~ Vue edition ~
[Road _node.js_1-1] Road to build Node.js Express MySQL environment using Docker
How to build docker environment with Gradle for intelliJ
Build an environment of Ruby2.7.x + Rails6.0.x + MySQL8.0.x with Docker
Easily build a Vue.js environment with Docker + Vue CLI
[Note] Build a Python3 environment with Docker in EC2
[Docker] Connection with MySQL
Build Java development environment with WSL2 Docker VS Code
Build WordPress environment with Docker (Local) and AWS (Production)
Register your own Docker image with ECR using AWS CLI
Easy environment construction of MySQL and Redis with Docker and Alfred
Rails application development environment construction with Docker [Docker, Rails, Puma, Nginx, MySQL]
Build a CentOS 8 virtual environment on your Mac with VirtualBox
How to build Rails, Postgres, ElasticSearch development environment with Docker
Build a Node-RED environment with Docker to move and understand
Java: Start WAS with Docker and deploy your own application
[Vagrant] Prepare LAMP development environment with Vagrant (centos + apache + MySQL + PHP)
Pytorch execution environment with Docker
Redmine (Docker) environment construction memo
[Docker] Rails 5.2 environment construction with docker
React environment construction with Docker