Skip to content

Instantly share code, notes, and snippets.

@mikehaertl
Last active September 19, 2022 21:20
Show Gist options
  • Save mikehaertl/0a4a83e8ee82ce835384 to your computer and use it in GitHub Desktop.
Save mikehaertl/0a4a83e8ee82ce835384 to your computer and use it in GitHub Desktop.
How to develop with docker and Yii2

Docker Development Workflow

These ideas are WORK IN PROGRESS!

1. Initialize your project

  • Prepare your initial project files in a directory (e.g. app/)
  • Create an initial Dockerfile that includes the above directory
  • Create a fig.yml file to describe your server setup (e.g. web, db, ...)
  • Issue a fig build web to let docker build the initial project image

You can now distribute what you created so far:

  • Commit all the above including the Dockerfile and fig.yml to your repository
  • Push the docker image to a docker repository (TODO: How will this work? And how to reuse that in the fig.yml?)

2. Use it in development

  • Clone your project repository
  • Run fig up to start your environment
  • The local app/ directory is shared with the running docker container, so you can now develop/modify code there. It will override whatever is stored in the docker image, so that you always can work with the latest code.
  • When done, you can commit and push your code as usual.

3. Prepare a release

  • When you're ready for a release, run fig build web again, to update the docker image and push that image. (TODO: How to tag that image with fig?)
  • Pull this latest image to production and start a container from there

Example

  • Create a new Yii2 project in an app/ directory
composer create-project --prefer-dist --stability=dev yiisoft/yii2-app-basic basic
  • Modify the configuration to use environment variables e.g. for DB host credentials
'db' => [
    'dsn' => 'mysql:host='.getenv('DB_PORT_3306_TCP_ADDR').';dbname=yii',
    'username' => 'app',
    'password' => 'secret',
],

  • Create a Dockerfile with a minimal PHP setup
FROM debian:wheezy

# Modify the PHP modules below to match your project requirements
RUN apt-get update && \
    apt-get install -y \
        php5-cli \
        php5-imagick \
        php5-intl \
        php5-mcrypt \
        php5-mysql \
        php-apc && \
    rm -rf /var/lib/apt/lists/*

RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin && \
    ln -s /usr/local/bin/composer.phar /usr/local/bin/composer

COPY app/ /app
  • Create a fig.yml
web:
    build: .
    command: php -S 0.0.0.0:8000 -t /app/web
    ports:
        - "8000:8000"
    volumes:
        - ./app:/app
    links:
        - db
db:
    image: mysql
    environment:
        MYSQL_ROOT_PASSWORD: secretroot
        MYSQL_USER: app
        MYSQL_PASSWORD: secret
        MYSQL_DATABASE: yii
  • Run fig up to start your setup

Best practices

  • Use one container per process
  • Make the containers self contained! I.e. include all your app code and app dependencies (No more provisioning on deployment!)
  • Build ephemeral containers! You should be able to exchange your container in production without data loss.
  • Configure the containers through environment variables. Some environment variables are automatically provided by docker. You can pass more into the container through your fig.yml. In your PHP application you can also use phpdotenv to use a .env file for configuration.

Tipps

  • To install a composer package you fire a one-off command:
fig run web composer require some/package

Open questions

  • How to provision the DB (yii migrate)?
  • How to persist the DB data?
@piksar
Copy link

piksar commented Oct 29, 2015

Good idea, I just want to comment that FIG is now deprecated and replaced with Docker Compose.
I am also looking at otto ( https://ottoproject.io) to achieve this..

Copy link

ghost commented Nov 17, 2017

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment