Using docker compose I specified all the images I am going to use for my application. This will create and execute the containers needed for my application, also it will create automatically the virtual network that will connect all of this containers, the name of the container will be also its name in the network.
In the next docker compose file is specified that I will have the containers php, nginx and my sql.
```yaml
version: '3.8'
services:
php:
build:
context: ./php/
container_name: programwithgio-app
restart: always
volumes:
- ./:/var/www/html
nginx:
build:
context: ./nginx/
container_name: programwithgio-nginx
restart: always
volumes:
- ./:/var/www/html
depends_on:
- php
ports:
- 8080:80
mysql:
image: mysql:latest
restart: always
environment:
MYSQL_ROOT_PASSWORD: password
volumes:
- ./data:/var/lib/mysql
```
## MORE ABOUT DOCKER COMPOSE
- php: I specified that I will build a container from a docker file that is in the folder php and also that templates(local location of the templates of the web application) will be mapped to /var/www/html/ inside the container.
- DOCKERFILE PHP:
```Dockerfile
FROM php:fpm-alpine
RUN docker-php-ext-install mysqli && docker-php-ext-enable mysqli
```
- nginx: I specified that it is build from a Dockerfile and it is dependent on php and that the port is going to be 8080 in my machine and 80 in the container
- mysql: I specified that the password for the user root(is by default) is going to be password and also I mapped the local database data to /var/lib/mysql inside the container to be sure that once the container is shot down the data is in somewhere
- CONNECT TO THE DATABASE WHILE RUNNING
- We can connect to the database while running to create the database table and atrributes
- Connection:
```bash
docker exec -it mysql-server mysql -uroot -p
```
- SQL code for creating the database
```sql
CREATE DATABASE NOTAS;
CREATE TABLE nota (
id VARCHAR(60),
noteContent VARCHAR(60)
);
```
## HOW TO START,STOP AND REMOVE THE APP
- Prepare and star the app via the command bash start-app.sh
Docker will build and execute the containers with the info given in docker compose file (-d is set to execute in detached mode)
- Start-app.sh:
```sh
docker-compose up -d
```
- Stop the app via bash stop-app.sh
- Stop-app.sh:
```sh
docker-compose stop
```
- Remove the app via bash remove-app.sh
- Remove-app.sh:
```sh
docker-compose down
```
## VIEW THE APP IN THE WEB
Once you have download all the requiered software and you use bash start-app and everything is done you can see the web application searching localhost:8080