-1

I'm trying to run docker on embedded Linux running OpenWRT.

Since the embedded Linux is a "resource constraint" I don't want Docker to install already installed packages, therefore I want to call a custom shell script with docker:

RUN $CMD_STRING = $(gcc)
RUN $CMD_OUTPUT=$(${CMD_STRING} -version) 

RUN if [[ ${CMD_OUTPUT} == *"not found"* ]]; echo ${CMD_STRING} "was NOT FOUND, Installing..." 
    opkg update
    opkg install gcc
fi

I will like a similar simple if/else structure.

I keep getting:

-ash: gcc: not found
-ash: -rw-r--r--: not found
cyber101
  • 2,822
  • 14
  • 50
  • 93
  • The Docker container will be fully isolated from the host system; it can't use anything installed on the host, including the host's package manager. Container images are frequently based on a mainstream Linux distribution (Debian, Ubuntu, or Alpine) and you need to use the image's distribution's package manager (`apt-get` or `apk`), regardless of what the host system is. – David Maze Jan 18 '21 at 05:29

2 Answers2

0

I don't have some OpenWRT for test but this may work if its only an "ash" and "docker" problem. I tested it on alpine since it also have ash (from busybox).

Dockerfile:

from alpine:latest

RUN ash -c "if ! gcc 2>/dev/null; then echo 'not found..'; echo 'installing..'; fi"

Build it:

docker build . 
Sending build context to Docker daemon  3.072kB
Step 1/2 : from alpine:latest
 ---> 389fef711851
Step 2/2 : RUN ash -c "if ! gcc 2>/dev/null; then echo 'not found..'; echo 'installing..'; fi"
 ---> Running in 2c47bee97dfc
not found...
installing.. 
Removing intermediate container 2c47bee97dfc
 ---> 35e698d1aea6
Successfully built 35e698d1aea6
Dr Claw
  • 636
  • 4
  • 15
0

You have extra spaces in your first command, and shouldn't be using a variable name with a dollar sign at the beginning. I think you probably also don't want to be assigning that with $(), since you haven't tested if it exists yet. Trying to run a command to see if it exists also isn't a great way to go about it. You can see if a program is installed like this:

if ! command -v gcc &> /dev/null; then
  opkg install gcc
fi

(That's POSIX-compatible so should work in ash.)

You could also run opkg list-installed and check the output (see the docs) which may be useful for packages that aren't executables in your PATH.

Zac Anger
  • 6,983
  • 2
  • 15
  • 42