Jihane Eter
Jihane Eter

Reputation: 47

Writing a correct Dockerfile

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.

Upvotes: 0

Views: 163

Answers (1)

user2932688
user2932688

Reputation: 1704

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).

Upvotes: 2

Related Questions