WiredCoder
WiredCoder

Reputation: 926

Passing variables from docker build file to docker file

I am trying to pass a variable simply from docker build file to docker file, however the variable is never resolved

version: '3'
services:
    movie-discovery-server:
        container_name: movie-discovery-server
        build:
            args:
              PORT: 8761
            context: /Users/admin/Downloads/movie-discovery-server
            dockerfile: Dockerfile
        image: movie-discovery-server:latest
        environment:
          - PORT=8761
        expose:
            - 8761

And here is the Dockerfile

FROM openjdk:8
ADD ./target/movie-discovery-server-0.0.1-SNAPSHOT.jar movie-discovery-server-0.0.1-SNAPSHOT.jar
ARG PORT
ENTRYPOINT ["java", "-jar", "movie-discovery-server-0.0.1-SNAPSHOT.jar", "--server.port=$PORT"]

however the $PORT is never resolved

---EDIT--- It turned out that I have made couple of mistakes in my files, I my answer below will contain the right file format

Upvotes: 0

Views: 240

Answers (2)

WiredCoder
WiredCoder

Reputation: 926

It turned out that I was doing couple of things in the wrong way. For the docker compose file I introduced the following changes:

  1. Removed the args tag as it had no effect
  2. Replace the expose tag with ports tag, as the mapping was not working correctly

    version: '3'
    services:
      movie-discovery-server:
        container_name: movie-discovery-server
        build:
            context: /Users/admin/Downloads/movie-discovery-server
            dockerfile: Dockerfile
        image: movie-discovery-server:latest
        ports:
          - "8761:8761"
        environment:
          - PORT=8761
    

For the DockerFile, I have applied the advice I had from @codestation and let go with the JSON format

FROM openjdk:8
ADD ./target/movie-discovery-server-0.0.1-SNAPSHOT.jar movie-discovery-server-0.0.1-SNAPSHOT.jar
ARG PORT
ENTRYPOINT java -jar movie-discovery-server-0.0.1-SNAPSHOT.jar --server.port=$PORT

Upvotes: 0

codestation
codestation

Reputation: 3498

Neither ENTRYPOINT nor CMD will resolve variables when json array format is used.

If you need to resolve the PORT either use an entrypoint script or use the shell form notation for ENTRYPOINT

ENTRYPOINT java -jar movie-discovery-server-0.0.1-SNAPSHOT.jar --server.port=$PORT

Upvotes: 2

Related Questions