Phuc Nguyen
Phuc Nguyen

Reputation: 365

How to replace file content with sed

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 [email protected]

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!

Upvotes: 1

Views: 1296

Answers (2)

tamth
tamth

Reputation: 154

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

Upvotes: 1

atline
atline

Reputation: 31564

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...

Upvotes: 1

Related Questions