diff --git a/README.md b/README.md index e3c24aa..c1fa51e 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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 +docker stop # stop the container (alternative) -docker stop -# 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 +``` + +```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 @@ -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 -``` \ No newline at end of file +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 +``` diff --git a/index.php b/index.php index be195d5..ece0afd 100644 --- a/index.php +++ b/index.php @@ -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;