0

Are there any ways to share data between containers. There is following docker-compose file

version: '3'
services:
    app_build_prod:
        container_name: 'app'
        build:
            context: ../
            dockerfile: docker/Dockerfile
            args:
                command: build:prod
    nginx:
        container_name: 'nginx'
        image: nginx:alpine
        ports:
            - "80:80"
        depends_on:
            - app_build_prod

Dockerfile content is:

FROM node:10-alpine as builder

## Installing missing packages, fixing git self signed certificate issue
RUN apk update && apk upgrade && \
    apk add --no-cache bash git openssh && \
    rm -rf /var/cache/apk/* && \
    git config --global http.sslVerify false

## Defigning app directory
WORKDIR /usr/app

## Copying files. Files listed in .dockerignore are omitted
COPY . .

## node_modules are on a separate intermediate image will prevent unnecessary npm installs at each build
RUN npm ci

## Declaring arguments and environment variables. Important to declara env var to consume them on run stage
ARG command=build:prod
ENV command=$command
ENTRYPOINT npm run ${command}

Tried with @Robert's solution, but couldn't make it work - app container crashes because of:

EBUSY: resource busy or locked, rmdir '/usr/app/dist
Error: EBUSY: resource busy or locked, rmdir '/usr/app/dist'

My assumption is that /usr/app/dist directory is mounted with read-only access, therefore when Angular attempt to remove it prior the build, it throws an error.

Need to send data following direction

app_build_prod:/usr/app/dist => nginx:/usr/share/nginx/html
Timothy
  • 3,213
  • 2
  • 21
  • 34
  • Does this answer your question? [Docker Compose - Share named volume between multiple containers](https://stackoverflow.com/questions/44284484/docker-compose-share-named-volume-between-multiple-containers) – Sebastian Brosch Jan 21 '20 at 08:56
  • Nope. @Robert's approach crashes container. – Timothy Jan 21 '20 at 10:09
  • Why do you say it can't be a multi-stage build? That's the right Docker pattern for this. – David Maze Jan 21 '20 at 10:50
  • @DavidMaze Dockerfile is just a builder. And nginx is conditional. For example we need to run nginx container only if app runs in `build:prod` mode. Furthermore there are gonna be 15 other docker-compose services (which passes different container build scrips: `test`, `track`, `watch`, etc...) – Timothy Jan 21 '20 at 11:06

1 Answers1

0

I have the same problem and change the sharing to use multi-stage build :

FROM alpine:latest AS builder
...build app_build_prod 

FROM nginx:alpine  
COPY --from=builder /usr/app/dist /usr/share/nginx/html

and change docker-compose to:

version: '3'
services:
    nginx:
        container_name: 'nginx'
        build:
             ...
        ports:
            - "80:80"
Farhoud
  • 91
  • 6
  • 1
    Thank you mate. I used to achieve this by using multi-stage builds, but I can't use it in this particular case, I need to find a way to use docker-compose only! – Timothy Jan 21 '20 at 09:11
  • Can I ask Why app container crashes? I think it most be permission error check app_build_prod user – Farhoud Jan 21 '20 at 09:19
  • It crashes because of "EBUSY: resource busy or locked". Updated the question – Timothy Jan 21 '20 at 09:55