forked from Archives/langchain
docker development image and helper makefile
separate makefile and build env: - separate makefile for docker - only show docker commands when docker detected in system - only rebuild container on change - use an unpriviliged user builder image and base dev image: - fully isolated environment inside container. - all venv installed inside container shell and available as commands. - ex: `docker run IMG jupyter notebook` to launch notebook. - pure python based container without poetry. - custom motd to add a message displayed to users when they connect to container. - print environment versions (git, package, python) on login - display help message when starting container
parent
9962bda70b
commit
a9b8cf07fb
@ -0,0 +1,143 @@
|
|||||||
|
.vscode/
|
||||||
|
.idea/
|
||||||
|
# Byte-compiled / optimized / DLL files
|
||||||
|
__pycache__/
|
||||||
|
*.py[cod]
|
||||||
|
*$py.class
|
||||||
|
|
||||||
|
# C extensions
|
||||||
|
*.so
|
||||||
|
|
||||||
|
# Distribution / packaging
|
||||||
|
.Python
|
||||||
|
build/
|
||||||
|
develop-eggs/
|
||||||
|
dist/
|
||||||
|
downloads/
|
||||||
|
eggs/
|
||||||
|
.eggs/
|
||||||
|
lib/
|
||||||
|
lib64/
|
||||||
|
parts/
|
||||||
|
sdist/
|
||||||
|
var/
|
||||||
|
wheels/
|
||||||
|
pip-wheel-metadata/
|
||||||
|
share/python-wheels/
|
||||||
|
*.egg-info/
|
||||||
|
.installed.cfg
|
||||||
|
*.egg
|
||||||
|
MANIFEST
|
||||||
|
|
||||||
|
# PyInstaller
|
||||||
|
# Usually these files are written by a python script from a template
|
||||||
|
# before PyInstaller builds the exe, so as to inject date/other infos into it.
|
||||||
|
*.manifest
|
||||||
|
*.spec
|
||||||
|
|
||||||
|
# Installer logs
|
||||||
|
pip-log.txt
|
||||||
|
pip-delete-this-directory.txt
|
||||||
|
|
||||||
|
# Unit test / coverage reports
|
||||||
|
htmlcov/
|
||||||
|
.tox/
|
||||||
|
.nox/
|
||||||
|
.coverage
|
||||||
|
.coverage.*
|
||||||
|
.cache
|
||||||
|
nosetests.xml
|
||||||
|
coverage.xml
|
||||||
|
*.cover
|
||||||
|
*.py,cover
|
||||||
|
.hypothesis/
|
||||||
|
.pytest_cache/
|
||||||
|
|
||||||
|
# Translations
|
||||||
|
*.mo
|
||||||
|
*.pot
|
||||||
|
|
||||||
|
# Django stuff:
|
||||||
|
*.log
|
||||||
|
local_settings.py
|
||||||
|
db.sqlite3
|
||||||
|
db.sqlite3-journal
|
||||||
|
|
||||||
|
# Flask stuff:
|
||||||
|
instance/
|
||||||
|
.webassets-cache
|
||||||
|
|
||||||
|
# Scrapy stuff:
|
||||||
|
.scrapy
|
||||||
|
|
||||||
|
# Sphinx documentation
|
||||||
|
docs/_build/
|
||||||
|
|
||||||
|
# PyBuilder
|
||||||
|
target/
|
||||||
|
|
||||||
|
# Jupyter Notebook
|
||||||
|
.ipynb_checkpoints
|
||||||
|
notebooks/
|
||||||
|
|
||||||
|
# IPython
|
||||||
|
profile_default/
|
||||||
|
ipython_config.py
|
||||||
|
|
||||||
|
# pyenv
|
||||||
|
.python-version
|
||||||
|
|
||||||
|
# pipenv
|
||||||
|
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
|
||||||
|
# However, in case of collaboration, if having platform-specific dependencies or dependencies
|
||||||
|
# having no cross-platform support, pipenv may install dependencies that don't work, or not
|
||||||
|
# install all needed dependencies.
|
||||||
|
#Pipfile.lock
|
||||||
|
|
||||||
|
# PEP 582; used by e.g. github.com/David-OConnor/pyflow
|
||||||
|
__pypackages__/
|
||||||
|
|
||||||
|
# Celery stuff
|
||||||
|
celerybeat-schedule
|
||||||
|
celerybeat.pid
|
||||||
|
|
||||||
|
# SageMath parsed files
|
||||||
|
*.sage.py
|
||||||
|
|
||||||
|
# Environments
|
||||||
|
.env
|
||||||
|
.venv
|
||||||
|
.venvs
|
||||||
|
env/
|
||||||
|
venv/
|
||||||
|
ENV/
|
||||||
|
env.bak/
|
||||||
|
venv.bak/
|
||||||
|
|
||||||
|
# Spyder project settings
|
||||||
|
.spyderproject
|
||||||
|
.spyproject
|
||||||
|
|
||||||
|
# Rope project settings
|
||||||
|
.ropeproject
|
||||||
|
|
||||||
|
# mkdocs documentation
|
||||||
|
/site
|
||||||
|
|
||||||
|
# mypy
|
||||||
|
.mypy_cache/
|
||||||
|
.dmypy.json
|
||||||
|
dmypy.json
|
||||||
|
|
||||||
|
# Pyre type checker
|
||||||
|
.pyre/
|
||||||
|
|
||||||
|
# macOS display setting files
|
||||||
|
.DS_Store
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
# docker
|
||||||
|
docker/
|
||||||
|
!docker/assets/
|
||||||
|
.dockerignore
|
@ -0,0 +1,6 @@
|
|||||||
|
# python env
|
||||||
|
PYTHON_VERSION=3.11.2
|
||||||
|
|
||||||
|
# langchain env
|
||||||
|
OPENAI_API_KEY=${OPENAI_API_KEY:-}
|
||||||
|
SERPAPI_API_KEY=${SERPAPI_API_KEY:-}
|
@ -0,0 +1,102 @@
|
|||||||
|
# vim: ft=dockerfile
|
||||||
|
#
|
||||||
|
# see also: https://github.com/python-poetry/poetry/discussions/1879
|
||||||
|
# - with https://github.com/bneijt/poetry-lock-docker
|
||||||
|
# see https://github.com/thehale/docker-python-poetry
|
||||||
|
# see https://github.com/max-pfeiffer/uvicorn-poetry
|
||||||
|
|
||||||
|
# use by default the slim version of python
|
||||||
|
ARG PYTHON_IMAGE_TAG=slim
|
||||||
|
ARG PYTHON_VERSION=${PYTHON_VERSION:-3.11.2}
|
||||||
|
|
||||||
|
####################
|
||||||
|
# Base Environment
|
||||||
|
####################
|
||||||
|
FROM python:$PYTHON_VERSION-$PYTHON_IMAGE_TAG AS lchain-base
|
||||||
|
|
||||||
|
ARG UID=1000
|
||||||
|
ARG USERNAME=lchain
|
||||||
|
|
||||||
|
ENV USERNAME=$USERNAME
|
||||||
|
|
||||||
|
RUN groupadd -g ${UID} $USERNAME
|
||||||
|
RUN useradd -l -m -u ${UID} -g ${UID} $USERNAME
|
||||||
|
|
||||||
|
# used for mounting source code
|
||||||
|
RUN mkdir /src
|
||||||
|
VOLUME /src
|
||||||
|
|
||||||
|
|
||||||
|
#######################
|
||||||
|
## Poetry Builder Image
|
||||||
|
#######################
|
||||||
|
FROM lchain-base AS lchain-base-builder
|
||||||
|
|
||||||
|
ENV HOME=/root
|
||||||
|
ENV POETRY_HOME=/root/.poetry
|
||||||
|
ENV POETRY_VIRTUALENVS_IN_PROJECT=false
|
||||||
|
ENV POETRY_NO_INTERACTION=1
|
||||||
|
ENV CACHE_DIR=$HOME/.cache
|
||||||
|
ENV POETRY_CACHE_DIR=$CACHE_DIR/pypoetry
|
||||||
|
ENV PATH="$POETRY_HOME/bin:$PATH"
|
||||||
|
|
||||||
|
WORKDIR /root
|
||||||
|
|
||||||
|
RUN apt-get update && \
|
||||||
|
apt-get install -y \
|
||||||
|
git \
|
||||||
|
curl
|
||||||
|
|
||||||
|
SHELL ["/bin/bash", "-o", "pipefail", "-c"]
|
||||||
|
|
||||||
|
RUN mkdir -p $CACHE_DIR
|
||||||
|
|
||||||
|
## setup poetry
|
||||||
|
RUN curl -sSL -o $CACHE_DIR/pypoetry-installer.py https://install.python-poetry.org/
|
||||||
|
RUN python3 $CACHE_DIR/pypoetry-installer.py
|
||||||
|
|
||||||
|
|
||||||
|
# # Copy poetry files
|
||||||
|
COPY poetry.* pyproject.toml ./
|
||||||
|
|
||||||
|
RUN mkdir /pip-prefix
|
||||||
|
|
||||||
|
RUN poetry export --with dev -f requirements.txt --output requirements.txt --without-hashes && \
|
||||||
|
pip install --no-cache-dir --disable-pip-version-check --prefix /pip-prefix -r requirements.txt
|
||||||
|
|
||||||
|
|
||||||
|
# add custom motd message
|
||||||
|
COPY docker/assets/etc/motd /tmp/motd
|
||||||
|
RUN cat /tmp/motd > /etc/motd
|
||||||
|
|
||||||
|
RUN printf "\n%s\n%s\n" "$(poetry version)" "$(python --version)" >> /etc/motd
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
###################
|
||||||
|
## Runtime Image
|
||||||
|
###################
|
||||||
|
FROM lchain-base AS lchain
|
||||||
|
|
||||||
|
#jupyter port
|
||||||
|
EXPOSE 8888
|
||||||
|
|
||||||
|
COPY docker/assets/entry.sh /entry
|
||||||
|
RUN chmod +x /entry
|
||||||
|
|
||||||
|
COPY --from=lchain-base-builder /etc/motd /etc/motd
|
||||||
|
COPY --from=lchain-base-builder /usr/bin/git /usr/bin/git
|
||||||
|
|
||||||
|
USER ${USERNAME:-lchain}
|
||||||
|
ENV HOME /home/$USERNAME
|
||||||
|
WORKDIR /home/$USERNAME
|
||||||
|
|
||||||
|
COPY --chown=lchain:lchain --from=lchain-base-builder /pip-prefix $HOME/.local/
|
||||||
|
|
||||||
|
COPY . .
|
||||||
|
|
||||||
|
SHELL ["/bin/bash", "-o", "pipefail", "-c"]
|
||||||
|
RUN pip install --no-deps --disable-pip-version-check --no-cache-dir -e .
|
||||||
|
|
||||||
|
|
||||||
|
entrypoint ["/entry"]
|
@ -0,0 +1,47 @@
|
|||||||
|
#do not call this makefile it is included in the main Makefile
|
||||||
|
.PHONY: docker docker.jupyter docker.run docker.force_build
|
||||||
|
|
||||||
|
# read python version from .env file
|
||||||
|
PYTHON_VERSION := $(shell grep PYTHON_VERSION docker/.env | cut -d '=' -f2)
|
||||||
|
|
||||||
|
|
||||||
|
DOCKER_SRC := $(shell find docker -type f -not -name .env)
|
||||||
|
DOCKER_IMAGE_NAME = langchain/dev
|
||||||
|
|
||||||
|
# SRC is all files matched by the git ls-files command
|
||||||
|
SRC := $(shell git ls-files -- '*' ':!:docker/*')
|
||||||
|
|
||||||
|
# set DOCKER_BUILD_PROGRESS=plain to see detailed build progress
|
||||||
|
DOCKER_BUILD_PROGRESS ?= auto
|
||||||
|
|
||||||
|
# extra message to show when entering the docker container
|
||||||
|
DOCKER_MOTD := docker/assets/etc/motd
|
||||||
|
|
||||||
|
ROOTDIR := $(shell git rev-parse --show-toplevel)
|
||||||
|
|
||||||
|
docker: docker.run
|
||||||
|
|
||||||
|
docker.run: docker.build
|
||||||
|
@echo "Docker image: $(DOCKER_IMAGE_NAME):$(GIT_HASH)"
|
||||||
|
@docker run --rm -it -u lchain -v $(ROOTDIR):/src $(DOCKER_IMAGE_NAME):$(GIT_HASH)
|
||||||
|
@# $(local source mounted at $(ROOTDIR) at /src)
|
||||||
|
|
||||||
|
docker.jupyter: docker.build
|
||||||
|
@docker run --rm -it -v $(ROOTDIR):/src $(DOCKER_IMAGE_NAME):$(GIT_HASH) jupyter notebook
|
||||||
|
|
||||||
|
docker.build: $(SRC) $(DOCKER_SRC) $(DOCKER_MOTD)
|
||||||
|
ifdef $(DOCKER_BUILDKIT)
|
||||||
|
@docker buildx build --build-arg PYTHON_VERSION=$(PYTHON_VERSION) \
|
||||||
|
--progress=$(DOCKER_BUILD_PROGRESS) \
|
||||||
|
-f docker/Dockerfile -t $(DOCKER_IMAGE_NAME):$(GIT_HASH) .
|
||||||
|
else
|
||||||
|
@docker build --build-arg PYTHON_VERSION=$(PYTHON_VERSION) \
|
||||||
|
-f docker/Dockerfile -t $(DOCKER_IMAGE_NAME):$(GIT_HASH) .
|
||||||
|
endif
|
||||||
|
@docker tag $(DOCKER_IMAGE_NAME):$(GIT_HASH) $(DOCKER_IMAGE_NAME):latest
|
||||||
|
@touch $@ # this avoids docker rebuilds the build dependencies have not changed
|
||||||
|
@ # you can remove the file docker_build to force a rebuild
|
||||||
|
|
||||||
|
docker.force_build: $(DOCKER_SRC)
|
||||||
|
@docker build --no-cache -f docker/Dockerfile -t $(DOCKER_IMAGE_NAME):$(GIT_HASH) .
|
||||||
|
|
@ -0,0 +1,10 @@
|
|||||||
|
#!/usr/bin/env bash
|
||||||
|
|
||||||
|
export PATH=$HOME/.local/bin:$PATH
|
||||||
|
|
||||||
|
if [ -z "$1" ]; then
|
||||||
|
cat /etc/motd
|
||||||
|
exec /bin/bash
|
||||||
|
fi
|
||||||
|
|
||||||
|
exec "$@"
|
@ -0,0 +1,9 @@
|
|||||||
|
The dependencies have been installed in the current shell.
|
||||||
|
There is no virtualenv or a need for `poetry` inside the
|
||||||
|
container.
|
||||||
|
|
||||||
|
Running the command `make docker.run` at the root directory
|
||||||
|
of the project will build the container the first time. On
|
||||||
|
the next runs, it will use the cached image or rebuild it if
|
||||||
|
a change is made on the source code.
|
||||||
|
|
@ -0,0 +1,15 @@
|
|||||||
|
version: "3.7"
|
||||||
|
|
||||||
|
services:
|
||||||
|
langchain:
|
||||||
|
hostname: langchain
|
||||||
|
image: langchain/dev:latest
|
||||||
|
build:
|
||||||
|
context: ../
|
||||||
|
dockerfile: docker/Dockerfile
|
||||||
|
args:
|
||||||
|
PYTHON_VERSION: ${PYTHON_VERSION}
|
||||||
|
|
||||||
|
restart: unless-stopped
|
||||||
|
ports:
|
||||||
|
- 127.0.0.1:8888:8888
|
Loading…
Reference in New Issue