Help for writing yml with docker run --env xxx=`cat example.json`


#1

hi everyone,
I am new here and trying to deploy a microservice on an agent. The microservice should be started with

image

there is backtick here. I’m not sure how to write this in yml file.
currently, I use volume deployment to deploy example.json on agent;
then I write:

image

in the yml file

the image is deployed on the agent, but there is sysntaxError THAT unexpected token ` in JSON at position 0

How can I implement this ?

thank you.
best regards,
tempo


(Pixcell) #2

@tempo,

Thank you for your question!
Unfortunately, what you are trying to achieve relies on a shell feature. As we use software to manage the docker engine and containers running on the ioFog Agent, we don’t run shell commands (which would then interpret the backquotes, run the shell command and set the env variable value to the output of the shell command supplied between the backquotes), but we talk to the docker engine API, which only support JSON and string values for the env variables.

The “canonical” way of achieving something similar to your cat file example would be to use volume mounting and have your docker container read the content of the file and use this as required.

Please let us know if you have any more question,
Kind regards,


(Kilton Hopkins) #3

@tempo you can also map the environment variables in your application YAML file in ioFog so although you cannot dump the value of example.json directly into the container as you did with the Docker command line, you can still add those environment variables. Have a look here for the format, especially the “container.env” values:

https://iofog.org/docs/2/reference-iofogctl/reference-application.html#application


#4

I try it and it works!
Except the volume mounting function is not very stable, sometimes the files in the destination disappear.
Thank you!


#5

Thanks for your reply! I try to map the .json file into YAML format, but have some problems.
the value of one key is complex, it’s like this

"--EndPoint", 
{ 
 "coap": { "port": 5683 },
 "host": "localhost"  
 }

I want to put it in container.commands.

Do you have any idea to write the kind of value ?

Thank you very much!


(Saeid) #6

@tempo You can pass container commands to microservice in deployment yaml file.
Note that the cmd that you provide in yaml file overrides CMD and ENTRYPOINT that is defined in the Dockerfile

Here is an example:

apiVersion: iofog.org/v2
kind: Application
metadata:
  name: application-name
spec:
  microservices:
    - name: msvc-name
      agent:
        name: agent-name
      images:
        arm: arm-image-name
        x86: x86-image-name
      container:
        cmd:
          - /path/to/your/exec
          - '--EndPoint'
          - '{\"coap\":{\"port\":5683},\"host\":\"localhost\"}'

#7

Hi thanks for the answer.
it works! Thank you!
There is another issue, could you help me with it?
that is for the env, there are multiple values for one key: for example:

Probe: ["@fog/wot","@fog/serial"]

I try some formats

env:
- key: Probe
value: ["@fog/wot","@fog/serial"]

but there is error.
do you know how to organize it?
thank you!


(Serge Radinovich) #8

Environment variables are key-value string pairs. So your Probe environment variable would have to be a string representing an array which your container runtime would decode.

If you can’t encode the array into a string which your container runtime decodes, then the only option might be to treat each element in your Probe array as an environment variable. E.g.

env:
- key: ProbeWot
  value: "@fog/wot"
- key: ProbeSerial
  value: "@fog/serial"

#9

Hi, thanks for your reply!
Actually, the whole thing is like this: in the .json file:

“Probe”:[
[
@fog/wot",
“–EndPoint”,
{
“coap”: { “port”: 5683 },
“host”: “localhost”
}
],
[
@fog/serial”,
“–name”,
“test01”
]
]

there is limited instructions in the documentation, so I am just trying.
I try to put “@fog/wot” “@fog/serial” in container.env, and “–EndPoint” in container.commands
currently, “–EndPoint” in container.commands (questions asked above) works well,
but errors in “@fog/wot” “@fog/serial” in container.env
Do you think container.env is a good place to set these? I’m quite confusing…
Thank you very much!


(Serge Radinovich) #10

Sorry that doesn’t appear to be valid JSON.

If you have a JSON file full of configuration for your container, and your container can read that JSON file, you should be mounting it as a volume:
https://iofog.org/docs/2/agent-management/volumes.html
http://iofog.staging.edgeworx.io/docs/2/reference-iofogctl/reference-application.html#microservices

Otherwise go ahead and enter those values to the container however you see fit. Either as environment variables or arguments. It is up to you.

What is the error you receive when you try use environment variables for your Probe strings?


#11

Thanks. Yes, I think volume mounting method is better.
I’m just trying if there is another way to do this.
When I use env for probe strings, it shows

✘ Failed to unmarshal input file.
yaml: unmarshal errors:
line 27: cannot unmarshal !!seq into string
Make sure to use camel case field names. E.g. keyFile: ~/.ssh/id_rsa