Using a docker file on github codespaces results in 502 bad gateway - docker

I have a fastapi python script that works on codespaces when I use the following command:
uvicorn main:fast_API_app --reload
The following code appears and my api's work fine:
INFO: Will watch for changes in these directories: ['/workspaces/WebAPI']
INFO: Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
INFO: Started reloader process [3229] using WatchFiles
INFO: Started server process [3241]
INFO: Waiting for application startup.
INFO: Application startup complete.
resulting in
Running it in github codespaces works fine
However, when I turn this into a docker container, running it results in a 502 Bad Gateway
terminal:
docker container run <username>/<container name>:v0.0.1
INFO: Started server process [1]
INFO: Waiting for application startup.
INFO: Application startup complete.
INFO: Uvicorn running on http://0.0.0.0:8000 (Press CTRL+C to quit)
Whether i select port 8000 to be public or private in github codespaces makes no difference.
Below is my Dockerfile which is used to build the image.
# For more information, please refer to https://aka.ms/vscode-docker-python
FROM python:3.10-slim
EXPOSE 8000
# Keeps Python from generating .pyc files in the container
ENV PYTHONDONTWRITEBYTECODE=1
# Turns off buffering for easier container logging
ENV PYTHONUNBUFFERED=1
# Install pip requirements
COPY requirements.txt .
RUN python -m pip install -r requirements.txt
WORKDIR /app
COPY . /app
# Creates a non-root user with an explicit UID and adds permission to access the /app folder
# For more info, please refer to https://aka.ms/vscode-docker-python-configure-containers
RUN adduser -u 5678 --disabled-password --gecos "" appuser && chown -R appuser /app
USER appuser
# During debugging, this entry point will be overridden. For more information, please refer to https://aka.ms/vscode-docker-python-debug
CMD ["uvicorn", "main:fast_API_app", "--host", "0.0.0.0", "--port","8000"]
It results in the following error:
image of the error
It does not show an error code.
What I already tried (but potentially also did wrong):
I tried exposing different ports
tried running gunicorn instead of uvicorn
Searched on Stackoverflow.com for docker codespaces and bad gateway
Toggling the port forward to public / private
Changing the Port Protocol to https
rebuilding the container several times

Related

Docker Port Forwarding for FastAPI REST API

I have a simple FastAPI project called toyrest that runs a trivial API. The code looks like this.
from fastapi import FastAPI
__version__ = "1.0.0"
app = FastAPI()
#app.get("/")
def root():
return "hello"
I've built the usual Python package infrastructure around it. I can install the package. If I run uvicorn toyrest:app the server launches on port 8000 and everything works.
Now I'm trying to get this to run in a Docker image. I have the following Dockerfile.
# syntax=docker/dockerfile:1
FROM python:3
# Create a user.
RUN useradd --user-group --system --create-home --no-log-init user
USER user
ENV PATH=/home/user/.local/bin:$PATH
# Install the API.
WORKDIR /home/user
COPY --chown=user:user . ./toyrest
RUN python -m pip install --upgrade pip && \
pip install -r toyrest/requirements.txt
RUN pip install toyrest/ && \
rm -rf /home/user/toyrest
CMD ["uvicorn", "toyrest:app"]
I build the Docker image and run it, forwarding port 8000 to the running container.
docker run -p 8000:8000 toyrest:1.0.0
INFO: Started server process [1]
INFO: Waiting for application startup.
INFO: Application startup complete.
INFO: Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
When I try to connect to http://127.0.0.1:8000/ I get no response.
Presumably I am doing the port forwarding incorrectly. I've tried various permutations of the port forwarding argument (e.g. -p 8000, -p 127.0.0.1:8000:8000) to no avail.
This is such a basic Docker command that I can't see how I'm getting it wrong, but somehow I am. What am I doing wrong?
try to add this line to yourCMD in ̀dockerfile`:
CMD ["uvicorn", "toyrest:app","--host", "0.0.0.0"]

R10 Error when deploying a Docker image to Heroku

I faced some teething issue when trying to deploy my Docker image which contains a simple streamlit app to Heroku. My issue is that I am unable to access my Docker after deployment. On closer look, I discovered the following error:
Error R10 (Boot timeout) -> Web process failed to bind to $PORT within 60 seconds of launch
I had researched and understood that this is because the port is unavailable, since Heroku will dynamically assign port number.
I had made sure that this will not happen by putting the following my Dockerfile.
Dockerfile:
FROM python:3.7
COPY . /app
WORKDIR /app
RUN pip install streamlit
ENTRYPOINT ["streamlit","run", "--server.enableCORS", "false" ,"--server.port", "$PORT"]
CMD ["app.py"]
I am now able to see that the Network URL and External URL port number are assigned by Heroku as it is not the typical 5901 number.
What puzzled me, however, is why is the container unable to bind to the given dynamic port number? I thought the app would be using the given dynamic number?
The problem is that $PORT does not get replaced with the corresponding environment variable when the Docker run is executed on the Heroku Docker Registry.
An alternative is to create a Docker file which invokes .sh script
FROM python:3.7
COPY . /app
WORKDIR /app
RUN pip install streamlit
ENTRYPOINT "/startup.sh"
and the startup.sh
echo PORT $PORT
streamlit run --server.enableCORS false --server.port $PORT app.py

Can't access Dockerized Vue's localhost:8080 on host machine (despite EXPOSE-ing port)

I can't access my Vue app on localhost:8080 anymore after Dockerizing the app.
I have a Dockerfile with the following contents:
# Base the image off of the NodeJS image
FROM node
# Set the working directory to be the HOME directory
WORKDIR /root
# Install NPM dependencies early in the build process
COPY ./package.json /root
COPY ./package-lock.json /root
RUN npm install
# Specify what port will be available - necessary for VPC network
EXPOSE 8080
# Copy our application files to the image
COPY ./.browserslistrc /root
COPY ./.eslintrc.js /root
COPY ./.env /root
COPY ./babel.config.js /root
COPY ./README.md /root
COPY ./vue.config.js /root
COPY ./public /root/public
COPY ./src /root/src
# Start the container running our Node app
CMD ["npm", "run", "serve"]
(Before Dockerizing, npm run serve allowed me to access the Vue app through my web browser.)
Then I run the PS command docker build:
PS C:\Users\User\mealSocial-dev> docker build -t finalvue app
Sending build context to Docker daemon 126.8MB
Step 1/15 : FROM node
---> 448d0873ea84
[...]
Step 15/15 : CMD ["npm", "run", "serve", "--port", "\"8080\""]
---> Running in c4840f98e5dc
Removing intermediate container c4840f98e5dc
---> 904928fa859c
Successfully built 904928fa859c
Successfully tagged finalvue:latest
SECURITY WARNING: You are building a Docker image from Windows against a non-Windows Docker host. All files and directories added to build context will have '-rwxr-xr-x' permissions. It is recommended to double check and reset permissions for sensitive files and directories.
...Then docker run -p 8080:8080:
PS C:\Users\User\mealSocial-dev> docker run -p 8080:8080 finalvue
> meal-app#0.1.0 serve /root
> vue-cli-service serve
INFO Starting development server...
<s> [webpack.Progress] 0% compiling
[...]
DONE Compiled successfully in 8147ms11:39:59 AM
<s> [webpack.Progress] 100%
App running at:
- Local: http://localhost:8080/
It seems you are running Vue CLI inside a container.
Access the dev server via http://localhost:<your container's external mapped port>/
Note that the development build is not optimized.
To create a production build, run npm run build.
Despite it saying It seems you are running Vue CLI inside a container. Access the dev server via http://localhost:<your container's external mapped port>/, I get This page isn’t working. localhost didn’t send any data. ERR_EMPTY_RESPONSE:
I'm EXPOSE-ing the port in the Dockerfile and adding the -p 8080:8080 tag when I run docker run. What am I missing?
from the comment section:
add --host 0.0.0.0 to npm run serve
or
add host: 0.0.0.0 to a config (./vue.config.js)

Expose Both Ports 8080 and 3000 For Cloud Run Deployment

TL:DR - I am trying to deploy my MERN stack application to GCP's Cloud Run. Struggling with what I believe is a port issue.
My React application is in a client folder inside of my Node.js application.
Here is my one Dockerfile to run both the front-end and back-end:
FROM node:13.12.0-alpine
WORKDIR /app
COPY . ./
# Installing components for be connector
RUN npm install --silent
WORKDIR /app/client
RUN npm install --silent
WORKDIR /app
RUN chmod +x /app/entrypoint.sh
ENTRYPOINT [ "/app/entrypoint.sh" ]
... and here is my entrypoint.sh file:
#!/bin/sh
node /app/index.js &
cd /app/client
npm start
docker-compose up works locally, and docker run -p 8080:8080 -p 3000:3000 <image_id> runs the image I built. Port 8080 is for Node and port 3000 for the React app. However, on Cloud Run, the app does not work. When I visit the app deployed to Cloud Run, the frontend initially loads for a split second, but then the app crashes as it attempts to make requests to the API.
In the Advanced Settings, there is a container port which defaults to 8080. I've tried changing this to 3000, but neither works. I cannot enter 8080,3000, as the field takes valid integers only for the port. Is it possible to deploy React + Node at the same time to Cloud Run like this? How can I have Cloud Run listen on both 8080 and 3000, as opposed to just 1 of the 2?
Thanks!
It's not currently possible.
Instead, you can run multiple processes inside Cloud Run, but instead use nginx to proxy requests between them depending on the URL, similar to what's recommended in this answer.

Docker gets Flask server running, but I can't connect to it with my browser

I am trying to run my flask application through Docker. I had it working last week, but now it is giving me some issues. I can start the server, but I can't seem to visit any content through my browser.
Here is my app.py file (reduced):
... imports ...
app = Flask(__name__)
DATABASE = "users.db"
app.secret_key = os.environ["SECRET_KEY"]
app.config['UPLOAD_FOLDER'] = 'static/Content'
BASE_PATH = os.path.dirname(os.path.abspath(__file__))
# For Dropbox
__TOKEN = os.environ["dbx_access_token"]
__dbx = None
... functions ...
if __name__ == '__main__':
app.run(port=5001, threaded=True, host=('0.0.0.0'))
Here is my Dockerfile:
# Use an official Python runtime as a parent image
FROM python:3
# Set the working directory to /VOSW-2016-original
WORKDIR /VOSW-2016-original
# Copy the current directory contents into the container at /VOSW-2016-original
ADD . /VOSW-2016-original
# Putting a variables in the environment
ENV SECRET_KEY="XXXXXXXX"
ENV dbx_access_token="XXXXXXXX"
# Install any needed packages specified in requirements.txt
RUN pip install --trusted-host pypi.python.org -r requirements.txt
# Make port 8000 available to the world outside this container
EXPOSE 8000
# Run app.py when the container launches
CMD ["python", "app.py", "--host=0.0.0.0"]
Here are the commands I am using to start the server:
docker build -t vosw_original ./VOSW-2016-original/
and then:
docker run -i -t -p 5001:8000 vosw_original
Then I get the message:
* Running on http://0.0.0.0:5001/ (Press CTRL+C to quit)
So the server seems to be running, but I can't seem to visit it when I do any of the following:
http://0.0.0.0:5001/
http://0.0.0.0:8000/
http://127.0.0.1:5001/
http://127.0.0.1:8000/
Where am I going wrong?
You are running your app on the wrong port:
app.run(port=5001, threaded=True, host=('0.0.0.0'))
while exposing 8000. So by -p 5001:8000 you are mapping container's 8000 port (on which nothing is listening) to host's 5001 port. While your app is actually running on port 5001 within the container which is what the message is about.

Resources