Hello… I’m having a small issue where my static fi...
# general
g
Hello… I’m having a small issue where my static files aren’t available(loading
404
) when I run a shell script (
shell_sources
) that among other things executes
collectstatic
This is only happening when I run the docker version. Here’s how the
Dockerfile
looks like
Copy code
FROM python:3.10-slim

### Arg
ARG DEBIAN_FRONTEND=noninteractive

### Env
ENV APP_HOST=.
ENV APP_DOCKER=/app
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1

COPY twoops_tracker.py/main.pex /bin/dj

# Expose server port
EXPOSE 8000

### Volumes
WORKDIR ${APP_DOCKER}
RUN mkdir -p media staticfiles logs

COPY twoops_tracker/sh/*.sh /
RUN chmod +x /cmd.sh

### Run app
CMD ["/cmd.sh"]
I’ve also noticed that the files are being saved under
/root/.pex/user_code/a517e67b58e274b35127ae63804115e6156bfe3d/staticfiles
which I think is the reason why they can’t be found. Any ideas on how I can solve this?
h
Hi - to be sure I understand the context, it sounds like you have a Django app you’re building into
main.pex
with some version of Pants, and then running it in a container?
g
exactly
Here’s the
Build
for the docker
Copy code
docker_image(
    name = "twoops-tracker",
    dependencies = [
        "twoops_tracker/py:main",
        "twoops_tracker/sh:sh",
    ],
    image_tags = [
        "{build_args.VERSION}",
        "latest",
    ],
)
h
And what do the targets that wrap the static files look like?
g
not sure I fully understand. What do you mean wrap static files?
h
Your static files are in the repo and presumably are the
sources
of a
resources
or
files
target? How are they getting into the pex?
g
I see. We aren’t adding them to them to the pex. They’re being generated when the container first starts. I guess that’s why they’re being placed in that ‘random’ location.
h
Interesting!
So these aren’t static files that live in the repo?
Like .css files or something?
g
They don’t live in the repo. But yeah it’s css, javascript e.t.c
Thanks for helping me think this through. Fixed it by using the correct
STATIC_ROOT
which was previously being based on
BASE_DIR = Path(__file__).resolve().parent.parent
which was causing the issue.
h
Aha! Glad that got sorted out
Out of curiosity, where doe these files originate? How do they get into the docker image?
g
They are basically from Django but also any app e.g Django rest framework also have its static files. While running on a development server, Django is responsible for directly serving the files but on production it’s not recommended and that’s why we need to run the
collectstatic
command in order to have all static files from different apps into one location. From there we just configure Nginx or any webserver to respond to
/static
request with files from the location where
collectstatic
saved the files. Just to note, for our case we store the files in S3 but we still wanted to be able to run the docker image locally without having to use the s3 files.
h
Ah yes, I get all that, but
collectstatic
typically collects the static files from your source tree.
So my question was about where is the source of these .css files
g
good question but I’m not sure. Does the generated
pex
file include the
django
static files?
h
That’s what I’m asking 🙂 You probably want it to…
collectstatic
is a Django utility that gathers your static files from all over your codebase and dumps them into a single dir for serving purposes
So when you run collectstatic it must be collecting them from… somewhere
presumably your source tree
(oh and this can also collect static files in 3rdparty apps)
g
yes. Right now it’s collecting them from my source tree