How to add users to Docker container?

后端 未结 8 1225
无人及你
无人及你 2020-11-28 17:07

I have a docker container with some processes (uwsgi and celery) running inside. I want to create a celery user and a uwsgi user for these processes as well as a worker grou

8条回答
  •  一向
    一向 (楼主)
    2020-11-28 17:44

    Adding user in docker and running your app under that user is very good practice for security point of view. To do that I would recommend below steps:

    FROM node:10-alpine
    
    # Copy source to container
    RUN mkdir -p /usr/app/src
    
    # Copy source code
    COPY src /usr/app/src
    COPY package.json /usr/app
    COPY package-lock.json /usr/app
    
    WORKDIR /usr/app
    
    # Running npm install for production purpose will not run dev dependencies.
    RUN npm install -only=production    
    
    # Create a user group 'xyzgroup'
    RUN addgroup -S xyzgroup
    
    # Create a user 'appuser' under 'xyzgroup'
    RUN adduser -S -D -h /usr/app/src appuser xyzgroup
    
    # Chown all the files to the app user.
    RUN chown -R appuser:xyzgroup /usr/app
    
    # Switch to 'appuser'
    USER appuser
    
    # Open the mapped port
    EXPOSE 3000
    
    # Start the process
    CMD ["npm", "start"]
    

    Above steps is a full example of the copying NodeJS project files, creating a user group and user, assigning permissions to the user for the project folder, switching to the newly created user and running the app under that user.

提交回复
热议问题