From 5d7869d3d52e9e83adebb4d322f9d65af0e9c402 Mon Sep 17 00:00:00 2001 From: Linus Gasser Date: Sat, 6 Jan 2024 17:44:49 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=B3=20:=20Improving=20Docker=20Build?= =?UTF-8?q?=20(#1415)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Improving builds When adding LibreChat to ansible, it rebuilt way too often, even if I only changed the configuration. With this PR, it should build only when the files of the app change. Also removed the 'volumes' section for the 'api' in the docker-compose.yml. At least with our installation it works fine like this. * @danny-avila's comments - removed 'env_file' from docker-compose.yml - re-added link to '.env' in volumes * Adding latest changes from main * @danny-avila's comments * Updating installation instructions * @danny-avila's comments - Remove unused environment in docker-compose.yml - Re-add some steps for cleaning docker images --- .dockerignore | 11 +++---- Dockerfile | 12 ++++---- docker-compose.yml | 11 +------ .../installation/docker_compose_install.md | 30 +++++++++---------- 4 files changed, 27 insertions(+), 37 deletions(-) diff --git a/.dockerignore b/.dockerignore index 30910059c..c945657a3 100644 --- a/.dockerignore +++ b/.dockerignore @@ -1,6 +1,7 @@ -**/node_modules -client/dist/images +node_modules data-node -.env -**/.env -librechat.yaml \ No newline at end of file +meili_data* +librechat* +Dockerfile* +# Ignore all hidden files +.* diff --git a/Dockerfile b/Dockerfile index 1dac186b1..37ccb7c14 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,17 +1,15 @@ # Base node image -FROM node:19-alpine AS node +FROM node:18-alpine AS node COPY . /app WORKDIR /app +# Allow mounting of these files, which have no default +# values. +RUN touch .env librechat.yaml # Install call deps - Install curl for health check RUN apk --no-cache add curl && \ - # We want to inherit env from the container, not the file - # This will preserve any existing env file if it's already in source - # otherwise it will create a new one - touch .env && \ - # Build deps in seperate - npm ci + npm ci # React client build ENV NODE_OPTIONS="--max-old-space-size=2048" diff --git a/docker-compose.yml b/docker-compose.yml index 873eb5c5f..264d50b90 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -17,20 +17,13 @@ services: restart: always user: "${UID}:${GID}" extra_hosts: - - "host.docker.internal:host-gateway" - env_file: - - .env + - "host.docker.internal:host-gateway" environment: - HOST=0.0.0.0 - MONGO_URI=mongodb://mongodb:27017/LibreChat - MEILI_HOST=http://meilisearch:7700 volumes: - - /app/client/node_modules - - /app/api/node_modules - - ./api:/app/api - ./.env:/app/.env - - ./.env.development:/app/.env.development - - ./.env.production:/app/.env.production - ./images:/app/client/public/images - ./librechat.yaml:/app/librechat.yaml mongodb: @@ -45,8 +38,6 @@ services: container_name: chat-meilisearch image: getmeili/meilisearch:v1.5 restart: always - env_file: - - .env user: "${UID}:${GID}" environment: - MEILI_HOST=http://meilisearch:7700 diff --git a/docs/install/installation/docker_compose_install.md b/docs/install/installation/docker_compose_install.md index d42f38799..21d1f6ce1 100644 --- a/docs/install/installation/docker_compose_install.md +++ b/docs/install/installation/docker_compose_install.md @@ -25,10 +25,11 @@ Install Docker on your system. **[Docker Desktop](https://www.docker.com/product ### LibreChat Configuration Before running LibreChat with Docker, you need to configure some settings: -- Edit the credentials you see in `docker-compose.yml` under the API service as needed. - - See my notes below for specific instructions on some of the configuration - Provide all necessary credentials in the `.env` file before the next step. - Docker will read this env file. See the **[/.env.example](https://github.com/danny-avila/LibreChat/blob/main/.env.example)** file for reference. +- If you want to change the `docker-compose.yml` file, please create a `docker-compose.override.yml` file based on the `docker-compose.override.yml.example`. + This allows you to update without having to modify `docker-compose.yml`. +- Either create an empty `librechat.yaml` file or use the example from `librechat.example.yaml`. #### [AI Setup](../configuration/ai_setup.md) (Required) At least one AI endpoint should be setup for use. @@ -42,17 +43,18 @@ How to set up the user/auth system and Google login. ### Running LibreChat Once you have completed all the setup, you can start the LibreChat application by running the command `docker-compose up` in your terminal. After running this command, you can access the LibreChat application at `http://localhost:3080`. -If you build your own containers out of the git checkout with `docker-compose up --build` you should pre-create the mount points for the volumes. This avoids occasional trouble with directory permissions when rebuilding: -``` -mkdir meili_data images .env.production .env.development data-node -``` - **Note:** MongoDB does not support older ARM CPUs like those found in Raspberry Pis. However, you can make it work by setting MongoDB’s version to mongo:4.4.18 in docker-compose.yml, the most recent version compatible with That's it! If you need more detailed information on configuring your compose file, see my notes below. ## Updating LibreChat -- Run `npm run update` from the project directory for a clean installation. +The following commands will fetch the latest code of LibreChat and build a new docker image. + +```bash +git pull +docker-compose down +docker-compose up --build +``` If you're having issues running this command, you can try running what the script does manually: @@ -61,15 +63,15 @@ Prefix commands with `sudo` according to your environment permissions. ```bash # Stop the container (if running) docker-compose down -# Fetch the latest changes from Github -git fetch origin # Switch to the repo's main branch git checkout main # Pull the latest changes to the main branch from Github -git pull origin main +git pull # Prune all LibreChat Docker images docker rmi librechat:latest -# Remove all unused dangling Docker images +# Remove all unused dangling Docker images. +# Be careful, as this will delete all dangling docker images on your +# computer, also those not created by LibreChat! docker image prune -f # Building a new LibreChat image without cache docker-compose build --no-cache @@ -83,6 +85,7 @@ docker-compose up ### Config notes for docker-compose.yml file Modification to the `docker-compose.yml` should be made with `docker-compose.override.yml` whenever possible to prevent conflicts when updating. You can create a new file named `docker-compose.override.yml` in the same directory as your main `docker-compose.yml` file for LibreChat, where you can set your .env variables as needed under `environment`, or modify the default configuration provided by the main `docker-compose.yml`, without the need to directly edit or duplicate the whole file. +The file `docker-compose.override.yml.example` gives some examples of the most common reconfiguration options used. For more info see: @@ -94,9 +97,6 @@ For more info see: - **[docker docs - merge-compose-files](https://docs.docker.com/compose/multiple-compose-files/merge/#merge-compose-files)** - **[docker docs - specifying-multiple-compose-files](https://docs.docker.com/compose/reference/#specifying-multiple-compose-files)** -- You can also view an example of an override file for LibreChat in your LibreChat folder and on GitHub: - - **[docker-compose.override.example](https://github.com/danny-avila/LibreChat/blob/main/docker-compose.override.yaml.example)** - - Any environment variables set in your compose file will override variables with the same name in your .env file. Note that the following variables are necessary to include in the compose file so they work in the docker environment, so they are included for you. ```yaml