Docker Community Forums

Share and learn in the Docker community.

Using localhost to access running container

docker

(Dkrsoundar) #1

I have created a Dockerfile for my project which uses graphql as a service.
My Dockerfile is as follows.

# ImageName
FROM node:8.8.1

# Create app required directories
ENV appDir /usr/src/app
RUN mkdir -p /usr/src/app /usr/src/app/datas /usr/log/supervisor

# Change working directory
WORKDIR ${appDir}

# Install dependencies
RUN apt-get update && \
    apt-get -y install vim\
    supervisor \
    python3 \
        python3-pip \
        python3-setuptools \
        groff \
        less \
    && pip3 install --upgrade pip \
    && apt-get clean

RUN pip3 --no-cache-dir install --upgrade awscli

# Install app dependencies
COPY graphql/package.json /usr/src/app
RUN npm install
RUN npm install -g webpack

# Copy app source code
COPY graphql/ /usr/src/app
COPY datas/ /usr/src/app/datas

# Set Environment Variables
RUN echo export DATA_DIR=/usr/src/app/datas/ >> ~/.data_variables  && \
  echo "source ~/.data_variables" >> ~/.bash_login  && \
    echo "source ~/.data_variables" >> ~/.bashrc 

COPY supervisord.conf /etc/supercvisor/conf.d/supervisord.conf

# Expose API port to the outside
EXPOSE 5000

# Launch application
CMD ["/usr/bin/supervisord", "-c", "/etc/supercvisor/conf.d/supervisord.conf"]

My docker-compose file

version: '3'
services:
  web:
    build: .
    image: graphql_img
    container_name: graphql_img_master
    ports:
      - "5000:5000"

My supervisord.conf file

[supervisord]
nodaemon=true

[program:babelWatch]
command=npm run babelWatch

[program:monitor]
command=npm run monitor

The output of

docker ps

CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 
db3723a242c0 graphql_img "/usr/bin/supervisor…" 2 hours ago Up 2 hours 0.0.0.0:5000->5000/tcp graphql_img_master

As you can see I’ve exposed the port 5000, but when I try to check the output on the browser using the command localhost:5000/graphql it shows an error

This site can’t be reached

I even tried to check for the ip address of docker container using “docker inspect” command and I’ve used that container ip address with the port still I’m getting the error. Can somebody please help me out on this. Any help would be much appreciated.

Additionally, it would also really helpful to know how to make the program “run monitor” to run on foreground using supervisor


(Sam) #2

what tells the node app (graphql) to listen on port 5000 in the container?

if you do

docker exec graphql_img_master ps -ef

what is running in the container?

does

docker logs graphql_img_master

show any useful info?

netstat is probably not installed in the container, it would show you what sockets are being used.


(Dkrsoundar) #3

Thank you so much for the reply @sdetweil

I’ve configured graphql to run on port 5000 inside my configuration file.
I ran both the commands that you have mentioned and got the following output

UID        PID  PPID  C STIME TTY          TIME CMD
root         1     0  0 11:51 ?        00:00:01 /usr/bin/python /usr/bin/supervisord -c /etc/supercvisor/conf.d/supervisord.conf
root         8     1  0 11:51 ?        00:00:00 npm                                
root         9     1  0 11:51 ?        00:00:00 npm                                   
root        28     8  0 11:51 ?        00:00:00 sh -c nodemon dist/SimpleGraphQLHTTPServer.js
root        29     9  0 11:51 ?        00:00:00 sh -c rm -rf dist/* && babel src --out-dir dist --source-maps --watch
root        30    28  0 11:51 ?        00:00:02 node /usr/src/app/node_modules/.bin/nodemon dist/SimpleGraphQLHTTPServer.js
root        36    29  0 11:51 ?        00:00:03 node /usr/src/app/node_modules/.bin/babel src --out-dir dist --source-maps --watch
root       206     0  0 11:52 pts/0    00:00:00 bash
root       236     0  0 11:53 pts/0    00:00:00 node dist/SimpleGraphQLHTTPServer.js
root       271     0  0 13:13 ?        00:00:00 ps -ef

The output is partial of what I expect it…Can you tell me how I can see the complete output.

And when I run the

docker logs graphql_img_master

The output I got was

2018-01-08 11:51:15,611 CRIT Supervisor running as root (no user in config file)
2018-01-08 11:51:15,614 INFO supervisord started with pid 1
2018-01-08 11:51:16,617 INFO spawned: 'monitor' with pid 8
2018-01-08 11:51:16,620 INFO spawned: 'babelWatch' with pid 9
2018-01-08 11:51:17,869 INFO success: monitor entered RUNNING state, process has stayed up for > than 1 seconds (startsecs)
2018-01-08 11:51:17,869 INFO success: babelWatch entered RUNNING state, process has stayed up for > than 1 seconds (startsecs)
2018-01-08 11:51:18,686 CRIT reaped unknown pid 66)
2018-01-08 11:51:18,925 CRIT reaped unknown pid 94)
2018-01-08 11:51:19,023 CRIT reaped unknown pid 107)
2018-01-08 11:51:19,101 CRIT reaped unknown pid 122)
2018-01-08 11:51:19,174 CRIT reaped unknown pid 138)
2018-01-08 11:51:19,230 CRIT reaped unknown pid 148)
2018-01-08 11:51:19,309 CRIT reaped unknown pid 159)
2018-01-08 11:51:19,368 CRIT reaped unknown pid 173)
2018-01-08 11:51:19,410 CRIT reaped unknown pid 189)

(Sam) #4
I’ve configured graphql to run on port 5000 inside my configuration file.

where? I didn’t see that

that is everything running inside the container as processes.

what do you think is missing?


(Dkrsoundar) #5

(Sam) #6

in the supervisord.conf file, where is the info to start the graphql api server?
the thing listening on port 5000
the sample here uses port 4000


(Dkrsoundar) #7

Can you please tell me how to mention that inside supervisord.conf file, as I have mentioned it inside the config.js file as mentioned earlier.


(Sam) #8

no idea myself… the sample explicitly starts the server.js file using node

node server.js

the supervisord file uses npm for the other parts…

I don’t know any of this technology myself

if u forget about supervisord… how would you run all this manually?


(Dkrsoundar) #9

I type those two commands mentioned in supervisord.conf file on two different terminal windows
and the process works


(Sam) #10

ok, maybe the graphql http server is running…

add this to your dockerfile apt-get install list
net-tools

and rebuild and restart the container

so we can run docker exec graphql_img_master netstat
so see the ports being used


(Dkrsoundar) #11

I’ve installed net-tools and rebuilt the docker container, and when I can ran thedocker exec graphql_img_master netstat command, the output came as

Active Internet connections (w/o servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State      
Active UNIX domain sockets (w/o servers)
Proto RefCnt Flags       Type       State         I-Node   Path

Can you let me know what should I do now


(Sam) #12

sorry, missed a parm

docker exec graphql_img_master netstat -a

shows something like this on one of my containers

docker exec VSE1 netstat -a
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address         State      
tcp        0      0 127.0.0.11:41085        *:*                     LISTEN     
tcp        0      0 *:http                  *:*                     LISTEN     
udp        0      0 127.0.0.11:59316        *:*  

(Dkrsoundar) #13

I got the following output

Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address         State      
tcp        0      0 127.0.0.11:45016        *:*                     LISTEN     
udp        0      0 127.0.0.11:34096        *:*                                
Active UNIX domain sockets (servers and established)
Proto RefCnt Flags       Type       State         I-Node   Path

(Sam) #14

ok… nothing is listening on port 5000 (or any other port) , which would be your graphql http server


(Dkrsoundar) #15

Okay… Do you have any idea what I should do to get this working…


(Sam) #16

not really…

when u run the two commands in the separate windows, if you kill both, then try to connect to the graphql server via the browser on port 5000 does it work? (not give cannot connect)

what does the dist/SimpleGraphQLHTTPServer.js look like?


(Sam) #17

trying to help, where did u get the graphql distribution from?

i see you copy it to the image,

COPY graphql/ /usr/src/app

not install it…


(Dkrsoundar) #18

The graphql here refers to the codebase that we have done using graphql.
I’m installing graphql with the help of package.json file, where it is mentioned as a dependency.


(Sam) #19

ok, two things…

  1. on the server where it works, in a separate window, what does
ps -ef | grep node 

show

  1. and can u show me the dist/SimpleGraphQLHTTPServer.js… my email is my userid at gmail.com
    i don’t find a copy of that file anywhere.
    every example has a custom server.js file of some sort

(Dkrsoundar) #20

Sure thing… I’ll email you in sometime… We can discuss this over there.