Docker: rename single file with dynamic name to a Static name - docker

I have an undeterministic script that outputs a json file with a dynamic name in the working directory ie foo.bar.baz.json is there a way to copy/paste this file and give it another name, say output.json?
I tried:
ADD ./*.json output.json
but I can see no additional file is added when I ls after.

Ah! COPY/ADD work on source directory -> image directories. Here I needed to run the copy inside the buid. This did it:
RUN cp /out/*.json /out/output.json

Related

How to COPY a content of a file and use it as environment variable in DOCKER?

I'm trying to perform automation process using docker.
I have couple of files in my local system and these files will be used inside of a container, so my code so far looks like the following :
FROM docker_image_on_the_internet:stable
COPY . /root/
# Now I have a command that will execute the copied files
CMD cmd1 --opt-dir --file-copied
# After executing cmd1, a file will be generated inside of the container, the file name consists of timestamp and a public key, example : UTC--2022-03-07T10-53-08.532008168Z--84e9ed8d078f2bdc71e4ca9a322d2f4222c9b7b7
# Now I have another command that will be executed USING THE FILENAME CREATED
CMD cmd2 --opt-dir --file-created-inside-the-container
Is there any way to get the created file from the container and use it directly in cmd2 line? because this file is a JSON file that looks like this :
File name : UTC--2022-03-07T10-53-08.532008168Z--84e9ed8d078f2bdc71e4ca9a322d2f4222c9b7b7
File content :
{"address":"84e9ed8d078f2bdc71e4ca9a322d2f4222c9b7b7","crypto":{"cipher":"aes-128-ctr","ciphertext":"a14e5429363ce09903aeccf1afa9ea49a02a4974e0fa032d14f7077ceae21588","cipherparams":{"iv":"a214bcb73a74554bb9be59df158c4d4e"},"kdf":"scrypt","kdfparams":{"dklen":32,"n":262144,"p":1,"r":8,"salt":"d933d92a667eec528299c354e1efe0da730cea2a53a082dbeafc9ed70ca40a71"},"mac":"803325a434a6541c5d93d15f11762d2907e1da1c6520fc33632934565822733d"},"id":"694ca50f-3ae9-4520-94f9-a80003c3f95f","version":3}
Because I need the address public key to be executed as an option in the CMD2, and my local files already contains a .ENV file which supposedly contains that variable (need to copy the address and added to the .env file - or execute it directly in the cmd2 as a string option without using environment variables)
Is there anyway I can achieve this ?
Thanks in advance !
You could create the file elsewhere select him using UTC--* (since this would be the only matching file), move him to the correct destination if you want at the end of the process.
By using jq you could extract this variable: ADDRESS=$(cat <your_file> | jq -r .address) and then use it.

Dockerfile COPY failed with particular directory name

A normal COPY line in my Dockerfile is preventing the image from being built.
COPY privoxy.config /etc/privoxy/config/
The log says
failed to copy files: failed to create new directory: mkdir /var/lib/docker/overlay2/e6748c046ce142595c7d4fec886898f88abf9a932876b80b436ea5fd24b705a5/merged/etc/privoxy/config: not a directory
Just want to figure out why this peculiar problem happens. I've made sure the dest path is with the trailing slash and tried to change the path name, the problem dissappered when I changed the config in the dest path to conf. I've also presumed it was related to the privoxy.config extension name but after experiment it was not the case.
COPY will create the destination directory in the image if it doesn't already exist. However, if the destination already exists but it's a file (or something else that's not a directory) you can get this error.
You can verify this with a temporary container:
docker run --rm the-base-image \
ls -ld /etc/privoxy/config
If the destination is a file, and you want to COPY that source file to that specific image file, then you can remove the / at the end of the path to cause Docker to overwrite the existing file.
COPY privoxy.config /etc/privoxy/config # <-- no trailing /

How to copy multiple files in one layer using a Docker file to different locations?

Is it possible to copy multiple files to different locations in a Dockerfile?
I'm looking to go from:
COPY outputs/output/build/.tools /root/.tools
COPY outputs/output/build/configuration /root/configuration
COPY outputs/output/build/database/postgres /root/database/postgres
I have tried the following, but no joy:
COPY ["outputs/output/build/.tools /root/.tools","outputs/output/build/configuration /root/configuration","outputs/output/build/database/postgres /root/database/postgres"]
Not sure if this is even possible.
Create a file .dockerignore in your docker build context directory. Create a soft link (ln) of the root directory you want to copy and exclude the directories you don't want in your .dockerignore.
In your case, you don't need the soft link as the directory is already in the docker build context. Now, add the directories in the .dockerignore file that you don't want eg. if you don't want bin directory you can do that as,
# this is .dockerignore file
outputs/output/build/bin*
Finally in your Dockerfile,
COPY outputs/output/build/ /root/
Details are here.
it looks like you are trying to do bash style [] brace expansion
RUN command uses sh NOT bash
see this SO article discussing it
Bash brace expansion not working on Dockerfile RUN command
or the docker reference
https://docs.docker.com/engine/reference/builder/#/run

docker cp not working

I'm following this tutorial and when I get to the part where I call:
cp /tf_files/stripped_retrained_graph.pb bazel-bin/tensorflow/examples/android/assets/stripped_output_graph.pb
and
cp /tf_files/retrained_labels.txt bin/tensorflow/examples/android/assets/imagenet_comp_graph_label_strings.txt
They both say "No such file or directory".
As you can see in this image I can cd to the tf_files folder and see that the files are there.
I can also cd to /tensorflow/tensorflow/examples/android/assets and call ls which shows there's just a BUILD file there.
In the cp command is there supposed to already be a stripped_output_graph.pb file in the destination which gets replaced? Or is it meant to just be creating a new file there?
Is there some way of doing cp [source] [current directory] rather than specifying the destination as a path?
I've tried removing the file path part in hope that it just uses the source filename but that doesn't work.
Calling
cp /tf_files/stripped_retrained_graph.pb /tensorflow/tensorflow/examples/android/assets/stripped_output_graph.pb
and
cp /tf_files/retrained_labels.txt /tensorflow/tensorflow/examples/android/assets/imagenet_comp_graph_label_strings.txt
finally worked, wasn’t at all obvious that I’d have to change the destination path or what it should be though.
Also I accidentally saved a file as .p rather than .pb but managed to remove it using $ docker exec <container> rm -rf /tensorflow/tensorflow/examples/android/asset
s/stripped_output_graph.p
Now I managed to copy the files in correctly, but then when I installed the app it was still just running the regular demo app.
Not sure why it didn’t work, so frustrating.
When I rebuilt it after copying the files in I got these conflict messages
Are these normal to have?
It looks like maybe a different labels file is taking priority over mine, how can I reach the external/inception5h/imagenet_comp_graph_label_strings.txt file to delete it so my file is used instead?
Does the “external” part mean that I can’t actually access it?

docker add extract to custom directory

A docker add will nicely extract the supplied compressed file into the directory specified in the zip/tar file
How can I extract it into a different directory?
Eg. if the file extracts to /myfile but I would prefer /otherFile
Don't believe there's any way to do this just using the ADD instruction. ADD supports a target directory obviously, like ADD ["<src>", "<dest>"] however it's still going to extract into the dir you have in the tar within that.
2 options, either rename the dir in the tar or do a RUN mv myfile otherfile after adding.
Is there a specific reason you need it to be named something in particular?
Think about this scenario where you build a tomcat image,
ADD apache-tomcat-8.0.48.tar.gz /opt
This cmd will extract the tar to /opt/apache-tomcat-8.0.48 , if you don't like the long folder name(apache-tomcat-8.0.48) then the requirement happens.

Resources