How to deploy a Docker image and the environement variables in a virtual machine?

Viewed 37

I created two images with docker-compose in my laptop and I pushed them in a private repo. Now, I would like to deploy the images in a virtual machine so I did docker pull to fetch them and docker run -d to run the containers. Problem is that a container can't start because the .env file with all the variables isn't found.

/entrypoint: line 12: STRATEGY_DB_NAME: unbound variable
Waiting for PostgreSQL to become available...

This file is present in my laptop in ./strategy/.env and Im wondering where I'm supposed to copy this file in the virtual machine.

I'm new with deployment and it's not clear if I need to upload all my project files in the virtual machine with git push and then docker run the Docker image. I assume it's not necessary because these files are embeded in the Docker image.

My question is why the .env file can't be found and where am'I supposed to copy it ?

docker-compose.yml

version: '3.8'

services:
  django:
    build:
      context: .
      dockerfile: ./compose/local/django/Dockerfile
    image: strategy
    command: /start
    volumes:
      - .:/app
    ports:
      - "8004:8004"
    env_file:
      - strategy/.env                        <--------------- file with secrets
    depends_on:
      - redis-4
    networks:
      - mynetwork

Dockerfile

FROM python:3.10

ENV PYTHONUNBUFFERED 1
ENV PYTHONDONTWRITEBYTECODE 1
WORKDIR /app

RUN apt-get update \
  && apt-get install -y gcc build-essential \
  && apt-get install -y libpq-dev \
  && apt-get install -y gettext \
  && apt-get install -y git \
  && apt-get install -y openssh-client \
  && apt-get install -y libcurl4-openssl-dev libssl-dev \
  && apt-get install -y build-essential \
  && apt-get install -y libpq-dev \
  && apt-get install -y procps telnet \
  && apt-get install -y nano \
  && apt-get install -y postgresql-client \
  && apt-get purge -y --auto-remove -o APT::AutoRemove::RecommendsImportant=false \
  && rm -rf /var/lib/apt/lists/*

RUN mkdir logs \
    && touch logs\flat_line.log \
    && touch logs\json.log

# Requirements are installed here to ensure they will be cached.
COPY ./requirements.txt /requirements.txt
RUN pip install -r /requirements.txt
RUN pip install ccxt --upgrade \
  && pip install numpy --upgrade \
  && pip install psycopg2 --upgrade

COPY ./compose/local/django/entrypoint /entrypoint
RUN chmod +x /entrypoint

COPY ./compose/local/django/start /start
RUN chmod +x /start

COPY ./compose/local/django/celery/worker/start /start-celeryworker
RUN chmod +x /start-celeryworker

COPY ./compose/local/django/celery/beat/start /start-celerybeat
RUN chmod +x /start-celerybeat

COPY ./compose/local/django/celery/flower/start /start-flower
RUN chmod +x /start-flower

RUN mkdir /app/strategy
COPY ssl /app/strategy

ENTRYPOINT ["/entrypoint"]

entrypoint

#!/bin/bash

# if any of the commands in your code fails for any reason, the entire script fails
# fail exit if one of your pipe command fails
# exits if any of your variables is not set

set -o errexit
set -o pipefail
set -o nounset

postgres_ready() {
python << END
import sys

import psycopg2

try:
    psycopg2.connect(
        dbname="${STRATEGY_DB_NAME}",
        user="${STRATEGY_DB_USER}",
        password="${STRATEGY_DB_PASSWORD}",
        host="${STRATEGY_DB_HOST}",
        port="${STRATEGY_DB_PORT}",
        sslmode="require",
    )
except psycopg2.OperationalError:
    sys.exit(-1)
sys.exit(0)

END
}
until postgres_ready; do
  >&2 echo 'Waiting for PostgreSQL to become available...'
  sleep 1
done
>&2 echo 'PostgreSQL is available'

exec "$@"
1 Answers

Supposing you want to run a container from your image with docker-compose, you need to specifiy your environnement file with env_file:

#docker-compose.yml
version: '3.8'

services:
  container_name:
    image: imagee:1.0
    ...
    env_file: .env
...

As with Docker, you can add argument --env-file, such as:

docker run image:1.0 --env-file .env
Related