joe, to programming

We have talked about docker a few times in the past. Most recently, we talked about it in the context of running Ollama. For today’s post, I wanted to talk about how to turn your code into a docker container that you can run somewhere.

What is Docker

Docker provides the ability to package and run an application in a loosely isolated environment called a container. Docker containers can be deployed to just about any machine without any compatibility issues so your software stays system agnostic, making software simpler to use, less work to develop, and easier to maintain and deploy.

Once upon a time, a web application would be run on a physical piece of hardware that is running an operating system like Linux or Windows and then virtualization became a thing. Virtual machines access the hardware of a physical machine through a hypervisor. The host machine has an operating system (Ubuntu, Windows, MacOS, etc) and a hypervisor. Each virtual machine has an operating system of its own, binaries and libraries, and the actual web app. When using containers, the host machine has an operating system and a container engine but the containers only have binaries and libraries and the actual web app (no guest OS is necessary).

A dockerfile is needed to create an image and a container is the result of running an image. Today I am going to show how to go from a basic web app to a running docker container.

A Basic Node Example

If we are going to be dockerizing a web app, we need a web app to dockerize. In yesterday’s demo on how to pass an array as a property into a web component, we looked at three ways to turn an array into an unordered list. I figured that we could do the same with today’s demo.

In the above Node app, we are setting const items as being an array, using <a href="https://www.w3schools.com/nodejs/met_http_createserver.asp">createServer()</a> to create a new HTTP server, and then we are setting it to listen on port 8080. If you save the file locally as app.js, assuming that you have Node installed on your machine, you can run node app.js from the terminal to start the server.

https://i0.wp.com/jws.news/wp-content/uploads/2024/04/Screenshot-2024-04-15-at-12.09.13%E2%80%AFPM.png?resize=1024%2C764&ssl=1

Creating a Dockerfile

A Dockerfile isn’t anything special. It is just a file called Dockerfile. For our test app, the Dockerfile only needs three things:

  1. A base image
  2. What to copy from the host machine and where to copy it to in the container
  3. The command that you want to run when the container launches

Our Dockerfile for this demo looks like this:

You will notice that it also includes the line EXPOSE 8080, to expose port 8080 but as you will see below, it is more for documentation purposes than anything else.

Creating a Dockerignore

If you are familiar with git, you likely know what a .gitignore file is. A .dockerignore file does something similar. A .dockerignore is a configuration file that describes files and directories that you want to exclude when building a Docker image. Usually, you put the Dockerfile in the root directory of your project, but there may be many files in the root directory that are not related to the Docker image or that you do not want to include. .dockerignore is used to specify unwanted files and not include them in the Docker image.

Building a Docker Image

Now that you have what you are dockerizing, a Dockerfile, and a .dockerignore, you can simply build by running docker build . in the terminal.

https://i0.wp.com/jws.news/wp-content/uploads/2024/04/Screenshot-2024-04-15-at-3.44.17%E2%80%AFPM.png?resize=1024%2C856&ssl=1

If you run docker images, you can see the result.

https://i0.wp.com/jws.news/wp-content/uploads/2024/04/Screenshot-2024-04-15-at-5.37.20%E2%80%AFPM.png?resize=1024%2C806&ssl=1

If you want to aid in maintainability a little, you can add -t [image name] to the build command. When you run docker build -t node-app . in the terminal, it looks like this …

https://i0.wp.com/jws.news/wp-content/uploads/2024/04/Screenshot-2024-04-15-at-8.10.32%E2%80%AFPM.png?resize=1024%2C806&ssl=1

… and when you rerun docker images, it now looks like …

https://i0.wp.com/jws.news/wp-content/uploads/2024/04/Screenshot-2024-04-15-at-8.13.57%E2%80%AFPM.png?resize=1024%2C806&ssl=1

Running your Docker Container

As I said above, an image becomes a container when you execute it. You can execute it by running docker run -d -p 8080:8080 6cced9894e8c where -d runs it as a daemon (a background process) and -p [port number]:[port number] tells the container what port to give it on the host machine. The 6cced9894e8c hash is the “Image ID” value from when I ran docker images above. If you tagged the image in the above step, you can use that value instead of the hash, though.

https://i0.wp.com/jws.news/wp-content/uploads/2024/04/Screenshot-2024-04-15-at-9.20.55%E2%80%AFPM.png?resize=1024%2C742&ssl=1

If you run docker ps after starting the container, you can verify that it is running. Go to http://localhost:8080/ and witness the splendor (now running in a docker container).

https://i0.wp.com/jws.news/wp-content/uploads/2024/04/Screenshot-2024-04-15-at-9.24.06%E2%80%AFPM.png?resize=1024%2C729&ssl=1

https://jws.news/2024/how-to-dockerize-a-node-app/

#Docker #NodeJs

joelanman, to programming
@joelanman@hachyderm.io avatar

In Node you can easily load json like this:

const myData = require('data.json')  

is there an equivalent in the new import syntax?

watzon, to golang
@watzon@watzonmanor.com avatar

Ok peeps, we're 4 months into 2024 and I've been without work this whole time so we're going to try this again. If you know of any senior software engineering positions that are actually being hired for, please drop them below.

I have 12 cumulative years of experience, so that shouldn't be an issue, and I know most of the languages in use nowadays well enough to be dangerous, but I am extremely proficient in TypeScript, Python, and Ruby. What I'd rather do more than anything though is have an opportunity to use Go professionally.

linuxtldr, to linux
@linuxtldr@noc.social avatar
leanpub, to programming
@leanpub@mastodon.social avatar

P4NR - IoT Programmer https://leanpub.com/b/p4nr-iotprogrammer by Iniationware, Joel Krec, and Maryam Jalil is the featured bundle on the Leanpub homepage! https://leanpub.com #NodeJs #InternetOfThings #Javascript #Typescript #SoftwareEngineering #CloudComputing

schenklklopfer, to programming German
@schenklklopfer@chaos.social avatar

Grund 409638045983 warum ich dieses ganze Universum verachte:

Habe Code - nicht meiner, kann daran nichts ändern.
Code hat sich seit 7 Monaten nicht geändert.
Vor 7 Monaten ist die Pipeline gelaufen und hat ein Image erfolgreich gebaut.
Heute läuft die Pipeline nicht mehr, weil sich irgendeine Abhängigkeit anders verhält als vor 7 Montaten.

Und nu?

Ja, bleiben diese 9,8er CVEs halt drin in dem Image, weil neu Bauen geht nicht.

xarvh, to apple
@xarvh@functional.cafe avatar

I'm finally at the point where I have to start working on perhaps the main feature of my programming language Squarepants: the ability to compile to GPU Shaders.

The most attractive target would be which is an intermediate representation that works almost everywhere... Except on browsers, and only because didn't want to give control of the standard to the group that develops SpirV.
Instead, Apple imposed , which is a language instead than an intermediate representation, so it's a pain in the ass to target and will end up with the same problem as .

At some point there will be translators from SpirV to WGSL, but I can't rely on those now.

So, what am I going to target?
Right now Squarepants compiles to javascript, so can run easily in both browsers and .

There is a project to run SpirV (via Vulkan) on node, but has been dead for years, which means that if I want to compile to a native application, I need Squarepants to compile to C or LLVM first.

OTOH if I go through the square-peg-in-round-hole and target WGSL, then I can target browsers.

-sigh-, I feel like I have no good option.

maralorn, to programming
@maralorn@chaos.social avatar

current status: calling a executable from typed template

alxd, to typescript
@alxd@writing.exchange avatar

Time for a #jobSearch post!

I'm looking for a #typeScript / #python / #RustLang 100% #remote #softwareDev position, both contract and permanent, GMT+2 timezone.

I previously worked as a Senior / Lead / Principal #fullStack developer with #cyberSecurity , #softwareArchitecture and #devOps experience.

I specialize in #react , #nodejs , #django , #fastAPI , #pandas , #postgresql , #docker , #kubernetes , #AWS and #digitalOcean .

https://www.linkedin.com/in/paul-ngei/

#fediHire #fediHired #jobHunt #software

mauve, to programming
@mauve@mastodon.mauve.moe avatar

Did you know that you don't need to commit to an entire @agregore browser to make use of it's tech? If you already have installed you can run agregore-compatible modules from your cli over any protocol supported by the browser. (e.g. or )

npx agregore run hyper://blog.mauve.moe/example.js  

With this you can share code between applications and command line utilities.

https://github.com/AgregoreWeb/agregore-cli

melroy, to random
@melroy@mastodon.melroy.org avatar

@lukekarrys How to buy you a coffee? I want to thank you for all your npm contributions, especially the maxSockets issue was a big deal to fix.

83r71n, to Cybersecurity
@83r71n@ioc.exchange avatar

A critical vulnerability, named BatBadBut, was discovered in the Rust programming language, affecting not just Rust but also Erlang, Go, Python, Ruby, and potentially others. This vulnerability, with a severity score of 10/10, could allow attackers to execute arbitrary commands on Windows systems by exploiting how Rust handles batch files. The issue arises from Rust's standard library improperly escaping arguments when invoking batch files on Windows, leading to potential command injection. The vulnerability has been addressed with a fix in Rust version 1.77.2, which developers are urged to update to. Other programming languages and systems, including Node.js, PHP, and Java, are also affected and are working on patches.

https://flatt.tech/research/posts/batbadbut-you-cant-securely-execute-commands-on-windows/

https://blog.rust-lang.org/2024/04/09/cve-2024-24576.html

#cybersecurity #rust #batbadbut #vulnerability #erlang #go #python #ruby #nodejs #php #java #windows #commandinjection #RyotaK #Grub4K #flattsecurity

f3rno64, to programming
@f3rno64@aus.social avatar

As I've noticed it's and have seen some wonderful artwork posted by people, as a programmer, I'd like to share a project I made, a command line time tracker with the purely textual interface.

Since I spent a good deal of time designing the textual output and UX I figure it's akin to art.

The interface is natural language input of times and dates representing when you start and end tasks.

It's available at https://f3rno64.io/a-nodejs-cli-time-tracker and the associated blog post is at https://f3rno64.io/a-nodejs-cli-time-tracker

A list of timesheets and the sum of the durations of tasks within them.
A concise listing of a short period of time in a timesheet with a few tasks listed.

carlton, (edited ) to random
@carlton@fosstodon.org avatar

The coffee must be good this morning.

Just drafting some thoughts about and « ”I’m just going to write my own”, is the battle-cry of morons » gently flows from the fingers. ☕️

May have to copy-edit 🤔

chanakya,
@chanakya@social.screamingatmyscreen.com avatar
nurkiewicz, to programming
@nurkiewicz@fosstodon.org avatar

10 years ago 4.0 was released. 5.x is still in beta https://www.npmjs.com/package/express/v/4.0.0

voxpelli, to programming
@voxpelli@mastodon.social avatar

Completed setup of a small Windows PC today and remoted into it from my MacBook to some code that was failing its windows tests on GitHub Actions

Is almost 20 years since Windows was my primary dev machine – it feels so alien nowadays when all my dev work is centered around git and cli-tools

leanpub, to devops
@leanpub@mastodon.social avatar

Learn Kubernetes & Docker - .NET Core, Java, Node.JS, PHP or Python by Arnaud Weil is free with a Leanpub Reader membership! Or you can buy it for $11.99! http://leanpub.com/k8s

joelanman, (edited ) to programming
@joelanman@hachyderm.io avatar

hard to believe that the only built in way to update a package to the latest version in npm is

npm uninstall [package name]  
npm install [package name]  

update, you can use

npm install [package name]@latest  

thanks @boutell

andy_blum, to programming
@andy_blum@drupal.community avatar

Ever worked on projects locally and wished for a more standardized, production-like experience for your team? Try @ddev! I walk you through setting your local up with in my latest article on @lullabot

https://www.lullabot.com/articles/nodejs-development-ddev

aral, to web
@aral@mastodon.ar.al avatar

Just improved the display of error messages in Kitten¹.

They should be far more robust now.

Run kitten update to get the latest.

:kitten:💕

¹ https://codeberg.org/kitten/app

aral, to programming
@aral@mastodon.ar.al avatar

Me: Hmm, maybe I should look into Bun again and see how its Node.js compatibility is coming along.

Also me: I wonder who makes Bun…

Me, yet again: Ah, it’s a venture-capital funded startup called Oven (see what they did there?)

Finally, me: rm -rf ~/.bun

(Remember, kids: Venture capital is the fart that precedes enshittification. It’s best not to linger once you’ve caught a whiff of it.)

leanpub, to programming
@leanpub@mastodon.social avatar

P4NR - IoT Programmer https://leanpub.com/b/p4nr-iotprogrammer by Iniationware and Joel Krec is the featured bundle on the Leanpub homepage! https://leanpub.com

jonippolito, to Cybersecurity
@jonippolito@digipres.club avatar

A cybersecurity researcher finds that 20% of software packages recommended by GPT-4 are fake, so he builds one that 15,000 code bases already depend on, to prevent some hacker from writing a malware version.

Disaster averted in this case, but there aren't enough fingers to plug all the AI-generated holes 😬

https://it.slashdot.org/story/24/03/30/1744209/ai-hallucinated-a-dependency-so-a-cybersecurity-researcher-built-it-as-proof-of-concept-malware

jaandrle, (edited ) to programming
@jaandrle@fosstodon.org avatar

🎉 nodejsscript@v1.0.0 :nodejs:
Easy cross-platform “one–file” scripting using JavaScript.

You can use it as:

Gradually replenished thread 👇

jaandrle,
@jaandrle@fosstodon.org avatar

🎉 nodejsscript@v1.0.1 :nodejs:

use CSS-like syntax for styling your outputs → simple loading/spin animation

  • All
  • Subscribed
  • Moderated
  • Favorites
  • JUstTest
  • tacticalgear
  • DreamBathrooms
  • InstantRegret
  • magazineikmin
  • Youngstown
  • everett
  • anitta
  • slotface
  • GTA5RPClips
  • rosin
  • thenastyranch
  • kavyap
  • mdbf
  • Leos
  • modclub
  • osvaldo12
  • Durango
  • khanakhh
  • provamag3
  • cisconetworking
  • ngwrru68w68
  • cubers
  • tester
  • ethstaker
  • megavids
  • normalnudes
  • lostlight
  • All magazines