I want to set ENV only when ARG exist.
What I want to do like:
FROM ruby:3.0.1
ARG RAILS_MASTER_KEY
if [[ -n "$RAILS_MASTER_KEY" ]] ;
then ENV RAILS_MASTER_KEY=$RAILS_MASTER_KEY
fi
Environments;
- ruby on rails using credentials
- credentials/development.yml.enc
- development.key
- CircleCI context for secret env
- Dockerfile.local for local
I have 3 repositories for one service.
- web(front-end)
- app(back-end)
- api
And each repository has docker-compose.yml and it build own image by Dockerfile and pull other repository's image from ECR.
When merged to main branch, CircleCI build the image by Dockerfile and push to ECR.
Because I'm managing my secret key by CircleCI context,
so I pass the key by --build-arg
when CircleCI build the image, and I don't need the ENV when I build the repository locally because I have key files in local.
Here is what I tried, but the ENV is still set with empty value like RAILS_MASTER_KEY=
. Because of this problem, my rails container doesn't up when docker-compose up.
Dockerfile before
FROM ruby:3.0.1
ARG RAILS_MASTER_KEY
ENV RAILS_MASTER_KEY=$RAILS_MASTER_KEY
what i tried:
- not work
FROM ruby:3.0.1
ARG RAILS_MASTER_KEY
RUN if [[ -n "$RAILS_MASTER_KEY" ]] ; then export RAILS_MASTER_KEY=$RAILS_MASTER_KEY; fi
- not work
FROM ruby:3.0.1
ARG RAILS_MASTER_KEY
ENV RAILS_MASTER_KEY=$RAILS_MASTER_KEY
RUN if [[ -z "$RAILS_MASTER_KEY" ]] ; then unset RAILS_MASTER_KEY; fi
Below is CircleCi config file FYI.
...
commands:
# image-build-push for k8s
build-push-image:
steps:
- checkout
- setup_remote_docker:
docker_layer_caching: true
- aws-ecr/build-and-push-image:
dockerfile: ${DOCKER_FILE_PATH}
no-output-timeout: 20m
region: AWS_DEFAULT_REGION
repo: ${ECR_REPO_NAME}
tag: ${IMAGE_TAG}
extra-build-args: --build-arg RAILS_MASTER_KEY=${RAILS_MASTER_KEY}
...