# Running the app

# Starting the app

To start all the containers, simply run the following command from inside the app folder ~/Sites/demo

docker-compose up

You should see php-fpm, nginx, redis and mysql starting up. By default, the app is exposed on port 8080 on localhost, so go ahead and try to open http://localhost:8080 in your browser. You should see the Laravel homepage.

TIP

By default docker-compose up will run in the foreground of the terminal window that starts the command, and outputs all the logs generated by containers to stdout. If you would like to run the containers in the background the pass -d to the docker-compose up -d command.

# Stopping the app

If the application has been started with docker-compose up in the foreground, you can terminate it by pressing CTRL + c.

If the application has been started in the background, using docker-compose up -d, or if you would like to stop the application from a different terminal session, execute:

docker-compose stop

This will stop all the running containers inside the docker-compose.yml file.

WARNING

Not to be confused with docker-compose down. This command will delete all your volumes including your database. However, you will NOT lose your application files.

# Rebuilding the containers

When you run docker-compose up for the first time, it will build and cache the Docker files under the docker folder. Every subsequent attempt to start the stack using docker-compose up will use the cached images. If you need to make changes to the Dockerfiles, you will need to force a re-build of the cached containers. To do that, here are some helpful commands:

docker-compose build # to rebuild all services
docker-compose build nginx # to rebuild a single service
docker-compose up nginx # to bring it back up after it was rebuilt

# Running the build containers

As mentioned in the local dev overrides section, the development docker-compose.override.yml file contains two additional containers (composer and node) that we can leverage to install composer packages or interact with NodeJS & npm.

# Running composer

Now that the app is running, let's see how we can install new packages using composer. Let's assume we would like to install the laravel/passport package. Simply type in:

docker-compose run composer composer require "laravel/passport"

Once the command finishes, take a look at composer.json file in your project. You should now see laravel/passport as a required package.

# Compiling assets with Laravel Mix

Out of the box, our docker-compose.override.yml file is configured to run npm run watch, which means any JavaScript files that you change under resources/js folder will automatically rebuild.

If you would like to install a new package using npm install you can simply type:

 docker-compose exec node npm install axios --save

After installing a new package, you will have to reload the watcher:

docker-compose restart node

This will stop the node container and restart npm run watch.

# Accessing the database

In our docker-compose.override.yml file, we have exposed MySQL on port 3306 on the host machine. You can connect using Sequel Pro using host 127.0.0.1, username root and password secret.

If you would like to bind the database on a different port than 3306 on the host machine, then modify the docker-compose.override.yml file:

// find this block
ports:
  - "3306:3306"
  
// and replace it with 
ports:
  - "3307:3306"

Now we are listening on port 3307 on localhost, but inside the container it still maps to port 3306.