以AWS EC2上的ec2用户身份运行Docker容器

我正在AWS EC2上运行docker,并希望以用户ec2-user身份启动mkdir afolder容器,以便使用命令mkdir afolder在容器内创build的任何文件夹都将归ec2-user

我将user: ec2-user添加到docker-compose.yml但是docker-compose.yml拒绝启动并给出错误:

Cannot start service web: linux spec user: unable to find user ec2-user: no matching entries in passwd file

这是因为容器没有用户ec2-user 。 我不想在构build时在Dockerfile中创buildec2-user ,这意味着在部署到差异服务器时,必须修改Dockerfile。

有什么更好的方法来解决这个问题?

PS:我的Dockerfile和docker-compose.yml设置正确,所以在运行docker docker-compose up -d容器可以像预期的那样启动。

我的Dockerfile

 FROM codemix/yii2-base:2.0.12-apache #FROM codemix/yii2-base:2.0.11.2-php7-fpm #FROM codemix/yii2-base:2.0.11.2-hhvm # Copy the Yii2 specific config apache config COPY apache2.conf /etc/apache2/apache2.conf # PHP configuration COPY php.ini /usr/local/etc/php/php.ini # Composer packages are installed first. This will only add packages # that are not already in the yii2-base image. COPY composer.json /var/www/html/ COPY composer.lock /var/www/html/ RUN composer self-update --no-progress && \ composer install --no-progress # Copy the working dir to the image's web root COPY . /var/www/html # The following directories are .dockerignored to not pollute the docker images # with local logs and published assets from development. So we need to create # empty dirs and set right permissions inside the container. RUN mkdir -p runtime frontend/web/assets backend/web/assets \ && chown www-data:www-data runtime frontend/web/assets backend/web/assets # Expose everything under /var/www (vendor + html) # This is only required for the nginx setup VOLUME ["/var/www"] 

我的docker-compose.yml

 version: '2' services: web: container_name: vl build: context: . dockerfile: Dockerfile volumes: - ./:/var/www/html/ links: - db # For Apache based image: ports: - "8080:80" db: image: mysql:5.6 ports: - "8081:3306" expose: - "3306" environment: MYSQL_ROOT_PASSWORD: secret-root MYSQL_DATABASE: web MYSQL_USER: web MYSQL_PASSWORD: web volumes: - mysql_data:/var/lib/mysql volumes: mysql_data: # Autostart at boottime #restart: always 

为了使Web服务器在文件夹下mkdir,这些文件夹需要由www-data用户拥有。 在php初始化脚本中,使用chownchgrp更改文件夹权限。