Spin up a lightweight Nodejs docker container running your application
Key Takeaways
Spins up a lightweight Nodejs docker container running an application using a Dockerfile
Full Transcript
hey guys I'm bringing you a quick video to show you how can you can spin up a lightweight node runtime container that contains your application in a fully essentially capsulated way where you can stateless Li spin up a container execute your application and you can spin it down at any time right so this is very useful especially if you're let's say running a Jenkins jobs right or CI CD pipeline or like like you're part of a cook and kubernetes is a cluster you want your application to be an image or a docker container where you say hey execute this spin up this container execute stuff do something and spin down right you can do something like that right so I want to show you how to do that essentially and there will be two steps to this process first we're gonna build that docker image from which we can spin up that container and in order to build that docker image we're gonna build up our own docker file right and then we're gonna pull from the node lightweight docker image that is already there and we're gonna essentially write our own little bit application like an Express application and we're gonna spin it up right though the image and then spin up container running on port certain port right so that's a very simple thing you can do the exact same thing with Python exact same thing words go any runtime so I'm gonna show you their node and you can just do that and do it with any of the runtime you like so let's just jump into it all right so I have Visual Studio code I obviously I have couldn't docker running here so I'm gonna go ahead and just open a brand new folder and I'm going to my JavaScript folder here just a background and I'm gonna create a folder called docker and the code will be available in the description below guys for you alright so it's a brand new folder and here's the first thing I want to create I want since we're building an image we need to create a docker file alright that docker file I literally call it docker file you have to be the same cases I think as well right won't you do that right that's the dockerfile we're gettin right code and some some code in order to build our image okay and that code will in hurt that image will inherit from the node runtime and specific version as well so another thing I want to build here is all my application I'm gonna create a folder here called app write that application will contain an indexed rjs okay let's start building application then won't get it built or docker how about that okay so we have a folder here inside the index J s I'm gonna go to terminal am I in the app let's go to the app let's make sure I am in the app and then just do NPM in it - why I mean shut up I know I'm doing just great they're just packaged it Jason and then let's just create an application Const a pickle require Express very simple stuff right and then do app to get when someone visit this thing this Duke RS dots and hello from a light-weight container right very simple stuff right and then AB does listen I'm gonna listen to port nine nine nine nine just for fun okay and would you console the log listening some pull stuff right this is this isn't my application right there so let's go ahead and quickly install what do we need Express and the move to a new store Express actually it's gonna be added to my package to Jason and that's a very important thing okay write my check package edition requires Express and that's important okay so you saw them what I did right I wrote my application is run NPM install expressed and now if I do just in p.m. install my package to Jason we'll see hey you require Express I'm gonna install Express for you okay that's the cool part of this alright let's just run the application how do we run the application let's let's create a script here let's call the script app right and then when they're on the script run I'm gonna just run in Dexter Jess so if I do NPM run app I'm gonna sync it for $9.99 sweet let's go to browser localhost 9na that's obviously a lie we're not running from container or running from the machine but we're gonna change that real soon all right so let's go to the docker file now so what I want to do is build an beautiful image so that this image will first of all we're gonna inherit from the node image and now I want a specific version from note so if you're watching this video three years from now you don't like install the latest version node and this breaks the application for some reason so from node twelve right and here's what I want to do working the working directory of this thing is I'm gonna just make up a working directory in the container itself and we call it home node app it's just completely made up directly that doesn't exist so the working directory is where my application is running essentially okay and guess what up to now that container doesn't have really any code right because we didn't really copy that code so what I want to do is if imagine this application is running right here right if I want to copy how do I copy I want to copy the app folder to that container so what do I do I do literally copy app which is the location I'm writing go because there I'm running from this location docker file copy the app - guess what home node app just copy the stuff there copy the content that includes the index that condones their action - Jason probably we shouldn't copy the node module because we really really don't need that Bashar it's just a test right and then here's the way we need to do next once you copy it now when we were in a blank container imagine yourself in this blank container what do you do next guys we need to install the dependencies right and there are two their comment called run and there is a command called CMD and each one executes on a certain stage right the run command executes when you build that image from the docker file the CMD executes when you actually run an instance of the container from that image okay and then so that's what we're interested in running that thing right so NPM install run NPM install that's it I don't need to say any PM still Express because from the package to Jason if I am here and I say NPM run install I'm gonna look in the package decisions okay what do you want to install you want to still Express so that will install it for us okay sweet that's all what we need and final thing is we want to expose write the port nine nine nine nine that's the portal listening to write sometimes you don't really need to expose any ports if your application like I don't know executes and computes the first 70 prime numbers and then brightest tourist endpoint and shuts down that's that's completely stateless workload right but in this case when I do expose nine and I want I'm actually listening to a port and I won't expose it to the outside world okay so that's the port all right here's the thing now guys this is stuff is missing something I want to actually open the notification will never run if I run the container so how do how do I actually run my application when the container is spun up when the container is actually run okay that's the command CMD yeah if I do npm run up that's that code right we wrote in literally here that will execute this so I think we have everything we need if I come here and I need to go one level up I am now in the docker file if I do docker build dash T give it a name like called node app anything really and then dot very important the dot to build a cannon directory right the conductor and we found that thing and we stopped building all that stuff and we're building and everything looks good guys we installed we built and everything looks good yeah so now we have an image called node app how do we spin up a container from that we've done this many times in this channel so how do we do that docker run right give me the name node app you can call it anything you want it's just the same height called it the same name that's the container now when I do run write the theme D will execute right when I built the image the run command was executed I know it's all bit confusing so if I do that and I want to expose which port a porn expose nine nine nine nine to nine nine nine now this could be anything you want this has to be nine nine nine nine because that's the port inside that container okay and the finally know that and we're listening to nine nine nine but this is a container guys this is completely right a stateless container in itself completely contained by itself right I can destroy it and I can I can give you that image I can give you that docker file you guys can I'm gonna push that code and if you pull it if you clone that Creepo and you do darker build the same exact comment I did you'll be able to do this thing exactly the same right so and we can able to do essentially what will it be able to like spin up a Jenkins instance and do that docker run and do that stuff as well right so let's see if our toughest actually works right and obviously it works but this times actually yeah I am inside that container alright guys so that's a that was like a very lightweight video I wanted to make with you guys to share with you and how to make a lightweight nodejs docker container that have your code and you can do so much stuff with it obviously if you don't care about the the container anymore you can destroy it and here is how you do it you do docker stop node app and you can do docker RM No and that application is gone just like that it's it's guilt right you can spin up multiple docker containers if you want to right let's do that let's do that alright guys so the power of this now now that we have a container running this thing that application you can spin up as many containers as you want so I can spin up docker run - be like port 8000 they're listening to map it to port 9000 I $9.99 and this is will be my first container I'm gonna call it node app then I'm gonna for fun I'm gonna just detach it because I want I want just this application to run and detach itself right and I'm going to create another port 8000 and one and another container 8,000 and - so now let's see if these 8,000 8,000 and 1 it doesn't actually working they should be they better be right so if I do 8,000 that works 8,000 one that works it doesn't - that works how easy is this guys with a few line of code I spun up three web servers running my applications essentially three containers and you can put them behind a load balancer it's as easy as like a D or an engine X or H a proxy which we made a lot of videos on this channel right and you can just blow it balance the whole thing right which we might do in another video so we start building all these microservices architecture with containers right so this is really powerful stuff you think of all right guys all right guys hope you enjoyed this video I'm gonna see you in the next one you guys stay awesome
Original Description
In this video we will write a Dockerfile that will allow you to build your NodeJS application in a docker container
Source Code
https://github.com/hnasr/javascript_playground/tree/master/docker
🏭 Software Architecture Videos
https://www.youtube.com/playlist?list=PLQnljOFTspQXNP6mQchJVP3S-3oKGEuw9
💾 Database Engineering Videos
https://www.youtube.com/playlist?list=PLQnljOFTspQXjD0HOzN7P2tgzu7scWpl2
🛰 Network Engineering Videos
https://www.youtube.com/playlist?list=PLQnljOFTspQUBSgBXilKhRMJ1ACqr7pTr
🏰 Load Balancing and Proxies Videos
https://www.youtube.com/playlist?list=PLQnljOFTspQVMeBmWI2AhxULWEeo7AaMC
🐘 Postgres Videos
https://www.youtube.com/playlist?list=PLQnljOFTspQWGrOqslniFlRcwxyY94cjj
🧮 Programming Pattern Videos
https://www.youtube.com/playlist?list=PLQnljOFTspQV1emqxKbcP5esAf4zpqWpe
🛡 Web Security Videos
https://www.youtube.com/playlist?list=PLQnljOFTspQU3YDMRSMvzflh_qXoz9zfv
🦠 HTTP Videos
https://www.youtube.com/playlist?list=PLQnljOFTspQU6zO0drAYHFtkkyfNJw1IO
🐍 Python Videos
https://www.youtube.com/playlist?list=PLQnljOFTspQU_M83ARz8mDdr4LThzkBKX
🔆 Javascript Videos
https://www.youtube.com/playlist?list=PLQnljOFTspQWab0g3W6ZaDM6_Buh20EWM
Support me on PayPal
https://bit.ly/33ENps4
Become a Patreon
https://www.patreon.com/join/hnasr?
Stay Awesome,
Hussein
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from Hussein Nasser · Hussein Nasser · 0 of 60
← Previous
Next →
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
Extending ArcObjects (IGeometry) - 01 - Getting Started
Hussein Nasser
Extending ArcObjects (IGeometry) - 02 - The Document, The Map and The Layers
Hussein Nasser
Channel Update - New Book, New Job, New Videos
Hussein Nasser
Learn Programming with VB.NET - 01 - Getting Started
Hussein Nasser
Learn Programming with VB.NET - 02 - Classes and Objects (Part 1)
Hussein Nasser
Learn Programming with VB.NET - 03 - Classes and Objects (Part 2)
Hussein Nasser
Learn Programming with VB.NET - 04 - User Interface
Hussein Nasser
Learn Programming with VB.NET - 05 - By Value v. By Reference
Hussein Nasser
Learn Programming with VB.NET - 06 - Variable size, 32 bit vs 64 bit
Hussein Nasser
Learn Programming with VB.NET - 07 - Conditional Statements
Hussein Nasser
Learn Programming with VB.NET - 08 - Inheritance
Hussein Nasser
Learn Programming with VB.NET - 09 - Strategy Design Pattern
Hussein Nasser
Learn Programming with VB.NET - 10 - How did I learn programming
Hussein Nasser
IGeometry 2016 Retrospective - Channel Update
Hussein Nasser
Javascript by Example - The Vook
Hussein Nasser
Vlog - Keep your servers close and your database closer
Hussein Nasser
Vlog - Client/Server Programming Languages
Hussein Nasser
Javascript By Example L1E01 - Getting Started
Hussein Nasser
Persistent Connections (Pros and Cons)
Hussein Nasser
Javascript By Example L1E02 - Building the Calculator Interface
Hussein Nasser
Happy new Year from IGeometry!
Hussein Nasser
Synchronous v. Asynchronous
Hussein Nasser
Javascript By Example L1E03 - Displaying the Digits on Calculator Screen
Hussein Nasser
Show Your Work. Blog, Vlog, Write, Create and Develop!
Hussein Nasser
Relational Database Atomicity Explained By Example
Hussein Nasser
Javascript By Example L1E04 - Operators, All Clear with Arrow Functions
Hussein Nasser
What Comes First, User Experience or Software Architecture?
Hussein Nasser
Javascript By Example L1E05 - Evaluate the Calculator Expressions with eval
Hussein Nasser
Fastest Way to Learn Programming Language or Technology
Hussein Nasser
Javascript By Example L1E06 - Fix Leading Zero Bug with Conditions
Hussein Nasser
Stateful vs Stateless Applications (Explained by Example)
Hussein Nasser
Javascript By Example L1E07 - Running our Calculator on the Mobile Phone
Hussein Nasser
Advice for New Software Engineers and Developers
Hussein Nasser
Why JSON is so Popular?
Hussein Nasser
Building Scalable Software - SLA, HS, VS
Hussein Nasser
Vlog (Istanbul) - Datacenter Proximity
Hussein Nasser
Should Software Engineers Learn Bleeding-Edge Technologies?
Hussein Nasser
Do Developers Build Bad User Interfaces/Experience?
Hussein Nasser
Learn By Doing.
Hussein Nasser
I Wrote Bad Front-End Code That Broke Chrome
Hussein Nasser
My Story
Hussein Nasser
Vlog - Horizontal vs Vertical Scaling
Hussein Nasser
Can User Experience Help Build Better Rest API?
Hussein Nasser
Reverse engineering Instagram in flight mode
Hussein Nasser
The Benefits of the 3-Tier Architecture (e.g. REST API)
Hussein Nasser
Stateless v. Stateful Architecture (Podcast)
Hussein Nasser
The evolution from virtual machines to containers
Hussein Nasser
Proxy vs. Reverse Proxy (Explained by Example)
Hussein Nasser
Canary Deployment (Explained by Example)
Hussein Nasser
No Excuses
Hussein Nasser
Synchronous vs Asynchronous Applications (Explained by Example)
Hussein Nasser
What is an Asynchronous service?
Hussein Nasser
Difference between Client Polling vs Server Push in Notifications
Hussein Nasser
Software vs. Hardware AdBlockers (Explained by Example)
Hussein Nasser
HTTP Caching with E-Tags - (Explained by Example)
Hussein Nasser
Simple Object Access Protocol Pros and Cons (Explained by Example)
Hussein Nasser
Nodejs Express "Hello, World"
Hussein Nasser
Reverse Engineering Instagram feed
Hussein Nasser
Popup Modal Dialog with Javascript and HTML
Hussein Nasser
MIME and Media Type sniffing explained and the type of attacks it leads to
Hussein Nasser
More on: Systems Design Basics
View skill →Related Reads
📰
📰
📰
📰
Stop Writing Angular That Only Works Today
Dev.to · Ekta Dubey
Simple is Simple, Complex is Possible
Medium · Programming
Unlocking Modern Development: How the Odin Programming Language is Redefining Systems-Level App Innovation
Dev.to · Tamiz Uddin
Every System Design Interview Is Secretly About These 16 Companies.
Medium · Programming
🎓
Tutor Explanation
DeepCamp AI