How to pull and run a docker image from a repo - Docker - docker

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.

Related

docker container can't communicate to other container on the same network

I have two images that should communicate with each other. The first image is the web API, and the second image is the web itself.
Before I build and run the images, I create a new network called nat using the command:
docker network create nat
After this, I start to create my images which I called image-api that runs on port 8080 and image-web that run on port 8081.
Then, I run the image-api image using the command:
docker run -d -p 3000:8080 --network nat image-api
I mapped the container port 8080 to my host port 3000. I tried to access the localhost port 3000 in my browser and it's running without an error and giving me the response as it should be.
The problem here is, my second image, the image-web image. I try to run it using the command:
docker run -d -p 3001:8081 --network nat image-web
When I try to access localhost:3001 in my browser, it's running, but not giving the data from image-api container. When I try to logs the image-web container, it's giving me an error like:
Error: getaddrinfo ENOTFOUND image-api
Just in case, I try to access the image-api container from my image-web container using URL like this:
http://image-api/ping
Here's my image-api Dockerfile:
FROM node:14 as builder
WORKDIR /app
COPY package.json /app
RUN npm install
FROM node:14
EXPOSE 8080
CMD ["node", "server.js"]
WORKDIR /server
COPY --from=builder /app/node_modules /server/node_modules
COPY server.js .
And here's my image-web Dockerfile:
FROM node:14 as builder
WORKDIR /app
COPY package.json .
RUN npm install
FROM node:14
ENV IMAGE_URL=http://image-api/ping
EXPOSE 8081
CMD ["node", "app.js"]
WORKDIR /web
COPY --from=builder /app/node_modules /web/node_modules
COPY /src .
Just in case, I already tried to run both of them without docker, and they run as it should be in my local computer.
EDIT:
I've also tried to run the container using name like this:
docker run -d -p 3000:8080 --network nat --name imageapi image-api
And try to access it using:
http://imageapi/ping
But it's still giving me the same error
SOLUTIONS:
As being pointed out by #davidMaze on the answer. After running our container using --name tag. I can access my container using http://imageapi:8080/ping
You need to explicitly specify a docker run --name when you start the container. Since you can run multiple containers from the same image, Docker doesn't automatically assign a container name based on the image name; you need to set it yourself.
docker run -d -p 3000:8080 --network nat --name image-api image-api
docker run -d -p 3001:8081 --network nat --name image-web image-web
Once you set the container name, Docker provides an internal DNS service and the container will be reachable by that name from other containers on the same network.
Use bridge networks in the Docker documentation describes this further, though mostly in contrast to an obsolete networking mode.

Docker run asp.net core image

I have simple asp.net core app with /students endpoint thats return test data for me. I have following docker file
FROM mcr.microsoft.com/dotnet/core/aspnet:3.1
COPY bin/Release/netcoreapp3.1/publish/ App/
WORKDIR /App
EXPOSE 5000
ENTRYPOINT ["dotnet", "Node.dll", "--urls", "http://0.0.0.0:5000"]
Then, i run it using docker run node -p 5000:5000 and expect if I go for localhost:5000/students I get response but I'm not.
If I do something like docker exec -it container_name /bin/bash and then curl 0.0.0.0:5000/students I get response so I'm sure it's running. How can I use my app outside of container? I've tried other ports like 80 and 443 etc.
Well, the problem was you have to write docker run -p 5000:80 node to make it work as espected because docker didn't apply any -p args

I am able to run the container in docker but unable to view in the browser

I am new to Docker. Firstly, I have created Dockerfile with in source code location.
Here is my Dockerfile
FROM nginx:latest
RUN mkdir /app
COPY . /app
EXPOSE 8000
lately, build an image using: docker build -t mywebapp:v1 .
and i have run the container using following command:
docker run -d -p 8000:8000 mywebapp:v1
problem is : container is running using port 8000, but unable to view in the browser
http://192.168.13.135:8000
please help me out in this problem inorder to view in the browser.

pwa-studio docker configuration dev-environmet

In order to work with pwa-studio I have to use docker due to the fact that I am using windows. So I made it in the simplest possible way for me with Dockerfile:
FROM node:10
RUN mkdir /app
WORKDIR /app
EXPOSE 3000
then I created container:
winpty docker run -it -p 3000:3000 --mount type=bind,source="$(pwd)",target=/app nfr:1.0 bash
all commands related to installation packages or running app I make being attached to container
Using address localhost:3000 I can see my app running since I expose the port.
PROBLEM:
One of the first configuration steps in pwa-studio is to add a custom hostname and SSL cert, which is done with
yarn buildpack create-custom-origin ./
As a result app inside container is no longer available via localhost:3000 but via domain name, in my case it is: https://pwa-aynbv.local.pwadev:3000/
How to configure docker to expose this domain outside of the container ?
Thanks in advance for any help

Cannot access server running in container from host

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

Resources