Code Spirit
Code Spirit

Reputation: 5071

docker-compose useradd tells me user already exists

I am using Docker on Windows with the following docker file:

FROM php:7.4-fpm

ARG uid=1000
ARG user=devuser

USER root
# Install system packages
RUN apt-get update && apt-get install -y apt-transport-https && apt-get install -y git curl zip unzip libicu-dev

# Cleanup apt
RUN apt-get clean && rm -rf /var/lib/apt/lists/*

# Install php extensions
RUN docker-php-ext-install pdo_mysql intl iconv

RUN useradd -G www-data,root -u $uid -d /home/$user $user

RUN mkdir -p /home/$user/.composer && chown -R $user:$user /home/$user

# Install composer
COPY --from=composer:latest /usr/bin/composer /usr/local/bin/composer

# Copy project files to image
COPY . /var/www

# Set working directory
WORKDIR /var/www

RUN composer install

# Change user
USER $user

docker-compose.yaml

version: "3.7"

services:

  app:
    container_name: app
    build:
      context: ./
      dockerfile: Dockerfile
    image: php:7.4-fpm
    ports:
      - 9000:9000
    working_dir: /var/www/
    volumes:
      - ./:/var/www
    networks:
      - dev
    depends_on:
      - database


  server:
    container_name: server
    image: nginx:1.21.0-alpine
    ports:
      - 8000:80
    volumes:
      - ./:/var/www
      - ./docker/nginx:/etc/nginx/conf.d
    networks:
      - dev


  database:
    container_name: database
    image: mariadb:10-bionic
    environment:
      MYSQL_ROOT_PASSWORD: root
    networks:
      - dev
    volumes:
      - database-data:/var/lib/mysql

volumes:
  database-data:
networks:
  dev:
    external: true

It worked on the first run but when I wanted to make changes I get the below error. I have already tried stopping, killing, downing all containers but nothing works.

When I run docker-compose up --force-recreate --build -d I get the following output:

 => ERROR [stage-0  5/10] RUN useradd -G www-data,root -u 1000 -d /home/devuser devuser                                                                                  0.4s
------
 > [stage-0  5/10] RUN useradd -G www-data,root -u 1000 -d /home/devuser devuser:
#8 0.354 useradd: user 'devuser' already exists
------
executor failed running [/bin/sh -c useradd -G www-data,root -u $uid -d /home/$user $user]: exit code: 9
ERROR: Service 'app' failed to build : Build failed

Why does this happen? Arent containers encapsulated? How does it conflict when setting up a new environment? I have stoped all running containers

EDIT: When I change uid and user to, for example, 1001 and devuser1 it works again once.

Upvotes: 1

Views: 4592

Answers (1)

BMitch
BMitch

Reputation: 265045

You've named the image you are building the same as your base image:

image: php:7.4-fpm

That means every time you run a build you add more layers and mutate from the last build rather than recreating from the original base image.

The fix is to name your image something unique to you, rather than stepping on the upstream library image name.

image: <your_hub_user>/app:latest

And then pull the upstream library image again to revert it to a known clean state:

docker pull php:7.4-fpm

Upvotes: 9

Related Questions