1

I'm trying to build a nodejs app on docker. I have the following files

--Dockerfile
FROM mhart/alpine-node:12

RUN echo -e "http://nl.alpinelinux.org/alpine/v3.5/main\nhttp://nl.alpinelinux.org/alpine/v3.5/community" > /etc/apk/repositories

RUN apk add --no-cache make gcc g++ python git
USER root

RUN apk --update add bash
RUN apk add dos2unix --update-cache --repository http://dl-3.alpinelinux.org/alpine/edge/community/ --allow-untrusted

COPY . /app
WORKDIR /app

RUN rm -rf ./docker-entrypoint.sh
COPY docker-entrypoint.sh /
RUN dos2unix /docker-entrypoint.sh
RUN chmod 777 -R /docker-entrypoint.sh
RUN npm install -g npm@6.14.5

RUN npm -v
RUN node -v

RUN npm install

RUN rm -rf ./config/test.json
COPY config/test.json ./config/test.json

RUN rm -rf /var/cache/apk/*
EXPOSE 3001
ENTRYPOINT [ "/docker-entrypoint.sh"]
# CMD npm start
ENTRYPOINT npm start
--docker-entrypoint.sh file
#!/bin/sh

config="./config/test.json"
mongoDB="mongodb:\/\/xxx.xxx.xxx.xxx:27017\/dbname"

sed -e "s/\"mongo\": \"\${MONGO}\"/\"mongo\": \"$mongoDB\"/g" -i $config

and this is my config/test.json output file in Docker

"mongo" : "${MONGO}",

The content of test.json has not been changed.

Could someone tell me where was wrong? Please show me how to fix it. Thank you!

Phuc Nguyen
  • 371
  • 1
  • 3
  • 14
  • Please extract a [mcve] and provide that instead. There's a lot that isn't actually necessary here. – Ulrich Eckhardt Dec 25 '20 at 16:22
  • Btw, don't put each command in a separate RUN instruction. Each instruction in Dockerfile produces a new layer - in particular, it means that your apk cache actually won't be deleted from the image. Besides, it adds unnecessary complexity to your image and litters it with extra metadata. Better chain commands, where possible. Here's the explanation https://stackoverflow.com/a/65076307/4486909 – Olesya Bolobova Dec 25 '20 at 23:06

2 Answers2

1

In this particular case it is your spacing before the :.

Try sed -e "s/\"mongo\" : \"\${MONGO}\"/\"mongo\": \"$mongoDB\"/g" -i $config

Probably not applicable here, but you can also match any spacing using regex: sed -e "s/\"mongo\"[ ]*:[ ]*\"\${MONGO}\"/\"mongo\": \"$mongoDB\"/g" -i $config

tamth
  • 154
  • 3
1

ENTRYPOINT [ "/docker-entrypoint.sh"]
ENTRYPOINT npm start

You define multiple entrypoint, see this:

Only the last ENTRYPOINT instruction in the Dockerfile will have an effect.

This means only npm start make effect, your docker-entrypoint.sh even did not run...

atline
  • 28,355
  • 16
  • 77
  • 113