Permalink
77 lines (49 sloc) 2.64 KB

Usage

Packages

Replacing your current base image with the Docker Alpine Linux image usually requires updating the package names to the corresponding ones in the Alpine Linux package index. We use the apk command to manage packages. It works similar to apt or yum.

An example installing the nginx package would be apk add --update nginx. The --update flag fetches the current package index before adding the package. We don't ship the image with a package index (since that can go stale fairly quickly).

Example

Here is a full example Dockerfile that installs the Python runtime and some build dependencies:

FROM gliderlabs/alpine:3.3

RUN apk add --update \
    python \
    python-dev \
    py-pip \
    build-base \
  && pip install virtualenv \
  && rm -rf /var/cache/apk/*

WORKDIR /app

ONBUILD COPY . /app
ONBUILD RUN virtualenv /env && /env/bin/pip install -r /app/requirements.txt

EXPOSE 8080
CMD ["/env/bin/python", "main.py"]

Disabling Cache

As of Alpine Linux 3.3 there exists a new --no-cache option for apk. It allows users to install packages with an index that is updated and used on-the-fly and not cached locally:

FROM gliderlabs/alpine:3.3

RUN apk --no-cache add nginx

EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]

This avoids the need to use --update and remove /var/cache/apk/* when done installing packages.

Convenience Cleanup

The gliderlabs variant of this image contains a small unofficial wrapper script that assists in the cleanup of the package index after installing packages. However, this functionality is now available in the upstream apk utility as of Alpine version 3.2 (using apk --no-cache). This script may be removed from the gliderlabs/alpine images in the future.

Virtual Packages

Another great apk add feature for cleanup is the concept of virtual packages using the --virtual or -t switch. Packages added under this virtual name can then be removed as one group. An example use of this would be removing a group of build dependencies all at once:

FROM gliderlabs/alpine:3.3

WORKDIR /myapp
COPY . /myapp

RUN apk --update add python py-pip openssl ca-certificates
RUN apk --update add --virtual build-dependencies python-dev build-base wget \
  && pip install -r requirements.txt \
  && python setup.py install \
  && apk del build-dependencies

CMD ["myapp", "start"]

Additional Information

Check out the Alpine Linux package management documentation for more information and usage of apk.