Skip to content

karllhughes/docker-php-cli-example

Repository files navigation

Sample Command Line PHP/Docker Application

Codeship Status for karllhughes/docker-php-cli-example

This is a simple Laravel command line application demonstrating using Docker and Docker Compose for PHP application development, continuous integration, and deployment through the Codeship Pro platform.

Commands

For convenience, I have wrapped a number of common docker commands in npm scripts. For more details, see the package.json file.

  • npm run -s app:up Starts up the application and database using Docker Compose.
  • npm run -s app:restart Restarts the application making any code changes take effect.
  • npm run -s app:down Brings down the application and database.
  • npm run -s app:test Runs the test suite.*
  • npm run -s artisan -- ARTISAN:COMMAND Wrapper to run artisan commands.*
  • npm run -s artisan -- item:update Update the next item's checked_at date.
  • npm run -s composer:install Install PHP packages via composer.
  • npm run -s composer:update Update PHP packages via composer.
  • npm run -s composer:dump Wrapper for composer dump-autoload command.

* Note: The app must be running in order to run this command.

Local development

In order to run this project locally for development, uncomment these lines in the docker-compose file that mount the code as a volume:

  app:
    build: .
    links:
      - database
    env_file:
      - .env
    command: cron -f
    # Uncomment these lines:
    volumes:
     - ./:/app

This will ensure that any updates you make take effect immediately in the application container.

Run the composer install command:

$ npm run -s composer:install

Now, copy .env.example to .env and customize with your own variables and keys.

Next, run the npm command to bring up the docker containers:

$ npm run -s app:up

If it's your first time running this app, set up the database:

$ npm run -s db:create      # Creates the database
$ npm run -s db:migrate     # Runs the migrations
$ npm run -s db:seed        # Seeds the database

Now you can run the app's primary artisan command:

$ npm run -s artisan -- item:update

To bring the containers down when you're finished:

$ npm run -s app:down

Testing Locally

Testing within containers

Once the app is set up and running, you can run the acceptance test:

$ npm run -s app:test

This runs phpunit from within the running application container.

Testing and deploying with Jet

If you have Codeship's Jet installed, you can run tests from Jet as well.

First, bring the application down (if it's running):

$ npm run -s app:down

Generate an AES encryption key:

$ jet generate

Encrypt your .env files:

$ jet encrypt .env .env.encrypted && jet encrypt deployer/.env deployer/.env.encrypted

Build the containers, run the tests, and deploy the latest on the master branch:

$ jet steps

Continuous integration with Codeship

This project was built to use Codeship's Docker continuous integration platform.

  1. Copy your Codeship AES key into a file called codeship.aes at the root of this project.

  2. Make sure your deployer/.env file is filled out with your server information and private rsa key (with no line breaks).

  3. Encrypt your .env files: $ jet encrypt .env .env.encrypted && jet encrypt deployer/.env deployer/.env.encrypted

  4. Push your code to a repository that is attached to a Codeship Pro CI instance.

This should run your tests and deploy your code to the server specified in deployer/.env

License

This is open-sourced software licensed under the MIT license.

About

Example of a Laravel PHP cli application running in Docker containers and using Codeship for CI

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published