67

I'm trying to pull the repository I'm working on while doing a docker build, so I followed this tutorial so I added in my Dockerfile

# this is our first build stage, it will not persist in the final image
FROM ubuntu as intermediate

# install git
RUN apt-get update \
    apt-get upgrade \
    apt-get install git

# add credentials on build
RUN mkdir ~/.ssh && ln -s /run/secrets/host_ssh_key ~/.ssh/id_rsa

# make sure your domain is accepted
RUN touch /root/.ssh/known_hosts
RUN ssh-keyscan github.org >> /root/.ssh/known_hosts

RUN git clone [email protected]:my-repo/myproject.git

FROM ubuntu
# copy the repository form the previous image
COPY --from=intermediate /your-repo /srv/your-repo

In my docker-compose.yml I added

secrets:
  host_ssh_key:
    file: ~/.ssh/id_rsa

And I'm adding

secrets:
  - host_ssh_key

In one of the service.

Since this is only for my local use (I don't plan on deploying or using this for the production, I just want to test it out) it's fine to use it this way - if it'll work because currently the build fails at the git install stage with error

E: The update command takes no arguments

ERROR: Service 'web' failed to build: The command '/bin/sh -c apt-get update apt-get upgrade apt-get install git' returned a non-zero code: 100

What am I missing?

3
  • Any chance you could accept an answer? So that it indicates to others that some solution was helpful for you. Would be great if you did on all your question that have answers. Commented Apr 16, 2019 at 10:22
  • Sure, even though in the end I did it differently. Thanks for the help :) Commented Apr 16, 2019 at 12:53
  • Yeah its fine for local, and in fact not even a terrible idea for a dev server (But NOT staging or production!!!!) Commented Jul 15, 2022 at 3:24

3 Answers 3

142

You are overanalyzing this. It's just a simple typo. Should be:

RUN apt-get update && \
    apt-get upgrade -y && \
    apt-get install -y git

Those are 3 separate commands.

Sign up to request clarification or add additional context in comments.

5 Comments

Still getting The command '/bin/sh -c apt-get update && apt-get upgrade && apt-get install git' returned a non-zero code: 1
It's another issue, now apt-get install is waiting for you to submit the installation. So you should add -y after install. From your typo it seems to me like you don't know how to work with Linux. Maybe you should read everything around apt-get?
I'll try with the flag, but overall I'll probably just skip the git clone in the build process. At least that's what the devops is doing. I'm just testing the docker out, not my main area of expertise :D
in node:alpine: RUN apk update && \ apk upgrade && \ apk add git
Run update after git installation. Try that one: RUN apt-get install --assume-yes git && apt-get update && apt-get clean all
21

For those working with alpine images, RUN apk add --no-cache git did the trick for me.

1 Comment

Thank You this saved the some time. Please why did you use ---no-cache?
14

Some thoughts:

    1. Replace apt-get install git with apt-get install --assume-yes git. Without the --assume-yes it will prompt you for confirmation, which you are unable to give and it will be smart enough to figure that out and assume you meant "NO".
    1. You added the ssh key, but did you confirm it was 0600. I would just copy it and specifically chmod 0600 ~/.ssh/id_rsa to be sure.

Overall, your Dockerfile looks very clever and I got some fresh ideas from reading it.

2 Comments

Just read the tutorial - very good. They use the -y switch. I use the --assume-yes switch. They are synonyms and I think that is what you are missing. If you supplied a valid 'id_rsa' file then my second point is not applicable.
your edit to the question should be reverted, it clearly conflicts with author's intent.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.