Progress on blog rewrite

All right, this is where I say I can actually get something done.

Achievements for the blog project include:

  • APIs for log in/out, posts CRUD, comments CRUD, like/dislike
  • 93% coverage on APIs mentioned above
  • Using React-Redux to maximize data reuse and minimize the number of API calls
  • Using universal-cookie to store the logged in state (okay this might not deserve a stand alone bullet point)
  • Using Docker (Dockerfile and docker-compose) to automate the deployment process.

Today, lucky for you, I’ve decided to talk about how docker-compose in this project works.

Docker is the company driving the container movement and the only container platform provider to address every application across the hybrid cloud.

^ From Docker’s self introduction. What that means for me is that with proper usage, I wouldn’t have to set up production machines with all the dependencies that my project needs whenever I would like to deploy. Ideally all I would have to do is to write Dockerfiles and docker-compose.yml, install Docker and let Docker handle the rest.

In this blog project, separating the backend and the frontend, the dependencies (required on the environment, not the npm ones) are:

  • backend:
    • MongoDB
    • Node/npm
  • frontend:
    • Node/npm (for building)
    • Nginx (for serving)

With these in mind, I was able to write a Dockerfile and a docker-compose.yml for the backend following documentations and random StackOverflow answers online:

Dockerfile:

FROM node:carbon

WORKDIR /app

COPY package*.json ./

RUN npm install

COPY . .

RUN npm run build-server

EXPOSE 1717

RUN ["chmod", "+x", "/app/wait-for-it.sh"]

CMD ["node", "build/server.js"]

docker-compose.yml

version: '3'
services:
  blog-api:
    build:
      context: ./
      dockerfile: Dockerfile
    restart: always
    depends_on:
      - mongodb
    environment:
      MONGO_URL: mongodb://mongodb:27017/blog
    ports:
      - "1717:1717"
    command: bash /app/wait-for-it.sh mongodb:27017 -- node build/server.js
  mongodb:
    image: mongo:latest
    restart: always

The Dockerfile specifies the config for the blog-api container, while the docker-compose.yml tells Docker how my blog-api container relates to the mongodb service container.

Several things to notice:

  • Each Docker container is like a VM by itself, so the WORKDIR is the directory in the container, and when I do a ‘COPY . .’, naturally it copies from the current directory in the host to the current directory in the container.
  • Notice how I copied the package.json file first and npm installed before copying anything else. The reason for this is that Docker uses a layering cache system that is able to reuse previous versions of images if nothing changes in Dockerfile. Therfore if I only change some api route file, I wouldn’t have to wait for the long npm install process again.
  • wait-for-it is a tool to wait for a process to listen to a port before doing something. It has automatic retires that is very useful in this case. I could, however, just let blog-api restart always as is, but this tool doesn’t have as much overhead.

Later I added another Dockerfile for the frontend, which looks like this:

FROM nginx

RUN apt-get update

RUN apt-get install -y curl wget gnupg

RUN curl -sL https://deb.nodesource.com/setup_8.x | bash

RUN apt-get install -y nodejs

WORKDIR /app

COPY package*.json ./

RUN npm install

COPY . .

RUN npm run build

RUN cp -a /app/dist/* /usr/share/nginx/html

RUN cp /app/nginx.conf /etc/nginx/

This image extends from nginx, so the default CMD starts up the nginx server. I need nodejs for building the static files, so I added the couple lines there. The last two lines copy the static files to nginx’s serving directory and my config file to nginx’s config directory.

With the frontend added, I added one more service to docker-compose.yml:

web:
    build:
      context: ./
      dockerfile: Dockerfile-frontend
    restart: always
    ports:
      - "80:80"

This simply links my container for the web frontend to docker-compose so that I wouldn’t have to manually start up every container. Instead, I would only have to do docker-compose build and docker-compose up -d.

I also added automatic seeding for the MongoDB database but I’m too lazy to paste the steps here again so screw you.

This following point is unrelated to Docker, but I spent some time on it and felt like it would be interesting to include here. It is my nginx.conf file. Since I’m building the frontend with React single-page-serves-it-all pattern, I have to make sure that the nginx server returns the index.html file no matter what the sub url paths are. The only exception is that the client is requesting some js or resource file. With this in mind:

server {
    listen 80;
    root /usr/share/nginx/html;
    location / {
        try_files $uri /index.html;
    }
}

It tries to file the file specified in the uri first, before returning index.html regardless. 404 is handled on the frontend by my React application.

For the next step, I’ll be working on attachments to posts as a feature request from this person.

41 thoughts on “Progress on blog rewrite”

  1. Thanks for the recommendations shared on your blog. Something also important I would like to express is that weight loss is not information on going on a fad diet and trying to get rid of as much weight as you’re able in a set period of time. The most effective way to lose weight is by getting it slowly and using some basic recommendations which can enable you to make the most from your attempt to lose fat. You may be aware and already be following most of these tips, although reinforcing know-how never does any damage.

  2. roadster cycle with side basketcar attached, or draught conveyance, a donkey with wicker trap or smart phaeton with good working solidungular cob (roan gelding, 14 h). What might be the name of this erigible or erected residence? Bloom Cottage. Saint Leopold’s. Flowerville. Could Bloom of 7 Eccles street foand visit my page : 바카라사이트 https://akram37.com

  3. , subjective and objective genitive, may be the only true thing in life. Paternity may be a legal fiction. Who is the father of any son that any son should love him or he any son? What the hell are you driving at? I know. Shut up. Blast you. I have reasons. Amplius. Adhuc. Iterum. Postea. Are you condemPlease visit my webpage : 온라인바카라 https://gam77.xyz

  4. ied. By God, you’re as good as ever you were. Better, said Tomgin Kernan. Most trenchant rendition of that ballad, upon my soul and honour It is. Lablache, said Father Cowley. Ben Dollard bulkily cachuchad towards the bar, mightily praisefed and all big roseate, on heavyfooted feet, his gouty fingers nakkerAlso visit my webpage : 우리카지노사이트 https://bxx100.com

  5. who eventually euchred their third companion, were patently trying as if the whole bally station belonged to them to give Stephen the slip in the confusion, which they did. There was no response forthcoming to the suggestion however, such as it was, Stephen’s mind’s eye being too busily engaged in repicturing his Also visit my site : 카지노사이트 https://akram37.com

  6. been for an indefinite time incapable of moving or uttering sounds. From somnambulism: once, sleeping, his body had risen, crouched and crawled in the direction of a heatless fire and, having attained its destination, there, curled, unheated, in night attire had lain, sleeping. Had this latter or any cognate phenomAlso visit my site : 달팽이게임 https://bxx100.com

  7. on and they treat you like dirt I dont care what anybody says itd be much better for the world to be governed by the women in it you wouldnt see women going and killing one another and slaughtering when do you ever see women rolling around drunk like they do or gambling every penny they have and losing it on horses yesAlso visit my webpage : 월드카지노 https://bxx100.com

  8. lks. One life is all. One body. Do. But do. Afar, in a reek of lust and squalor, hands are laid on whiteness. Buck Mulligan rapped John Eglinton’s desk sharply. Whom do you suspect? he challenged. Say that he is the spurned lover in the sonnets. Once spurned twice spurned. But the court wanton spurned him fand visit my page : 인터넷바카라 https://akram37.com

  9. er Hamlet is Shakespeare or James I or Essex. Clergymen’s discussions of the historicity of Jesus. Art has to reveal to us ideas, formless spiritual essences. The supreme question about a work of art is out of how deep a life does it spring. The painting of Gustave Moreau is the painting of ideas. The deepest poetry ofAlso visit my webpage : 우리카지노 https://gam77.xyz

Leave a Reply

Your email address will not be published. Required fields are marked *