Docker refuse to use ssh key despite mounting it as a volume

I am trying to use git within the container without needing to sign to it everytime so I generated ssh key and tried mounting them as volume bs it gives me an error denied access to public key

What is your exact full error message? Public key should be accessible, but private keys must be readable only by the owneer, If you generated the keys on Windows and mounted into a Linux container, that means your keys are probably readable by everyone. Although you wrote in the title you mounted the keys “as a volume”, if it is actually a bind mount where you mount a folder from the host that might not work. The best way is to generate the keys in a container into an actual named volume and mount that in the container where you need it.

Note that the keys must have the right ownet as well. If you generate the keys with a user who has the UID 1000 and then try to use that in a container in which the UID is 1002, that will not work either.

Furthermore, I think it makes sense to share the Dockerfile and the command/compose file that triggers the build.

I mounted the ssh folder like this


And the error i got the the private key is too open and permission denied to public key

Ps:i am working on docker windows so my question what is the right way to do it ?

Please show us you Dockerfile as well, as the key to use a volume during build must be in there. The volumes in the compose file are just runtime configurations, not build time configurations.

this is the docker file :

FROM node:16.17.0
# Add a work directory
WORKDIR /app
# Copy app files
COPY . .
COPY docker-entrypoint.sh docker-entrypoint.sh
RUN chmod +x docker-entrypoint.sh
# Expose port
EXPOSE 3000

this the docker-entry.sh:

#!/bin/bash
set -e
# Remove a potentially pre-existing server.pid for Rails.
rm -f /app/tmp/pids/server.pid
yarn install
# Then exec the container's main process (what's set as CMD in the Dockerfile).
exec "$@"

this docker-compose.yml :

version: "3.5"
services:
  app:
    build: .
    entrypoint: /app/docker-entrypoint.sh
    command: yarn start
    volumes:
      - .:/app
      - node_modules:/app/node_modules
      - ~/.ssh:/root/.ssh:ro
       ~/.gitconfig:/root/.gitconfig 
    ports:
      - 3001:3000
       
volumes:
  node_modules:
networks:
  default:
    name: connection
    external: true

i made a wrong assumption: I thought you try to clone the repo during image build…
though, I don’t see any git related command in either your Dockerfile nor in your entrypoint script.

Your compose file misses a - for the last entry in the volumes sequence. I am surprised docker compose didn’t complain about it…

Also you need to make sure that you follow the suggestions @rimelek made in his response.

I have already told you the only think I could recommend. Please, read my previous post again and ask about the part that you don’t understand so I can explain it an other way.

However @meyay noticed what I could not. That missing “-” before the volume mapping should have given you a syntax error. It is not just invalid compose file, but an invalid YAML file. Maybe you just tried something to fix the problem and forgot to remove that part before sharing. If it happened, make sure you always share the code that you have tested.