QStorm
QStorm

Reputation: 183

How to replace content of a file when starting a nginx docker container

I would like to change the content of a configuration file when starting a docker nginx container via docker-compose. I copied a script containing a sed command to the "docker-entrypoint.d" directory in order to be executed on startup. Note: nginx 1.19 introduced an entrypoint and a few scripts to alter the configuration on the start of the container.

However, I encounter the following issues:

  1. "sed" always returns "No such file or directory" no matter under which path I place "env.js". I have already tried different paths.
  2. The init scripts located in "docker-entrypoint.d" are ignored (not executed) if "ENTRYPOINT ["nginx", "-g", "daemon off;"]" is set in the Dockerfile. Therefore, ENTRYPOINT is commented in the posted Dockerfile.

Here are the relevant files:

Dockerfile:

# 1.) build
FROM node:14.16.1-alpine3.13 AS builder
WORKDIR /app
COPY . .

# 2.) target image
FROM nginx:1.21.3-alpine
COPY --from=builder /app/myinit.sh ./docker-entrypoint.d
COPY --from=builder /app/env.js /usr/share/nginx/html

#ENTRYPOINT ["nginx", "-g", "daemon off;"]

myinit.sh:

sed -i 's~__env.apiUrlRoot.*~__env.apiUrlRoot = '$MYENV';~g' /usr/share/nginx/html/env.js

env.js:

(function (window) {
  window.__env.apiUrlRoot = 'http://localhost:9090';

}(this));

docker-compose.yml:

version: '3.8'

services:
  mytest:
    image: 'tst:latest'
    build: 
      context: .
      dockerfile: Dockerfile
    container_name: tst1
    environment:
      - MYENV=SomeValue

docker-compose command:

docker-compose -f docker-compose.yml up --build

Upvotes: 0

Views: 1536

Answers (1)

QStorm
QStorm

Reputation: 183

I found the root cause of the first issue: "myinit.sh" contained windows style CRLF instead of unix style LF. I changed that and the shell script executed properly. BTW: you can run "dos2unix " to replace CRLF by LF.

Upvotes: 1

Related Questions