1

I am trying some something out with gitlab-runner image,

    FROM gitlab/gitlab-runner:alpine
    WORKDIR /app
    COPY . /app
    RUN apk add yarn && yarn install

    RUN yarn --version        # this layer prints 1.16.0

    RUN ng build --prod
    EXPOSE 3000
    CMD ["yarn", "run", "start"]

above is the docker file I have created

    docker build -t runner:1 .

I was able to build the image successfully

    docker run -p 3000:3000 runner:1

but when I try to run the container it gives me below error

`*FATAL: Command yarn not found.*`

not sure about the behavior, if it is able to install yarn (apk add yarn) in base images and install the dependencies using yarn install then how it is not able to find the yarn command while running the container? Where I am going wrong.

Also at which directory yarn is installed in the alpine ?

I know it is not an efficient docker file, but I am trying to run the container first before optimizing it.

β.εηοιτ.βε
  • 33,893
  • 13
  • 69
  • 83
Dan
  • 651
  • 1
  • 14
  • 31
  • check yarn is there or not using `yarn --version` by adding line. – Harsh Manvar May 28 '20 at 19:34
  • @HarshManvar yarn is present in the container, I have verified by adding a layer **(RUN yarn --version)** to docker file and in response I received **(1.16.0)**. do you know th directory it is installed in the alpine ? – Dan May 28 '20 at 19:46
  • Might be totally unrelated, but why are you using `gitlab/gitlab-runner:alpine` and not `alpine` ? – Blusky May 28 '20 at 19:52
  • @Blusky I am planning to integrate the app with gitlab runner registration – Dan May 28 '20 at 19:56

2 Answers2

2

It outputs the version. It means the yarn is installed already. You could find the path the same as you find the version.

RUN which yarn

Step 6/10 : RUN which yarn
 ---> Running in 0f633b81f2ed
/usr/bin/yarn

We can see the /usr/bin/ has added to PATH.

 Step 7/11 : RUN echo $PATH
 ---> Running in fc3f40b6bfd9
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

But I couldn't figure out why isn't reading yarn from the PATH.

So, we have set the PATH explicitly in our Dockerfile.

ENV PATH=${PATH} 

But still, the issue persists. Now we have to separate yarn and commands as ENTRYPOINT and CMD respectively in the Dockerfile.

ENTRYPOINT ["yarn"]
CMD ["run", "start"]

Updated Dockerfile

FROM gitlab/gitlab-runner:alpine

ENV PATH=${PATH}

WORKDIR /app
COPY . /app
RUN apk add yarn && yarn install

RUN yarn --version        # this layer prints 1.16.0
RUN ng build --prod

EXPOSE 3000
ENTRYPOINT ["yarn"]
CMD ["run", "start"]
---

$ docker run -p 3000:3000 harik8/yarn:latest 
yarn run v1.16.0
error Couldn't find a package.json file in "/app"
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

The behaviours of the base image look unusual. It'd be better to go through it.

hariK
  • 2,722
  • 13
  • 18
  • In this setup you would replace gitlab runner with the app. Is that the aim? – Miq May 29 '20 at 19:03
  • I added the entrypoint and it worked, and you are right the behaviour is unexpected, still not able to unsertand why it is not taking the yarn path – Dan May 31 '20 at 05:34
1

To build your app you shouldn't use gitlab-runner image, but a 'node' one.

Gilab-runner image is for running a gitlab agent which can be connected to docker engine and spawn the node container in which you will execute your build, in your case a docker image build.

To use gilab you need to prepare a gitlab-ci file where you will define what steps and which 'services' you need to do your build.

Tl;dr: change base image to node:latest and as a entirely separate work setup gitlab runner.

However if your aim is to have your application to extend gitlab runner, try docker multistage builds.

First, use node:latest image to build your app, and then copy the build output into gitlab-runner.

Runtime images such as gitlab-runner are stripped from build tools like yarn or npm, that's why your image fails. Main goal is to keep runtime images as small as possible and sdk's are not needed and sometimes dangerous when it comes to production-level work.

Miq
  • 3,931
  • 2
  • 18
  • 32
  • @Mig thanks for the sharing the details, but is there any way to run the app using gitlab-runner images only, I have used it because of requirement. is there any other way to run it? – Dan May 29 '20 at 05:00
  • Docker containers are meant to run a single app only and close when app exits. I theory you could but its strongly not recommended. What's the reason behind the requirement? I'd suggest rethinking it. – Miq May 29 '20 at 18:59