Docker Build from compose using a Dockerfile from Host?

Hi,

i want to build my own Image from a Dockerfile via Docker-compose. The Dockerfile for this is on the Host System in /home/docker/

How to do this ?

version: "1.0"
name: "test"
services:
    test:
        container_name: test
        build:
          context: /home/docker/test
          dockerfile: dockerfile
        network_mode: host
        environment:
          - TZ=Europe/Berlin
        volumes:
             - /home/docker/test:/home/docker/test      
        logging:
          driver: json-file   
        restart: unless-stopped

The Dockerfile can not be outside the build context. Every file in the context path will be copied into the build context. COPY will only be able to copy files from the build context into the image.

You either need to put the Dockerfile called dockerfile (the default name is Dockerfile and not dockerfile) into /home/docker/test, or use `/home/docker/ as context, and make sure to align the paths inside the Dockerfile as well.

2 Likes

Hmm, that doesn’t work.

I’m using Portainer. There might be other paths here ?

“doesn’t work” is rarely helpful for others that are not sitting in front of your screen. Please start sharing exact error messages (as in copy/paste them), so we don’t have to guess what exactly doesn’t work.

On another note: an api client must be able to access the files/folders, to pass the client side verification. I assume you did not bind the host path /home/docker into the Portainer container’s path /home/docker.

I never understood why people use Portainer for anything else that execing into containers, and checking logs
 It just adds another layer of complexity that leads to confusion, if not properly understood. Functional issues caused by using Portainer, are best discussed in a Portainer related forum amongst peers that use Portainer as well.

1 Like
docker build -t test -f /home/docker/test/Dockerfile .

Builds it fine

Portainer doenst work with a Docker compose “Stack” Here i get the Error:

Failed to interpolate config for stack test. Error: compose config operation failed: failed to load the compose file: validating /data/compose/32/v4/docker-compose.yml: services.test.build Additional property Dockerfile is not allowed

Thats true :grinning_face:

The error message says it all: either Portainer itself or the docker sdk version it uses does not allow the build property.

This is clearly an issue with Portainer, and should be addressed to its maintainers.

If it was copy-pasted, one thing I notice is that the message mentions “Dockerfile” with an uppercase first letter, which should be lowercase as it was in your compose file in your first post.

By the way you can also try the alternative property, dockerfile_inline

So you don’t need an external Dockerfile and you can keep everything in the compose file.

Quote from the docs:

services:
 base:
  build:
    context: .
    dockerfile_inline: |
      FROM alpine
      RUN ...
 my-service:
  build:
    context: .
    dockerfile_inline: |
      FROM base # image built for service base
      RUN ...
    additional_contexts:
      base: service:base
2 Likes