简体   繁体   English

MacOS:如何使用 docker-compose.yml 和 Dockerfile 挂载卷?

[英]MacOS: How to mount volumes using docker-compose.yml and Dockerfile?

My project's folder structure looks like:我的项目的文件夹结构如下:

+ cython_test/
    + ny/
        + nyc/
            + docker-compose.yml
            + Dockerfile
            + pigeon.py
            + data/
                + temps.txt

The docker-compose.yml : docker-compose.yml

version: '2.1'
services:
  worker:
    build: .  
    volumes: 
      - ./:/opt

The Dockerfile : Dockerfile

FROM python:3.6-alpine3.10

RUN sleep 5000

To get the container started, I do:要启动容器,我会:

docker-compose -f "ny/nyc/docker-compose.yml" up -d --build

And I get the output:我得到了 output:

Alexs-MacBook-Pro:cython_test alexvissup$ cd "/Users/alexvissup/Codes/cython_test"
Alexs-MacBook-Pro:cython_test alexvissup$ docker-compose -f "ny/nyc/docker-compose.yml" up -d --build
Creating network "nyc_default" with the default driver
Building worker
Step 1/2 : FROM python:3.6-alpine3.10
 ---> 92a867c54c0f
Step 2/2 : RUN sleep 5000
 ---> Running in fdeb933e6cfc

When I exec into the container I expect to go into /opt and see everything inside my local nyc folder, but it's empty.当我执行到容器中时,我希望exec进入/opt并查看本地nyc文件夹中的所有内容,但它是空的。

I'm required to specify the volumes in the docker-compose.yml .我需要在docker-compose.yml中指定volumes Is this possible?这可能吗? What am I doing wrong?我究竟做错了什么?

Docker volume will be created when the containers are created and not during the build time. Docker 卷将在创建容器时创建,而不是在构建期间。 Your docker file is execting a sleep command for a long time, therefore the docker-compose does not create the container until the sleep functions completely finish.您的 docker 文件长时间执行睡眠命令,因此 docker-compose 在睡眠功能完全完成之前不会创建容器。 (I don't see why you need the sleep at all, it is not a common thing in Docker world) (我根本不明白你为什么需要睡眠,这在 Docker 世界中并不常见)

The second issue that you have here is that you do not define the container command nether in the docker-compose nor in the Dockerfile.您在这里遇到的第二个问题是您没有在 docker-compose 和 Dockerfile 中定义容器命令。 You must define one in order to keep the container running after building the docker image.您必须定义一个以在构建 docker 映像后保持容器运行。

You can try to use the below command to fix the issue (You may need to install python dependencies also )您可以尝试使用以下命令来解决问题(您可能还需要安装 python 依赖项)

FROM python:3.6-alpine3.10
# RUN pip install flask -- if you need to have some dependencies
WORKDIR /opt
ENTRYPOINT ["python", "pigeon.py"]

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

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