Skip to content

Commit ba4e33f

Browse files
committed
part 1
1 parent 131f002 commit ba4e33f

File tree

13 files changed

+168
-138
lines changed

13 files changed

+168
-138
lines changed

.docker/mysql/my.cnf

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[mysqld]
2+
collation-server = utf8mb4_unicode_ci
3+
character-set-server = utf8mb4

.docker/nginx/conf.d/php.conf

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
server {
2+
listen 80;
3+
listen [::]:80;
4+
server_name php.test;
5+
root /var/www/php;
6+
index index.php;
7+
8+
location ~* \.php$ {
9+
fastcgi_pass php:9000;
10+
include fastcgi_params;
11+
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
12+
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
13+
}
14+
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
FROM php:7.0-fpm
1+
FROM php:7.4-fpm
22

33
RUN docker-php-ext-install pdo_mysql

.env.example

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
COMPOSE_PROJECT_NAME=demo

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.env

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
The MIT License (MIT)
22

3-
Copyright (c) 2017 Yannick Chenot
3+
Copyright (c) 2020 Yannick Chenot
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

README.md

Lines changed: 68 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,92 @@
1-
**[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.**
1+
# Docker for local web development, part 1: a basic LEMP stack
22

3-
# Docker tutorial
3+
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.
44

5-
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.
5+
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.
66

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

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

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

13-
Clone the project:
17+
## Prerequisites
1418

15-
$ git clone git@github.com:osteel/docker-tutorial.git
19+
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/).
1620

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

19-
$ docker-compose up -d
23+
## Directions of use
2024

21-
Get the Docker Machine IP:
25+
Add the following domain to your machine's `hosts` file:
2226

23-
$ docker-machine ip default
27+
```
28+
127.0.0.1 php.test
29+
```
2430

25-
Access it from your browser.
31+
Clone the repository and change the current directory for the project's root:
2632

27-
## Description
33+
```
34+
$ git clone [email protected]:osteel/docker-tutorial.git
35+
$ cd docker-tutorial
36+
```
2837

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

31-
There are 6 of them:
40+
```
41+
$ cp .env.example .env
42+
```
3243

33-
- a container for Nginx
34-
- a container for PHP-FPM
35-
- a container for MySQL
36-
- a container for phpMyAdmin
37-
- a container to make MySQL data persistent
38-
- a container for the application code
44+
Run the following command:
3945

40-
All of them are using official images.
46+
```
47+
$ docker-compose up -d
48+
```
4149

42-
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).
50+
This may take a little bit of time, as some Docker images might need downloading.
4351

44-
A [default Nginx configuration](https://github.com/osteel/docker-tutorial/blob/master/nginx/default.conf) is also copied over.
52+
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).
4553

46-
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.
54+
## Explanation
4755

48-
The MySQL data sits in its own directory mounted into its own container to make it persistent.
56+
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).
4957

50-
The application is available on the port 80 of the host machine.
58+
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`).
5159

52-
phpMyAdmin is available on port 8080.
60+
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).
5361

54-
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").
62+
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.
63+
64+
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).
65+
66+
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.
67+
68+
## Cleaning up
69+
70+
To stop the containers:
71+
72+
```
73+
$ docker-compose stop
74+
```
75+
76+
To destroy the containers:
77+
78+
```
79+
$ docker-compose down
80+
```
81+
82+
To destroy the containers and the associated volumes:
83+
84+
```
85+
$ docker-compose down -v
86+
```
87+
88+
To remove everything, including the images:
89+
90+
```
91+
$ docker-compose down -v --rmi all
92+
```

docker-compose.yml

Lines changed: 40 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,49 @@
1-
nginx:
2-
build: ./nginx/
3-
ports:
4-
- 80:80
5-
links:
6-
- php
7-
volumes_from:
8-
- app
1+
version: '3.7'
92

10-
php:
11-
build: ./php/
12-
expose:
13-
- 9000
14-
links:
15-
- mysql
16-
volumes_from:
17-
- app
3+
# Services
4+
services:
185

19-
app:
20-
image: php:7.0-fpm
6+
# Nginx Service
7+
nginx:
8+
image: nginx:1.17
9+
ports:
10+
- 80:80
2111
volumes:
22-
- ./www/html:/var/www/html
23-
command: "true"
12+
- ./src:/var/www/php:ro
13+
- ./.docker/nginx/conf.d:/etc/nginx/conf.d:ro
14+
depends_on:
15+
- php
2416

25-
mysql:
26-
image: mysql:latest
27-
volumes_from:
28-
- data
29-
environment:
30-
MYSQL_ROOT_PASSWORD: secret
31-
MYSQL_DATABASE: project
32-
MYSQL_USER: project
33-
MYSQL_PASSWORD: project
17+
# PHP Service
18+
php:
19+
build: ./.docker/php
20+
working_dir: /var/www/php
21+
volumes:
22+
- ./src:/var/www/php
23+
depends_on:
24+
- mysql
3425

35-
data:
36-
image: mysql:latest
26+
# MySQL Service
27+
mysql:
28+
image: mysql:8
29+
environment:
30+
MYSQL_ROOT_PASSWORD: root
31+
MYSQL_DATABASE: demo
3732
volumes:
38-
- /var/lib/mysql
39-
command: "true"
33+
- ./.docker/mysql/my.cnf:/etc/mysql/conf.d/my.cnf:ro
34+
- mysqldata:/var/lib/mysql
4035

41-
phpmyadmin:
42-
image: phpmyadmin/phpmyadmin
36+
# PhpMyAdmin Service
37+
phpmyadmin:
38+
image: phpmyadmin/phpmyadmin:5
4339
ports:
44-
- 8080:80
45-
links:
46-
- mysql
40+
- 8080:80
4741
environment:
48-
PMA_HOST: mysql
42+
PMA_HOST: mysql
43+
depends_on:
44+
- mysql
45+
46+
# Volumes
47+
volumes:
48+
49+
mysqldata:

nginx/Dockerfile

Lines changed: 0 additions & 3 deletions
This file was deleted.

nginx/default.conf

Lines changed: 0 additions & 36 deletions
This file was deleted.

0 commit comments

Comments
 (0)