【问题标题】:Run docker container as ec2-user on AWS EC2在 AWS EC2 上以 ec2-user 身份运行 docker 容器
【发布时间】:2018-01-23 21:05:27
【问题描述】:

我在 AWS EC2 上运行 docker,并希望以用户 ec2-user 启动 docker 容器,因此使用命令 mkdir afolder 在容器内创建的任何文件夹都将归 ec2-user 所有。

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

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

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

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

PS:我的 Dockerfile 和 docker-compose.yml 设置正确,因此在运行 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

【问题讨论】:

  • 您是否将用户 ec2-user 添加到您的 ec2 节点上的 DOCKER 组?
  • @Dere0405 是的。 ec2-user 属于以下组:ec2-user wheel docker
  • 并确保在将 ec2-user 添加到 docker 组后重新启动
  • @Dere0405 是的。我认为 ec2-user 的组很好,因为我可以运行 docker-compose up -d 没问题。
  • 现在我明白了,您的 Web 服务期望由容器用户 ec2-user 运行,启动脚本是这样硬编码的吗?

标签: docker amazon-ec2


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    您应该在容器中创建一个名为 ec2-user 的用户。 在 docker 文件中应该添加这样的命令

    RUN groupadd -g 1000 ec2-user && \    
        useradd -u 1000 -g ec2-user -m ec2-user -G docker_env && \   
        usermod -p "*" ec2-user
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-09-07
      • 2019-04-01
      • 2021-11-01
      • 2017-11-19
      • 2017-12-17
      • 2019-02-08
      • 2019-12-05
      • 2020-01-13
      相关资源
      最近更新 更多