Since I built the server CI of work related code with docker I made my own introduction
-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
・ 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
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
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