I have created a php web application and hosted it in Docker.
Now Im trying to access the application from the host machine(not through docker) and Im unable to open it.Any help will be greatly appreciated!
Details-
OS - Catalina
Accessing :- http://localhost:60
Error-
This page isn’t workinglocalhost didn’t send any data.
ERR_EMPTY_RESPONSE
Followed the below steps :-
Shrutis-MacBook-Pro:MyDockerImages shrutipatnaik$ ls
index.php world.txt
Dockerfile
Shrutis-MacBook-Pro:MyDockerImages shrutipatnaik$ docker build -t jenkins_php .
Sending build context to Docker daemon 653.8MB
Step 1/3 : FROM php:7.4-apache
---> 05e7c943eaa9
Step 2/3 : COPY . /var/www/html
---> e30136f8e0c7
Step 3/3 : CMD ls && whoami && pwd ;
---> Running in 4c29020952fb
Removing intermediate container 4c29020952fb
---> dd0a9d7f8ccd
Successfully built dd0a9d7f8ccd
Successfully tagged jenkins_php:latest
Shrutis-MacBook-Pro:MyDockerImages shrutipatnaik$docker run -it -d -p 60:60
jenkins_php:latest /bin/sh
26e3590f0e6a249f26251c33020a8180610ce07ff11004dc3dc2460a3aa41790
Shrutis-MacBook-Pro:MyDockerImages shrutipatnaik$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED
STATUS PORTS NAMES
26e3590f0e6a jenkins_php:latest "docker-php-entrypoi…" 6 hours ago
Up 6 hours 60/udp, 0.0.0.0:60->60/tcp, 80/tcp kind_chatelet
OK two things:
first - when you run the container with the /bin/sh command in the end that's the command the container is running, not your web app.
See your used docker image dockerfile and ENTRYPOINT and CMD commands.
when you add the command in the end of the docker run command you override the preconfigured startup commands:
Usage: docker run [OPTIONS] IMAGE [COMMAND] [ARG...]
For that run docker run -dp 60:60 jenkins_php:latest
example with nginx:
when running like suggested in question:
docker run -itdp 80:80 nginx /bin/sh
We get trying to access through chrome:
when running "regularly" with:
docker run -dp 80:80 nginx
or even
docker run -itdp 80:80 nginx
We get trying to access through chrome:
and in either case when running docker ps it doesn't show the new command but the old entry-point script (just like in the question) even though it was overridden:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
3fe92585a40c nginx "/docker-entrypoint.…" 3 seconds ago Up 2 seconds 0.0.0.0:80->80/tcp, :::80->80/tcp adoring_heisenberg
second - it is also possible that after fixing that it wont work cause you also publish port 80/tcp and unless you specifically chose that it would make more sense to publish on 80 by default so I'd check that along the way.
for that run docker run -dp 80:80 jenkins_php:latest
if we try to replicate like in the previous misconfiguration we get the same error
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
146d05cc5275 nginx "/docker-entrypoint.…" 3 seconds ago Up 2 seconds 80/tcp, 0.0.0.0:80->1000/tcp, :::80->1000/tcp goofy_lamport
localhost didn’t send any data.
ERR_EMPTY_RESPONSE
Related
I've followed getting started with Logic Apps in a Container, in Azure Tips & Tricks #311, which worked OK. I also consulted Vinnie James, which is similar
The Logic App runs on receipt of a HTTP request, which - inside VS Code - is localhost, easily used from a browser
But when I go to the next step, to build an image and run it in Docker, I'm not at all clear how to to make a similar HTTP request; Docker is running on the same W10 machine, using WSL-2
Dockerfile (corrected):
FROM mcr.microsoft.com/azure-functions/dotnet:3.0.14492-appservice
ENV AzureWebJobsStorage=DefaultEndpointsProtocol=https;AccountName=XXX;AccountKey=XXX;EndpointSuffix=core.windows.net
ENV AzureWebJobsScriptRoot=/home/site/wwwroot \
AzureFunctionsJobHost__Logging__Console__IsEnabled=true \
FUNCTIONS_V2_COMPATIBILITY_MODE=true
ENV WEBSITE_HOSTNAME localhost
ENV WEBSITE_SITE_NAME testqueue1
ENV AZURE_FUNCTIONS_ENVIRONMENT Development
COPY . /home/site/wwwroot
Attempted Docker run: docker run -p 80:7071 image1
Response is HTTP request sent, awaiting response... No data received
This despite (very simple) Logic App issuing a 200 response immediately after trigger; looks like App is not initiated ...
First of all and regarding the answer in the comments: please just add the contents of the Dockerfile and the run command to the question, otherwise they're difficult to read.
Taking into account that you have the following Dockerfile:
FROM mcr.microsoft.com/azure-functions/node:3.0
ENV AzureWebJobsStorage DefaultEndpointsProtocol=https;AccountName=XXX;AccountKey=XXX;EndpointSuffix=core.windows.net
ENV AzureWebJobsScriptRoot=/home/site/wwwroot \
AzureFunctionsJobHost__Logging__Console__IsEnabled=true \
FUNCTIONS_V2_COMPATIBILITY_MODE=true
ENV WEBSITE_HOSTNAME localhost
ENV WEBSITE_SITE_NAME test1
ENV AZURE_FUNCTIONS_ENVIRONMENT Development
COPY . /home/site/wwwroot
and that you're running it with docker run -p 5000 image1
The main thing that I see here is that you're only indicating the containerPort but not the hostPort, so you're exposing a random port in the host.
If you do docker ps, you'll be able to see the port that you're forwarding. For example let's say that I have my image busybox and that I run it with docker run -it --rm -p 5000 busybox.
❯ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
05927642abfc busybox "sh" 6 seconds ago Up 5 seconds 0.0.0.0:51883->5000/tcp elated_gauss
As you can see, I'd have to access to the port 51883 in localhost to access to the port 5000 in the container.
On the other hand, if I run my container with: docker run -it --rm -p 5000:5000 busybox
❯ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c0aab9646d1a busybox "sh" 1 second ago Up 1 second 0.0.0.0:5000->5000/tcp busy_roentgen
Now I can access to the port 5000 of the container referring to the port 5000 on localhost.
My container of play/scala application starts at [info] p.c.s.AkkaHttpServer - Listening for HTTP on /0.0.0.0:9000. But I am unable to connect to it from the browser. I am running the container on my windows machine after having build the image using Docker for Windows
The Dockerfile is
FROM openjdk:8
WORKDIR deploy
COPY target/universal/myapp-1.0.zip .
COPY conf/logback_dev.xml ./logback.xml
COPY conf/application_dev.conf ./application.conf
RUN unzip myapp-1.0.zip
RUN chmod +x myapp-1.0/bin/myapp
EXPOSE 9000
ENTRYPOINT myapp-1.0/bin/myapp -Dplay.http.secret.key=changemeplease -Dlogger.file=/deploy/logback.xml -Dconfig.file=/deploy/application.conf
I am starting the container as docker run myApp -p 9000:9000 -network="host" and also tried docker run myApp -p 9000:9000 -network="host"
UPDATE
this is interesting.
If I specify image name before port then the application isn't reachable
docker run myApp -p 9000:9000
In docker container ps -a, I see (no mapping of localhost:9000 to 9000)
C:\Users\manuc>docker container ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
4d16547cd96d myApp "/bin/sh -c 'myApp…" 10 seconds ago Up 9 seconds 9000/tcp, 9042/tcp ecstatic_bell
but if I specify port before image name, then the application is reachable
docker run -p 9000:9000 myApp
In docker container ps -a, I see mapping of localhost:9000 -> 9000
C:\Users\manuc>docker container ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
24b571cc0057 myApp "/bin/sh -c 'MyApp…" 39 seconds ago Up 38 seconds 0.0.0.0:9000->9000/tcp, 9042/tcp silly_yalow
Things to do when your container is not behaving like you want:
Check if your application is running in your computer.
After you run your container, check if it is healthy with docker ps. If it is not healthy, the problem is usually in your application.
Ensure it is running without errors, check logs with docker logs <container-id>. If logs are ok, problem is usually in the container network configuration.
Ensure you can access your application with docker exec -it <container-id> bash. And try to access port with curl or wget. If it is not reachable problem can be in iptables, firewall, or your application.
If you can ensure all the steps above working as expected. The problem is in docker network configuration.
Docker network host only works in linux, not mac and windows. You can run container with docker run -p 9000:9000 myapp. Checkout documentation: https://docs.docker.com/network/host/#:~:text=The%20host%20networking%20driver%20only,the%20docker%20service%20create%20command.
General form of the docker run command is docker run [OPTIONS] IMAGE[:TAG|#DIGEST] [COMMAND] [ARG...] as you can see in documentation. You need to specify port options before image name.
I've created a Dockerfile which looks like this:
FROM openjdk:8-jdk
COPY . .
ENTRYPOINT ["/bin/graphdb"]
EXPOSE 7200
On doing docker run 34a1650b461d -p 127.0.0.1:7200:7200 I see my service running as shown in the terminal output - however when I go to localhost:7200 I keep seeing This site can’t be reached 127.0.0.1 refused to connect.
Could anyone explain what I'm missing?
Also fyi - when I do docker ps, under PORTS I see 7200/tcp.
I read this page and followed what was described but to no luck.
Any help appreciated.
Thanks.
For docker run the order of the parameters matter, so this:
docker run 34a1650b461d -p 7200:7200
Is not the same as:
docker run -p 7200:7200 34a1650b461d
In the first case you are passing the parameters -p 7200:7200 to your ENTRYPOINT command /bin/graphdb; whereas in the second case, you are passing -p 7200:7200 to docker run, which is what you wanted.
How to validate when ports are correctly forwarded?
You can validate this by running docker ps and checking the PORTS column:
$ docker run -d 34a1650b461d -p 7200:7200
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
03abc0b390ef mytest "/bin/graphdb -p 720…" 6 seconds ago Up 5 seconds 7200/tcp elegant_wescoff
Do you see how the COMMAND includes your -p? That's not what you wanted. So docker run was not interpreting that parameter at all. Also, you can see the PORTS column, which shows the port is exposed but not forwarded.
Whereas doing it like this:
$ docker run -d -p 7200:7200 34a1650b461d
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
03abc0b390ef mytest "/bin/graphdb" 6 seconds ago Up 5 seconds 0.0.0.0:7200->7200/tcp elegant_wescoff
You can see now that -p is not being passed to COMMAND and that the port is forwarded: 0.0.0.0:7200->7200/tcp.
I wan´t to play around with Dockerfiles a bit. So I tried running Apache Nifi in Docker. I know that there is already a container available, this is for training purpose.
I configured my Dockerfile as followed:
FROM openjdk:8-jre-alpine
COPY . /app
WORKDIR /app
EXPOSE 8080
CMD ["sh","nifi-1.9.0/bin/nifi.sh","start"]
Now this seems to work, but when I try to run the container with:
docker build --tag nifid .
docker run --name nifi-app -p 8080:8080 nifid
It seems to run but the port is neither exposed, nor can I reach the App via localhost:8080.
Any suggestions on how to proceed?
This is what docker ps --all shows me
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
ccf75612d5ac nifid "sh nifi-1.9.0/bin/n…" 20 hours ago Exited (0) 20 hours ago nifi-app
I realize that the app was excited now.
Anything wrong i the Dockerfile?
EDIT:
The docker logs [Container ID] spitted following:
Java home: /usr/lib/jvm/java-1.8-openjdk/jre
NiFi home: /app/nifi-1.9.0
Bootstrap Config File: /app/nifi-1.9.0/conf/bootstrap.conf
EDIT:
Found a nice "documentation" https://hub.docker.com/r/apache/nifi/dockerfile
A bit to indepth but it should to the job
Well it seems like your container has stoppen (status Exited)
docker ps (shows running containers)
docker ps -a (shows all containers, even stopped)
Try
docker logs <container-id>
To see the output from the container. It will properly tell you why the container stopped.
Container need a pid 0 process to run forever until killed or errors.
For example,
Please refer to https://stackoverflow.com/a/45450456/1926952 to keep a container running
based on Moving docker-compose containersets
I have loaded the images :
$ docker images -a
REPOSITORY TAG IMAGE ID CREATED SIZE
br/irc latest 3203cf074c6b 23 hours ago 377MB
openjdk 8u131-jdk-alpine a2a00e606b82 5 days ago 101MB
nginx 1.13.3-alpine ba60b24dbad5 4 months ago 15.5MB
but now i want to run them, as they would run with docker-compose, but i cannot find any example.
here is the docker-compose.yml
version: '3'
services:
irc:
build: irc
hostname: irc
image: br/irc:latest
command: |
-Djava.net.preferIPv4Stack=true
-Djava.net.preferIPv4Addresses
run-app
volumes:
- ./br/assets/br.properties:/opt/br/src/java/br.properties
nginx:
hostname: nginx
image: nginx:1.13.3-alpine
ports:
- "80:80"
links:
- irc:irc
volumes:
- ./nginx/assets/default.conf:/etc/nginx/conf.d/default.conf
so how can i run the container, and attach to it, to see if its running, and in what order do i run these three images. Just started with docker, so not sure of the typical workflow ( build, run, attach etc )
so even though i do have docker-compose yml file, but since i have the build images from another host, can i possibly run docker commands to run and execute the images ? making sure that the local images are being referenced, and not the ones from docker registry.
Thanks #tgogos, this does give me a general overview, but specifically i was looking for:
$ docker run -dit openjdk:8u131-jdk-alpine
then:
$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
cc6ceb8a82f8 openjdk:8u131-jdk-alpine "/bin/sh" 52 seconds ago Up 51 seconds vibrant_hodgkin
shows its running
2nd:
$ docker run -dit nginx:1.13.3-alpine
3437cf295f1c7f1c27bc27e46fd46f5649eda460fc839d2d6a2a1367f190cedc
$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
3437cf295f1c nginx:1.13.3-alpine "nginx -g 'daemon ..." 20 seconds ago Up 19 seconds 80/tcp vigilant_kare
cc6ceb8a82f8 openjdk:8u131-jdk-alpine "/bin/sh" 2 minutes ago Up 2 minutes vibrant_hodgkin
then: finally:
[ec2-user#ip-10-193-206-13 DOCKERLOCAL]$ docker run -dit br/irc
9f72d331beb8dc8ccccee3ff56156202eb548d0fb70c5b5b28629ccee6332bb0
$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
9f72d331beb8 br/irc "/opt/irc/grailsw" 8 seconds ago Up 7 seconds 8080/tcp cocky_fermi
3437cf295f1c nginx:1.13.3-alpine "nginx -g 'daemon ..." 56 seconds ago Up 55 seconds 80/tcp vigilant_kare
cc6ceb8a82f8 openjdk:8u131-jdk-alpine "/bin/sh" 2 minutes ago Up 2 minutes vibrant_hodgkin
All three UP !!!!
Your question is about docker-compose but you also ask things about run, build, attach which makes me think I should try to help you with some basic information (which wasn't so easy for me to cope with a couple of months ago :-)
images
Images are somehow the base from which containers are created. Docker pulls images from http://hub.docker.com and stores them in your host to be used every time you create a new container. Changes in the container do not affect the base image.
To pull images from docker hub, use docker pull .... To build your own images start reading about Dockerfiles. A simple Dockerfile (in an abstract way) would look like this:
FROM ubuntu # base image
ADD my_super_web_app_files # adds files for your app
CMD run_my_app.sh # starts serving requests
To create the above image to your host, you use docker build ... and this is a very good way to build your images, because you know the steps taken to be created.
If this procedure takes long, you might consider later to store the image in a docker registry like http://hub.docker.com, so that you can pull it from any other machine easily. I had to do this, when dealing with ffmpeg on a Raspberry Pi (the compilation took hours, I needed to pull the already created image, not build it from scratch again in every Raspberry).
containers
Containers are based on images, you can have many different containers from the same image on the same host. docker run [image] creates a new container based on that image and starts it. Many people here start thinking containers are like mini-VMs. They are not!
Consider a container as a process. Every container has a CMD and when started, executes it. If this command finishes, or fails, the container stops, exits. A good example for this is nginx: go check the official Dockerfile, the command is:
CMD ["nginx"]
If you want to see the logs from the CMD, you can docker attach ... to your container. You can also docker stop ... a running container or docker start ... an already stopped one. You can "get inside" to type commands by:
docker exec -it [container_name] /bin/bash
This opens a new tty for you to type commands, while the CMD continues to run.
To read more about the above topics (I've only scratched the surface) I suggest you also read:
Is it possible to start a shell session in a running container (without ssh)
Docker - Enter Running Container with new TTY
How do you attach and detach from Docker's process?
Why docker container exits immediately
~jpetazzo: If you run SSHD in your Docker containers, you're doing it wrong!
docker-compose
After you feel comfortable with these, docker-compose will be your handy tool which will help you manipulate many containers with single line commands. For example:
docker compose up
Builds, (re)creates, starts, and attaches to containers for a service.
Unless they are already running, this command also starts any linked services.
The docker-compose up command aggregates the output of each container (essentially running docker-compose logs -f). When the command exits, all containers are stopped. Running docker-compose up -d starts the containers in the background and leaves them running
To run your docker-compose file you would have to execute:
docker-compose up -d
Then to see if your containers are running you would have to run:
docker ps
This command will display all the running containers
Then you could run the exec command which will allow you to enter inside a running container:
docker-compose exec irc
More about docker-compose up here: https://docs.docker.com/compose/reference/up/