Skip to content

Commit c65463d

Browse files
cwbitcwbit
cwbit
authored and
cwbit
committed
(feat) moved mysql variables to .env file
you should now specify your root password, database, and username/password in the `.env` file instead of the `docker-compose.yml` file. This lets us keep the passwords local so we can add our `docker-compose.yml` file to our repo without compromising db security
1 parent 2e7ab32 commit c65463d

File tree

3 files changed

+31
-7
lines changed

3 files changed

+31
-7
lines changed

.env.sample

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
MYSQL_ROOT_PASSWORD=root
2+
MYSQL_DATABASE=myapp
3+
MYSQL_USER=myapp
4+
MYSQL_PASSWORD=myapp

README.md

+23-3
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,23 @@ Here is an example of what my typical setup looks like
100100
php-ini-overrides.ini
101101
```
102102

103-
Then, **Find/Replace** `myapp` with the name of your app.
103+
Next, **Update the Environment File**
104+
105+
Copy or Rename `docker/.env.sample` to `docker/.env`.
106+
This is an environment file that your Docker Compose setup will look for automatically which gives us a great, simple way to store things like your mysql database credentials outside of the repo.
107+
108+
By default the file will contain the following
109+
110+
```
111+
MYSQL_ROOT_PASSWORD=root
112+
MYSQL_DATABASE=myapp
113+
MYSQL_USER=myapp
114+
MYSQL_PASSWORD=myapp
115+
```
116+
117+
Docker Compose will automatically replace things like `${MYSQL_USER}` in the `docker-compose.yml` file with whatever corresponding variables it finds defined in `.env`
118+
119+
Lastly, **Find/Replace** `myapp` with the name of your app.
104120

105121
> **WHY?** by default the files are set to name the containers based on your app prefix. By default this is `myapp`.
106122
> A find/replace on `myapp` is safe and will allow you to customize the names of the containers
@@ -118,9 +134,13 @@ That's it. You can now access your CakePHP app at
118134

119135
`localhost:8180`
120136

137+
> **tip**: start docker-compose with `-d` to run (or re-run changed containers) in the background.
138+
>
139+
> `docker-compose up -d`
140+
121141
**Connecting to your database**
122142

123-
Also by default the first time you run the app it will create a `MySQL` database with the following credentials
143+
Also by default the first time you run the app it will create a `MySQL` database with the credentials you specified in your `.env` file (see above)
124144

125145
``` yaml
126146
host : myapp-mysql
@@ -146,7 +166,7 @@ Your `app/config.php` file should be set to the following (it connects through t
146166
],
147167
```
148168

149-
To change these defaults edit the `docker-compose.yml` file under `myapp-mysql`'s `environment` section.
169+
To change these defaults edit the variables in the `docker/.env` file or tweak the `docker-compose.yml` file under `myapp-mysql`'s `environment` section.
150170

151171
## Now, how to run `bin/cake` and `mysql`
152172

docker-compose.yml

+4-4
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@ myapp-mysql:
1717
ports:
1818
- "8106:3306"
1919
environment:
20-
- MYSQL_ROOT_PASSWORD=root
21-
- MYSQL_DATABASE=myapp
22-
- MYSQL_USER=myapp
23-
- MYSQL_PASSWORD=myapp
20+
- MYSQL_ROOT_PASSWORD=${MYSQL_ROOT_PASSWORD}
21+
- MYSQL_DATABASE=${MYSQL_DATABASE}
22+
- MYSQL_USER=${MYSQL_USER}
23+
- MYSQL_PASSWORD=${MYSQL_PASSWORD}
2424

2525
myapp-nginx:
2626
image: phpdockerio/nginx:latest

0 commit comments

Comments
 (0)