0

I created an app using Javascript (with D3.js), JQuery, CSS, but no Node.js. It's your typical 'index.html' browser-run interface. I've been going through Docker tutorials and trying to figure out how to set my app up to a server, but I've had no luck accomplishing it and have only been able to find tutorials using apps with Node. I cannot, for the life of me, figure out what I'm doing wrong, but I'm wondering if the problem (or one of them) lies in my Dockerfile. Also, do I need to have used Node.js for all this to work? My app consists of the following:

A directory called Arena-Task. Within this directory, I have my index.html, my main javascript file called arena.js, and my CSS files. My other necessary files (like images, etc.) are located within two other folders in the same directory called data and scripts.

So now, how would I write my Dockerfile so that I can build it using Docker and publish it to a server? I've attempted following Docker's example Dockerfile:

FROM node:current-slim

WORKDIR /usr/src/app
COPY package.json .
RUN npm install

EXPOSE 8080
CMD [ "npm", "start" ]

COPY . .

But to be honest I'm not quite sure how to make the changes to accommodate my program. I can't figure out if a package.json is required because if it is, then don't I need to be using Node? I don't use any node modules or project dependencies like that in my app. Do I need to be? Is my problem more than just an incorrect Dockerfile?

Sorry that this question is all over the place, but I'm really new to the realm of the server-side. I appreciate any help! Let me know if I can provide any clarification.

  • 1
    Are you deploying a node server or just a static website? – see sharper Jul 08 '20 at 00:30
  • If all of your assets are static and do not need to be compiled, I would think you could just use an `apache` or `nginx` image. – fubar Jul 08 '20 at 00:36
  • @seesharper yep, pretty much. Every user will see a variation of the same thing (eg, random pictures from the same set of images) and be able to interact with it the same way. – Jihane Eter Jul 08 '20 at 00:55

1 Answers1

2

lets clarify few things: node and npm is when you need them, like you are using some npm packages. package.json - is in use by npm - it stores installed package list in it.

For you case i don't see need of node. so you can create simple image and then you going to need simple web server - something which can serve you html/css/js files on web requests over http. the simplest i know would be nginx.

Also in Dockerfile you need to copy all you resources into image you are building. that is what COPY package.json . was doing. but in you case you have to copy whole app folder into some app folder in docker image. (assuming app is a folder which holds all you files)

so we are going to have steps

  1. Dockerfile should look something like this:
FROM ubuntu

RUN apt-get install -y nginx

COPY app app

COPY startup.sh /startup.sh

COPY ./nginx-default /etc/nginx/sites-available/default

no need in default commands because you going to start something else during docker run.

  1. nginx-default is a configuration for nginx to work as webserver:

it should look something like this:

server {
  listen 8080;
  server_name localhost;

  root /app
}

nginx is very flexible - if you need something from it google it.

  1. docker image should do something all the time, otherwise image going to stop (some blocking process).

the easiest way i know is to create startup.sh file which going to start nginx as first step and then going to do infinity loop:

exit_script() {
  trap - SIGINT SIGTERM # clear the trap
  sudo service nginx stop
  exit 1
}

sudo service nginx start

while sleep 20; do
  CURRENT_TIME=$(date +"%T")
  echo "app is running: $CURRENT_TIME"
done

exit_script - is a trap which helps to stop docker image in fast way, but not like terminate. but you can omit that for testing purposes.

  1. finally, build image (docker build -t {your-image-name} .) and to start image use something like this: docker run -p 8080:8080 {your-image-name} bash /startup.sh

that should work :), though most probably you going to face few errors because i was writing it from the head. (like you may need something else for nginx, or sudo is not installed by default in ubuntu lates image).

user2932688
  • 1,546
  • 11
  • 24
  • I followed this and executed the last command and I got the following: "Status: Downloaded newer image for bash:latest /usr/local/bin/docker-entrypoint.sh: line 11: /startup.sh: No such file or directory" Any idea why? I created the startup.sh file. Could it have anything to do with missing nginx dependencies? – Jihane Eter Jul 08 '20 at 01:21
  • sorry, updated last stage - run command should go against your image name. and you Should build image from Dockerfile right before step 4 – user2932688 Jul 08 '20 at 02:16
  • it was showing that message because docker thought that you need to run image `bash` from public repository. meanwhile bash is what docker need to start from your image – user2932688 Jul 08 '20 at 02:23
  • thanks! I had to make some changes to the Dockerfile to install the sudo command but when I run ' $ docker run -p 8080:8080 arenatask:2.0 bash /startup.sh ' I get this (plus the infinite loop): sudo: setrlimit(RLIMIT_CORE): Operation not permitted nginx: unrecognized service app is running: 02:50:28 app is running: 02:50:48' – Jihane Eter Jul 08 '20 at 02:52
  • Update: I think I resolved these issues, and according to my terminal response it looks like the app should be running, but when I go to the localhost on my browser it says the site can't be reached-- both when I do that and try typing in the IP address – Jihane Eter Jul 08 '20 at 04:11
  • once your docker is running, find container id by running `docker ps`, then exec into it by `docker exec -it {container_id} bash`. you goin to see same terminal but in container. than you can do `curl -XGET http://localhost/index.html`, assuming you have index.html file. it should spit out content of your html file. But if it says something like timeout then you should check if nginx is running. by `sudo service nginx status` – user2932688 Jul 08 '20 at 14:22
  • if `curl` inside of docker returns html then something is wrong with mapping docker ports to local ports. but most probably your nginx just isn't running. because inside of docker container `sudo service nginx start` should work just fine. and if it doesn't try installing it again right in the container terminal by `sudo apt-get install nginx` – user2932688 Jul 08 '20 at 14:25