使用docker-compose时如何使用容器内的主机用户修改卷文件

时间:2017-10-09 12:35:35

标签: docker

我有一个由docker-compose管理的docker容器,它在启动时会运行一个构建脚本,将一堆文件添加到映射到主机文件夹的卷上,因为容器始终以root身份运行,它不断创建文件在父文件夹中,如何设置docker-compose,以便创建的文件与在主机文件系统上运行docker-compose的用户属于同一用户?

docker-compose file

version: "2"
services:

  # Data API and authentication/authorization
  api:
    build:
      context: ../api/
      dockerfile: Dockerfile.dev
    hostname: api
    depends_on:
      - db
      - redis
    environment:
      - CORS_ORIGIN=http://localhost:3000,http://localhost:3001
      - DATABASE_URL=postgres://postgres@db:5432/dev
      - DATABASE_DEBUG=false
      - REDIS_URL=redis://redis:6379/0
      - SESSION_SECRET=wZjwhFtzQsd7r87W6AZw45Sm
      - FACEBOOK_ID=1821424564802638
      - FACEBOOK_SECRET=2339bdf25f236a42fc3a18280bf455e8
      - GOOGLE_ID=xxxxx.apps.googleusercontent.com
      - GOOGLE_SECRET=xxxxx
      - TWITTER_KEY=xxxxx
      - TWITTER_SECRET=xxxxx
    ports:
      - "8080:8080"
      - "127.0.0.1:9229:9229" # V8 inspector for tools/run.js
      - "127.0.0.1:9230:9230" # V8 inspector for src/server.js
    volumes:
      - yarn:/home/node/.cache/yarn
      - ../api/:/usr/src/app
    command: node tools/run.js # or, `node --inspect=0.0.0.0:9229 tools/run.js`

  # SQL and document data store
  db:
    image: postgres:9.6.5-alpine
    read_only: true
    tmpfs:
      - /tmp
      - /var/run/postgresql
    volumes:
      - db:/var/lib/postgresql/data
      - ./postgres-initdb.sh:/docker-entrypoint-initdb.d/initdb.sh
    # ports:
    #   - "127.0.0.1:5432:5432" # you can override it via docker-compose.override.yml

  # Distributed in-memory cache
  redis:
    image: redis:4.0.2-alpine
    read_only: true
    volumes:
      - redis:/data

volumes:
  db:
  redis:
  yarn:

api目录下的dockerfile.dev

FROM node:8.6.0-alpine

# Set a working directory
WORKDIR /usr/src/app

# If you have native dependencies, you'll need extra tools
RUN apk add --no-cache make g++ python2 libsodium-dev && \
  npm install -g node-gyp && \
  mkdir -p /home/node/.cache/yarn && \
  chown -R node:node /home/node/.cache/yarn && \
  chmod 777 /home/node/.cache/yarn

VOLUME /home/node/.cache/yarn

1 个答案:

答案 0 :(得分:2)

没有直接的方法可以做到这一点,但你可以在一个内部创建一个用户 容器与主机用户具有相同的用户和组ID。你可以这样做 在docker-compose build期间使用构建参数。

在dockerfile中

ARG UID
ARG GID

RUN groupadd -g $GID %group_name% && useradd -u $UID -g $GID --create-home -s /bin/bash %user_name%

USER %user_name%

在docker-compose.yml

version: "2"
services:

  api:
    build:
      context: ../api/
      dockerfile: Dockerfile.dev
      args:
        UID: %your uid%
        GID: %you gid%

更新1(自动化)

为了让这个过程对于多个用户来说更容易,你可以自动化它 位。

在docker-compose.yml中

你可以像这样使用variable substitution

version: "2"
services:

  api:
    build:
      context: ../api/
      dockerfile: Dockerfile.dev
        args:
          UID: ${UID_VAR}
          GID: ${GID_VAR}

比你可以编写脚本,它在repo中生成.env文件   特定用户(假设它的名称是init.sh):

echo "UID_VAR=`id -u $USER`" > .env && echo "GID_VAR=`id -g $USER`" >> .env

因此,您的每个用户都会执行以下操作:

git clone repo
cd repo
./init.sh # generates .env
docker-compose build # earch user is have to build container because of GID and UID
docker-compose up