简体   繁体   English

docker-compose音量未正确安装

[英]docker-compose volume not mounting correctly

I am following the flask/docker tutorial on testdriven.io . 我正在关注testdriven.io上的flask / docker教程。 Within Part One on the third section Docker Config I am following the instructions in order to mount the project directory as a volume within the docker container for development. 在第三部分Docker配置的第一部分中,我按照说明将程序目录作为卷安装在docker容器中进行开发。 Unfortunately following the instructions in the tutorial does not mount the volume correctly with docker-compose. 不幸的是,按照本教程中的说明操作,无法使用docker-compose正确安装卷。 Instead the directory inside the container is empty. 而是容器内的目录为空。

The following is the Dockerfile for the container in question. 以下是有问题的容器的Dockerfile

FROM python:3.6.1

# set working directory
RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app

# add requirements (to leverage Docker cache)
ADD ./requirements.txt /usr/src/app/requirements.txt

# install requirements
RUN pip install -r requirements.txt

# add app
ADD . /usr/src/app

# run server
CMD python manage.py runserver -h 0.0.0.0

And this is the docker-compose.yml file 这是docker-compose.yml文件

version: '2.1'

services:

  users-service:
    container_name: users-service
    build: .
    volumes:
      - '.:/usr/src/app'
    ports:
      - 5001:5000 # expose ports - HOST:CONTAINER

The directory in question is the /usr/src/app directory inside the users-service container. 有问题的目录是users-service容器内的/ usr / src / app目录。 When I do: 当我做:

>> docker-compose build 
>> docker-compose up -d
>> docker-compose run users-service bash

When I ls inside the container I am in the correct directory (/usr/src/app) but the directory is empty. 当我ls容器我在正确的目录(/ usr / src目录/应用程序)内,但该目录是空的。 This also causes the CMD to fail as it can't find the manage.py file which is in the root of the project directory. 这也会导致CMD失败,因为它无法找到项目目录根目录中的manage.py文件。

I've seen other posts on stack-overflow with similar titles, unfortunately none of the solutions have been able to solve my problem. 我已经看到堆栈溢出的其他帖子有类似的标题,不幸的是没有一个解决方案能够解决我的问题。 I've tried changing the local volume path from relative to absolute with no difference in result.Thank you in advance for anybody who is able to help a fellow out. 我已经尝试将本地音量路径从相对位置更改为绝对值,但结果没有差异。请提前感谢任何能够帮助其他人的人。

EDIT: 编辑:

Having run docker inspect on the container, below is the information found under Mounts and Config - Volume : 在容器上运行docker inspect ,下面是Mounts and Config - Volume下的信息:

"Mounts": [
            {
                "Type": "bind",
                "Source": "/home/jonathan/projects/flask-microservices-users",
                "Destination": "/usr/src/app",
                "Mode": "rw",
                "RW": true,
                "Propagation": "rprivate"
            }
        ],
 ...
 "Config": {
        ...
        "Image": "flaskmicroservicesusers_users-service",
        "Volumes": {
            "/usr/src/app": {}
        },
        "WorkingDir": "/usr/src/app",
        ...

The issue is that you can't mount a volume to an existing path in the container and expect the data within that path to be syncd. 问题是您无法将卷装入容器中的现有路径,并期望同步该路径中的数据。 This is not how unix volume mounts work. 这不是unix卷安装工作的方式。

An explanation can be found here: 可以在这里找到解释:

https://github.com/moby/moby/issues/32203 https://github.com/moby/moby/issues/32203

Have a look here if you need to make data inside a container persistent: 如果您需要在容器内部生成数据,请查看此处:

https://github.com/moby/moby/issues/4361#issuecomment-36317097 https://github.com/moby/moby/issues/4361#issuecomment-36317097

Remove the following from your Dockerfile : Dockerfile中删除以下内容

# add app
ADD . /usr/src/app

Review the answer here for more info: 在此处查看答案以获取更多信息:

https://stackoverflow.com/a/27753725/1799408 https://stackoverflow.com/a/27753725/1799408

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM