当我想重建 docker 镜像时,它会产生一个错误

时间:2021-06-23 08:23:00

标签: docker docker-compose dockerfile

我制作了 docker 镜像并使用 docker-compose.yml 构建它们,第一次一切正常,但是当我在 Dockerfile 上添加/更新一些配置并重建镜像时,关于我正在创建的组或用户的错误图像内部生成:

> [ 2/11] RUN addgroup -g 1000 laravel && adduser -G laravel -g laravel -s /bin/sh -D laravel:
#5 0.396 addgroup: group 'laravel' in use

这里我将 laravel 添加为用户和组。

我唯一的解决方法是删除所有构建的图像:docker system prune -a

以下是 Docker 文件和 docker-compose.yml 文件配置:

.env 文件:

PROJECT_NAME=myproject
DEV_DOMAIN=docker.local

USERNAME=laravel
UID=1000

PHP_VERSION=7.4-fpm-alpine

MYSQL_VERSION=latest
MYSQL_DATABASE=local_db
MYSQL_USER=homestead
MYSQL_PASSWORD=secret

XDEBUG_REMOTE_HOST=172.31.33.213
XDEBUG_IDE_KEY=VSCODE
XDEBUG_PORT=7765

docker-compose.yml

version: "3.5"

services:
    nginx:
        build:
          args:
                user: ${USERNAME}
                uid: ${UID}
          context: .
          dockerfile: ./docker/nginx/Dockerfile
        image: "nginx:stable-alpine"
        container_name: ${PROJECT_NAME}-nginx
        ports:
          - 80:80
        volumes:
          - "./code:/var/www/html"
          - "./docker/nginx/vhost:/etc/nginx/conf.d/default.conf"
          - "./docker/nginx/nginx.conf:/etc/nginx/nginx.conf"
        depends_on:
          - php
          - mysql
        networks:
          - devproject

    php:
        build:
            args:
                PHP_VERSION: ${PHP_VERSION}
                user: ${USERNAME}
                uid: ${UID}
                XDEBUG_REMOTE_HOST: ${XDEBUG_REMOTE_HOST}
                XDEBUG_IDE_KEY: ${XDEBUG_IDE_KEY}
                XDEBUG_PORT: ${XDEBUG_PORT}
            context: .
            dockerfile: ./docker/php/Dockerfile
        image: php:${PHP_VERSION}
        container_name: ${PROJECT_NAME}-php
        volumes:
            - "./code:/var/www/html"
            - "./docker/php/www.conf:/usr/local/etc/php-fpm.d/www.conf"
        ports:
            - 9000:9000
        networks:
            - devproject

    mysql:
        image: mysql:${MYSQL_VERSION}
        container_name: ${PROJECT_NAME}-mysql
        restart: unless-stopped
        tty: true
        ports:
            - 3306:3306
        environment:
            MYSQL_DATABASE: ${MYSQL_DATABASE}
            MYSQL_USER: ${MYSQL_USER}
            MYSQL_PASSWORD: ${MYSQL_PASSWORD}
            MYSQL_ROOT_PASSWORD: ${MYSQL_PASSWORD}
            SERVICE_TAGS: dev
            SERVICE_NAME: mysql
        volumes:
            - devprojectmysql:/var/lib/mysql
        networks:
            - devproject

networks:
    devproject:
        driver: bridge

volumes:
    devprojectmysql:
        driver: local

docker/php/Dockerfile

ARG PHP_VERSION
FROM php:$PHP_VERSION

ARG XDEBUG_REMOTE_HOST
ARG XDEBUG_IDE_KEY
ARG XDEBUG_PORT
ARG user
ARG uid

ENV XDEBUG_REMOTE_HOST $XDEBUG_REMOTE_HOST
ENV XDEBUG_IDE_KEY $XDEBUG_IDE_KEY
ENV XDEBUG_PORT $XDEBUG_PORT

RUN addgroup -g $uid $user && adduser -G $user -g $user -s /bin/sh -D $user

RUN mkdir -p /var/www/html

RUN chown $user:$user /var/www/html

WORKDIR /var/www/html

RUN apk add --no-cache --update icu-dev gettext gettext-dev git curl libpng-dev libzip-dev \
            libmcrypt-dev libpng-dev libjpeg-turbo-dev libxml2-dev icu-dev postgresql-dev curl-dev \
            libmemcached-dev build-base autoconf zip unzip oniguruma-dev &&\
    apk add --update --virtual build-dependencies build-base gcc wget autoconf

RUN pecl install xdebug-3.0.4

RUN docker-php-ext-install gd ctype fileinfo mysqli pdo pdo_mysql mbstring exif pcntl bcmath xml curl json intl gettext tokenizer zip simplexml

RUN docker-php-ext-configure intl &&\
    docker-php-ext-configure gettext &&\
    docker-php-ext-enable xdebug

RUN apk del build-dependencies &&\
    rm -rf /var/cache/apk/* &&\
    rm -rf /var/lib/apt/lists/*

RUN echo "xdebug.remote_autostart=1" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini \
    && echo "xdebug.remote_enable=1" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini \
    && echo "xdebug.remote_handler=dbgp" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini \
    && echo "xdebug.remote_connect_back=0" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini \
    && echo "xdebug.remote_log=/tmp/xdebug_remote.log" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini \
    && echo "xdebug.remote_port=$XDEBUG_PORT" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini \
    && echo "xdebug.idekey=$XDEBUG_IDE_KEY" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini \
    && echo "xdebug.remote_host=$XDEBUG_REMOTE_HOST" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini

docker/nginx/Dockerfile

FROM nginx:stable-alpine

# Arguments defined in docker-compose.yml
ARG user
ARG uid

RUN mkdir -p /var/www/html

RUN addgroup -g $uid $user && adduser -G $user -g $user -s /bin/sh -D $user

RUN chown $user:$user /var/www/html

1 个答案:

答案 0 :(得分:1)

撰写文件中的图像字段,例如

image: php:${PHP_VERSION}

是您正在创建的图像。您已将创建的镜像命名为与 Dockerfile 中的基本镜像相同的名称,覆盖上游名称。这是您撰写文件中的一个错误,并且会令其他人感到困惑,因为运行具有该名称的映像会根据您是拉取上游映像还是运行本地构建的映像执行不同的操作。

相反,您的撰写文件应使用您自己的注册表命名空间中的内容命名图像以避免冲突。在涉及预先设置您的 Hub 用户名的 Hub 中:

image: <hubuser>/<appname>:<tag>