I
I
Igor2019-09-10 22:14:01
Docker
Igor, 2019-09-10 22:14:01

Condition in Dockerfile?

Good evening dear colleagues!
I plan to divide the project into dev and prod
After reading a little, in google.
I liked the article
To do this, I created 2
docker-compose.yml files
docker-compose.dev.yml
Two files with exactly the same configuration, except for some parameters
Project stack
----------------- ----------------------
Database - MySQL percona
Front - Nuxt x2 (admin + website)
Back - php framework
nginx x3 (for admin, website and back end)
phpmyadmin
The project is already configured!
And I already like the beginning.
But I would not want to produce a Dockerfile!

FROM node:lts-alpine

RUN mkdir -p /nuxt-app/
WORKDIR /nuxt-app/

COPY ./package*.json /nuxt-app/
VOLUME . .

RUN npm install
RUN npm run build  # При определённых условиях я не хочу что бы эта строчка выполнялась

CMD ["npm", "start"] # Вот эту строку планирую вынести наружу. dev or start

Question!
Can conditions be met in a Dockerfile?
For example:
docker-compose.dev.yml
# Nuxt publication
  nuxt-public:
    container_name: gorodbrand.nuxt_public
    restart: always
    build: ./nuxt_public
    ports:
      - 3200:3000
    expose:
      - 3000
    environment:
      - ENV_MODE=""
...

docker-compose.yml
# Nuxt publication
  nuxt-public:
    container_name: gorodbrand.nuxt_public
    restart: always
    build: ./nuxt_public
    ports:
      - 3200:3000
    expose:
      - 3000
    environment:
      - ENV_MODE="RUN npm run build"
...

FROM node:lts-alpine
...
RUN npm install
ENV_MODE
...

Or what are the practices of separation?
Share your experience.
Thank you!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
dimagusarov1, 2019-09-12
@dimagusarov1

you can use arguments in dockerfile
https://docs.docker.com/engine/reference/builder/#arg
1 option

ARG BUILD_VERSION=1
FROM alpine AS base
RUN touch base
FROM base AS build-1
RUN touch build-1
FROM base AS build-2
RUN touch build-2
FROM build-${BUILD_VERSION}
RUN touch finish

docker build --build-arg BUILD_VERSION=2 .

Option 2
ARG RAILS_ENV=development
RUN if [ "$RAILS_ENV" = "production" ]; then \
    bundle install --without development test; \
    else \
    bundle install; \
    fi

docker build --build-arg RAILS_ENV=production .

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question