How can I get the current date in a Dockerfile? - docker

I'm not sure if this approach is the best so I'm open to suggestions.
My code is structured as follows:
src/
program1.py
util.py
jarfile.jar
Dockerfile
The Dockerfile looks like this:
WORKDIR /app
COPY src /app
COPY jarfile.jar /app
WORKDIR /app/src
RUN python program1.py
The main program being invoked is program1.py which generates a directory at the same level of src of the format data_{today-date}, so data_20200122 for example. The JAR file is to be invoked by passing this data directory as a command line argument, such as java -jar jarfile.jar -inputData /app/data/data_20200122
Since the data directory is dynamic, how can I have Docker generate the dir name? I thought about using a shell script instead but I'm new to Docker and wanted to know if there's a better approach.

Related

Dockerfile COPY creates undesired subdirectory in image

I want to create a Docker container and I wrote image. Everything works great except the COPY command where I got confused. My Dockerfile:
RUN HOME=/home/ros rosdep update
RUN mkdir -p /home/ros/workspace
# Copy the files
COPY $PWD/src/a_file /home/ros/workspace/src
COPY $PWD/src/b_file /home/ros/workspace/src
a_file is a directory like a b_file. When I try to copy these directories into a newly created directory called /home/ros/workspace/src I want a_file and b_file to be both inside /home/ros/workspace/src. Instead of this, I get another src directory /home/ros/workspace/src/src) and the contents of a_file and b_file are inside that directory.
What am I doing wrong?
As mentioned in other answers, $PWDrefers to the image context.
Try to use . instead.
To setup your working directory, use WORKDIR
Also, both a_file and b_file are in src/
All in all, this should work (not tested):
FROM <your-base-image>
WORKDIR /home/ros
RUN rosdep update
RUN mkdir -p workspace
# Copy the files
COPY ./src workspace/src
In your Dockerfile, PWD variable refers to image context (i.e: inside the image).
From COPY documentation:
paths of files and directories will be interpreted as relative to the source of the context of the build.
If src directories are in the root of your build context, your example it will be:
...
COPY src/a_file /home/ros/workspace/src
COPY src/b_file /home/ros/workspace/src
...

Docker Desktop for Windows -- How to specify an executable entrypoint?

What is the correct syntax for specifying an executable entrypoint? For instance, I build a project which produces an executable (e.g. "example.exe") which gets copied to the docker container under C:\app. I cannot seem to get the Dockerfile entrypoint correct, it always fails always relating to not being able to find the specified exe, the path being invalid, etc. The Dockerfile looks like:
FROM microsoft/aspnet:4.6.2-windowsservercore
ARG source=.
WORKDIR /app
COPY $source .
ENTRYPOINT ["/app/example.exe"]
I've tried numerous strings in the entrypoint:
example.exe
C:\\app\\example.exe
/app/example.exe
none of these strings work so I'm confused on how to run that exe as the entrypoint.
Or perhaps I'm misunderstanding the use of "entrypoint" and I need to use something else like "run"?
I had to use the "shell" form:
FROM microsoft/aspnet:4.6.2-windowsservercore
ARG source=.
WORKDIR /app
COPY $source .
ENTRYPOINT "example.exe"

Copy specific file first and the copy all in Dockerfile

The following is what I have for my Dockerfile:
FROM node:4.6.0
WORKDIR /src
COPY node_modules/ /src/node_modules
COPY . /src/
CMD ["/bin/bash"]
I wanted to make it efficient such that node modules are copied only when there's any change in the directory. Otherwise, I want only the source files are copied into the image.
Would this work as intended?
This will copy twice. Docker cache looks at the current command and previous layer. If something has changed in the Docker context for the current command it will run. It does not use a partial cache to run.

What is the easiest method to copy files and directories in dockerfile

I tried to copy some files from source to destination (flask app) in a dockerfile but it seems things are not working as expected when building the image. With last 2 line showing:
Step 3 : COPY pkl_objects/* /home/jovyan/work/movieclassifier/pkl_objects/
No source files were specified
This is the docker file.
FROM jupyter/datascience-notebook
RUN pip install flask flask-wtf
COPY pkl_objects/* /home/jovyan/work/movieclassifier/pkl_objects/
COPY static/* /home/jovyan/work/movieclassifier/static/
COPY templates/* /home/jovyan/work/movieclassifier/templates/
COPY app.py /home/jovyan/work/movieclassifier
COPY reviews.sqlite /home/jovyan/work/movieclassifier
COPY vectorizer.py /home/jovyan/work/movieclassifier
WORKDIR /home/jovyan/work/movieclassifier
ENV FLASK_APP=app.py
# ENV FLASK_DEBUG=0
CMD ["flask", "run", "--host=0.0.0.0"]
Looks like there are no files in the pkl_objects folder, and when the wildcard (*) is expanded, it results in no source files being specified.
Maybe you could add an empty file in there, so that when the wildcard picks up files, you at least get one source file.
Example file could be: .nonempty or something like that.

Is it possible to optionally use the docker COPY instruction?

I'm using the docker COPY instruction to copy files from <src> to <dest> as described in the documentation. However it is possible that there will be no <src> file which causes docker-compose build to fail. Like so:
Step 7 : COPY cts/application.properties /cts/
ERROR: Service 'redirector' failed to build: lstat cts/application.properties: no such file or directory
Is there a way to only copy the file if it's there or turn off the errors?
The only way to do that, is copying all files inside cts folder, for that, you can user COPY with a wildcard.
COPY cts/* /cts/
Or set your WORKDIR as cts, copy all files, and then set back your WORKDIR
WORKDIR cts/
ADD . /cts/
WORKDIR old_workdir_path
But, if you want to copy conditionally one file using COPY command, you can't.

Resources