skip to Main Content

I have created a docker app with Symfony/PHP, Apache and Mysql stack.
When i run the php container, i’m automatically connected as root user.
So when i create a new file inside the container, it’s created as read-only mode because of user root and group root.
I always have to change the permissions to edit it in local side.
How can I fix the problem ? Is it possible to connect to container as my local user directly ?

My dockerfile:

FROM php:7.2-fpm
RUN apt-get update && apt-get install -y 
        libfreetype6-dev 
        libjpeg62-turbo-dev 
        libpng-dev 
    && docker-php-ext-install -j$(nproc) iconv 
    && docker-php-ext-configure gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/ 
    && docker-php-ext-install -j$(nproc) gd 
    && docker-php-ext-install zip 
    && docker-php-ext-install mysqli pdo pdo_mysql

RUN curl -sSk https://getcomposer.org/installer | php -- --disable-tls && 
   mv composer.phar /usr/local/bin/composer

RUN apt-get install -y git

My docker-compose file:

version: "3.7"
services:
        apache:
                container_name: apache
                build: ./docker/apache
                ports:
                        - ${APACHE_PORT}:80
                depends_on:
                        - php
                volumes:
                        - ./docker/apache/httpd.conf:/usr/local/apache2/conf/httpd.conf
                        - .:${PROJECT_DIR}
                        - ./docker/apache/log:/var/log/apache2

        mysql:
                container_name: mysql
                image: mysql
                ports:
                        - ${MYSQL_PORT}:3306
                restart: always
                volumes:
                        - ./docker/data/db:/var/lib/mysql
                command: --default-authentication-plugin=mysql_native_password
                environment:
                        MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD}
                        MYSQL_DATABASE: ${MYSQL_DATABASE}
                        MYSQL_USER: ${MYSQL_USER}
                        MYSQL_PASSWORD: ${MYSQL_PASSWORD}

        phpmyadmin:
                container_name: phpmyadmin
                image: phpmyadmin/phpmyadmin
                links:
                        - mysql:db
                ports:
                        - ${PHPMYADMIN_PORT}:80
                environment:
                        MYSQL_USER: ${PHPMYADMIN_USER}
                        MYSQL_PASSWORD: ${PHPMYADMIN_PASSWORD}
                        MYSQL_ROOT_PASSWORD: ${PHPMYADMIN_ROOT_PASSWORD}

        php:
                container_name: php
                build: ./docker/php
                ports:
                        - ${PHP_PORT}:9000
                links:
                        - mysql
                working_dir: ${PROJECT_DIR}
                volumes:
                        - .:${PROJECT_DIR}

2

Answers


  1. Chosen as BEST ANSWER

    Up.

    Is it possible to connect to container as my machine user instead of root user ? By default, it's connected as root user :

    docker exec -it php bash
    

    That's why the permissions of files that created inside container are changed.


  2. I finally resolved the problem but don’t know if it’s the best practice. I added a few lines in my docker-compose.yml.

            php:
                    container_name: php
                    build: ./docker/php
                    ports:
                            - ${PHP_PORT}:9000
                    links:
                            - mysql
                    working_dir: ${PROJECT_DIR}
                    volumes:
                            - .:${PROJECT_DIR}
                            - /etc/passwd:/etc/passwd:ro
                            - /etc/group:/etc/group:ro
                    user: 1000:1000
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search