-3

I'm attempting to dockerize a program on Linux OpenWRT, but docker cant find opkg although I can run opkg from any path:

Anyways opkg is in my path /etc , so i started docker from /etc, using WORKDIR ./etc.

Here is the Dockerfile:

FROM python:3

WORKDIR ./etc

# Setup env
ENV LANG C.UTF-8
ENV LC_ALL C.UTF-8
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONFAULTHANDLER 1

# Install pipenv and compilation dependencies & pip itself
#RUN apt-get update
RUN opkg update
#RUN apt-get install -y --no-install-recommends gcc
RUN opkg install -y --no-install-recommends gcc
#RUN apt install -y build-essential libssl-dev libffi-dev python3-dev
RUN opkg install -y --no-install-recommends gcc
#RUN apt install -y python3-pip
RUN opkg install -y python3-pip
#RUN apt install -y python3-venv
RUN opkg install -y python3-venv
#going forward use pip3 to install packages


#setup vitual environment using venv

#change the working dir again to actual program path 
WORKDIR /etc/my_program/
RUN mkdir environments
RUN cd environments
RUN python3 -m venv my_env
RUN source my_env/bin/activate

#your python files
ADD hello.py /

# Run the application
CMD ["python3", "./main.py"]

When I run docker build -t my_img .

A snippet of output fails of step 7/18:

Step 7/18 : RUN opkg update


 ---> Running in da0016984897
/bin/sh: 1: opkg: not found

Why isnt docker finding opkg when I specifically pointed to /etc using WORKDIR ./etc?

How can I fix this?

Thanks

cyber101
  • 2,822
  • 14
  • 50
  • 93

2 Answers2

0

I believe you are confusing your client or docker host environment with that of the image being created. Docker builds do not have any dependencies on the host where the build is running, and the only filesystem input you have is to pass the build context (typically the current directory or .) which is used for COPY and ADD steps.

The RUN step of a docker build runs a temporary container and execute the requested command within that container. The image used for the container is based on the current state of the image build. Therefore

FROM python:3
RUN opkg update

is the same as executing:

docker run python:3 opkg update

Similarly the WORKDIR step changes the current directory that commands use from within the container. You would not use RUN cd /etc because the cd is a shell command that would be lost as soon as the shell exits at the end of that run step.

BMitch
  • 231,797
  • 42
  • 475
  • 450
  • @BMitchh, What your saying is true, however that doesn't explain what "opkg" is not found. – cyber101 Jan 20 '21 at 01:30
  • @cyber101 where is opkg, on your client, the docker host, or inside the image? Testing by myself and Terry show it is not inside the image, and that is the filesystem you are isolated within during the RUN step. – BMitch Jan 20 '21 at 01:54
-1

It doesn't look like 'opkg' exists in PATH in that image. See below:

C:\Users\terry> docker run python:3 which opkg
C:\Users\terry> docker run python:3 which apt-get
/usr/bin/apt-get
C:\Users\terry> docker run python:3 find / -name opkg
C:\Users\terry> docker run python:3 find / -name apt-get
/usr/bin/apt-get
C:\Users\terry>

You might want to see this answer also: Installing "opkg"?

Terry Sposato
  • 572
  • 2
  • 7
  • Opkg is installed and easily called from the shell on any path. However i cant call it from within docker. Please read carefully. The issue is docker expects to find opkg in /bin/sh, although its in /etc and i specifically pointed to /etc. Thanks – cyber101 Jan 18 '21 at 19:19
  • As Terry points out, opkg is not in the image filesystem. The RUN steps do not run on your client that requests the build, or on the docker host that executes the build, but rather inside of a temporary docker container based off the current image state. Also WORKDIR is not added to the PATH unless the value of PATH contains a `.`. – BMitch Jan 18 '21 at 19:33
  • Use their image which has opkg already bundled is probably the easier thing to do, see https://openwrt.org/docs/guide-user/virtualization/docker_openwrt_image & https://github.com/openwrt/docker – Terry Sposato Jan 20 '21 at 02:00
  • @BMitch, so I would have to build & install opkg inside the image itself? – cyber101 Jan 20 '21 at 02:01