This is an example setup of two Docker containers:
mysql
: Running MySQL 8.0.web
: Running PHP 7.2 on Debian's Apache httpd.
- Docker
- Make
- A web browser
Familiarise yourself with the file structure. It will help you understand what everything does.
Your custom .sql
and/or .sh
files. These are executed in alphanumerical order when the mysql
container is
initialised.
php.ini
is your custom PHP configuration.Dockerfile
:- Copies the aforementioned
php.ini
file. - Installs the
pdo_mysql
extension.
- Copies the aforementioned
Sample PHP code that is executed on the web
container, and connects to a sample app
database on the mysql
container.
Example file for you to copy, when creating your custom .env
file. The .env
file will contain environment variables
that are used to build the containers.
Tells git
to ignore the .env
file from commits, since it's environment-specific.
Defines the mysql
and web
containers.
Defines make
commands to be run from the command line in the project's root directory.
This includes common Docker commands, but you can add other common developer commands as well.
- Copy
.env.example
to create your own.env
file in the root project directory. - Run
make clean
to build the Docker containers.
- Run
make web
to connect to theweb
container as theroot
user in a new Bash session. - Run
ls -la
, and optionally try some other Bash commands. - Run
exit
when you're done.
- Run
make mysql
to connect to themysql
container as theroot
user in a new MySQL session. - Run
show databases;
, and optionally try some other MySQL commands. - Run
exit
when you're done.
If you'd prefer to connect via a database management app, the details are:
Label | Value |
---|---|
Host | 0.0.0.0 |
Username | root |
Password | The value of MYSQL_ROOT_PASSWORD in your .env file |
Port | The value of HOST_PORT_MYSQL in your .env file |
- Visit
http://localhost:90
in your web browser, where90
is the port you configured. - The first time the page loads successfully, it'll create a user in the database.
- Subsequent page loads will fetch the previously-created user's data from the database.
- Try adding
echo 'Hello world';
toindexAction
within the filesrc/AppBundle/Controller/IndexController.php
, then reload the page. Your change should take effect immediately.
If a container falls over, check make logs-mysql
or make logs-web
to work out why.
- "Connection refused" occurs when the MySQL container has not fully initialised yet, so you may need to wait a few seconds for it to finish.
- If the containers are killed (e.g. you restart your machine), running
make up
will restart them, without losing any modifications (i.e. if you have manually modified the database or filesystem aftermake clean
). - If you run
make clean
again, this will destroy the containers and create new ones. - If you make changes to
service/mysql/init
, you will need to runmake clean
for the changes to take effect.