DEV Community

Cover image for Docker For Frontend Developers

Docker For Frontend Developers

Aks on May 02, 2019

This is short and simple guide of docker, useful for frontend developers. Why should you useΒ docker? Long long back when business nee...
Collapse
 
derek profile image
derek • Edited

Oh, also you can get more bang for your buck...

if you use

FROM node:8-alpine
Enter fullscreen mode Exit fullscreen mode

or

FROM gcr.io/distroless/nodejs
Enter fullscreen mode Exit fullscreen mode

Becasue... Size matters 😜, and so does security πŸ”’

Image Size
node:8 681MB
gcr.io/distroless/nodejs 76MB
node:8-alpine 69.7MB
Collapse
 
akanksha_9560 profile image
Aks

You are right Derek, slimming your images is important, but then i think that is an vast agenda having your multistage bulids, and trimming your code among other things :)

Collapse
 
derek profile image
derek

Indeed. But hypothetically if you could only do one thing, utilizing alpine or distroless is a low hanging fruit with a huge ROI.

Because, even if you do a multistage build without it you won't trim too much in comparison.

Image Size
node:8 681MB
node:8 with multi stage build 678MB

πŸ‘†πŸ½is a basic Hello World express app

Thread Thread
 
shindesharad71 profile image
SHARAD SHINDE

Docker Slim: Hold my beer πŸ˜…

Take a look at docker-slim - dockersl.im/

Thread Thread
 
derek profile image
derek

🍺held! Very cool

Confirmed! They've successfully implemented the middle-out compression πŸ˜†
confirmed

Collapse
 
slidenerd profile image
slidenerd

does it have node-gyp?

Collapse
 
vishnuharidas profile image
Vishnu Haridas • Edited

Earlier when I started on Docker tutorials online, I couldn't understand how different OSs make it possible to run an isolated environment without having a VM.

Later only I understood that Docker is designed for Linux and uses a kernel-level isolation feature that is built into the Linux OS. And when installing Docker in Windows or Mac, they are running Docker inside a Linux OS that is running on a VM inside that Windows/Mac computer.

I really like every Docker tutorial to include the line clearly saying that "A Container is all about filesystem/resources isolation, which is a system-level feature built-into Linux, and Docker is a tool that abstracts this feature."

Collapse
 
jcarlosr profile image
Juan Ramos

So, if we want to use Docker in Windows or Mac, we will end up using VM technology at the end of the day. Is that correct?

Collapse
 
vishnuharidas profile image
Vishnu Haridas • Edited

Correct. From Wikipedia: "Docker on macOS uses a Linux virtual machine to run the containers. It is also possible to run those on Windows using Hyper-V or docker-machine."

Windows has two types of containers β€” Windows Server Containers and Hyper-V Isolation β€” in which Hyper-V is a VM. More details are here - docs.microsoft.com/en-us/virtualiz...

I understand that production servers are running on Linux machines.

--

Note: Microsoft will soon start shipping a full Linux kernel within Windows. This may change how Docker runs in Windows computers. Let's wait and see.

Collapse
 
dimitrisnl profile image
Dimitrios Lytras

Why copy package.json separately?

Collapse
 
skythet profile image
Raimbek • Edited

I'll try answer with my bad english :)

It's very good question. Docker creates layers for each command COPY/ADD (and some others, you need to read documentation). In build time docker will see to the changes, if in some layer will detected change all below layers will be rebuild.

For example, assume we have like this Dockerfile:

 WORKDIR /app  
 COPY . /app  
 RUN npm install  
Enter fullscreen mode Exit fullscreen mode

And we will change source code very frequently, then docker will execute npm install for each change. It's very bad and not efficient. Because for each little change you will reinstall whole nodejs packages (and if you not using volume or cache it will take loooong time).

In this case:

 WORKDIR /app  
 COPY package.json /app
 RUN npm install
 COPY . /app  
Enter fullscreen mode Exit fullscreen mode

we will execute npm install only when package.json changes (some package added or removed so on).

Collapse
 
dimitrisnl profile image
Dimitrios Lytras

Thank you for the detailed response! Your English are perfect btw.

Collapse
 
akanksha_9560 profile image
Aks

right on :)

Collapse
 
gautamkrishnar profile image
Gautam Krishna R

Great explanation. I too had the same question.

Collapse
 
anantvir profile image
anantvir

The best explanation so far. Much better than 99% of the youtubers/tutorials online ! Thanks Raimbek ! This is awesome

Collapse
 
somangim profile image
SomangIm

Thank you! i was looking for this answer too

Collapse
 
daviddennis02 profile image
David Dennis • Edited

This is used to download the node package/dependencies your application needs.
With the help of adding node_modules folder to your DockerIgnore file, you only need to use the package.json file which gives a clear description of all the packages your node app needs inorder to run.

Collapse
 
vschoener profile image
Vincent Schoener

Still the same article without a proper solution to work with IDE feature as autocomplete.

node_modules is installed in the container and you also need to install it locally and try to not mess with it when you mount your source if you want to have the best of the 2 worlds. (Currently working on it and writing an article describing the need and the issue all developer met).

Otherwise, nice article :)

Collapse
 
akosyakov profile image
Anton Kosyakov

You should check out Gitpod. It builds your image together with the project within it, deploys it in the cloud and provides VS Code like browser IDE with autocomplete and so on. Also VS Code releases remote extensions which are deployed in containers. Although i'm not sure how they get files from host to container os, if they mount them then you will get the same issues.

Collapse
 
cookrdan profile image
Dan

If I understand you right, VS Code just implemented an extension so you can use vs code in the container environment. It needs the insiders build. They just announced this yesterday.

Collapse
 
akanksha_9560 profile image
Aks

In order to solve this problem, just develop locally on your container no?

Collapse
 
vschoener profile image
Vincent Schoener

That's not possible, how can I use an IDE that way and be productive? Using Vim or other text editor is not the solution at all :)

Collapse
 
cookrdan profile image
Dan

Thank you very much for this write up! It's very easy to understand and I have been curious about docker because I see the term often.

In your example you have set up a node server. As of right now I don't need to do that because I'm focused on front end things. Do you see any reason to use docker for front end development? (I.e. simple webpages that don't require backend services)

Also, one thing that is unclear to me about docker containers is, where are the files (for example if I make hello.txt in the container? Are those files in the docker file folder? Can I access them if I'm not using the docker container? In a docker container if I run cd ~, where does that take me? Virtual or real home?

Collapse
 
akanksha_9560 profile image
Aks

Hey Dan,

Thank you for your feedback :)

While dockers are not a necessity to develop frontend, it is advisable to do so. For example there are simple webpages, you need a server which serves it to consumers, that entire setup should be dockerized (in my opinion) so that other devs do not have to put effort in setting up their local server. It just makes development easier. Also, next part is deployment
While the config for that is little more complex, but in simple terms docker would allow you to scale it as and when needed based on traffic.

For your second question, for example you have my-app folder, then your folder structure is going to be like this:
Docker folder structure

Then your docker file your can write

# Dockerfile  
    FROM some image that you want
    COPY hello.txt  <to wherever you want your file in docker container, can be a folder or not>
    EXPOSE 8081  

Also for your last question , Inside container, paths that you navigate, they point to virtual paths inside container.

Collapse
 
cookrdan profile image
Dan

Wow thank you! I understand now about the COPY part in the docker file. I had to go back up and re-read your explanation. Makes sense now.

I'm certainly interested in trying these things out - but at the moment I think it will introduce a great many other things that I need to learn and I think I will wait for a bit. But your post certainly helped clear some things up in my head about what docker containers are all about.

So please correct me if I'm wrong, but I'm curious about the following:

  • locally you have a container set up for development. You have this docker file and etc.
  • locally anyone on your team can then easily set up the same environment because they can use the same docker file
  • server side you also have the same docker file but that container is constantly running to serve a website or whatever

I assume that you would also use version control for all of this, including the docker file. I also assume that the container running on the server has some kind of way to update files from version control when a commit is done on a certain branch. (I know that hooks exist for this sort of thing with GitHub but I don't have experience with this yet.)

If you have the time, I'd be interested to read more about this sort of stuff in another dev.to post. Diagrams are helpful for sure! There's a lot of moving parts for all of this technology and it's a challenge to see how things fit together.

Thread Thread
 
akanksha_9560 profile image
Aks • Edited

Hey Dan,

Yes using dockerfile, you can build and share your image. There is a version control for docker images, hub.docker.com/, here you can store and share your images.

I did not understand your last question, could you please elaborate?

Collapse
 
krthr profile image
Wilson Tovar

Thaaaaaaaaaank you! I was loking for a tutorial like this. <3

Collapse
 
svijaykoushik profile image
Vijay Koushik, S. πŸ‘¨πŸ½β€πŸ’»

Thanks for the post πŸ‘πŸ½. Not only I learnt what docker is, I also learnt how to use docker in node.js πŸ™‚

Collapse
 
lennythedev profile image
Lenmor Ld

I do more frontend at work, and I just needed a quick intro to the concepts so I can grasp what the DevOps guys are talking about. πŸ˜…
Didn't want to sit through a 2-hour tutorial, so this is exactly what I needed.
Thanks for this short-and-sweet demo!

Collapse
 
leslieongit profile image
Leslie

This is awesome, absolutely love this platform because of such information, I'm going to complicate things about and try with ASP.NET

Collapse
 
thejoezack profile image
Joe Zack

Microsoft has really nice base images that will do this, it's a bit tricky because the sdk you need to compile is separate from the runtime you need to run it so they do a 2 pass build.

It's not bad, but I would definitely start with their examples: docs.microsoft.com/en-us/aspnet/co...

Collapse
 
leslieongit profile image
Leslie

Thank you, be blessed πŸ™

Thread Thread
 
mithiridi23 profile image
mithiridi23

Very nice article

Collapse
 
bijurama profile image
biju ramachandran

Hello Akanksha:

Great tutorial - now I have a better understanding of docker. But when I run the following command, I get - > /bin/sh: 1: mode: not found.?

ubuntu@ubuntu-VirtualBox:~/my-node-app$ sudo docker container run -p 4000:8081 hello-world
/bin/sh: 1: mode: not found

Thanks and much appreciated.
/Biju

Collapse
 
akanksha_9560 profile image
Aks

Hi Biju,

It is hard to say what should be causing this error. Maybe you need to source /bin/sh. I maybe wrong. Do not have a lot of experience in ubuntu. Sorry :(

Collapse
 
joelbonetr profile image
JoelBonetR πŸ₯‡

Nice post!

Here my little contribution for front-end project with docker and bundler (also valid with few mods to help on backend/ full stack projects).

dev.to/joelbonetr/simplifying-dock...

Hope it helps :)

Collapse
 
weakish profile image
Jang Rush

Hi, I'd like to translate this excellent tutorial to Chinese. The translated text will be published at nextfe.com
Can you give me the permission?

Collapse
 
akanksha_9560 profile image
Aks

Yes sure, send me the link when it is published :)

Collapse
 
weakish profile image
Jang Rush

Just finished translation: nextfe.com/docker-for-frontend-dev...

During translation, I noticed some possible typos:

It is a running instance of docker image. there can be many containers running from same docker image.

t -> T

containerise very node.js simple app

a very simple node.js app

Collapse
 
bngcebetsha profile image
Buntu Ngcebetsha

Wonderful introduction, thank you

Collapse
 
ltdat287 profile image
dat le tien

Thanks for explain each line by line of Dockerfile.

Collapse
 
javinpaul profile image
javinpaul

The first few paragraphs are wonderful, you have a knack of explaining things in simple language, keep it up. +1 from my side.

Collapse
 
rush profile image
Aarush Bhat • Edited

Is it actually effective? I just started with docker, but it just slows down my deving? Nice explaination btw :')

Collapse
 
souarikarim profile image
SouariKarim

Thank you for the explanation ! But i think you should change the title to "Docker for backend developers" , FrontEnd ones have nothing to do with express servers !

Collapse
 
dylankilkenny profile image
Dylan Kilkenny

I build my react apps with webpack and point nginx to the dist folder. Would there ever be a point in using docker for a setup like this?

Collapse
 
nekcs profile image
Nektarios Liakis

Thanks for the intuitive tutorial!

Collapse
 
abhikhatri profile image
Abhi Khatri

Really nice article πŸ™Œ
but I think you should do a part 2 with docker-compose and volumes so backend devs can use it too. πŸ‘

Collapse
 
yeahch profile image
ch

brief, but broad explanation.
Thank you Akanksha

Collapse
 
mfuatnuroglu profile image
M Fuat NUROGLU

Simple and straightforward.
Thank you