What is the workflow to build with docker AND docker-compose?

7/19/2018

I have this repo, and docker-compose up will launch the project, create 2 containers (a DB and API), and everything works.

Now I want to build and deploy to Kubernetes. I try docker-compose build but it complains there's no Dockerfile. So I start writing a Dockerfile and then discover that docker/Dockerfiles don't support loading ENV vars from an env_file or .env file. What gives? How am I expected to build this image? Could somebody please enlighten me?

What is the intended workflow for building a docker image with the appropriate environment variables?

-- rm.rf.etc
docker
docker-compose
kubernetes

2 Answers

7/19/2018

I've updated the project on github, it now all works, and the readme documents how to run it.

I realized that env vars are considered runtime vars, which is why --env-file is an option for docker run and not docker build. This must also (I assume) be why docker-compose.yml has the env_file option, which I assume just passes the file to docker build. And in Kubernetes, I think these are passed in from a configmap. This is done so the image remains more portable; same project can be run with different vars passed in, no rebuild required.

Thanks ignacio-millán for the input.

-- rm.rf.etc
Source: StackOverflow

7/19/2018

Those environment variables shouldn't be set at docker build step but at running the application on Kubernetes or docker-compose.

So:

Write a Dockerfile and place it at root folder. Something like this:

FROM node
COPY package.json .
RUN npm install
COPY . .
ENTRYPOINT ["npm", "start"]

Modify docker-compose.yaml. In the image field you must specify the name for the image to be built. It should be something like this:

image: YOUR-DOCKERHUB-USERNAME/node-rest-auth-arangodb

There is no need to set user and working_dir

Build the image with docker-compose build (you can also do this with docker build)

Now you can use docker-compose up to run your app locally, with the .env file

To deploy it on Kubernetes you need to publish your image in dockerhub (unless you run Kubernetes locally):

docker push YOUR-DOCKERHUB-USERNAME/node-rest-auth-arangodb

Finally, create a Kubernetes manifest. Sadly kubernetes doesn't support env files as docker-compose do, you'll need to manually set these variables in the manifest:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: platform-api
  labels:
    app: platform-api
spec:
  replicas: 1
  selector:
    matchLabels:
      app: platform-api
  template:
    metadata:
      labels:
        app: platform-api
    spec:
      containers:
      - name: platform-api
        image: YOUR-DOCKERHUB-USERNAME/node-rest-auth-arangodb
        ports:
        - containerPort: 8080
        env:
          - name: NODE_ENV
            value: develop

---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: platform-db
  labels:
    app: platform-db
spec:
  replicas: 1
  selector:
    matchLabels:
      app: platform-db
  template:
    metadata:
      labels:
        app: platform-db
    spec:
      containers:
      - name: arangodb
        image: YOUR-DOCKERHUB-USERNAME/node-rest-auth-arangodb
        ports:
        - containerPort: 8529
        env:
          - name: ARANGO_ROOT_PASSWORD
            value: localhost

Deploy it with kubectl create

Please note that this code is just indicative, I don't know exactly your user case. Find more information in docker-compose and kubernetes docs and tutorials. Good luck!

-- Ignacio Millán
Source: StackOverflow