LycheeOrg / Lychee-Docker

Docker image for Lychee
https://lycheeorg.github.io/
167 stars 55 forks source link

Full source code in the docker image #178

Open tinohager opened 9 months ago

tinohager commented 9 months ago

I took a closer look at the docker image today and noticed that the entire code including github workflows etc. is contained in the docker image and not just the finished source code.

The docker image would be smaller if only the required code was transferred

https://github.com/LycheeOrg/Lychee-Docker/blob/d10de82598ff3b696aa3a5b47fa17cd02b2b46d7/Dockerfile#L81-L83 the last line should be enough

image

d7415 commented 9 months ago

Fair point that they're not required, but the .github folder is only 55K. May be worth checking for anything larger though.

the last line should be enough

Good spot, thanks!

tinohager commented 9 months ago

Sample Folder is over 100MB /var/www/html/Lychee/tests/Samples/

image

ildyria commented 9 months ago

Then why is this not working?

https://github.com/LycheeOrg/Lychee-Docker/blob/d10de82598ff3b696aa3a5b47fa17cd02b2b46d7/Dockerfile#L65-L66

tinohager commented 9 months ago

maybe a better solution https://shisho.dev/blog/posts/how-to-use-dockerignore/

ildyria commented 9 months ago

When running it on my instance, it does find the,

root@vps:/var/docker/Lychee-docker  perso ✗                                             167d14h ◒  
▶ docker exec -it lychee bash
root@e7d513f87a2f:/var/www/html/Lychee# find . -wholename '*/[Tt]ests/*'
./tests/LoadedSubscriber.php
./tests/Samples
./tests/Samples/xcf.xcf
./tests/Samples/gif.gif
./tests/Samples/night.jpg
./tests/Samples/orientation-180.jpg
./tests/Samples/train.jpg
./tests/Samples/orientation-90.jpg
./tests/Samples/tags.json
./tests/Samples/ettlinger-alb.jpg
./tests/Samples/hochuferweg.jpg
./tests/Samples/orientation-270.jpg
./tests/Samples/pdf.pdf
./tests/Samples/google_motion_photo_broken.jpg
./tests/Samples/gaming.mp4
./tests/Samples/tiff
./tests/Samples/webp.webp
./tests/Samples/google_motion_photo.jpg
./tests/Samples/train.mov
./tests/Samples/mongolia
./tests/Samples/tiff.tif
./tests/Samples/commits.json
./tests/Samples/without_exif.jpg
./tests/Samples/orientation-hflip.jpg
./tests/Samples/orientation-vflip.jpg
./tests/Samples/fin de journ??e.jpg
./tests/Samples/aarhus.jpg
./tests/Samples/mongolia.jpeg
./tests/Samples/undefined-exif-tag.jpg
./tests/Samples/png.png
./tests/Livewire
./tests/Livewire/WireableTest.php
./tests/Livewire/Forms
./tests/Livewire/Forms/ImportFromUrlTest.php
./tests/Livewire/Forms/Photo
...
ildyria commented 9 months ago

AFAIK .dockerignore won't help in this case. We are cloning inside the image, not outside and then doing a copy.

tinohager commented 9 months ago

this command change the directory

https://github.com/LycheeOrg/Lychee-Docker/blob/d10de82598ff3b696aa3a5b47fa17cd02b2b46d7/Dockerfile#L62

the delete command is use a dot, it therefore uses the current directory

ildyria commented 9 months ago

Nicely spotted!

d7415 commented 9 months ago
root@e7d513f87a2f:/var/www/html/Lychee# find . -wholename '*/[Tt]ests/*'

this command change the directory

https://github.com/LycheeOrg/Lychee-Docker/blob/d10de82598ff3b696aa3a5b47fa17cd02b2b46d7/Dockerfile#L62

I'm missing something here. That's the directory we want it to run in. But yes, they're not being deleted.

d7415 commented 9 months ago

This appears to be a (somewhat) common issue with Docker builds.

tinohager commented 9 months ago

i have tried to integrate the git clone module in a separate step. From there you could then only copy the files that are really needed. Possibly the error we have when deleting has to do with the onion structure of docker https://medium.com/@samhavens/how-to-make-a-docker-container-smaller-by-deleting-files-7354b5c6c8f1

FROM debian:bookworm-slim as clone

# Arguments
# To use the latest Lychee release instead of master pass `--build-arg TARGET=release` to `docker build`
ARG TARGET=dev

WORKDIR /lychee

# Install base dependencies, add user and group, clone the repo and install php libraries
RUN \
    set -ev && \
    apt-get update && \
    apt-get upgrade -qy && \
    apt-get install -qy --no-install-recommends \
    apt-transport-https \
    ca-certificates \
    curl \
    git && \
    update-ca-certificates

RUN \
    if [ "$TARGET" = "release" ] ; then RELEASE_TAG="-b v$(curl -s https://raw.githubusercontent.com/LycheeOrg/Lychee/master/version.md)" ; fi && \
    git clone --depth 1 $RELEASE_TAG https://github.com/LycheeOrg/Lychee.git . && \
    find . -wholename '*/[Tt]ests/*' -delete && \
    find . -wholename '*/[Tt]est/*' -delete && \
    rm -r storage/framework/cache/data/* 2> /dev/null || true && \
    rm    storage/framework/sessions/* 2> /dev/null || true && \
    rm    storage/framework/views/* 2> /dev/null || true && \
    rm    storage/logs/* 2> /dev/null || true && \
    echo "$TARGET" > docker_target

FROM debian:bookworm-slim as base

COPY --from=clone /lychee /var/www/html/Lychee

# Set version label
LABEL maintainer="lycheeorg"

# Environment variables
ENV PUID='1000'
ENV PGID='1000'
ENV USER='lychee'
ENV PHP_TZ=UTC

# Arguments
# To install composer development dependencies, pass `--build-arg COMPOSER_NO_DEV=0` to `docker build`
ARG COMPOSER_NO_DEV=1

# Install base dependencies, add user and group, clone the repo and install php libraries
RUN \
    set -ev && \
    apt-get update && \
    apt-get upgrade -qy && \
    apt-get install -qy --no-install-recommends \
    adduser \
    nginx-light \
    php8.2-mysql \
    php8.2-pgsql \
    php8.2-sqlite3 \
    php8.2-imagick \
    php8.2-mbstring \
    php8.2-gd \
    php8.2-xml \
    php8.2-zip \
    php8.2-fpm \
    php8.2-redis \
    php8.2-bcmath \
    php8.2-intl \
    curl \
    libimage-exiftool-perl \
    ffmpeg \
    jpegoptim \
    optipng \
    pngquant \
    gifsicle \
    webp \
    cron \
    composer \
    unzip && \
    addgroup --gid "$PGID" "$USER" && \
    adduser --gecos '' --no-create-home --disabled-password --uid "$PUID" --gid "$PGID" "$USER" && \
    cd /var/www/html/Lychee && \
    echo "Last release: $(cat version.md)" && \
    composer install --prefer-dist && \
    chown -R www-data:www-data /var/www/html/Lychee && \
    echo "* * * * * www-data cd /var/www/html/Lychee && php artisan schedule:run >> /dev/null 2>&1" >> /etc/crontab && \
    apt-get purge -y --autoremove git composer && \
    apt-get clean -qy && \
    rm -rf /var/lib/apt/lists/*

# Multi-stage build: Build static assets
# This allows us to not include Node within the final container
FROM node:20 as static_builder

WORKDIR /app

COPY --from=base /var/www/html/Lychee/ /app

RUN \
    npm ci --no-audit && \
    npm run build

# Get the static assets built in the previous step
FROM base
COPY --from=static_builder --chown=www-data:www-data /app/public /var/www/html/Lychee/public

# Add custom Nginx configuration
COPY default.conf /etc/nginx/nginx.conf

EXPOSE 80
VOLUME /conf /uploads /sym /logs

WORKDIR /var/www/html/Lychee

COPY entrypoint.sh inject.sh /

RUN chmod +x /entrypoint.sh && \
    chmod +x /inject.sh && \
    if [ ! -e /run/php ] ; then mkdir /run/php ; fi

HEALTHCHECK CMD curl --fail http://localhost:80/ || exit 1

ENTRYPOINT [ "/entrypoint.sh" ]

CMD [ "nginx" ]

but he is still complaining about a file where I do not yet know exactly where this comes from '/var/www/html/Lychee/.env': File exists

docker run 854c79503c4adac26a395e028fb97026476d309e297fb073073da24e32cf2d00

-------------------------------------
  _               _
 | |   _   _  ___| |__   ___  ___
 | |  | | | |/ __|  _ \ / _ \/ _ \
 | |__| |_| | (__| | | |  __/  __/
 |_____\__, |\___|_| |_|\___|\___|
       |___/

-------------------------------------
Lychee Version: 5.0.3 (dev)
Lychee Commit:  c0c2e69
https://github.com/LycheeOrg/Lychee/commit/c0c2e693669206d8b27abed8aef947d7e253efa6
-------------------------------------
**** Make sure the /conf /uploads /sym /logs folders exist ****
**** Create the symbolic link for the /uploads folder ****
**** Create the symbolic link for the /sym folder ****
**** Create the symbolic link for the /logs folder ****
**** Copy the default database to /conf ****
**** Create the symbolic link for the database ****
**** Copy the .env to /conf ****
ln: failed to create symbolic link '/var/www/html/Lychee/.env': File exists
d7415 commented 9 months ago

Possibly the error we have when deleting has to do with the onion structure of docker

That was my first thought, and may be the case, but running them all in the same command stops Docker creating a new layer so should deal with that. The deletion of .git content is working.