0

I am very new to Docker. I figured out on internet on how to build a dockerfile and then create an image.

This is how my docker file looks like:

FROM scratch

ADD abc.py .

RUN pip3 install requests json HTTPBasicAuth

CMD [ "python3", "./abc.py"]

I am using scratch (base image) as I am on a Linux server where we are not allowed to connect to the outside world.

The thing is I am getting the below error when i try to run Docker build command i.e. docker build -t testimage .

OCI runtime create failed: container_linux.go:345: starting container process caused "exec: "/bin/sh": stat /bin/sh: no such file or directory": unknown

Can anyone explain why is this and what would be the solution. I got some idea about the problem but couldn't identify the solution.

U Bhargava
  • 23
  • 1
  • 2
  • 6
  • `FROM scratch` is completely empty. It has nothing at all, certainly not `pip3` or `python3`. – Charles Duffy Jun 02 '21 at 15:48
  • There are some very good approaches to building Docker images without the Docker process having network access -- I'm a big fan of the [Nix dockerTools](https://nix.dev/tutorials/building-and-running-docker-images) toolchain; but the bits needed to do that need to be transferred _somehow_, even if it's sneakernet. – Charles Duffy Jun 02 '21 at 15:51

1 Answers1

0

The scratch base is only meant to be used when creating other base images. It contains nothing, not even a shell like bash. If you want to run a python script in a Docker image, I would suggest using a Python base image. For example, you could use python:3.8-slim, which is based on Debian Linux.

FROM python:3.8-slim
COPY abc.py .
RUN pip3 install --no-cache-dir requests
CMD ["python3", "./abc.py"]

I made some changes to your Dockerfile.

  • Changed ADD to COPY, because the docker documentation says to prefer COPY.
  • Added --no-cache-dir to pip install to prevent downloads from being cached. This reduces the size of the Docker image.
  • Removed some packages from pip install because I don't think they exist...

If you cannot pull this base image on your server, then you can try building the image elsewhere and pushing it to your server. If you can build the image somewhere else, then you can use docker export to export it to a tar file. Then you can upload it to your server, and use docker import to convert it to a Docker image.

jkr
  • 17,119
  • 2
  • 42
  • 68
  • Unfortunately I cannot build the image somewhere else as well. Is this the only option? – U Bhargava Jun 02 '21 at 15:08
  • I'm afraid so. You will definitely need internet access to build the image. And you cannot use `scratch` for what you want to do. – jkr Jun 02 '21 at 15:14