Skip to content
Michael Härtl edited this page Feb 1, 2017 · 7 revisions

How can I run Yii console commands?

docker-compose run --rm web yii migrate
docker-compose run --rm web yii mycommand/myaction

On Unix based systems you will probably want to create a handy alias like:

alias dcyii='docker-compose run --rm web ./yii'
# Then use like
dcyii migrate

Where are composer packages installed?

Composer packages are installed inside the container under /var/www/vendor. So they live outside of the app directory. This is because during development we usually mount the local app directory into the /var/www/html directory of the container. This would override any vendor packages we have there. So you would either have to locally install all the packages in vendor/ or somehow make sure, that the composer packages are updated after the local dir was shared.

By keeping the directory outside, we circumvent this issue. The docker images will always contain all required composer dependencies and use those at runtime.

How can I update or install new composer packages?

To update or install new packages you first need an updated local composer.lock file. Then the next time you issue docker-compose build it will pick up the changed file and create a new docker image with the updated packages inside:

docker-compose run --rm web composer update my/package
docker-compose build

Note: If you face the nasty API rate limit issue with github, you can create a personal API token and expose it in your docker-compose.yml file as API_TOKEN env var.

How can I make composer packages available locally?

Some IDE's may require a copy of the vendor directory somewhere on your local host for autocompleting commands or providing inline help. You can therefore copy the vendor directory to your local directory:

docker-compose run --rm web cp -r /var/www/vendor .

How can I log the container output to a file?

The default log configuration follows the Docker convention to log to STDOUT and STDERR. If you want a logfile for your containers you can add this configuration to the docker-compose.yml:

    log_driver: "syslog"
    log_opt:
        tag: "docker-web"

If you're on Debian and use Rsyslog for logging put the following into a new file in /etc/rsyslog.d/30-docker.conf:

# Log docker messages to one file per container
$template DockerLogs, "/var/log/docker/%syslogtag:R,ERE,1,ZERO:.*docker/([^\[]+)--end%.log"
if ($programname == 'docker' and $syslogtag contains 'docker/') then -?DockerLogs  

# Do not pollute default syslog with docker messages
& stop
$ sudo mkdir /var/log/docker
$ sudo chown syslog /var/log/docker

This will create one logfile per container in /var/log/docker/ with the logtag suffixed by .log as filename. You may also want to create a logrotate config in /etc/logrotate.d/docker-container:

/var/log/docker/*.log
{
    rotate 7
    daily
    missingok
    notifempty
    delaycompress
    compress
    postrotate
        reload rsyslog >/dev/null 2>&1 || true
    endscript
}

How can I configure seperate logfiles per category with rsyslog?

Create another rsyslog configuration in /etc/rsyslog.d/25-docker-yii2.conf, where you define the criteria for your logfiles:

$template LogTemplate,"%msg%\n"

$template LogFileComponents,"/var/log/docker/%syslogtag:R,ERE,1,DFLT:docker/(.*)\[--end%_components.log"
if $syslogtag startswith "docker/" and ($msg contains "][trace][" or $msg contains "][info][") and $msg contains "][app\\components\\" then ?LogFileComponents;LogTemplate

# Admin
$template LogFileAdmin,"/var/log/docker/%syslogtag:R,ERE,1,DFLT:docker/(.*)\[--end%_admin.log"
if $syslogtag startswith "docker/" and $msg contains "][app\\modules\\admin" then ?LogFileAdmin;LogTemplate

How can I use Yii's log routes?

If you log to the default directory, you should find the logfiles in your local runtime/logs/ directory. Note, that this directory will never be copied into the container, so there's no risk, that you end up with a bloated image.

How Can I Add PHP / HHVM Extensions?

Since the codemix/yii2-base image extends from the official php image, you can use docker-php-ext-install in your Dockerfile. Here's an example:

RUN apt-get update \
    && apt-get -y install \
            libfreetype6-dev \
            libjpeg62-turbo-dev \
            libmcrypt-dev \
            libpng12-dev \
        --no-install-recommends \
    && rm -r /var/lib/apt/lists/* \
    && docker-php-ext-install iconv mcrypt \
    && docker-php-ext-configure gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/ \
    && docker-php-ext-install gd

For HHVM extension please check the hhvm base image for details.

Why are there sometimes file permission issues when developing locally?

When you map a local volume into a container, the numeric owner ids are preserved. So a file, locally owned by the user with id 1001 will still have this numeric owner id inside the container. Since the webserver process in the container runs as user www-data and this user has id 1000 you must make sure, that all directories where Yii needs write access are owned by user 1000 locally - even if that user does not exist on the host!

sudo chown 1000 web/assets/ runtime/
# Alternatively from inside the container:
docker-compose run --rm web chown www-data web/assets/ runtime/

The same happens, if you create a file from inside the container, for example with yii migrate/create. In the container console commands are executed with root permissions. So any files in the shared volume will also have root permissions on your host.