Docker Community Forums

Share and learn in the Docker community.

Passing arguments to the same container that admits two different arguments in jar file to docker-compose.yml

First, I apologize if this is a topic already resolved, I think not because I’ve been reading and searching in this forum and Stackoverflow and I can not find the answer. I’ve also been reading the documentation, but I can’t get it to work.
This is my problem, I need to be able to raise two services, which use the same image, created by me, which use several dependencies, such as Kafka, zookeeper and Eureka, so in principle the ideal is to run the processes with the docker-compose up command, but I can not get the service to accept any parameter using docker-compose.

This is the docker-compose.yml file

version: '3.7'
services:
demo-quartz-btc:
image: aironman/demo-quartz:0.0.2-SNAPSHOT
build:
  context: .
  dockerfile: Dockerfile
  args:
    - crypt_type: "btc"
links: 
  - "eureka-server"
  - "zookeeper"
  - "kafka"
deploy:
  replicas: 5
  resources:
    limits:
      cpus: "0.5"
      memory: 512M
  restart_policy:
      condition: on-failure
      
demo-quartz-eth:
image: aironman/demo-quartz:0.0.2-SNAPSHOT
build:
  context: .
  dockerfile: Dockerfile
  args:
    - crypt_type: "eth"
links:
  - "eureka-server"
  - "zookeeper"
  - "kafka"
deploy:
  replicas: 5
  resources:
    limits:
      cpus: "0.5"
      memory: 512M
  restart_policy:
    condition: on-failure

zookeeper:
image: confluentinc/cp-zookeeper:5.0.0
hostname: zookeeper
container_name: zookeeper
ports:
  - "2181:2181"
environment:
  ZOOKEEPER_CLIENT_PORT: 2181
  ZOOKEEPER_TICK_TIME: 2000

kafka:
image: confluentinc/cp-enterprise-kafka:5.0.0
hostname: kafka
container_name: kafka
depends_on:
  - zookeeper
ports:
  - "9092:9092"
  - "29092:29092"
environment:
  KAFKA_BROKER_ID: 1
  KAFKA_ZOOKEEPER_CONNECT: 'zookeeper:2181'
  KAFKA_LISTENER_SECURITY_PROTOCOL_MAP: PLAINTEXT:PLAINTEXT,PLAINTEXT_HOST:PLAINTEXT
  KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://kafka:9092,PLAINTEXT_HOST://localhost:29092
  KAFKA_METRIC_REPORTERS: io.confluent.metrics.reporter.ConfluentMetricsReporter
  KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR: 1
  KAFKA_GROUP_INITIAL_REBALANCE_DELAY_MS: 0
  CONFLUENT_METRICS_REPORTER_BOOTSTRAP_SERVERS: kafka:9092
  CONFLUENT_METRICS_REPORTER_ZOOKEEPER_CONNECT: zookeeper:2181
  CONFLUENT_METRICS_REPORTER_TOPIC_REPLICAS: 1
  CONFLUENT_METRICS_ENABLE: 'true'
  CONFLUENT_SUPPORT_CUSTOMER_ID: 'anonymous'

eureka-server:
image:  springcloud/eureka
hostname: eureka-server
ports:
  - "8761:8761"

And this is the Dockerfile file.

FROM adoptopenjdk/openjdk12:latest
MAINTAINER Alonso Isidoro <alonsoir@gmail.com>
ARG JAR_FILE
ARG crypt_type
RUN mkdir /opt/app
COPY target/demo-quartz-0.0.2-SNAPSHOT.jar /opt/app/demo-quartz.jar
COPY entry-point.sh /
RUN chmod +x entry-point.sh
ENTRYPOINT ["/entry-point.sh"]
CMD ["${crypt_type}"]

This is entry-point.sh

#!/bin/sh
echo "arguments: $1"
java -jar /opt/app/demo-quartz.jar $1

I’m using Engine: 19.03.1 and Compose: 1.24.1 running on osx 10.14.6 (18G84)

When I run docker-compose up, I got this:

services.demo-quartz-btc.build.args contains {"crypt_type": "btc"}, which is an invalid type, it should be a string
services.demo-quartz-eth.build.args contains {"crypt_type": "eth"}, which is an invalid type, it should be a string

EDIT

I realized that the part of the arguments in the services had a flaw. It has to be that way:

demo-quartz-btc:
image: aironman/demo-quartz:0.0.2-SNAPSHOT
build:
  context: .
  dockerfile: Dockerfile
  args:
    - crypt_type = btc

Instead of that:

demo-quartz-btc:
image: aironman/demo-quartz:0.0.2-SNAPSHOT
build:
  context: .
  dockerfile: Dockerfile
  args:
    - crypt_type:"btc"

But the problem persists because I see in the log the following:

demo-quartz-btc_1  | Arguments MUST be btc or eth. Exit. ${crypt_type} argument was used!

demo-quartz-eth_1  | Arguments MUST be btc or eth. Exit. ${crypt_type} argument was used!

It is part of the service code, when it picks up the argument by parameter, which means that it is taking as literal value the parameter given to CMD in the Dockerfile, instead of doing the transformation. Is this a bug or am I doing it wrong?

This is the code I mentioned, no weird

if (args.length != 1) {
    System.out.println("Insufficient arguments. Must be btc or eth. Applying btc...");
    cryptoType = "btc";
    //System.exit(-1);
}else
    cryptoType = args[0];
if (!cryptoType.equals("btc") || !cryptoType.equals("eth")){
    System.out.println("Arguments MUST be btc or eth. Exit. " + cryptoType + " argument was used!");
    System.exit(-1);
}

Please, help.