Home > other >  How to omit build context in docker-compose build
How to omit build context in docker-compose build

Time:11-10

I can omit (set to empty) build context using docker build:

docker build -t my_useless_image_name - << EOF

FROM ubuntu:22.04
RUN echo "I need no context to be built. Thnx"

EOF

How could I omit build context in the same way using docker-compose.yml?

version: "3.8"

services:
  srv1:
    build:
      context: ??
  srv2:
    build:
      context: .

I need a way beyond using .dockerignore

I found no answer in docker-compose official documentation.

CodePudding user response:

Compose doesn't allow this. More generally, Compose has no way to read external files and take action on them, and doesn't route its own standard input to any particular container or image build, so if you didn't provide a build context there would be no other way to pass the Dockerfile into the container.

If you're just trying to use some unmodified base image, use image: instead of build:. You can do many customizations using environment: and volume: to inject files. So, for example, you'll frequently see an unmodified postgres image used as a database with several environment variables set plus injecting a /docker-entrypoint-initdb.d directory.

In any case I'd probably avoid the docker build - path. Since it has no build context, it can't COPY local files into the image, which significantly limits what you can do with this approach. And if you're doing anything at all here you probably want the Dockerfile on disk and checked into source control, at which point you can put it in an otherwise-empty directory, name it Dockerfile, and use that directory as the build context.

version: '3.8'
services:
  using-the-image-directly:
    image: ubuntu:22.04
    # environment:
    # volumes:
  with-a-trivial-build-context:
    build: ./my-useless-image
# Create an empty directory that _only_ contains the Dockerfile
rm -rf my-useless-image
mkdir my-useless-image
cat >my-useless-image <<EOF
FROM ubuntu:22.04
# ...
CMD echo 'hello world' && sleep 15
EOF

# That mostly-empty directory is the build: context
docker-compose up
  • Related