1

I need to run a python command while building a docker image. My docker file is as follows

FROM python:3.8.13-slim-buster
RUN /usr/local/bin/python3 -c "import yaml"

When I run docker build . -t hello, I get the following error:

Sending build context to Docker daemon  2.048kB
Step 1/2 : FROM python:3.8.13-slim-buster
---> 289f4e681a96
Step 2/2 : RUN /usr/local/bin/python3 -c "import yaml"
---> Running in 4eb13a1a1e9a
Traceback (most recent call last):
File "<string>", line 1, in <module>
ModuleNotFoundError: No module named 'yaml'
5
  • 1
    You need to install the yaml module in your container before that command runs. Commented Aug 18, 2022 at 13:47
  • 1
    You might won't to run pip earlier to install your dependencies. Commented Aug 18, 2022 at 13:47
  • 1
    1: pray the container has pip installed. 2: RUN /usr/local/bin/python3 -m pip install pyyaml first Commented Aug 18, 2022 at 13:47
  • 1
    pip install pyyaml is the usual solution Commented Aug 18, 2022 at 13:47
  • @DownloadPizza Why not pray for a fully working Docker image with your code directly? Commented Aug 18, 2022 at 13:48

2 Answers 2

1

You need to install the pyyaml package also. You can modify your Dockerfile as below:

FROM python:3.8.13-slim-buster
RUN /usr/local/bin/pip3 install PyYAML
RUN /usr/local/bin/python3 -c "import yaml"
Sign up to request clarification or add additional context in comments.

Comments

1

The yaml module is not a part of Python's standard library. You'll need to install it with

RUN python3 -m pip install pyyaml

3 Comments

This requires that 1: python2 pip is not installed and 2: that pip is on the path. /usr/local/bin/python3 -m pip ... is the safer bet
Is the full path necessary?
Depends how much you trust the container authors to not mess things up. But in a python container python should generally be on the path so probably fine without full path

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.