用于码头作业的上下文或工作目录

我在学码头

我需要指定码头图像的工作目录,我想应该是这样的:

version: '2'
services:
test:
build:
context: ./dir

现在我想让图像 python:onbuild./dir上运行,但是我不想在 ./dir内部创建任何 Dockerfile

docker-compose手册上可没这么说。

有可能吗? 怎么做?

156402 次浏览

The build configuration in Docker Compose just ends up in a call to docker build, so you need to have a Dockerfile to use that workflow.

As the docs for python:onbuild say, you can start with a minimal Dockerfile that just contains FROM python:onbuild. But as they also say, :onbuild isn't a great option, you'll have much more control building your own Dockerfile FROM python.

I think you're looking for working_dir. Search for "working_dir" in the docker-compose reference.

You can specify the working directory as follows.

version: '2'
services:
test:
build:
context:    /path/to/source/dir/
dockerfile: /path/to/custom-Dockerfile/relative/tocontextdir/Dockerfile
working_dir: /app

Possibly not exactly what you were looking for, but you can specify the "context" for the docker build to be a different directory to where the actual Dockerfile lives.

build:
context: ./folder/containing/files
dockerfile: path/to/dockerfile/relative/to/context/Dockerfile

Any ADD/COPY commands in your Dockerfile then act as if they are relative to the context regardless of where the Dockerfile actually is.