I tried to make an introduction to PHP + MySQL with Docker

Since I built the server CI of work related code with docker I made my own introduction

Brief explanation of jargon

container

-Environment isolated from the host machine -Created from a container image -Container details and dependencies are described in docker-compose.yml ・ Container construction + startup with docker-compose up ・ Container list display with docker ps ・ Start/end/restart the container with docker start/stop/restart

Container image

・ Source for creating a container ・ Describe the contents in the Dockerfile ・ Build with docker build ・ List display with docker images -Deleted with docker rmi ・ There is a nice one to search on docker hub

Build a simple mackerel to display a string

It's easy to build without specifying a version Let's start the container from the PHP + Apache container image

docker-compose.yml

services:
  sample_app:
    image: php:5.5-apache
    volumes:
      - ./html:/var/www/html
    ports:
      - 8000:80
    container_name: sample_app

html/index.php

<?php
echo "HelloWolrd!";

php: 5.5-apache is a PHP5.5 + Apache container image provided by docker official. It's easy because you can build a server just by building a container using this container image. You can search for image from docker hub. You can also specify the version from tags

Then do docker-compose up -d Pull container image from docker hub → Build container → Start container Will do it automatically The d option is a background boot option

http://localhost:8000 When you access, "Hello World!" Is displayed.

You can see that the sample_app container is running by typing docker ps

Congratulations!

$ docker ps
CONTAINER ID   IMAGE               COMMAND                  CREATED          STATUS          PORTS                  NAMES
211a18290243   php:5.5-apache      "apache2-foreground"     54 seconds ago   Up 54 seconds   0.0.0.0:8000->80/tcp   sample_app

Type docker images to see a list of php: 5.5-apache container images

$ docker images
REPOSITORY                   TAG          IMAGE ID       CREATED       SIZE
php                          5.5-apache   ea0a3d41ce6c   4 years ago   390MB

However, it is not interesting to build such a system, isn't it? If it is a WEB server, I would like to prepare a DB as well. Let's start the DB container as well

Build a server to get data in DB

First, add a mysql container Input initial data, Go from index.php to access

docker-compose.yml

services:
  sample_app:
    image: php:5.5-apache
    volumes:
      - ./html:/var/www/html
    ports:
      - 8000:80
    container_name: sample_app
  sample_db:
    image: mysql:5.6
    environment:
      - MYSQL_ROOT_PASSWORD=root
      - MYSQL_DATABASE=test
      - MYSQL_USER=test
      - MYSQL_PASSWORD=test
    volumes:
      - ./sample_db/initdb:/docker-entrypoint-initdb.d #If you place it here, it will be executed when the container is built.
    container_name: sample_db

sample_db/initdb/init.sql

--File executed when creating DB
create table test.test (text text);
insert into test.test values ('first'),('second'),('third');

html/index.php

<?php
$dbh = new PDO('mysql:host=sample_db;dbname=test', 'root', 'root');
foreach ($dbh->query('select * from test') as $row) {
  echo $row['text'] . '<br/>';
}

This completes the setting change. Rebuild the container image container

#Stop / delete container
docker-compose down --remove-orphans
#Build and start a container
docker-compose up -d

It pulls the mysql5.6 container image and builds and starts the db container. Initial data is also input at the time of construction

Let's access http: // localhost: 8000

Fatal error: Uncaught exception 'PDOException' with message 'could not find driver' in /var/www/html/index.php:2 Stack trace: #0 /var/www/html/index.php(2): PDO->__construct('mysql:host=samp...', 'root', 'root') #1 {main} thrown in /var/www/html/index.php on line 2

I get a PDO error Actually you need pdo_mysql to access mysql from php

But since php is provided by php: 5.5-apache container image I can't touch it with docker-compose.yml

There are two ways to put pdo_mysql in the sample_app container

The first is to create a container from a container image that includes pdo_mysql The second is to install pdo_mysql on the existing container image

The second one is more flexible, so I think the second one is often taken care of. This time I will describe the second method

sample_app_image/Dockerfile

FROM php:5.5-apache

RUN apt-get -y update && apt-get -y upgrade \
  && docker-php-ext-install pdo_mysql

Describe the installation procedure in the Dockerfile,

docker-compose.yml

services:
  sample_app:
    build:
      context: ./sample_app_image
      dockerfile: Dockerfile
    image: sample_app
    volumes:
      - ./html:/var/www/html
    ports:
      - 8000:80
    container_name: sample_app
  sample_db:
    image: mysql:5.6
    environment:
      - MYSQL_ROOT_PASSWORD=root
      - MYSQL_DATABASE=test
      - MYSQL_USER=test
      - MYSQL_PASSWORD=test
    volumes:
      - ./sample_db/initdb:/docker-entrypoint-initdb.d #If you place it here, it will be executed when the container is built.
    container_name: sample_db

Describe the build destination in docker-compose.yml Then restart the container

#Stop / delete container
docker-compose down --remove-orphans
#Build and start a container
docker-compose up -d

If the build is successful, "First Second Thrid" will be displayed on the following page. You can see that you can get the data from the DB

http://localhost:8000

Thank you for your support

Q&A Q. It's troublesome to make apt-get of Dockerfile. It takes time to delete and rebuild the environment A. It is efficient to log in to the container, actually hit it, and if it succeeds, write it in the Dockerfile.

$ docker exec -it sample_app bash

Q. I want to see the log in the container A. It's easy to see if you use logs

$ docker logs sample_app

Q. PHP5.5 or dung fish www security support has ended www A. I'm using it at work

Recommended Posts

I tried to make an introduction to PHP + MySQL with Docker
I tried to make an automatic backup with pleasanter + PostgreSQL + SSL + docker
When I tried to build an environment of PHP7.4 + Apache + MySQL with Docker, I got stuck [Windows & Mac]
I tried to make an Android application with MVC now (Java)
I tried to build an API server with Go (Echo) x MySQL x Docker x Clean Architecture
I tried to verify AdoptOpenJDK 11 (11.0.2) with Docker image
I tried to make Basic authentication with Java
01. I tried to build an environment with SpringBoot + IntelliJ + MySQL (MyBatis) (Windows10)
I tried BIND with Docker
I started MySQL 5.7 with docker-compose and tried to connect
Rails6 I tried to introduce Docker to an existing application
I tried to build an environment using Docker (beginner)
I tried to make a machine learning application with Dash (+ Docker) part3 ~ Practice ~
I tried to make an application in 3 months from inexperienced
I tried to make Venn diagram an easy-to-understand GIF animation
I tried to interact with Java
I tried to create a padrino development environment with Docker
Update MySQL from 5.7 to 8.0 with Docker
I tried to build the environment of PlantUML Server with Docker
I tried connecting to MySQL using JDBC Template with Spring MVC
I tried to build an http2 development environment with Eclipse + Tomcat
I tried to make it an arbitrary URL using routing nesting
I tried to make a group function (bulletin board) with Rails
I want to make an ios.android app
I tried to get started with WebAssembly
I tried using Scalar DL with Docker
Deploy to heroku with Docker (Rails 6, MySQL)
What is Docker? I tried to summarize
I tried to implement ModanShogi with Kinx
I tried to make a machine learning application with Dash (+ Docker) part2 ~ Basic way of writing Dash ~
[iOS] I tried to make a processing application like Instagram with Swift
I tried to make a Web API that connects to DB with Quarkus
I tried to build a Firebase application development environment with Docker in 2020
After all I wanted to preview the contents of mysql with Docker ...
I tried to manage struts configuration with Coggle
I tried to manage login information with JMX
I tried to make a machine learning application with Dash (+ Docker) part1 ~ Environment construction and operation check ~
I tried installing docker on an EC2 instance
I tried to develop an application in 2 languages
[Rails] How to build an environment with Docker
I tried to break a block with java (1)
I tried to create a portfolio with AWS, Docker, CircleCI, Laravel [with reference link]
[First environment construction] I tried to create a Rails 6 + MySQL 8.0 + Docker environment on Windows 10.
I tried to make a simple game with Javafx ① "Let's find happiness game" (unfinished)
[Java] I tried to connect using a connection pool with Servlet (tomcat) & MySQL & Java
Rails6 I want to make an array of values with a check box
How to build an environment of [TypeScript + Vue + Express + MySQL] with Docker ~ Vue edition ~
[Android] I tried to make a material list screen with ListView + Bottom Sheet
[Docker] Connection with MySQL
Php settings with Docker
Disposable PHP with Docker
I tried what I wanted to try with Stream softly.
I tried to implement file upload with Spring MVC
[Introduction to Docker x ECS] ECS deployment with docker compose up
I tried to read and output CSV with Outsystems
I tried to implement TCP / IP + BIO with JAVA
[Java 11] I tried to execute Java without compiling with javac
I tried to summarize the state transition of docker
How to make an almost static page with rails
I tried to get started with Spring Data JPA
I tried to make a login function in Java