Dockerã¨pipenvã使ã£ãç°å¢æ§ç¯ã«ã¤ãã¦ã®è¨äºã¯ããã¤ãèªãã ã®ã§ãããPIpfile.lockãæ´æ°ããéç¨ã«ã¤ãã¦æ¸ããã¦ããè¨äºãå°ãªã(è¦ã¤ããããªãã£ã)ãããæ¸ãã¦ããã¾ãã
TL;DR
- Dockerfileå
ã§ã¯
pipenv install --system --ignore-pipfile --deploy
ã使ãã - Pipfile.lockã¯æ´æ°ç¨ã«ã³ã³ãããä½ã£ã¦ããã®ä¸ã§æ´æ°ã
docker cp
ã§ãã¹ãå´ã«æ»ãã pipenv install
ã¯ç¾ç¶æéããããã®ã§è»½ã使ã£ã¦ã¿ããã¨ãã¯pip install
ã§è©¦ãã- ããã¾ã§èªåãèãã¤ãããã©ã¯ãã£ã¹ãªã®ã§ããã£ã¨ããæ¹æ³ããããããã¦ãã§ã¯ãããã¦ãããã¨ããæè¦ãããã°æ¯éã³ã¡ã³ããã ããmm
課é¡
Dockerã¨pipenvã使ã£ãæãä¸è¬çãªç°å¢æ§ç¯ã¯Dockerfileå
㧠ADD Pipfile
ADD Pipfile.lock
ã㦠pipenv install --system
ããæ¹æ³ã ã¨æãã¾ãã
ãã®ã¨ãåé¡ã«ãªãã®ãã ãPipfile.lockã®æ´æ°æ¹æ³ãã§ããé¸æè¢ã¨ãã¦ã¯
- 1ã ã³ã³ããå
ã§
pipenv install
ãã¦æ´æ°ãããã¹ãå´ããdocker cp
ã使ã - 2ã ãã¹ãå´ã§
pipenv install
ãã¦æ´æ°ãã¦ãã¾ã
ã ã¨æãã®ã§ããã1. ã¯æéç¨ã®è¦ç´ ãå¤ããæ°ããProjectã«é¢ãã£ãã¡ã³ãã¼ã«åªãããªãã2. ã¯ã¤ã³ã¹ãã¼ã«ããç°å¢ãå¤ããã®ã§åªçæ§ãæ ä¿ããã¦ãããä¸å®(ç¹ã«Cæ¡å¼µã®ã©ã¤ãã©ãªãå«ãã¨ã)ã¨ããåé¡ãããã¾ãã
ã¾ããDockerfileå
㧠pipenv install --system --skip-lock
ã¨ãã¦ãimageãã«ãã®åº¦ã«ææ°çã«ãã¦ãã¾ããã¨ããè¨äºãããã¤ãè¦ããã¾ããããæ¬çªç°å¢ã§ä½¿ãã«ã¯Versionã®æ
ä¿ããªãã®ã§ãå½ç¶é¸æè¢ã¨ãã¦ã¯ãªãã¨æãã¾ãã
解決ç
ä¸è¨ã®åé¡ãèªåã¯ä¸ã®ãããªæ¹æ³ã§è§£æ±ºãã¾ããã
- 1ã Dockerfileå
ã§ã¯
pipenv install --system --ignore-pipfile --deploy
ã¨ãã¦ãPIpfile.lockããå®å ¨ã«ã¤ã³ã¹ãã¼ã«ãè¡ã - 2ã Pipfile.lockã®æ´æ°ã¯ãå°ç¨ã«ã³ã³ãããä½ã£ã¦ã¢ãããã¼ãããShell Scriptãæ¸ã
1ã Dockerfileå
ã§ã¯ pipenv install --system --ignore-pipfile --deploy
ãããããã¨ã§Pipfileã§ãªãPIpfile.lockã®ã¿ãåç §ãã¦å®å ¨ã«ã¤ã³ã¹ãã¼ã«ãè¡ããã¨ãã§ãã¾ãã ä¸å¿1ã¤1ã¤ãªãã·ã§ã³ã®èª¬æãæ¸ãã¦ããã¨ã
--system
: ä»®æ³ç°å¢ã§ãªãSystemã«ã¤ã³ã¹ãã¼ã«ãã¾ãã--ignore-pipfile
: Pipfile.lockã®ã¿ãåç §ãã¾ãã--delpoy
: Pipfile.lockã®ä¾åé¢ä¿ãå¤ããªã£ã¦ããã¨ãã«status_code 1ãè¿ãã¦çµäºãã¾ãã
å®éã®Dockerfileã¯ä¸ã®ããã«ãªãã¾ãã
FROM python:3.7.2 # Setting ENV LC_ALL=C.UTF-8 \ LANG=C.UTF-8 # ...ãã以å¤ã®å¦ç # Install PyPI packages ADD Pipfile* /tmp/ RUN cd /tmp && \ pip install -U pip && \ pip install pipenv && \ pipenv install --system --ignore-pipfile --deploy
pipenv install
ãã¦ãã¾ãã¨ãã以éã®ã¤ã¡ã¼ã¸ã®å®¹éãããªã大ãããªã£ã¦ãã¾ãã®ã§ãã¾ããæãå¤æ´å¯è½æ§ãé«ãé¨åãªã®ã§ãèªåã¯Dockerfileã®æå¾ã®ãã§ã¼ãºã§è¡ãããã«ãã¦ãã¾ãã
Note: ä»å¾ã¯ pipenv sync --system
ã§è¯ããªãå¯è½æ§ã
å®ã¯ pipenv install --ignore-pipfile --deploy
㯠pipenv sync
ã¨åããã®ãªã®ã§ããã ç¾ç¶ã§ã¯ pipenv sync
ã« --system
optionããªããããã¡ãã使ããã¨ã§ã§ãã¾ããã
ä¸ã®Issueã«ä¸ãã£ã¦ãã¾ãã®ã§ãå®è£
ãé²ãã°é·ãå¼æ°ãã¤ããå¿
è¦ããªããªãã¾ãã(OSSã³ããããã£ã³ã¹ãªã®ã§èªåãå®è£
ã§ããªããè¦ã¦ã¿ããã¨æãã¾ãã)
Support `--system` to `pipenv sync` · Issue #2227 · pypa/pipenv · GitHub
2ã Pipfile.lockã®æ´æ°ã¯ã³ã³ãããä½ã£ã¦ã¢ãããã¼ããã
1ã³ãã³ãã§ã¯ã§ããªãã®ã§ãããç¨ã®Shell Scriptãç¨æãã¾ããã
update_piplock.sh
#!/bin/bash IMAGE="docker-test" TAG="latest" CONTAINER="piplock_container" WORK_DIR="/tmp/" INSTALL_TIMEOUT=1800 # ã³ã³ãããèµ·å docker container run -e PIPENV_INSTALL_TIMEOUT="$INSTALL_TIMEOUT" --name "$CONTAINER" --rm -td "$IMAGE":"$TAG" # ãã¹ã -> ã³ã³ããã«Pipfile, Pipfile.lockã追å docker container cp Pipfile "$CONTAINER":${WORK_DIR}Pipfile docker container cp Pipfile.lock "$CONTAINER":${WORK_DIR}Pipfile.lock # ã³ã³ããã§Pipfile.lockæ´æ° docker container exec "$CONTAINER" pipenv lock # ã³ã³ãã -> ãã¹ãã«Pipfile.lockãåå¾ docker container cp "$CONTAINER":${WORK_DIR}Pipfile.lock Pipfile.lock # ã³ã³ãããåæ¢/åé¤ docker container stop "$CONTAINER"
ãã®ã¹ã¯ãªããã§ã¯ãPipfile.lockãæ´æ°ããå°ç¨ã§ã³ã³ãããèµ·åã㦠docker container cp
ã使ã£ã¦ãã¹ã<->ã³ã³ããã§ããåããããã¨ã«ãã£ã¦ã æ´æ°ãããPipfile.lockãåå¾ãã¦ãã¾ãã
æ¦ãåºæ¬çãªDockerã³ãã³ãã使ã£ã¦ããã®ã§ãããã¡ãã£ã¨è¦æ
£ããªããããããªãå¼æ°ã ã解説ãã¦ããã¾ãã
-td
ã¯CMDãçµäºããã¨ãã«ã³ã³ãããåæ¢ããªãããã«ä»ãã¦ãããªãã·ã§ã³ã§ãã
-d, --detach=false Run container in background and print container ID -t, --tty=false Allocate a pseudo-TTY
PIPENV_INSTALL_TIMEOUT
ã¯ããã©ã«ãã®900ç§(15å)ã§éã«åããªãã±ã¼ã¹ãããã®ã§ã(èªåã®ç°å¢ã ã¨Daskãã¤ã³ã¹ãã¼ã«ããã¨éã«åãã¾ãã)å ´åã«ãã£ã¦è¨å®ãã¦ãã ããã
Note: pipenv install
ãããªãé
ã
PIPENV_INSTALL_TIMEOUT
ã§ã®èª¬æããããããããã«ã pipenv lock
ã¯ç¾æç¹ã§ããªãé
ãã§ãâ¦ã
ããã¯Issueã«ãä½åº¦ãæãããã¦ããå
容ã§ãæ®å¿µã§ãããç°¡åãªè§£æ±ºçã¯ãªãããã§ãã
- Lock updating is very slow · Issue #1914 · pypa/pipenv · GitHub
- Locking is slow (and performs redundant downloads) · Issue #2284 · pypa/pipenv · GitHub
ãªã®ã§ã軽ã使ã£ã¦ã¿ããã¨ãã¯ã³ã³ããå
㧠pip install
ãã¦è©¦ããæ¬å½ã«ä½¿ãããã£ããããã¯ã°ã©ã¦ã³ã㧠./update_piplock.sh
ãã¦æ´æ°ããéç¨ã«ãã¾ããã
Note: PipfileãPIpfile.lockã®åæä½æ
ä¸è¨ã®ãã©ã¯ãã£ã¹ã§å°å³ã«ã©ãããããè¿·ãã®ããPipfileãPIpfile.lockã®åæä½æã§ãã å ã ãã¹ãå´ã§PipfileãPipfile.lockãããåæã§ã®ãã©ã¯ãã£ã¹ãªã®ã§ããã®ã¯ã¼ã¯ããã¼ã«ä¹ã£ã¦ããã¸ã§ã¯ããå§ãããã¨ããã¨ãããï¼ãã¨ãªãããããã¾ããã(ã¨ããããã®ãã©ã¯ãã£ã¹ã ãã§ãªãDocker&pipenvã§ç°å¢æ§ç¯ããã¨ãã®å ±éã®åé¡ããããã¾ãããã)
ããå²ãåã£ã¦ãã¹ãå´ã§ pipenv install
ãã¦ãã¾ã£ã¦ãããããããã¾ããããããã¾ã§ãã¹ãå´ã®Pythonç°å¢ãå
¨ã使ããã«éç¨ã§ãã¦ããã®ã§ãDockerç°å¢ã§å®çµããããããããã¾ãããããããå ´åã¯ä¸ã®ãããªã¹ã¯ãªããã§ç©ºã®PipfileãPipfile.lockãåå¾ãããã¨ãã§ããã¨æãã¾ãã
create_pipfiles.sh
#!/bin/bash IMAGE="python" TAG="3.7.2" WORK_DIR="/tmp/" CONTAINER="pipfile_container" # ã³ã³ãããèµ·å docker container run --name "$CONTAINER" --workdir "$WORK_DIR" --rm -td "$IMAGE":"$TAG" # ã³ã³ããã§Pipfile.lockæ´æ° docker container exec "$CONTAINER" pip install -U pip docker container exec "$CONTAINER" pip install pipenv docker container exec "$CONTAINER" pipenv install # ã³ã³ãã -> ãã¹ãã«PipfileãPipfile.lockãåå¾ docker container cp "$CONTAINER":"$WORK_DIR"Pipfile Pipfile docker container cp "$CONTAINER":"$WORK_DIR"Pipfile.lock Pipfile.lock # ã³ã³ãããåæ¢/åé¤ docker container stop "$CONTAINER"
æå¾ã«
- ä»åã¯Dockerã¨pipenvã使ã£ãç°å¢æ§ç¯ã«ã¤ãã¦å°ãè¸ã¿è¾¼ãã ãã©ã¯ãã£ã¹ãæ¸ãã¦ã¿ã¾ããã
- åé ã«ãæ¸ãã¾ããããä»ã®ã¢ã¤ãã£ã¢ãåé¡ç¹ãªã©ããã¾ãããæ¯éã³ã¡ã³ããã ããã
pipenv lock
ãé ãããåé¡ã«ã¤ãã¦ã¯æè¿å°ãæµè¡ã£ã¦ããpoetryã®è©¦ãã¦ã¿ããã¨æã£ã¦ãã¾ããé度è¨æ¸¬ãããããã°ã«æ¸ãããã¨æãã¾ãã
- ä»å使ã£ãã³ã¼ãã¯ãµã³ãã«ããã¸ã§ã¯ãã¨ãã¦ä¸ã®ãªãã¸ããªã«ç½®ãã¦ããã¾ãããããå¿ è¦ã§ããã°åç §ãã¦ãã ããã
- ããã§ã¯è¯ãDockerã¨pipenvã©ã¤ããï¼â¨ ð°â¨