我希望在docker容器中构建我的前端所需的资源,然后通过python框架为它们提供服务。在python尝试找到文件之前,所有内容似乎都按预期工作,这会导致FileNotFoundError。如果我附加到容器并尝试相同的命令,则找到文件没有问题。为什么python最初无法找到文件?
index.js (已剪切) - 构建目标设置为/alpine/dist
// Template for index.html
index: path.resolve(__dirname, '../../dist/index.html'), // /alpine/dist
// Paths
assetsRoot: path.resolve(__dirname, '../../dist'), // /alpine/dist
app.py (已剪切) - 阅读目录/alpine/dist
中的文件列表(预计为['index.html']
)
from alpine.utils import get_base_path, listfiles
logger.info(os.path.join(get_base_path(), 'dist')) # /alpine/dist
logger.info(listfiles(os.path.join(get_base_path(), 'dist'))) # /alpine/dist
utils.py (已剪切)
import os
def listfiles(path):
return [_ for _ in os.listdir(path) if os.path.isfile(os.path.join(path, _))]
def get_base_path():
current_directory_path = os.path.realpath(os.path.dirname(__file__))
base_path = os.path.split(os.path.split(current_directory_path)[0])[0]
return base_path
Dockerfile (已剪切) - 在/ alpine / dist中构建静态资源,打印文件结构,以及以高山用户身份启动脚本(在剪切部分中创建)
ENV INSTALL_PATH /alpine
ENV ALPINE_USER alpine
RUN adduser --disabled-password --gecos '' $ALPINE_USER
RUN mkdir -p $INSTALL_PATH
WORKDIR $INSTALL_PATH
COPY js js
WORKDIR $INSTALL_PATH/js
RUN npm run build
WORKDIR $INSTALL_PATH
COPY . .
RUN echo $INSTALL_PATH
RUN ls -la $INSTALL_PATH
RUN echo $INSTALL_PATH/dist
RUN ls -la $INSTALL_PATH/dist
CMD sudo -u $ALPINE_USER python manage.py api run
docker-compose.yml (已剪切)
services:
api:
build: .
volumes:
- '.:/alpine'
links:
- postgres:postgres
postgres:
image: 'postgres:10'
volumes:
- './tmp/postgres:/var/lib/postgresql/data'
docker-compose up --build (Snipped) - / alpine / dist存在且包含预期资产,但python无法在同一路径上看到它们
Step 25/29 : RUN echo $INSTALL_PATH
/alpine
Step 26/29 : RUN ls -la $INSTALL_PATH
total 80
drwxr-xr-x 15 root root 4096 Feb 8 15:14 .
drwxr-xr-x 85 root root 4096 Feb 8 15:14 ..
-rw-r--r-- 1 root root 1026 Feb 8 15:13 Dockerfile
-rw-r--r-- 1 root root 0 Aug 17 06:06 README.md
-rw-r--r-- 1 root root 0 Aug 17 06:04 __init__.py
drwxr-xr-x 2 root root 4096 Feb 8 15:14 config
drwxr-xr-x 3 root root 4096 Feb 8 04:22 dist
-rw-r--r-- 1 root root 379 Feb 8 15:13 docker-compose.yml
drwxr-xr-x 17 root root 4096 Feb 7 19:20 js
-rw-r--r-- 1 root root 319 Oct 2 19:19 manage.py
drwxr-xr-x 4 root root 4096 Oct 10 19:01 migrations
drwxr-xr-x 8 root root 4096 Jan 9 16:31 alpine
-rw-r--r-- 1 root root 738 Feb 7 17:04 requirements.txt
Step 27/29 : RUN echo $INSTALL_PATH/dist
/alpine/dist
Step 28/29 : RUN ls -la $INSTALL_PATH/dist
total 16
drwxr-xr-x 3 root root 4096 Feb 8 04:22 .
drwxr-xr-x 15 root root 4096 Feb 8 15:14 ..
-rw-r--r-- 1 root root 504 Feb 8 04:22 index.html
drwxr-xr-x 4 root root 4096 Feb 8 04:22 static
Step 29/29 : CMD sudo -u $ALPINE_USER python manage.py api run
api_1 | 2018-02-08 15:14:26,601 - alpine.app/_init_sanic - INFO - /alpine/dist
api_1 | Traceback (most recent call last):
api_1 | File "manage.py", line 19, in <module>
api_1 | cli()
api_1 | File "/usr/local/lib/python3.6/site-packages/click/core.py", line 722, in __call__
api_1 | return self.main(*args, **kwargs)
api_1 | File "/usr/local/lib/python3.6/site-packages/click/core.py", line 697, in main
api_1 | rv = self.invoke(ctx)
api_1 | File "/usr/local/lib/python3.6/site-packages/click/core.py", line 1066, in invoke
api_1 | return _process_result(sub_ctx.command.invoke(sub_ctx))
api_1 | File "/usr/local/lib/python3.6/site-packages/click/core.py", line 1066, in invoke
api_1 | return _process_result(sub_ctx.command.invoke(sub_ctx))
api_1 | File "/usr/local/lib/python3.6/site-packages/click/core.py", line 895, in invoke
api_1 | return ctx.invoke(self.callback, **ctx.params)
api_1 | File "/usr/local/lib/python3.6/site-packages/click/core.py", line 535, in invoke
api_1 | return callback(*args, **kwargs)
api_1 | File "/alpine/alpine/cli/api.py", line 55, in run
api_1 | app = AlpineApp(config_path=config_path)
api_1 | File "/alpine/alpine/app.py", line 80, in __init__
api_1 | self._init_sanic()
api_1 | File "/alpine/alpine/app.py", line 89, in _init_sanic
api_1 | logger.info(listfiles(os.path.join(get_base_path(), 'dist')))
api_1 | File "/alpine/alpine/utils/__init__.py", line 42, in listfiles
api_1 | return [_ for _ in os.listdir(path) if os.path.isfile(os.path.join(path, _))]
api_1 | FileNotFoundError: [Errno 2] No such file or directory: '/alpine/dist'
alpine_api_1 exited with code 1
docker run -it alpine_api sudo -u alpine python manage.py api run (Snipped) - 当附加到容器后,找到预期的文件
2018-02-08 15:34:34,920 - alpine.app/_init_sanic - INFO - /alpine/dist
2018-02-08 15:34:34,921 - alpine.app/_init_sanic - INFO - ['index.html']
[2018-02-08 15:34:35 +0000] [1] [INFO] Goin' Fast @ http://0.0.0.0:8000