Skip to content

Commit

Permalink
part 1
Browse files Browse the repository at this point in the history
  • Loading branch information
osteel committed Feb 21, 2020
1 parent 131f002 commit ba4e33f
Show file tree
Hide file tree
Showing 13 changed files with 168 additions and 138 deletions.
3 changes: 3 additions & 0 deletions .docker/mysql/my.cnf
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[mysqld]
collation-server = utf8mb4_unicode_ci
character-set-server = utf8mb4
14 changes: 14 additions & 0 deletions .docker/nginx/conf.d/php.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
server {
listen 80;
listen [::]:80;
server_name php.test;
root /var/www/php;
index index.php;

location ~* \.php$ {
fastcgi_pass php:9000;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
}
}
2 changes: 1 addition & 1 deletion php/Dockerfile → .docker/php/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
FROM php:7.0-fpm
FROM php:7.4-fpm

RUN docker-php-ext-install pdo_mysql
1 change: 1 addition & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
COMPOSE_PROJECT_NAME=demo
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.env
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
The MIT License (MIT)

Copyright (c) 2017 Yannick Chenot
Copyright (c) 2020 Yannick Chenot

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
98 changes: 68 additions & 30 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,54 +1,92 @@
**[WARNING] This repository is deprecated and only maintained for legacy purposes. For an up to date Docker set up, please head over [here](https://github.com/osteel/docker-tutorial-2), thank you.**
# Docker for local web development, part 1: a basic LEMP stack

# Docker tutorial
This repository accompanies a [tutorial series](https://tech.osteel.me/posts/docker-for-local-web-development-why-should-you-care "Docker for local web development, introduction: why should you care?") about leveraging Docker for local web development.

This is the companion repository for [From Vagrant to Docker: How to use Docker for local web development](http://tech.osteel.me/posts/2015/12/18/from-vagrant-to-docker-how-to-use-docker-for-local-web-development.html "From Vagrant to Docker: How to use Docker for local web development"). Please refer to it for a full explanation.
The current branch covers part 1 of the series - please refer to the [full article](https://tech.osteel.me/posts/docker-for-local-web-development-part-1-a-basic-lemp-stack "Docker for local web development, part 1: a basic LEMP stack") for a detailed explanation.

It contains a basic LEMP stack running with Docker, intented to be used for local web development.
## Content

## Get started
This branch contains a basic LEMP stack running on Docker and orchestrated by Docker Compose, including:

[Install Docker](https://docs.docker.com/engine/installation/ "Install Docker Engine") on your machine.
* A container for Nginx;
* A container for PHP-FPM;
* A container for MySQL;
* A container for phpMyAdmin;
* A volume to persist MySQL data.

Clone the project:
## Prerequisites

$ git clone git@github.com:osteel/docker-tutorial.git
Make sure [Docker Desktop for Mac or PC](https://www.docker.com/products/docker-desktop) is installed and running, or head [over here](https://docs.docker.com/install/) if you are a Linux user. You will also need a terminal running [Git](https://git-scm.com/).

From the project root:
This setup also uses localhost's port 80, so make sure it is available.

$ docker-compose up -d
## Directions of use

Get the Docker Machine IP:
Add the following domain to your machine's `hosts` file:

$ docker-machine ip default
```
127.0.0.1 php.test
```

Access it from your browser.
Clone the repository and change the current directory for the project's root:

## Description
```
$ git clone [email protected]:osteel/docker-tutorial.git
$ cd docker-tutorial
```

The different containers are described in [`docker-compose.yml`](https://github.com/osteel/docker-tutorial/blob/master/docker-compose.yml).
Copy `.env.example` to `.env`:

There are 6 of them:
```
$ cp .env.example .env
```

- a container for Nginx
- a container for PHP-FPM
- a container for MySQL
- a container for phpMyAdmin
- a container to make MySQL data persistent
- a container for the application code
Run the following command:

All of them are using official images.
```
$ docker-compose up -d
```

When building and starting containers for the first time with Docker Compose, a database named `project` will be created by default. You can change this in [`docker-compose.yml`](https://github.com/osteel/docker-tutorial/blob/master/docker-compose.yml).
This may take a little bit of time, as some Docker images might need downloading.

A [default Nginx configuration](https://github.com/osteel/docker-tutorial/blob/master/nginx/default.conf) is also copied over.
Once the script is done, visit [php.test](http://php.test) (you might initially get a MySQL error message: this is because the database is still being created; the error will soon disappear upon refreshing the page).

The `www/html/` directory is mounted into the one served by Nginx on the container, so any update to the code is available without having to rebuild the container.
## Explanation

The MySQL data sits in its own directory mounted into its own container to make it persistent.
The images used by the setup are listed and configured in [`docker-compose.yml`](https://github.com/osteel/docker-tutorial/blob/part-1/docker-compose.yml).

The application is available on the port 80 of the host machine.
When building and starting the containers based off the images for the first time, a MySQL database named `demo` is automatically created (you can pick a different name in the MySQL service's description in `docker-compose.yml`).

phpMyAdmin is available on port 8080.
A [minimalist Nginx configuration](https://github.com/osteel/docker-tutorial/blob/part-1/.docker/nginx/conf.d/php.conf) for the PHP application is also copied over to Nginx's container, making it available at [php.test](http://php.test).

Again, for the complete tutorial please head to the [original post](http://tech.osteel.me/posts/2015/12/18/from-vagrant-to-docker-how-to-use-docker-for-local-web-development.html "From Vagrant to Docker: How to use Docker for local web development").
The `src/` directory containing the application is mounted onto both Nginx's and the application's containers, meaning any update to the code is immediately available upon refreshing the page, without having to rebuild any container.

The database data is persisted in its own local directory through the volume `mysqldata`, which is mounted onto MySQL's container. A phpMyAdmin interface is available at [localhost:8080](http://localhost:8080).

Please head over to the [full article](https://tech.osteel.me/posts/docker-for-local-web-development-part-1-a-basic-lemp-stack "Docker for local web development, part 1: a basic LEMP stack") for a detailed explanation.

## Cleaning up

To stop the containers:

```
$ docker-compose stop
```

To destroy the containers:

```
$ docker-compose down
```

To destroy the containers and the associated volumes:

```
$ docker-compose down -v
```

To remove everything, including the images:

```
$ docker-compose down -v --rmi all
```
79 changes: 40 additions & 39 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,48 +1,49 @@
nginx:
build: ./nginx/
ports:
- 80:80
links:
- php
volumes_from:
- app
version: '3.7'

php:
build: ./php/
expose:
- 9000
links:
- mysql
volumes_from:
- app
# Services
services:

app:
image: php:7.0-fpm
# Nginx Service
nginx:
image: nginx:1.17
ports:
- 80:80
volumes:
- ./www/html:/var/www/html
command: "true"
- ./src:/var/www/php:ro
- ./.docker/nginx/conf.d:/etc/nginx/conf.d:ro
depends_on:
- php

mysql:
image: mysql:latest
volumes_from:
- data
environment:
MYSQL_ROOT_PASSWORD: secret
MYSQL_DATABASE: project
MYSQL_USER: project
MYSQL_PASSWORD: project
# PHP Service
php:
build: ./.docker/php
working_dir: /var/www/php
volumes:
- ./src:/var/www/php
depends_on:
- mysql

data:
image: mysql:latest
# MySQL Service
mysql:
image: mysql:8
environment:
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: demo
volumes:
- /var/lib/mysql
command: "true"
- ./.docker/mysql/my.cnf:/etc/mysql/conf.d/my.cnf:ro
- mysqldata:/var/lib/mysql

phpmyadmin:
image: phpmyadmin/phpmyadmin
# PhpMyAdmin Service
phpmyadmin:
image: phpmyadmin/phpmyadmin:5
ports:
- 8080:80
links:
- mysql
- 8080:80
environment:
PMA_HOST: mysql
PMA_HOST: mysql
depends_on:
- mysql

# Volumes
volumes:

mysqldata:
3 changes: 0 additions & 3 deletions nginx/Dockerfile

This file was deleted.

36 changes: 0 additions & 36 deletions nginx/default.conf

This file was deleted.

Binary file added src/hello.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
39 changes: 39 additions & 0 deletions src/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Hello there</title>
<style>
body {
font-family: "Arial", sans-serif;
font-size: larger;
}

.center {
display: block;
margin-left: auto;
margin-right: auto;
width: 50%;
}
</style>
</head>
<body>
<img src="hello.gif" alt="Hello there" class="center">
<?php
$connection = new PDO('mysql:host=mysql;dbname=demo;charset=utf8', 'root', 'root');
$query = $connection->query("SELECT TABLE_NAME FROM information_schema.TABLES WHERE TABLE_SCHEMA = 'demo'");
$tables = $query->fetchAll(PDO::FETCH_COLUMN);

if (empty($tables)) {
echo '<p class="center">There are no tables in database <code>demo</code>.</p>';
} else {
echo '<p class="center">Database <code>demo</code> contains the following tables:</p>';
echo '<ul class="center">';
foreach ($tables as $table) {
echo "<li>{$table}</li>";
}
echo '</ul>';
}
?>
</body>
</html>
28 changes: 0 additions & 28 deletions www/html/index.php

This file was deleted.

0 comments on commit ba4e33f

Please sign in to comment.