0

I have a web server (JS) I want to run into container, it runs but I cannot connect to it from host. I am a docker noob so I took a basic script and Dockerfile that worked for a similar case with go server

here is the script

#!/usr/bin/env bash
docker image build --file Dockerfile --tag guess-image .
docker container run --detach  --publish 3000:3000 --rm --name guess-container guess-image

here is the Dockerfile

FROM alpine

# download the file with server
RUN wget address/where/to/download/zipped-JS.server

# extract
RUN apk add unzip
RUN unzip server.zip
WORKDIR ./server/

#unrelated stuff
...
    
# install javascript and install the server
RUN apk add npm
RUN npm install

# exposing port 3000
EXPOSE 3000

# run server
RUN node server.js

here is the docker ps result :

CONTAINER ID   IMAGE          COMMAND                  CREATED          STATUS          PORTS      NAMES
5c8028cb79c3   3723e0b6e109   "/bin/sh -c 'node se…"   57 seconds ago   Up 57 seconds   3000/tcp   quirky_buck

when not dockerized, the JS server worked perfectly when browsing to http://localhost:3000/ I chose not to paste JS server but I can do it if you think it is relevant.

so any comment on those script is welcome (for example : my guts tells my it is far from being as lean as possible and also naming is far from what I would expect ... )


edit as pointed by David changing the last line by

# run server
CMD ["node" ,"server.js"]

made it work


edit this is not a web server just a simple local server that run in a browser and offer UI to test a script against another. Sorry for the awful wording.

fulverin
  • 53
  • 6
  • I know i runs because it prints what it is supposed to print when successfully starting "~listening to port :3000" – fulverin Mar 30 '23 at 14:06
  • 6
    I don't think your image build is ever finishing. You're trying to `RUN` the server as part of the image build, but you need to set `CMD node server.js` as the main command when the container runs. Also see [Difference between RUN and CMD in a Dockerfile](https://stackoverflow.com/questions/37461868/difference-between-run-and-cmd-in-a-dockerfile). – David Maze Mar 30 '23 at 15:57
  • @DavidMaze well I tried this before but it did not work , but now , it works. , thanks – fulverin Mar 30 '23 at 16:06
  • your server code should listen on all network interfaces (0.0.0.0) not only on localhost interface – Kenovo Mar 31 '23 at 11:47

0 Answers0