Santiago Paradelo
Santiago Paradelo

Reputation: 93

Docker is building image with cached env variable

today I came across a weird bug. I am relatively new to Docker, today I was building a Docker environment for a Laravel application. I started with a docker-compose, this file contained the necessary environment variables. Then I deleted the docker-compose file to use only one Dockerfile, the problem occurs when building the app, I should copy the .env (which I know is bad practice, but now I'm just testing), but instead of using the values I have in .env uses the ones I had set in docker-compose. Now without this last file I still have the same problem. So far testie:

Some clue ? I've been trying to solve this since this afternoon and it makes me sick >:| .

This is my Dockerfile:

FROM ubuntu:20.04

ENV HOME /root
ENV LC_ALL          C.UTF-8
ENV LANG            en_US.UTF-8
ENV LANGUAGE        en_US.UTF-8

RUN apt-get update -y

RUN apt-get install software-properties-common -y

RUN add-apt-repository ppa:ondrej/php  && \
        apt-get update -y

RUN apt-get -y install php8.1 php-mysql php-curl php-mcrypt php-cli php-dev php-pear libsasl2-dev

RUN mkdir -p /usr/local/openssl/include/openssl/ && \
    ln -s /usr/include/openssl/evp.h /usr/local/openssl/include/openssl/evp.h && \
    mkdir -p /usr/local/openssl/lib/ && \
    ln -s /usr/lib/x86_64-linux-gnu/libssl.a /usr/local/openssl/lib/libssl.a && \
    ln -s /usr/lib/x86_64-linux-gnu/libssl.so /usr/local/openssl/lib/

RUN pecl install mongodb

RUN echo "extension=mongodb.so" > /etc/php/8.1/cli/conf.d/20-mongodb.ini && \
    echo "extension=mongodb.so" > /etc/php/8.1/mods-available/mongodb.ini && \
    echo "extension=mongodb.so" > /etc/php/8.1/cli/php.ini

RUN mkdir -p /usr/src/app

ENV PORT 8000

EXPOSE 8000

WORKDIR /usr/src/app

RUN apt-get install wget php-zip unzip -y

RUN wget -O composer-setup.php https://getcomposer.org/installer

RUN php composer-setup.php --install-dir=/usr/local/bin --filename=composer

COPY . /usr/src/app

RUN composer install

RUN php artisan fix:passport

RUN php artisan migrate

RUN php artisan passport:install

CMD ["php", "artisan", "serve"]

Upvotes: 2

Views: 1065

Answers (1)

Felipe Esteves
Felipe Esteves

Reputation: 415

I have created a Laravel project to test your Dockerfile. I found a few issues in it:

  1. mongodb extensions were present on laravel already do composer was raising an error due this conflict;
  2. all the compose and laravel commands to restore packages and run the project were running under root which is not recommended so I created an user named ubuntu to do so;
  3. every command in the Dockerfile will generate a docker "layer". As a good practice is recommended grouping those layers so they would make sense individually. E.g.: grouping all the required packages and installation steps (such as composer for instance) together in the same "RUN" command;

I have pushed the Dockerfile and a sample code to get composer running here: https://github.com/felipegouveiae/stackoverflow-73507087

I have left two branches: original, which I just made the necessary fixes to your Dockerfile and main, which I have enhanced that file as I would do on my projects.

This is the final Dockerfile

FROM ubuntu:20.04 as base

ENV HOME /root
ENV LC_ALL          C.UTF-8
ENV LANG            en_US.UTF-8
ENV LANGUAGE        en_US.UTF-8

RUN apt-get update -y && \
    apt-get install software-properties-common -y && \
    add-apt-repository ppa:ondrej/php  && \
    apt-get update -y

RUN apt-get -y install php8.1 php-mysql php-curl php-mcrypt \
    php-cli php-dev php-pear libsasl2-dev wget php-zip unzip

RUN mkdir -p /usr/local/openssl/include/openssl/ && \
    ln -s /usr/include/openssl/evp.h /usr/local/openssl/include/openssl/evp.h && \
    mkdir -p /usr/local/openssl/lib/ && \
    ln -s /usr/lib/x86_64-linux-gnu/libssl.a /usr/local/openssl/lib/libssl.a && \
    ln -s /usr/lib/x86_64-linux-gnu/libssl.so /usr/local/openssl/lib/

RUN wget -O composer-setup.php https://getcomposer.org/installer && \
    php composer-setup.php --install-dir=/usr/local/bin --filename=composer

FROM base AS build

RUN mkdir -p /usr/src/app

WORKDIR /usr/src/app

RUN useradd -ms /bin/bash ubuntu

RUN chown -R ubuntu:ubuntu /usr/src/app 

USER ubuntu

COPY . /usr/src/app

RUN composer install

RUN php artisan fix:passport &&\
    php artisan migrate && \
    php artisan passport:install

CMD ["php", "artisan", "serve"]

ENV PORT 8000

EXPOSE 8000

Please, test it with your project and let me know if that works.

Upvotes: 2

Related Questions