1

I´m building a docker image using that has a log file that must be mounted as an S3 folder. I´m using s3fs for the trick; however, I cannot run it, I´m constantly getting this bug:

s3fs: invalid option -- 'j'

This is an example of my Dockerfile:

FROM openjdk:11
RUN mkdir /home/app/
WORKDIR /home/app/
RUN mkdir logs
COPY ./target/MY_JAVA_APP.jar .

ENV AWS_ACCESS_KEY_ID=MY_KEY_ID
ENV AWS_SECRET_ACCESS_KEY=MY_SECRET_ACCESS_KEY
ENV AWS_REGION=MY_REGION

RUN apt-get update
RUN apt install s3fs -y

ARG S3_MOUNT_DIRECTORY=/home/app/logs
ENV S3_MOUNT_DIRECTORY=$S3_MOUNT_DIRECTORY
ARG S3_BUCKET_NAME=MY_BUCKET
ENV S3_BUCKET_NAME=$S3_BUCKET_NAME

RUN echo $AWS_ACCESS_KEY_ID:$AWS_SECRET_ACCESS_KEY > /root/.passwd-s3fs && \
    chmod 600 /root/.passwd-s3fs

EXPOSE 8080

CMD ["java","-jar","MY_JAVA_APP.jar"]

ENTRYPOINT ["s3fs", "MY_BUCKET:/logs", "/home/app/logs", "-o", "dbglevel=info", "-f", "-o", "curldbg"]

I don´t have any j anywhere, and I´m somehow stuck. I even asked in GitHub without a possible answer.

P.S.:

Important things:

  1. This is a proof of concept.
  2. This project is not going to be published in Docker Hub. We have a private repo for it.
  3. I´m planning to move the credentials to Secrets in AWS.
Federico Navarrete
  • 3,069
  • 5
  • 41
  • 76
  • 1
    `-jar` can be parsed as `-j` `-a` `-r`. Obviously, `/usr/bin/java` doesn't parse it that way, but if it's being seen as an option to `s3fs`, there's your problem. – Charles Duffy Apr 16 '21 at 14:30
  • 2
    Remember, when you specify both a CMD and an ENTRYPOINT, the CMD is added as extra arguments to the ENTRYPOINT; so what you're actually telling Docker to run is `["s3fs", "MY_BUCKET:/logs", "/home/app/logs", "-o", "dbglevel=info", "-f", "-o", "curldbg", "java", "-jar", "MY_JAVA_APP.jar"]` – Charles Duffy Apr 16 '21 at 14:31
  • ...given the above, I'm tempted to close this as a duplicate of https://stackoverflow.com/questions/47648775/docker-entrypoint-and-cmd-together – Charles Duffy Apr 16 '21 at 14:31
  • How can I run both commands @CharlesDuffy? Because I need to run the entry point ideally first and later the jar section. – Federico Navarrete Apr 16 '21 at 14:33
  • That's not how Docker works. It _doesn't_ run one first and the other later. – Charles Duffy Apr 16 '21 at 14:33
  • You're responsible for providing an ENTRYPOINT that can run the CMD it's given as a subprocess. It's the ENTRYPOINT's job to kick off the CMD correctly. – Charles Duffy Apr 16 '21 at 14:33
  • For a working proof of concept, I would recommend inspecting https://github.com/efrecon/docker-s3fs-client -- note how its `docker-entrypoint.sh` runs `exec "$@"` at the end -- that's how the entrypoint starts the CMD. – Charles Duffy Apr 16 '21 at 15:19
  • (Also remember that anyone who has the image can `docker run ... cat /root/.passwd-s3fs` or `docker run ... env | grep AWS` and retrieve your AWS credentials: you really don't want to include them in a Docker image like this.) – David Maze Apr 16 '21 at 15:29
  • Hi @CharlesDuffy, when I built the .sh, I got this message: not foundtart.sh: 2: /home/app/start.sh – Federico Navarrete Apr 16 '21 at 16:34
  • The `not found` overwriting the start of the message tells us that one of your files has DOS/Windows newlines instead of UNIX newlines. DOS newlines have two characters, `CR` `LF`, whereas UNIX newlines only have a `LF`. When a `CR` is printed to a terminal it sends the cursor to the front of the line, so that's why you see the front of the line overwritten. See also [Are shell scripts sensitive to encoding and line endings?](https://stackoverflow.com/questions/39527571/are-shell-scripts-sensitive-to-encoding-and-line-endings) – Charles Duffy Apr 16 '21 at 16:48

1 Answers1

2

My solution, thanks to Charles Duffy hints.

  1. I created a sh file (start.sh) that contains:
#REMEMBER. Take care with the Unix break lines if you use Windows or macOS.
#!/bin/sh
#The & is important to run s3fs in the background.
s3fs MY_BUCKET:/logs /home/app/logs &
java -jar MY_JAVA_APP.jar
  1. I added these lines of code in my Dockerfile:
COPY start.sh .

#the 777 must be changed, but I just needed to test to know if it worked.
RUN chmod 777 /home/app/start.sh

ENTRYPOINT ["sh", "start.sh"]
  1. I ran docker using the --privileged flag.
Federico Navarrete
  • 3,069
  • 5
  • 41
  • 76
  • 1
    Note that `#!/bin/sh` only works to select the shell to use to run the script if it's the very first line of the file -- so if you want to be sure the interpreter selection is honored, move the comment to the second line. – Charles Duffy Apr 16 '21 at 19:53
  • 1
    (And consider `chmod 755` instead of `chmod 777`; there's no reason for group or other to have write if all you want to be 100% sure of is that the script is runnable). – Charles Duffy Apr 16 '21 at 20:12