Poblem listeing on port when running phoenix through docker

Hello fellas I am trying to deploy my phoenix app using distillery releases and docker I have been following tthis tutorial to get started.

What I have been up so far is this
prod.exs:

 config :greek_coin, GreekCoinWeb.Endpoint,
   http: [port: 80],       
   url: [host: "localhost"],       
   force_ssl: [rewrite_on: [:x_forwarded_proto]],
   https: [
     :inet6,               
     port: 443,
     cipher_suite: :strong,
     otp_app: :greek_coin, 
     keyfile: "/path/to/key.txt",
     certfile: "/path/to/cert.cert"
   ]

Docker file only slightly modified from the one in the turorial

# Set this to true if this release is not a Phoenix app
ARG SKIP_PHOENIX=false
# If you are using an umbrella project, you can change this
# argument to the directory the Phoenix app is in so that the assets
# can be built
ARG PHOENIX_SUBDIR=.

ENV SKIP_PHOENIX=${SKIP_PHOENIX} \
    APP_NAME=${APP_NAME} \
    APP_VSN=${APP_VSN} \
    MIX_ENV=${MIX_ENV}

# By convention, /opt is typically used for applications
WORKDIR /opt/app

# This step installs all the build tools we'll need
RUN apk update && \
  apk upgrade --no-cache && \
  apk add --no-cache \
    nodejs \
    yarn \
    git \
    build-base && \
  mix local.rebar --force && \
  mix local.hex --force



# This copies our app source code into the build container
COPY . .

RUN mix do deps.get, deps.compile, compile 

# This step builds assets for the Phoenix app (if there is one)
# If you aren't building a Phoenix app, pass `--build-arg SKIP_PHOENIX=true`
# This is mostly here for demonstration purposes
RUN if [ ! "$SKIP_PHOENIX" = "true" ]; then \
  cd ${PHOENIX_SUBDIR}/assets && \
  yarn install && \
  yarn deploy && \
  cd - && \
  mix phx.digest; \
fi

RUN \
  mkdir -p /opt/built && \
  mix distillery.release --verbose && \
  cp _build/${MIX_ENV}/rel/${APP_NAME}/releases/${APP_VSN}/${APP_NAME}.tar.gz /opt/built && \
  cd /opt/built && \
  tar -xzf ${APP_NAME}.tar.gz && \
  rm ${APP_NAME}.tar.gz
# From this line onwards, we're in a new image, which will be the image used in production
FROM alpine:${ALPINE_VERSION}

# The name of your application/release (required)
ARG APP_NAME=greek_coin

RUN apk update && \
    apk add --no-cache \
      bash \
      openssl-dev \
      openssl\
      erlang-crypto


ENV REPLACE_OS_VARS=true \
    APP_NAME=${APP_NAME}

WORKDIR /opt/app

COPY --from=builder /opt/built .
RUN echo $DATABASE_HOST
RUN echo $DATABASE_USER
RUN echo $DATABASE_PASS
RUN echo $DATABASE_NAME
EXPOSE 80

CMD trap 'exit' INT; /opt/app/bin/${APP_NAME} foreground

make file

.PHONY: help
                               
APP_NAME ?= `grep 'app:' mix.exs | sed -e 's/\[//g' -e 's/ //g' -e 's/app://' -e 's/[:,]//g'`
APP_VSN ?= `grep 'version:' mix.exs | cut -d '"' -f2`
BUILD ?= `git rev-parse --short HEAD`

help:
    @echo "$(APP_NAME):$(APP_VSN)-$(BUILD)"
    @perl -nle'print $& if m{^[a-zA-Z_-]+:.*?## .*$$}' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}'

build: ## Build the Docker image
    docker build --build-arg APP_NAME=$(APP_NAME) \
        --build-arg APP_VSN=$(APP_VSN) \
        -t $(APP_NAME):$(APP_VSN)-$(BUILD) \
        -t $(APP_NAME):latest .

run: ## Run the app in Docker  
    docker run --env-file config/docker.env \
        --expose 80 -p 80:80 \ 
        --expose 443 -p 443:443 \
        --network host \       
        --rm -it $(APP_NAME):latest

docker-compose.yml

version: '3.5'

services:
  web:
    image: "greek_coin:latest"
    network_mode: host
    env_file:
      - docker.env

docker.env

HOSTNAME="localhost"
PORT=80
LANG=en_US.UTF-8  
REPLACE_OS_VARS=true           
ERLANG_COOKIE=greek_coin

Then I procced with the make build which outputs

Successfully built c244749bcf14
Successfully tagged greek_coin:0.1.1-114f7e3
Successfully tagged greek_coin:latest

And then I am running with docker-compose up
which only shows the following warning

Recreating greek_coin_web_1 ... done
Attaching to greek_coin_web_1
web_1  | warning: :simple_one_for_one strategy is deprecated, please use DynamicSupervisor instead
web_1  |   (elixir 1.10.4) lib/supervisor.ex:604: Supervisor.init/2
web_1  |   (stdlib 3.13) supervisor.erl:301: :supervisor.init/1
web_1  |   (stdlib 3.13) gen_server.erl:417: :gen_server.init_it/2
web_1  |   (stdlib 3.13) gen_server.erl:385: :gen_server.init_it/6
web_1  |   (stdlib 3.13) proc_lib.erl:226: :proc_lib.init_p_do_apply/3

Thing is that in my docker-compose.yml I am using the network_mode: host
using this parameter I got rid of the complains about database connection I had earlier but further more my expectation is that I should be able to access my app just by visiting localhost in my host machine. But this is not the case the container is runnig without any further output but I cannot access it through localhost. What do I miss ?

Any help or references that might help me understand my problem are greatly appriciated
Thanks in advance.

Your compose file isn’t set up to do any portmapping.

Please use the port directive for your web service.

thanks @NobbZ

Changing the docker-compose.yml to

version: '3.5'       

services:                      
  web:
    image: "greek_coin:latest" 
    network_mode: host         
    ports:
        - "80:80" 
        - "443:443"            
    env_file:                  
      - docker.env

or (because of nework_mode: host)

version: '3.5'       

services:                      
  web:
    image: "greek_coin:latest" 
    network_mode: host         
    ports:
        - "80"
        - "443"
    env_file:                  
      - docker.env

seem to make no difference !

the docker documentation also mentions:

ports

Expose ports.

Note
Port mapping is incompatible with network_mode: host

Maybe server: true is missing from the Endpoint config?

3 Likes

Oh, i haven’t realized the network mode, as I usually prefer explicit portmapping rather than polluting the host systems network randomly…

Thank you this was actually what was missing. I took some time because now I have different problems but thats probably because the server is running.

Thanks for your response but let me ask why do consider that polluting ? And can you please just give me a scenarion that this practice is problematic ? This is a genunie question as I am new to docker and chiefly using it as a container to avoid not in my computer issues and in general for easeay when shipping the app.

If one starts everything within the hosts network, one needs to consider ports already when building the containers and it is hard to spin the same container multiple times.

By using explicit portmapping, you can spin the very same container, just mapping different ports.