1

I have a Dockerfile with the following content:

FROM ubuntu:bionic as builder

ARG DIGITALOCEAN_ACCESS_TOKEN
ARG K8S_CLUSTER
ARG ARGO_SERVER
ARG ARGO_USERNAME
ARG ARGO_PW

RUN apt-get update
RUN apt-get install -y curl

#WORKDIR /app

RUN curl -L https://github.com/digitalocean/doctl/releases/download/v1.41.0/doctl-1.41.0-linux-amd64.tar.gz  | tar xz
RUN curl -LO https://storage.googleapis.com/kubernetes-release/release/`curl -s https://storage.googleapis.com/kubernetes-release/release/stable.txt`/bin/linux/amd64/kubectl
RUN curl -L https://github.com/argoproj/argo-cd/releases/download/v1.4.3/argocd-linux-amd64 -o argocd

RUN chmod +x ./argocd
RUN chmod +x ./kubectl

RUN mv ./argocd /usr/local/bin/
RUN mv ./kubectl /usr/local/bin/
RUN mv ./doctl /usr/local/bin/


CMD doctl auth init -t ${DIGITALOCEAN_ACCESS_TOKEN}; \
    doctl kubernetes cluster kubeconfig save ${K8S_CLUSTER}; \
    kubectl get svc; \
    argocd login ${ARGO_SERVER} --username ${ARGO_USERNAME} --password ${ARGO_PW} --grpc-web; \
    argocd context; \
    argocd repo list;

ENTRYPOINT ["/usr/local/bin/argocd"] 

Running the container:

 docker run --rm -it \                                    
        --env=DIGITALOCEAN_ACCESS_TOKEN=c24a7f963ba86e1d38aff12 \
        --env=K8S_CLUSTER=k8s \
        --env=ARGO_SERVER=argocd.pod.io \
        --env=ARGO_USERNAME=user \
        --env=ARGO_PW=password \
        --name argocd \
        argo-cli repo list 

it shows:

FATA[0000] Argo CD server address unspecified  

the connection to server should get established through the commands:

CMD doctl auth init -t ${DIGITALOCEAN_ACCESS_TOKEN}; \
    doctl kubernetes cluster kubeconfig save ${K8S_CLUSTER}; \
    kubectl get svc; \
    argocd login ${ARGO_SERVER} --username ${ARGO_USERNAME} --password ${ARGO_PW} --grpc-web; \
    argocd context; \
    argocd repo list; 

What I am trying to achieve is, when I pass the argument, like I did it above with argo-cli repo list, then the connection to the server should be already established to get the result.

softshipper
  • 32,463
  • 51
  • 192
  • 400
  • 1
    The single command your container will run, with this `Dockerfile` and `docker run` command, is `/usr/local/bin/argocd argo-cli repo list`. Is that a meaningful command? Is it what you intend? – David Maze Apr 19 '20 at 12:12

1 Answers1

2

When running a Docker container, any arguments you supply on the command line will overwrite the arguments in CMD.

From the CMD documentation:

There can only be one CMD instruction in a Dockerfile. If you list more than one CMD then only the last CMD will take effect.

The main purpose of a CMD is to provide defaults for an executing container. These defaults can include an executable, or they can omit the executable, in which case you must specify an ENTRYPOINT instruction as well.


To set up the server like you are trying to do with CMD, you can create a shell script to run setup and run that in your ENTRYPOINT.

create a file with some name, let's say setup_server.sh, with contents:

#!/usr/bin/env bash
doctl auth init -t ${DIGITALOCEAN_ACCESS_TOKEN}
doctl kubernetes cluster kubeconfig save ${K8S_CLUSTER}
kubectl get svc
argocd login ${ARGO_SERVER} --username ${ARGO_USERNAME} --password ${ARGO_PW} --grpc-web
argocd context
argocd repo list
"$@"

Copy that file into the Dockerfile:

COPY setup_server.sh /usr/local/bin

And modify your ENTRYPOINT to run setup_server.sh before /usr/local/bin/argocd:

ENTRYPOINT setup_server.sh /usr/local/bin/argocd

(all untested)

Community
  • 1
  • 1
jkr
  • 17,119
  • 2
  • 42
  • 68
  • But the `CMD` command should prepare for the server connection. – softshipper Apr 18 '20 at 20:41
  • `CMD` is meant to provide default arguments. If you run your container with other arguments on the command-line, `CMD` will have no effect. – jkr Apr 18 '20 at 20:42
  • The goal is, to prepare the container for the connection to the server, that at the end, I can just pass argument. – softshipper Apr 18 '20 at 20:46
  • "$@" what does it mean? – softshipper Apr 18 '20 at 21:18
  • `"$@"` is all of the parameters passed to a script (see https://stackoverflow.com/a/9994328/5666087). That means you can pass an executable to it and other command-line arguments to be run after the setup. – jkr Apr 18 '20 at 21:22