I am facing an error when running docker container on port 5000 on localhost. I deployed a fastapi Machine learning model and want to run it using docker container.
Error:
This site can’t be reached
The web page at http://0.0.0.0:5000/ might be temporarily down or it may have moved permanently to a new web address.
ERR_ADDRESS_INVALID
Dockerfile:
FROM tiangolo/uvicorn-gunicorn-fastapi:python3.8
WORKDIR /app
COPY . /app
RUN pip install -r requirements.txt
EXPOSE 5000
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "5000"]
Command I am running for docker to build container:
docker build -t tags:latest .
Command to run docker container I am using:
docker run -p 5000:5000 tags:latest
In case you are getting the error message from a client (e.g. your browser on the same machine):
When you open a server socket for IP 0.0.0.0 it means it opens that server socket on all interfaces.
When you connect with a client to a server, you need to give a concise address, such as 127.0.0.1 or one of the configured IPs for that machine. Connecting to 0.0.0.0 should always result in an error message.
Related
I have create a Docker Image based on the following DockerFile
FROM tiangolo/uvicorn-gunicorn-fastapi:python3.7
COPY . /usr/app/
EXPOSE 80
WORKDIR /usr/app/
RUN pip install -r requirements.txt
CMD ["uvicorn", "app_Testing_Praveen:app", "--host", "0.0.0.0", "--port", "80"]
following the documentation available at
https://fastapi.tiangolo.com/deployment/docker/
After running the command
docker run -p 80:80 image_name
My docker image is running but giving the address as 0.0.0.0:80
But I am not able to find the absolute link to open the application. I know, due to virtualization there will be different external IP address for docker.
I found that IP on my docker network interface as "docker subnet mask" but that value is also not opening the applicatiln on browser.
My docker version is Docker version 20.10.5, build 55c4c88 and I am running this on windows.
You reach your services inside Docker containers, via the IP of the host machine
So you either access your service by http://localhost:80 or, from another machine, with http://<docker_host_ip>:80.
I am unable to load swagger api using Docker where as the jar file runs fine: java -jar abc.jar
My swagger api doc: http://localhost:8080/api-docs/swagger.json
Docker File
FROM openjdk:14.0.2
RUN mkdir /opt/app
COPY ./build/libs/OrderManagementSystem-1.0-SNAPSHOT.jar /opt/app
EXPOSE 8080
CMD ["java", "-jar", "/opt/app/OrderManagementSystem-1.0-SNAPSHOT.jar"]
Command
docker run -p 3333:8080 order-price
I am unable to load the page http://localhost:8080/swagger
it seems you are hitting the wrong URL.
it should be,
http://localhost:3333/swagger
or
http://localhost:3333/api-docs/swagger.json
when you are binding ports while running docker, it is docker run -p host-port:container-port
If you run your container like that:
docker run -p 3333:8080 order-price
You are telling docker to expose internal port 8080 to port 3333 on your host.
So if you wan't to access Swagger API, you have to use :
http://localhost:3333/swagger
If you really want to user port 8080 on your host, then you have to launch your container like that :
docker run -p 8080:8080 order-price
I have this docker file:
# Use the official image as a parent image
FROM mysql/mysql-server:8.0
# Set the working directory
RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app
# Copy the file from your host to your current location
COPY customers.sql .
COPY entrypoint.sh .
# Inform Docker that the container is listening on the specified port at runtime.
EXPOSE 1433:1433
# Run the command inside your image filesystem
RUN chmod +x entrypoint.sh
# Run the specified command within the container.
RUN /bin/bash ./entrypoint.sh
And entypoint.sh:
mysql --host=localhost --protocol=tcp -u root -pMypassword -e "create database customersDatabase; use customersDatabase; source customers.sql;"
but i get the following error message:
ERROR 2003 (HY000): Can't connect to MySQL server on 'localhost' (99)
when i run docker build
what is the correct way to build entrypoint.sh in order to run docker commands?
BEFORE OP EDIT:
problem:
./entrypoint.sh: line 2: docker: command not found
You are trying to run docker inside docker.
Possible solutions
1) Mount host's docker sock
or
2) Install docker inside docker before you run your
-> apt install docker.io
--> expect super size of your image
entrypoint
Difference between 1) and 2)
in 1) your docker's docker is the host's docker
while in 2) installed docker in the docker is independent and thus isolated from host
AFTER OP EDIT
problem:
ERROR 2003 (HY000): Can't connect to MySQL server on 'localhost' (99)
EDIT: And since you edit your question, which now doesnt correspond with your title, I will provide you with your second problem
You cannot connect to your localhost, because insider docker, localhost is docker itself, not your host.
This can be solved by using host network driver.
Or preferably, put your db in docker too, have in the same docker network,expose port, name your db container as mysql_database, and connect to it as mysql_database:port
Or dont try to connect to db which is in your container from within your container. Thats I think antipattern. Usually it should be possible to get into db's CLI where you can run commands
I have a WebApi created using .Net Core. I have a .dockerfile in the root of my solution:
FROM mcr.microsoft.com/dotnet/core/aspnet:2.1-stretch-slim AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443
FROM mcr.microsoft.com/dotnet/core/sdk:2.1-stretch AS build
WORKDIR /src
COPY ["Nuget.config", ""]
COPY ["Proyecto.WebApi/Proyecto.WebApi.csproj", "Proyecto.WebApi/"]
COPY ["Proyecto.Model/Proyecto.Model.csproj", "Proyecto.Model/"]
COPY ["Proyecto.Bl/Proyecto.Bl.csproj", "Proyecto.Bl/"]
RUN dotnet restore "Proyecto.WebApi/Proyecto.WebApi.csproj" --configfile "Nuget.config"
COPY . .
WORKDIR "/src/Proyecto.WebApi"
RUN dotnet build "Proyecto.WebApi.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "Proyecto.WebApi.csproj" -c Release -o /app/publish
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "Proyecto.WebApi.dll"]
I can build, run and push my Api without problem using those commands:
docker build -t dockerlocal .
docker run -d -p 8080:80 --name myapp dockerlocal
docker tag <image_id> docker.nexus.example.com/dockerlocal
docker push docker.nexus.example.com/dockerlocal
When I test my api from the browser http://localhost:8080/api/values, it works perfectly.
Now I need, download the image from the repo and run the Api, so I execute:
docker pull docker.nexus.example.com/dockerlocal
docker run -d -p 9090:90 --name mynexusapp docker.nexus.example.com/dockerlocal
According the console, everything is working. But when I test the API http://localhost:9090/api/values I get in my browser:
"localhost no envió ningún dato. ERR_EMPTY_RESPONSE"
What is the problem? Why I can't run my WebApi after docker pull comand.
Following discussion in the comments: the issue here was that the port mapping was changed from 8080:80 in the first command to 9090:90 in the second command. Switching mapping back to port 80 for container port fixes the issue as confirmed by the author.
Now, to explain what happens here: Port mapping 8080:80 means that you are mapping port 8080 on the host environment to port 80 on the guest environment. Where guest environment is the actual container.
Host port part of the mapping may be changed arbitrarily - i.e. changing it from 8080 to 9090 works - as long as the host port is not taken by another process. The same however is not true about the guest port, since the guest port is determined by the process that is run on the container. So if the container application listens to port 80, then switching guest mapping to port 90 (as happened in this case) won't work - because nothing is listening there.
It is also quite possible to have several port mapping, i.e. if in the container you have port 80 where your application listens and port 90 where some admin console listens, you may end up with 2 port mappings: say, 8080:80 and 9090:90.
If you want to use multiple environments at the same time - spawn multiple containers, i.e. you could do something like:
docker run -d -p 8080:80 --name myapp1 dockerlocal
docker run -d -p 9090:80 --name myapp2 dockerlocal
Note however, that you will end up with 2 separate containers running independent from each other.
I have a simple Dockerfile
FROM golang:latest
RUN mkdir -p /app
WORKDIR /app
COPY . .
ENV GOPATH /app
RUN go install huru
EXPOSE 3000
ENTRYPOINT /app/bin/huru
I build like so:
docker build -t huru .
and run like so:
docker run -it -p 3000:3000 huru
for some reason when I go to localhost:3000 with the browser, I get
I have exposed servers running in containers to the host machine before so not sure what's going on.
From the information provided in the question if you see logs of the application
(docker logs <container_id>) than the docker application starts successfully and it looks like port exposure is done correctly.
In any case in order to see ports mappings when the container is up and running you can use:
docker ps
and check the "PORTS" section
If you see there something like 0.0.0.0:3000->3000/tcp
Then I can think about some firewall rules that prevent the application from being accessed...
Another possible reason (although probably you've checked this already) is that the application starts and finishes before you actually try to access it in the browser.
In this case, docker ps won't show the exited container, but docker ps -a will.
The last thing I can think of is that in the docker container itself the application doesn't really answer the port 3000 (I mean, maybe the startup script starts the web server on some other port, so exposing port 3000 doesn't really do anything useful).
In order to check this you can enter the docker container itself with something like docker exec -it <container_id> bash
and check for the opened ports with lsof -i or just wget localhost:3000 from within the container itelf
Try this one, if this has any output log. Please check them...
FROM golang:latest
RUN apt -y update
RUN mkdir -p /app
COPY . /app
RUN go install huru
WORKDIR /app
docker build -t huru:latest .
docker run -it -p 3000:3000 huru:latest bin/huru
Try this url: http://127.0.0.1:3000
I use the loopback