Skip to content

Commit

Permalink
Manage environment variables to avoid hardcode and share code without…
Browse files Browse the repository at this point in the history
… revealing database login credentials
  • Loading branch information
kenvilar committed Aug 11, 2018
1 parent 9a71768 commit af94043
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 15 deletions.
38 changes: 27 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
# php-app-with-slim-and-docker
PHP Application with SLIMPHP web framework using Docker connecting to third-party API


## Build the image and tagged it as kenvilar/weather-app to easily create a container from this image
```docker
docker build . -t kenvilar/weather-app
```
$ docker pull php:latest
$ docker pull php:5.6


```docker
docker pull php:latest
docker pull php:5.6
# run the hello.php (optional)
docker run --rm -v $(pwd):/app php:latest php /app/hello.php
# install the slim library
Expand All @@ -13,16 +20,20 @@ docker run --rm -p 38000:80 -v $(pwd):/var/www/html php:apache
# or run the app in detached mode
docker run -d --rm -p 38000:80 -v $(pwd):/var/www/html php:apache
# name the container
docker run -d --rm --name=weather-app -p 38000:80 -v $(pwd):/var/www/html php:apache
docker run -d --rm --name=weather_app -p 38000:80 -v $(pwd):/var/www/html php:apache
# display running containers
docker ps
docker ps
# or display all containers including the stopped containers
docker ps -a
docker ps -a
# stop the container with id of three characters or more
docker stop <ID>
docker stop <ID>
# stop the container (alternative)
docker stop <container-name>
# run the docker container again by typing this command docker run -d --rm --name=weather-app -p 38000:80 -v $(pwd):/var/www/html php:apache
docker stop <container-name>
```

```docker
# run the docker container again
docker run -d --rm --name=weather_app -p 38000:80 -v $(pwd):/var/www/html php:apache
# run the database container and named it as weather_db from image mysql with version 5.7
docker run -d --rm --name=weather_db -e MYSQL_USER=kenvilar -e MYSQL_DATABASE=weather_app -e MYSQL_PASSWORD=kenvilarsamplepassword -e MYSQL_RANDOM_ROOT_PASSWORD=true mysql:5.7
# check if the database weather_app is running
Expand Down Expand Up @@ -51,6 +62,11 @@ mysql> show tables;

# Linking the PHP container
```docker
docker run -d --rm --name=weather-app -p 38000:80 -v $(pwd):/var/www/html --link weather_db kenvilar/weather-app
# if it gets an error then you have to remove the container named weather-app
```
docker run -d --rm --name=weather_app -p 38000:80 -v $(pwd):/var/www/html --link weather_db kenvilar/weather-app
# if it gets an error then you have to remove the container named weather_app
```

# Stop the container and re-run it with the environment variables
```docker
docker run -d --rm --name=weather_app -p 38000:80 -v $(pwd):/var/www/html --link weather_db -e DATABASE_HOST='weather_db' -e DATABASE_USER='kenvilar' -e DATABASE_PASSWORD='kenvilarsamplepassword' -e DATABASE_NAME='weather_app' kenvilar/weather-app
```
8 changes: 4 additions & 4 deletions index.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
},
'mysql' => function () {
$mysqli = new mysqli(
'weather_db',
'kenvilar',
'kenvilarsamplepassword',
'weather_app'
getenv('DATABASE_HOST'),
getenv('DATABASE_USER'),
getenv('DATABASE_PASSWORD'),
getenv('DATABASE_NAME')
);
if ($mysqli->connect_errno) {
echo "Failed to connect to MYSQL: " . $mysqli->connect_error;
Expand Down

0 comments on commit af94043

Please sign in to comment.