0

I have a dockerfile looking like this :

FROM python:3.10-slim
WORKDIR /app
COPY requirements.txt .
RUN pip install -r requirements.txt
ADD . /app
CMD python script.py

(requirements.txt contains "black").

I would like to run black on script.py before running script.py, so that script.py get formatted correctly when the container starts. I dont understand how I am supposed to do this, knowing that I cant use CMD twice . I feel like I'm missing how docker is supposed to be used.

178434577
  • 1
  • 3
  • While it is not a duplicate, I believe the *question itself* [here](https://stackoverflow.com/q/46797348/2550406) answers your question. Also see [here](https://serverfault.com/a/978107/241793) – lucidbrot Oct 31 '22 at 18:55
  • 4
    `RUN black script.py` would do the trick, I believe. – JNevill Oct 31 '22 at 18:56
  • RUN black script.py doesnt work. I can see it running in the logs, but it doesnt affect script.py. – 178434577 Oct 31 '22 at 18:59
  • 3
    [Black](https://pypi.org/project/black/) seems like something you'd run on your host system; if correctly-formatted code is already checked into source control, you wouldn't need to reformatted it when you build a Docker image (and especially not every time you run a container). – David Maze Oct 31 '22 at 19:03
  • 2
    `so that script.py get formatted correctly when the container starts` Why do you care if the script is formatted inside the container? – KamilCuk Oct 31 '22 at 19:06

1 Answers1

-2

Solution is here. In my case, it is :

Dockerfile:

FROM python:3.10-slim
WORKDIR /app
COPY requirements.txt .
RUN pip install -r requirements.txt
ADD . /app
ADD start.sh /
RUN chmod +x /start.sh

CMD ["/start.sh"]

start.sh:

#!/bin/bash

black script.py
python script.py
178434577
  • 1
  • 3
  • 2
    Why are you running black every time the container is run. It would make much more sense to run black in the docker file like others suggested so its only run once at container build time. – Chris Doyle Oct 31 '22 at 20:50
  • I dont understand. "Dont run black in the dockerfile, you should run it in the docker file". – 178434577 Nov 01 '22 at 10:22
  • I am saying, dont run black every time your run the container. Instead run black in the dockerfile its self so its only run once when the image is built. Currently you will be running black every time your run the container which is pointless – Chris Doyle Nov 01 '22 at 10:48