Build saleor dashboard docker image that accept environment variables

Build saleor dashboard docker image that accept environment variables

This hack is no longer needed as it is implemented in the official docker images, in this PR

Saleor as mentioned in its landing page is

An open-source, GraphQL-first e-commerce platform delivering ultra-fast, dynamic and personalized shopping experiences.

Saleor is built as separate components

  • saleor-core which is the graphql api
  • saleor-dashboard which is the amdin area for the app
  • saleor-storefront which is the user facing part of the ecommerce platform

Usually, we as developers use the first 2 components as provided by the saleor team and build a custom storefront on top of it

The problem

The saleor-dashboard is a react app that is build as a static site in a docker container but to do that we have to provide the API_URI at the build time so for example if you have 4 environment development, stagging, test and production then you will need to build 4 different versions of the dashboard

i looked a round to see if someone else asked the same question and i found this issue #1592

My solution

The only answer on the above mentioned issue suggested that modifying the config.ts and index.html file would do the trick, so i will explain how you can do it manually, then feel free to automate the process

Get the dashboard code

clone the repo and checkout the version you need to build

git clone --depth 1 --branch <ref>

Config file

we will create a bash script that read the $API_URI env variable and generate a config javascript file, this script will run every time before the docker container started

notice that the docker container will serve the dashboard from a folder called dashboard that is why the script will generate the env file in the same folder


touch $target_file
rm -rf $target_file

echo "window._env_ = {" >> $target_file
  echo "  API_URI: \"$API_URI\"," >> $target_file
echo "}" >> $target_file

The generated file should look like this

window._env_ = {
  API_URI: "<api-url-from-env>",

Modify src/index.html

Edit the main html file of the dashboard and add this script tag on the head

<!DOCTYPE html>
    <meta charset="UTF-8" />
      content="width=device-width, initial-scale=1.0, user-scalable=no"
    <meta name="robots" content="noindex" />
    <title>Saleor e-commerce</title>
    <script src="/dashboard/env-config.js"></script>
    <!-- add this line-->

    <div id="dashboard-app"></div>

Modify src/config.ts

now we will read the API_URI from the window object at the runtime instead of reading it from environment at build time

import packageInfo from "../package.json";
import { SearchVariables } from "./hooks/makeSearch";
import { ListSettings, ListViews, Pagination } from "./types";

export const APP_MOUNT_URI = process.env.APP_MOUNT_URI || "/";
export const APP_DEFAULT_URI = "/";
// remove this line
export const API_URI = process.env.API_URI;

// add these 2 lines, typescript build would fail without the @ts-ignore
// @ts-ignore
export const API_URI = window._env_.API_URI;

Modify Dockerfile

for the docker file we need to do 2 things

  1. copy the script into the docker image root

  2. change the command to run the before starting the container

FROM node:14 as builder
COPY package*.json ./
RUN npm install
COPY . .
ENV API_URI ${API_URI:-http://localhost:8000/graphql/}

FROM nginx:stable
COPY ./nginx/default.conf /etc/nginx/conf.d/default.conf
COPY --from=builder /app/build/ /app/
# Add the following lines
COPY ./ .
RUN chmod +x
CMD ["/bin/bash", "-c", "/app/ && nginx -g \"daemon off;\""]

No you can build saleor-dashboard image that take the API_URI from your deployment environment


For me to not repeat the above steps with every new version of the dashboard, i created a gitlab CI/CD pipeline to do this automatically for whatever version/tag/ref i specify but unfortunately this is something i can't share