Single Dockerfile for testing and production using multi-stage builds
Tags: docker, multistage, dockerfile, python
Published: 2020-03-27 12:17:13

---
title: Single Dockerfile for testing and production using multi-stage builds
published: true
date: 2020-03-27 11:11:07 UTC
tags: docker,multi-stage,dockerfile,python
canonical_url: https://suda.pl/single-dockerfile-for-testing-and-production/
---

 ![Single Dockerfile for testing and production using multi-stage builds](https://images.unsplash.com/photo-1531206715517-5c0ba140b2b8?ixlib=rb-1.2.1&q=80&fm=jpg&crop=entropy&cs=tinysrgb&w=1080&fit=max&ixid=eyJhcHBfaWQiOjExNzczfQ)

In different contexts like development, running tests or serving production, our apps have different needs when it comes to their Docker images. For development, you might want a live-reload server with all necessary dependencies. For testing you probably need some more packages that you **definitely** don't want on production.

There are ways to go about it, the first one that pops in mind is using multiple Dockerfiles and just telling Docker which one to use. But this approach has its limits, especially along the road, where each file can very easily develop its own life, diverging from each other beyond recognition.

Fortunately since Docker 17.05 we have the concept of [multi-stage builds](https://docs.docker.com/develop/develop-images/multistage-build/#use-a-previous-stage-as-a-new-stage). An ability to declare separate images in one `Dockerfile` and freely inherit or copy data between each other. This is the exact approach I used when building images for [Lox](https://lox.sh/?source=dev.to):

```Dockerfile
#############################################
# Base container with all necessary deps
FROM tiangolo/uvicorn-gunicorn:python3.7-alpine3.8 AS base

ENV HOME=/app 
    BUILD_DEPS="build-base linux-headers postgresql postgresql-dev libffi-dev"
WORKDIR ${HOME}

# Copy the pipenv files
COPY Pipfile ${WORKDIR}/
COPY Pipfile.lock ${WORKDIR}/

# 1. Install system dependencies
# 2. Install pipenv
# 3. Use pipenv to install app deps
# 4. Remove system deps to save space
RUN apk add --no-cache ${BUILD_DEPS} \
    && pip install --no-cache-dir pipenv \
    && pipenv install --system \
    && apk del ${BUILD_DEPS}

#############################################
# Test container from a common base
FROM base AS test
# Same as in the base image but this time we also install the --dev packages
RUN apk add --no-cache ${BUILD_DEPS} \
    && pip install --no-cache-dir pipenv \
    && pipenv install --system --dev \
    && apk del ${BUILD_DEPS}

#############################################
# Live container with Webpack watcher
FROM base AS live
# Install Node.js dependencies
RUN apk add --no-cache nodejs npm
# Install all Webpack dependencies
COPY package.json package-lock.json ./
RUN npm install
# Finally copy the current sources and build the bundle
COPY . .
RUN npm run bundle:build

#############################################
# Final container with the app
FROM base AS production
# Only copy the ready app dir from the live step
COPY --from=live ${WORKDIR} ${WORKDIR}
```

**Note:** for the sake of brevity, this `Dockerfile` doesn't contain all size improvements and will produce slightly bigger images.

Now you're able to build separate images for each of your needs:

### Testing

```sh
$ docker build -t suda/lox/test --target test .
```

### Development w/ live-reload

```sh
$ docker build -t suda/lox/live --target live .
```

### Production

```sh
$ docker build -t suda/lox --target production .
```

**P.S.:** You might've noticed I used the great Python base image from [Sebastián Ramírez](https://github.com/tiangolo) and if you want to deploy a Django project using ASGI, [I have some instructions that can help you](https://suda.pl/deploying-django-3-asgi/).

<!--kg-card-end: markdown-->