cp command in yaml file

6/8/2018

i'm launching a glassfish pod on my Kubernetes cluster, and i'm trying to copy some .war files from a folder that's on my host, but the command cp always seems to fail.

my yaml file:

apiVersion: apps/v1
kind: Deployment
metadata:
 name: glassfish
spec:
# replicas: 2
 selector:
  matchLabels:
   app: glassfish
 strategy:
  type: Recreate
 template:
  metadata:
   labels:
    app: glassfish
  spec:
   containers:
   - image: glassfish:latest
     name: glassfish
     ports:
     - containerPort: 8080
       name: glassfishhttp
     - containerPort: 4848
       name: glassfishadmin
     command: ["/bin/cp"]
     args: #["/mnt/apps/*","/usr/local/glassfish4/glassfish/domains/domain1/autodeploy/"]
     - /mnt/apps/
     - /usr/local/glassfish4/glassfish/domains/domain1/autodeploy/
     volumeMounts:
     - name: glassfish-persistent-storage
       mountPath: /mount
     - name: app
       mountPath: /mnt/apps
   volumes:
   - name: glassfish-persistent-storage
     persistentVolumeClaim:
      claimName: fish-mysql-pvc
   - name: app
     hostPath:
      path: /mnt/nfs
      type: Directory

I'm trying to use the following command in my container:

cp /mnt/apps/* /usr/local/glassfish4/glassfish/domains/domain1/autodeploy

What am I doing wrong?

So far i've tried to do it with the /, without it, using /* When i use apps/ i see "item or directory not found", when i use apps/ i get "directory ommitted", i need only whats in the map, not the map itself so -r doesn't really help either.

-- kubernoobus
kubernetes
yaml

2 Answers

6/8/2018

What am I doing wrong?

here is correct command to execute:

command: ["sh", "-c", "cp -r /mnt/apps/* /usr/local/glassfish4/glassfish/domains/domain1/autodeploy/ && asadmin start-domain --verbose"]

With your cp command you effectively overwrite legitimate required command to start everything. You can see original one (running ok without your cp command) if you inspect that container. Initially, container is started with:

...
"Cmd": [
    "/bin/sh", 
    "-c",
    "asadmin start-domain --verbose"
],
...

simply adding it after the copy command solves your issue.

-- Const
Source: StackOverflow

6/8/2018

Two things to note here:

  1. If you want to copy a direcyory using cp, you have to provide the -a or -R flag to cp:
 -R    If source_file designates a directory, cp copies the directory and the entire subtree connected at
       that point.  If the source_file ends in a /, the contents of the directory are copied rather than
       the directory itself.  This option also causes symbolic links to be copied, rather than indirected
       through, and for cp to create special files rather than copying them as normal files.  Created
       directories have the same mode as the corresponding source directory, unmodified by the process'
       umask.

       In -R mode, cp will continue copying even if errors are detected.
  1. If you use /bin/cp as your entrypoint in the pod, then this command is not executed in a shell. The * in /path/to/* however is a shell feature.

  2. initContainers do not have args, only `command.

To make this work, use /bin/sh as the command instead:

command:
  - /bin/sh
  - -c
  - cp /mnt/apps/* /usr/local/glassfish4/glassfish/domains/domain1/autodeploy/
-- mhutter
Source: StackOverflow