Axiol
Axiol

Reputation: 5962

Create a file in a Docker image during a Gitlab build

I have a Gitlab CI where it build a Docker image, push it the Gitlab Registry and then pull and start it on a server.

Problem is my application needs a .env file. As I don't want to commit this file, I thought I'd create it during the build. But I can't manage to do it.

Here's what I already tried :

1 - Setting all the different variables as Gitlab environment variables and using a bash script to create the .env file with the one stored in Gitlab

deploy_staging:
  stage: deploy
  before_script:
    - chmod +x ./scripts/setup_env.sh
    - ./scripts/setup_env.sh
#!/bin/bash

echo NEXT_PUBLIC_GOOGLE_MAP_API_KEY=$NEXT_PUBLIC_GOOGLE_MAP_API_KEY >> .env
echo NEXT_PUBLIC_APOLLO_BROWSER_URL=$NEXT_PUBLIC_APOLLO_BROWSER_URL >> .env
echo NEXT_PUBLIC_APOLLO_SERVER_URL=$NEXT_PUBLIC_APOLLO_SERVER_URL >> .env
echo NEXT_PUBLIC_APOLLO_PROD_URL=$NEXT_PUBLIC_APOLLO_PROD_URL >> .env

2 - Storing the whole .env as a file variable on Gitlab and copying it directly

deploy_staging:
  stage: deploy
  before_script:
    - echo "$ENV_FILE" > ./.env

But with either solution, same result: the .env file is nowhere to be seen. Even if in the build logs, all seems fine.

Any idea what I'm missing?

Upvotes: 0

Views: 767

Answers (1)

leoschet
leoschet

Reputation: 1866

If you want a file to be in your docker image, your Dockerfile needs to copy that file from the context to the image it is building. That said since your file is actually a .env I don't think it makes sense to have it inside the image.

When using the Docker executor from GitLab, every job is run in a new container. Meaning that unless you have a volume map between your host and the actual runner, the env file will not persist.

Our usual setup for docker deployments is to have a folder say /opt/project which is mapped to the runner. The deployment job writes the .env in this folder. The .env file is then used by the container.

As a side note, you can use CI secrets set in GitLab's UI to add secrets to the .env file. If for any reason you don't want to use CI secrets, you can manually setup the env file directly in the server.

Upvotes: 1

Related Questions