PHP cannot resolve hostname - docker

I'm learning docker for my first project using Slim framework (PHP). I created a dockerfile to build an image with all the source code in it. When I run the image with the run command it shows me
<b>Warning</b>: Unknown: php_network_getaddresses: getaddrinfo failed: Name does not resolve in <b>Unknown</b> on line <b>0</b><br />
[Sat Jun 15 09:41:14 2019] Failed to listen on 127.0.0.1:8080 (reason: php_network_getaddresses: getaddrinfo failed: Name does not resolve)
Dockerfile looks like:
FROM php:7-alpine
COPY . /var/www
WORKDIR /var/www
CMD [ "php", "-S 127.0.0.1:8080 -t public" ]
Docker run command is:
sudo docker run -it --rm --network="host" --expose 8080 --name cm2 collection_manager_1
Don't know how I can fix this. Can someone help me?

The CMD syntax seems to have some problem. All the arguments in the command should be comma separated and inside the double quotes.
I made a slight change in Dockerfile and it worked.
FROM php:7-alpine
COPY . /var/www
WORKDIR /var/www
CMD [ "php", "-S", "0.0.0.0:8080", "-t", "html" ]
docker build -t testimage:v1 .
[mchawre#jumphost try]$ docker run -it --rm --network="host" --expose 8080 --name testrun testimage:v1
PHP 7.3.6 Development Server started at Sat Jun 15 11:50:19 2019
Listening on http://0.0.0.0:8080
Document root is /var/www/html
Press Ctrl-C to quit.
NOTE: Change 127.0.0.1 to 0.0.0.0 so that you can hit the php using public/private ip of your machine rather than just localhost.

Related

Docker runs only on Port 80

I am unable to run my docker image on Port 4000, even though I can run it on Port 80. What am I doing wrong here?
FROM node:latest as build
WORKDIR /usr/src/app
COPY package*.json ./
RUN npm install
COPY . .
RUN npm run build
FROM nginx:latest
COPY --from=build /usr/src/app/dist/admin /usr/share/nginx/html
EXPOSE 4200
I'm creating the image using the following command:
docker build --pull --rm -f "DockerFile" -t admin1:v1 "."
When I run it on port 80, I'm able to use it:
docker run --rm -d -p 4200:4200/tcp -p 80:80/tcp admin1:v1
However, when I run the following command, I'm unable to use it:
docker run --rm -d -p 4200:4200/tcp -p 4000:4000/tcp admin1:v1
I have researched similar questions online, but I haven't been able to fix the problem. Any suggestion will be greatly appreciated!
You need to map the docker container port to the docker host port.
Try the following Command
docker run --rm -d -p 4200:4200/tcp -p 4000:80/tcp admin1:v1
The following is the extract from the Docker Documentation
-p 8080:80 Map TCP port 80 in the container to port 8080 on the Docker host.
You can refer the link for further information.
Docker Documentation

How to properly expose/publish ports for a WebUI-based application in Docker?

I'm trying to port this webapp to Docker. I wrote the following Dockerfile:
FROM anapsix/alpine-java
MAINTAINER <name>
COPY aard2-web-0.7-java6.jar /home/aard2-web-0.7-java6.jar
COPY start.sh /home/start.sh
CMD ["bash", "/home/start.sh"]
EXPOSE 8013/tcp
Here are the contents of start.sh:
#!/bin/bash
java -Dslobber.browse=true -jar /home/aard2-web-0.7-java6.jar /home/dicts/*.slob
Then I built the image:
docker build -t aard2-docker .
And I used the following command to run the container:
docker run --name Aard2 -p 127.0.0.1:8013:8013 -v /home/<name>/dicts:/home/dicts aard2-docker
The app is running normally, prompting that it's listening at http://127.0.0.1:8013. However, I opened the address only to find that I couldn't connect to the app.
I tried using the EXPOSE command (as shown in the Dockerfile snippet above) and variants of the -p flag, such as -p 127.0.0.1:8013:8013, -p 8013:8013, -p 8013:8013/tcp, but none of them worked.
How can I expose/publish the port to 127.0.0.1 properly? Thanks!
Here's the response from the original author:
you need to tell the server to listen on all network interfaces instead of localhost - that is you are missing -Dslobber.host=0.0.0.0
this works for me:
FROM anapsix/alpine-java
COPY ./build/libs/aard2-web-0.7.jar /home/aard2-web-0.7.jar
CMD ["bash", "-c", "java -Dslobber.host=0.0.0.0 -jar /home/aard2-web-0.7.jar /dicts/*.slob"]
EXPOSE 8013/tcp
and then run like this:
docker run -v $HOME/Downloads:/dicts -p 8013:8013 --rm aard2-web
-Dslobber.browse=true opens default browser, I don't think this has any effect in docker so don't need that.
https://github.com/itkach/aard2-web/issues/12#issuecomment-895557949

Running docker container not accessible from host (localhost:8081)

Using Ubuntu.
Based on this guide:
https://www.freecodecamp.org/news/how-to-use-routing-in-vue-js-to-create-a-better-user-experience-98d225bbcdd9/
I have created a minimal vuejs project with below project structure:
https://github.com/dev-samples/samples/tree/master/vuejs-001
frontend-router/
build/
config/
src/
static/
test/
build.sh
Dockerfile.dev
package-lock.json
package.json
Where:
Dockerfile.dev
FROM node:10
RUN apt install curl
RUN mkdir /app
# add `/app/node_modules/.bin` to $PATH
ENV PATH /app/node_modules/.bin:$PATH
# install and cache app dependencies
COPY package.json /app/package.json
# make the 'app' folder the current working directory before running npm install
WORKDIR /app
RUN npm install
CMD [ "npm", "run", "dev" ]
I am building the image and running the container from that image with:
docker build -t frontend-router-image -f Dockerfile.dev .
docker rm -f frontend-router-container
docker run -it -p 8081:8080 -v ${PWD}:/app/ -v /app/node_modules --name frontend-router-container frontend-router-image
which gives:
DONE Compiled successfully in 1738ms 3:49:45 PM
I Your application is running here: http://localhost:8080
Since I add -p 8081:8080 to docker run command I would expect that I can access the application from my host browser on:
http://localhost:8081/
but it just gives below error:
I works fine when I run it with vanilla npm from my host. But why can't I access the application when its run from inside a docker container?
Source code here:
https://github.com/dev-samples/samples/tree/master/vuejs-001
As suggested below I have tried:
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e011fb9e39e8 frontend-router-image "docker-entrypoint.s…" 12 seconds ago Up 9 seconds 0.0.0.0:8081->8080/tcp frontend-router-container
$ docker run -it --rm --net container:frontend-router-container nicolaka/netshoot ss -lnt
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 127.0.0.1:8080 0.0.0.0:*
For comparison this project works fine:
https://github.com/dev-samples/samples/tree/master/vuejs-002
Meaning that when I run a container I can access the web application on my host browser on localhost:8081
Based on this:
https://github.com/webpack/webpack-dev-server/issues/547
and:
https://dev.to/azawakh/don-t-forget-to-give-host-0-0-0-0-to-the-startup-option-of-webpack-dev-server-using-docker-1483
https://pythonspeed.com/articles/docker-connection-refused/
It works if I change:
host: 'localhost', // can be overwritten by process.env.HOST
to:
host: '0.0.0.0', // can be overwritten by process.env.HOST
in the file: /frontend-router/config/index.js
When you have connection reset it means usually that nobody is listen on the port .
It seems you are listening on localhost , you must
listening on 0.0.0.0 when you are in the docker .
in your file config/index.js , host is localhost , you must remove the host directive
If you listening on 127.0.0.1or localhost , you are listening on local network , so
inside the container , the web server can be accessed only by local process .
Another source of problems you can have , you are connecting to the wrong port .
if you run with docker run -it -p 8081:8080 you must acces to http://localhost:8081/
see
Publish or expose port (-p, --expose)
from https://docs.docker.com/engine/reference/commandline/run/

Getting error executable file not found in $PATH when trying verbose run docker container or when docker exec

I am trying to containerize my vue-js app: and below is my docker file:
FROM node:9.11.1-alpine
# install simple http server for serving static content
RUN npm install -g http-server
# make the 'app' folder the current working directory
WORKDIR /app
# copy both 'package.json' and 'package-lock.json' (if available)
COPY package*.json ./
# install project dependencies
RUN npm install
# copy project files and folders to the current working directory (i.e. 'app' folder)
COPY . .
# build app for production with minification
RUN npm run build
EXPOSE 8080
CMD [ "http-server", "dist" ]
when I try to run docker with verbose flag -v I get this error:
$ docker run -it vue-js-app -v
docker: Error response from daemon: oci runtime error: container_linux.go:265: starting container process caused "exec: \"-v\": executable file not found in $PATH".
and without -v flag I get the below:
$ docker run -it vue-js-app
Starting up http-server, serving dist
Available on:
http://127.0.0.1:8080
http://172.17.0.3:8080
Hit CTRL-C to stop the server
and then if I try to enter the container:
$ docker exec -it 0778c0e3ae05 bash
oci runtime error: exec failed: container_linux.go:265: starting container process caused "exec: \"bash\": executable file not found in $PATH"
I get same error , but navigating to ip address with port i get page not found
The format of the docker container run command is:
Usage: docker container run [OPTIONS] IMAGE [COMMAND] [ARG...]
So, when you execute docker run -it vue-js-app -v, the -v is being passed to the container as the command, replacing the command specified in the Dockerfile (CMD [ "http-server", "dist" ]). This results in the message “executable file not found in $PATH” because there is not a command named -v in the container.
If you are trying to pass -v to http-server, you have to repeat the existing command in the Dockerfile.
docker container run -it vue-js-app http-server dist -v
This will run http-server dist -v in the container.
As for executing a shell in the container, bash is not found in $PATH because bash is not installed in the image. node:9.11.1-alpine is based on Alpine Linux, which uses ash as the default shell. So, you should use ash to execute a shell process in your running container.
docker container exec -it 0778c0e3ae05 ash
You write: "get same error , but navigating to ip address with port i get page not found".
This is because you do not map the port. You EXPOSE it, bu you should also map it. Find information in docker docs in the reference for the run command under header:
EXPOSE (incoming ports):
https://docs.docker.com/v1.13/engine/reference/run/
-P : Publish all exposed ports to the host interfaces.
-p=[] : Publish a container᾿s port or a range of ports to the host
format: ip:hostPort:containerPort | ip::containerPort | hostPort:containerPort | containerPort
Both hostPort and containerPort can be specified as a
range of ports. When specifying ranges for both, the
number of container ports in the range must match the
number of host ports in the range, for example: -p 1234-1236:1234-1236/tcp
When specifying a range for hostPort only, the
containerPort must not be a range. In this case the
container port is published somewhere within the
specified hostPort range. (e.g., -p 1234-1236:1234/tcp) (use 'docker port' to see the actual mapping)

Run microsoft/nanoserver in a docker file

Link to microsoft/nanoserver
If I follow the process in the link above I can get docker nano-server to run inside of docker on the command line..
RUN --name nanoiis -d -it -p 8080:80 nanoserver/iis
is the commend line is use.
I want to put this into a docker file and build and image. So here is my dockerfile
FROM microsoft/nanoserver
# Set the working directory to /app
WORKDIR /app
# Copy the Public directory contents into the container at /app
ADD ./Public /app
# -p 8080:80 Map TCP port 80 in the container to port 8080 on the Docker host.
RUN --name nanoiis -d -it -p 8080:80 nanoserver/iis
I get an error
Error response from daemon: Dockerfile parse error line 12: Unknown
flag: name
My question is what am I doing wrong?
I am following the example on building a docker image here:
https://docs.docker.com/get-started/part2/
Next question is what is the command I would use to get my app running? In the example they use.
CMD ["python", "app.py"]
What should I use to get nano-server running?
Last point me to some documentation to get a web site running into the Nano-Server. It seems that Nano-Server has changed it role within Microsoft.

Resources