2

I'm trying to call a function runaudit inside of of my docker container.

The shell script, which contains the runaudit function, is copied inside of the image and the BASH_ENV environment variable is set to point to /etc/audittools/audit.sh

My question is, how do run this runaudit function inside of my container?

I tried to run it inside the container but I get the following message.

/ # runaudit
/bin/sh: runaudit: not found

The ENV output is showing me that the environment variable has been set.

Herewith the Dockerfile and small shell function.

NOTE : The real function is much larger and I need to keep it maintained inside of it's own file.

I appreciate any advice or help with this.

Dockerfile

FROM alpine:3.10

MAINTAINER acme

ARG VERSION=0.3.0

RUN apk add --no-cache curl tar gettext libintl

RUN envsubst --version

RUN mkdir -p /etc/audittools

COPY audit.sh /etc/audittools

RUN chmod a+x /etc/audittools/audit.sh

ENV BASH_ENV "/etc/audittools/audit.sh"

Shell Script - audit.sh

#!/usr/bin/env sh
function runaudit() {
    echo "Hello World"
}

Run the function

docker run -it test /bin/sh runaudit

3 Answers 3

2

Change this to be an ordinary shell script; in generic programming terms, make it a "program" and not a "function".

Remember that docker run starts a new container with a clean environment, runs whatever its main program is, and then exits. Also remember that the difference between a shell script and a shell function is that a function runs in the context of its calling shell and can change environment variables and other settings, whereas a shell script runs in a new shell environment; but since in a docker run context the parent shell environment is about to get torn down, it doesn't matter.

So I'd change the shell script to

#!/bin/bash
# ^^^ use the standard shell location
# vvv remove the function wrapper
echo "Hello World"

and then change the Dockerfile to run it on startup

...
COPY audit.sh /etc/audittools
RUN chmod a+x /etc/audittools/audit.sh
CMD ["/etc/audittools/audit.sh"]
Sign up to request clarification or add additional context in comments.

2 Comments

Hi David, Thanks for posting your answer. Unfortunately I need to expose the audit function for the other containers I use in Kubernetes. My Jenkins CI/CD pipeline utilises different containers as part of it's build stage.
You cannot expose functions in Docker (or Kubernetes) this way (again, as a general programming statement, not specific to shell functions). Most notably your docker run would audit a new empty container containing only the audit script. Your CI system (probably the per-image Dockerfile) needs to copy the script into the images that are being built.
2

you can run it like this:

docker run -ti test sh -c "source /etc/audittools/audit.sh && runaudit"

Comments

0

I tried with the CMD command and it worked fine for me from inside the dockerfile.

CMD ["/etc/audittools/audit.sh", "runaudit"]

You can add this line post the chmod for audit.sh. Your final dockerfile will look something like:

FROM alpine:3.10

MAINTAINER acme

ARG VERSION=0.3.0

RUN apk add --no-cache curl tar gettext libintl

RUN envsubst --version

RUN mkdir -p /etc/audittools

COPY audit.sh /etc/audittools

RUN chmod a+x /etc/audittools/audit.sh

ENV BASH_ENV "/etc/audittools/audit.sh"

CMD ["/etc/audittools/audit.sh", "runaudit"]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.