0

I'm trying to create a docker image with multi-stage build:

  1. First stage pulls a private git repo
  2. Second stage builds that repo and runs a script

I have my git credentials stored in /Users/kabc/.config/git/credentials file on my host machine.

I'm mounting this in docker-compose like so:

  joke-generator:
    build:
      context: ./
      dockerfile: Dockerfile.joke-gen
    volumes:
      - ${HOME}/.config:/etc/.config:ro
      - ${HOME}/.gitconfig:/etc/.gitconfig:ro

Here's the dockerfile:

FROM alpine/git:2.36.3 as repository

WORKDIR /code

RUN git config --global credential.helper 'store --file /etc/.config/git/credentials'
RUN git -c http.sslVerify=false clone https://github.com/private-company/joke.git

FROM openjdk:19-jdk-alpine3.16

WORKDIR /code

COPY --from=repository /code/joke/ /code

RUN cd joke-generator
RUN mvn clean install -DskipTests

CMD ["./generate-joke.sh"]

But I'm getting this error in the git clone step:

fatal: could not read Username for 'https://github.com': No such device or address

Can I get some help with this please?

Kartik
  • 7,677
  • 4
  • 28
  • 50
  • 1
    Why do you need to run `git clone` from inside the Dockerfile? Particularly with this straightforward setup, could you add the Dockerfile to the repository itself, check it out on the host, and `docker build` an image the same way you might `mvn install` a jar file? – David Maze Jan 04 '23 at 01:14
  • @DavidMaze currently all the devs are checking out the repo and running mvn install manually to generate a joke, which is then used to test this other app (that holds this docker-compose and dockerfile). I wanted to make it easy as just clicking a play button in our existing docker-compose. – Kartik Jan 04 '23 at 01:24
  • 1
    For the actual credentials: Is the url in the host written correctly? Is the protocol set to `https`? Do you see the expected output when you `cat` the `/etc/.config/git/credentails`-file from within the container? – Turing85 Jan 04 '23 at 01:28
  • Notice that this "loop" over checking out the repository and building the project has major drawbacks. Mainly: 1) for changes to be visible, they have to be in the repository and 2) maven dependencies are not cached, so every (local) build has to re-download all dependencies. A more convenient way would most probably building the `joke`-container in a CI/CD-pipeline and push the image to a registry, so developers can just pull the `joke`-container from the registry (this eliminates the caching issue) – Turing85 Jan 04 '23 at 01:31
  • 1
    Volumes are mounted at runtime, not during the build. – BMitch Jan 04 '23 at 01:43
  • Yes , @BMitch is right... have said this time and time again before... don't know how I was able to oversee it this time... – Turing85 Jan 04 '23 at 01:45
  • @BMitch ah, yes. Thanks, I'll try to think of some other solution. – Kartik Jan 04 '23 at 02:03
  • For the more generic question, here's a possible duplicate: https://stackoverflow.com/a/56077990/596285 – BMitch Jan 04 '23 at 10:13

0 Answers0