# contao-docker-test

#Installation:
- Create a new folder "cert" in project root, enter it in terminal and create SSL-certificate files:
  - `openssl req -x509 -new -out mycert.crt -keyout mycert.key -days 365 -newkey rsa:4096 -sha256 -nodes`
  - Enter a name for your local domain name e.g. "local-docker" as "Common Name", all other information are not relevant
    and you can skip them with enter
  - Make sure your local domain name is entered in the host file 
    (Unix, Mac: /etc/hosts | Windows: C:\Windows\System32\drivers\etc\hosts) like this:
    "127.0.0.1  localhost local-docker"
  - Later when the app is ready you can use this domain name to call with the browser
  - A full guide is here: 
    [SSL Docker Installation](https://forums.docker.com/t/setup-local-domain-and-ssl-for-php-apache-container/116015)
- Start docker app (e.g. Docker Desktop)
- Go to root folder in terminal and create docker containers: `docker-compose up -d`
- Create a database for contao in database container:
  - One way to do so is to open up the "Database"-Tab of PhpStorm
    - Click on "+" -> Data Source -> MariaDB
    - Add a name to configuration e.g. contao-db
    - Host: localhost 
    - Port: 3306
    - User: root (according to database-service in docker-compose.yml)
    - Password: root (according to database-service in docker-compose.yml)
    - Once you're connected go to Query Console (icon at the top "QL") and create the database for contao
      - Enter the following line into the console and press the "play"-button to execute the query:
      - `CREATE DATABASE contao CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;`
      - The database name is free to chose but in this example the database name 'contao' is being used
  - Another way is to connect to phpmyadmin via [localhost:8080](http://localhost:8080). The host name is "database"
    as it's configured in docker-compose.yml (service: database).
- Open [contao manager](http://localhost/contao-manager.phar.php) in browser and create an admin user for the manager.
- Open [contao install](http://localhost/contao/install) in browser after database creation in order to install contao
  - Step through the installation process until you get to enter the database credentials
  - NOTE: the 'host' isn't 'localhost' here, use the name of the database service 'database' as it's configured in
    the docker-compose.yml
  - Finish the installation and have fun :)
  - NOTE: For full installation you have to wait until composer is fully done with installation if you install
    the project for the first time (or setting it up freshly). If you use Docker Desktop you can go to "Containers"
    and click on the container named "contao". Then you'll see the console output of this container and wait until it's
    done to ensure the project is running properly.
  - Contao will be available here: [http://localhost/contao](http://localhost/contao)
- XDebug:
  - Xdebug is also available within this project. If you'd like to use it do the following in PHPStorm:
    1. Go to Preferences->PHP
    2. Click on "..."-button at CLI Interpreter
    3. A new window for CLI Interpreters will pop up
    4. Click on "+" in order to add a new CLI interpreter
    5. Select "From Docker, Vagrant, VM, WSL, Remote..."
    6. A new window will pop up and select radiobutton "Docker" (should be selected by default) here and then select
       the docker image, which is "php-apache-contao:latest" in this case
    7. After selection the PHP version will be displayed and also the XDebug version
    8. Actually you're done, but you should set the "PHP language level" on the first screen on the same PHP Version
       as your CLI Interpreter to make sure that PHPStorm helps you with your very PHP version :)
    9. To test if xdebug is running properly, just set a breakpoint in "app/contao/public/index.php" and execute
       some contao "action" in your browser e.g. [contao install](http://localhost/contao/install).
       Xdebug should stop at your breakpoint. Make sure PhpStorm is listening (Phone icon is green) and the breakpoint 
       is set properly. :) 

# Some important information
- The database content is stored in a container which is bind-mounted to the 'db'-folder of the project
- If you should renew your containers, make sure you don't delete this folder, 
  since all database data is contained in there
- Even if the container will be removed, the database data still exists in this folder and the data will be
  provided for the database container through the bind-mounting if you create a new database container
- If you want to export the database, either do it via phpmyadmin or enter the container and use mysql dump
- To enter a container e.g. the contao container run `docker exec -it contao /bin/bash`.
  - After this you're directly within the container and can browse through it's content via console.  
  - If you did changes e.g. in the composer.json on your host machine (your computer - not within a container) 
    to install or update bundles, then run e.g. `composer install` within the container
  - If you do changes on you host machine, these changes will be also available for the container via bind-mounting
- To exit the container, simply run `exit` and you'll get back to the console of your host machine ;)

# Links:
- https://raphaelstaebler.medium.com/run-contao-4-inside-a-docker-container-96422278ea02
- https://github.com/wsargent/docker-cheat-sheet/blob/master/README.md
- https://docs.tibco.com/pub/mash-local/4.3.0/doc/html/docker/GUID-BD850566-5B79-4915-987E-430FC38DAAE4.html

# Some helpful docker commands:
- `docker-compose up -d`
- `docker-compose down`
- `docker exec -it contao /bin/bash`
- `docker build -t spt-docker-contao .`
- `docker image rm spt-docker-contao`
- `docker ps`
- `docker ps --all`
- `docker container stop $(docker container ls -aq)`
- `docker container rm $(docker container ls -aq)`
- `docker exec b768b668cb75 cat contao/composer.json`
- `docker rmi $(docker images -q)`
- `docker stop contao && docker rm contao && docker rmi php-apache-contao && docker-compose up -d`

# Some helpful other commands:
- Restart Apache within the container
  - Go into docker container `docker exec -it contao /bin/bash`
  - Apache restart `sudo /etc/init.d/apache2 reload`

# Clean restart of Docker instance
1.  Stop the container(s) using the following command: `docker-compose down`
2.  Delete all containers using the following command: `docker rm -f $(docker ps -a -q)`
3.  Delete all volumes using the following command: `docker volume rm $(docker volume ls -q)`
4.  Restart the containers using the following command: `docker-compose up -d`