Docker-compose: How to share data between services without using named volumes or multi-stage builds

廉价感情. 提交于 2020-01-24 19:31:08

问题


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

回答1:


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"


来源:https://stackoverflow.com/questions/59825885/docker-compose-how-to-share-data-between-services-without-using-named-volumes-o

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!