Менеджер пакетов APT

APT от Advanced Package Tool

Starting in Docker version 17.05 multi-stage builds are available to help you optimize Dockerfiles. Using multi-stage builds is a clean way to set up a Docker build pipeline that simplifies the requirements you need on your CI/build server to create a Docker image for your app.

If you’re not familiar with multi-stage builds, no worries! Let me summarize by saying that before multi-stage builds if you wanted to install composer dependencies and frontend dependencies during a docker build of your app, your Docker image would need to have the necessary dependencies such as Node.js installed and Composer PHP.

The resulting Dockerfile and image can be fairly messy and frustrating to maintain.

Another approach that I’ve taken in my build environment is installing composer and node dependencies on the server running the build, and then copying the artifacts such as the vendor/ folder and production-ready JS and CSS build files.

A simplified version of building dependencies outside of Docker and copying them it might look something like the following:

#!/usr/bin/env bash

set -e

tag=${1:-latest}

composer install \
    --ignore-platform-reqs \
  --no-interaction \
  --no-plugins \
  --no-scripts \
  --prefer-dist

yarn install && yarn production

docker build -t my-app:$tag .

The above script is a simplified version to illustrate how you’d prepare the PHP and frontend dependencies and the docker build would copy the project into the image.

By using multi-stage builds, you no longer need to install those dependencies or cram them all into your Dockerfile, thus bloating your final image size!

Multi-Stage Approach

Using a Laravel 5.6 application as an example, we can start building the dependencies for production-ready images in one Dockerfile that contains multiple stages.

Поиск пакетов.

sudo apt search package

Установка пакетов.

If you’re following along, create a demo project from the command line with the following:

sudo apt install package

Next, create a .dockerignore file in the root of your Laravel app with the following:

.git/
vendor/
node_modules/
public/js/
public/css/
yarn-error.log

We aren’t going to go too deeply into how the .dockerignore file works, but the important part to understand is that it prevents copying the defined paths during a docker build. The ignore file means that the vendor/ folder won’t get copied in during a COPY or ADD instruction in the Dockerfile:

# copies the Laravel app, but skips the ignored files and paths
COPY . /var/www/html 

For example, ignoring the vendor/ folder means that we get a pristine build of the composer dependencies from our build.

2018-07-02