supernova32

supernova32

Hi everyone,

I have been banging my head against a wall for the last couple of days because of a really weird problem I’m having in production.

I have a very simple Phoenix app that lets an admin user upload an image when creating a “note”. I’m using arc and arc-ecto to handle the file uploads. Everything works great in development. I’m able to upload the images without issues, and I can see them on the “notes” page.

However, when the app is running in production, I am able to upload the images without errors, but Phoenix is giving me a 404 when trying to fetch the images.

I already added the Plug.Static declaration to may endpoint.ex file, which is why I am able to see the images in dev, but no matter what I try in production, I always get a 404.

I have this in my endpoint.ex file. I already tried replacing Path.expand("./uploads") with the full path of where the files are when in production, and no luck.

plug Plug.Static,
    at: "/uploads",
    from: Path.expand("./uploads/"),
    gzip: false

I know Elixir has access to this folder in production, because after uploading the images, they are there.

I’m using Distillery to create the release for production, and I’m deploying the app as a Docker image using docker-compose. I already made sure that the destination of the uploaded files is mounted as a volume on the Docker image, so that they don’t get destroyed after each deploy.

What could possibly be happening here?

I’m really close to just giving up trying to get Plug.Static to work in prod, and just Base64 encode the images and inlining them in the HTML file.

Any help, or guidance you can give me is greatly appreciated!

Showing Posts 1 to 10

NobbZ

NobbZ

WHat does Path.expand("./uploads/") return? Is it as expected and really the folder you have configured as target for copying the uploaded files to? relative pathes can be mean at times, especially if one isn’t careful or conscious about the actual working directory.

Sadly you haven’t told us how you are starting your application… That could give some hints about the actual working directory.

wojtekmach

wojtekmach

Hex Core Team

See this excerpt from the docs

The preferred form is to use :from with an atom or tuple, since it will make your application independent from the starting directory. For example, if you pass:

    plug Plug.Static, from: "priv/app/path"

Plug.Static will be unable to serve assets if you build releases or if you change the current directory. Instead do:

    plug Plug.Static, from: {:app_name, "priv/app/path"}

have you tried it with tuple?

supernova32

supernova32 OP

@wojtekmach I should have mentioned I tried that as well without any luck.

@NobbZ The path I want to access is /app/uploads. Docker mounts the app in /app so if I run a remote console to the running app in production and do Path.expand("./uploads") I get the correct path: /app/uploads. Plus, Arc is able to write to the correct path without any further modifications.

I think posting my Docker file might help:

# Build Stage
FROM elixir:1.9 as build

ENV DEBIAN_FRONTEND=noninteractive
ENV HOME=/opt/app/ TERM=xterm

RUN \
  curl -sL https://deb.nodesource.com/setup_10.x | bash - && \
  curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add - && \
  echo "deb http://dl.yarnpkg.com/debian/ stable main" | tee /etc/apt/sources.list.d/yarn.list && \
  apt-get update && apt-get -y install nodejs yarn

## Install Hex+Rebar
RUN mix local.hex --force && \
  mix local.rebar --force

WORKDIR /opt/app

ENV MIX_ENV=prod

## Cache elixir deps
RUN mkdir config
COPY config/* config/
COPY mix.exs mix.lock ./
RUN mix do deps.get, deps.compile

## Cache node deps
COPY assets/*.json ./assets/
COPY assets/yarn.lock ./assets/
RUN cd ./assets && yarn install

## Compile assests & create digest
COPY . .
RUN npm run deploy --prefix ./assets
RUN mix phx.digest

## Compile Elixir release
RUN MIX_ENV=prod mix distillery.release --env=prod

# Release Stage
FROM elixir:1.9-slim

EXPOSE 4000
ENV MIX_ENV=prod REPLACE_OS_VARS=true SHELL=/bin/sh

WORKDIR /app
COPY --from=build /opt/app/_build/prod/rel/c37 ./

ENTRYPOINT ["bin/c37"]

The app is started when docker-compose creates the web service with the foreground option

OvermindDL1

OvermindDL1

Really should set it to /app/uploads then, don’t use relative paths without the tuple format and even then only within an application itself, not outside of it. The CWD can change in unexpected ways in a massively concurrent system.

I am curious if Plug.Static requires things it is serving to be local though, hmm…

supernova32

supernova32 OP

Thanks for the suggestion, but I already tried “hardcoding” the full path, and it did not work either.

Is it possible to define environment specific plugs, or would having 2 plugs be good enough? The paths are different on prod and dev, so if I hardcode them to “/app/uploads” dev breaks.

NobbZ

NobbZ

As plug is a macro, I’m not even sure if it resolve the path during compile time or runtime. Wrap an IO.insoect and watch application build as well as start carefully for output. The :label option is of great help for such things.

OvermindDL1

OvermindDL1

At compile-time for Plug.Static.

And I just tested it here with an absolute directory and it worked, so there is something else going on that’s possibly not related to the path for Plug.Static?

supernova32

supernova32 OP

Yeah, I thought about that myself, but I really do not want to start a marathon of a debugging session when simply a Base64 encode of the image will solve my issues. Once this application gets more usage, uploaded assets will be stored on S3 anyways, but I just can’t fight this nagging feeling. What I’m trying to do should work.I just can’t figure out why.

OvermindDL1

OvermindDL1

Any chance at making an SSCCE that we can just git clone and spool up or so?

supernova32

supernova32 OP

Are you working with a Docker image as well?

Where Next? Top

Trending in Questions Top

katta
I having some trouble figuring out if I have set myself too strict of standards for my production server. Currently I can handle 75% of r...
New
achenet
Hello, I’m trying to build a basic Phoenix web-app, and I’d like to use Tailwind. However, when I launch mix phx.server, I get an error...
New
kpanic
Hi everyone, I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding. I sta...
New
Cxx-mlr
I’m working on a small exercise involving update_in/3, and I came up with this solution: data = %{ name: "Periodic Table", category:...
New
ChrisAmelia
I’ve got trouble wrapping my head around the order in which functions are called in this snippet (from Phoenix’s authentication): toke...
New
dillonoconnor
Is there any way to avoid the Hologram compiler running when using iex? It seems like the front-end code could potentially be disregarded...
New
thiagogsr
** (ArgumentError) expected :max_attempts to be a positive integer, got: {:@, [line: 10, column: 19], [{:max_attempts, [line: 10, column:...
New

Other Trending Topics Top

GenericJam
Edit: 2026 May 15 - This post is archived. Mob is alive!! Main docs: mob v0.7.11 — Documentation A bit of explanation for the slightly c...
New
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
budgie
A little off-topic, but I feel like people here have a good head on their shoulders. I used to be quite good at making software. Was luc...
New
KristerV
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews