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

jhx, to linux
@jhx@fosstodon.org avatar

In case anyone else ever has some fun with #docker and #libvirt / #kvm on #Linux :linux:

By default the FORWARD table drops all packages...
To get vm's back on the net simply leverage #iptables to make the packets flow again:

$ sudo iptables -A FORWARD -i br0 -o br0 -j ACCEPT

You can install iptables-persistent to save the current ruleset so it is applied every time you restart the system. 😉

Did that on my #Debian workstation... I always fall for it. 😂

ernie, to linux
@ernie@writing.exchange avatar

This one’s for the normies who just want Github to give them a goddamn exe file: The case for making self-hosted apps work more like, say, Flatpak.

https://tedium.co/2024/04/15/self-hosting-docker-flatpak/

new @tedium

potatomeow, to rust
@potatomeow@fosstodon.org avatar

what is a good based image for build my own container image for a project? currently using docker.io/library/rust:slim-bookworm right now. my proj is running on rust nightly though. idk if it's gonna work.

currently waiting for the build to finish...

graham_knapp, to django
@graham_knapp@hachyderm.io avatar

Why does an idle #django project (no api calls) running on #Docker compose on #wsl2 on #Windows constantly use 20% of my cpu?

BoydStephenSmithJr, to haskell
@BoydStephenSmithJr@hachyderm.io avatar

Anyone know if the images (e.g.: https://hub.docker.com/layers/fpco/stack-build/lts-22/images/sha256-09dcc6cf3739dbb5f73bbb84dc15ee815f463409653c5159673afc7d3b4134b7?context=explore) are still maintained, and what might be the best way to report a bug? :blobfoxconfused:

The LTS_SLUG seems wrong, and when I attempt a stack build in that image, I run out of space compiling GHC. (e.g.: https://gitlab.com/bss03/restman/-/jobs/6622698704) :blobfoxsurprised:

I expect the docker image would contain a GHC that stack would find and use; I am using the matching resolver for the tag (e.g.: 22.17). :blobfoxhappy:

leanpub, to devops
@leanpub@mastodon.social avatar

Network Automation Crash Course https://leanpub.com/b/networkautomationcrashcourse by GitforGits | Asian Publishing House is the featured bundle on the Leanpub homepage! https://leanpub.com

badnetmask, to Blog
@badnetmask@hachyderm.io avatar

New post! Check out this great app that automatically scans your Compose file, proxies all your services behind , and requests TLS certificates for all of them using a private CA (). The cherry on top is getting behind the proxy, and secure!

https://mteixeira.wordpress.com/2024/04/12/proxying-apps-behind-caddy-with-certs-from-private-ca-using-home-assistant-as-example/

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

danslerush, to linux
@danslerush@floss.social avatar

« ’s Dedication to : A Robust Foundation for Developers » :debian:
https://www.docker.com/blog/debian-for-docker-developers/

ampache, to random
@ampache@fosstodon.org avatar

The first builds for 7 have been built!

Check out ampache/ampache:preview to see what's going on.

guerda, to homeassistant German
@guerda@ruhr.social avatar

I have an odd problem with in . In its docker container, the command wget does not resolve any domains. It just times out. Curl does resolve hosts correctly. Problem is: is using wget for updates, which then fail after a while (silently).

Other containers have a working wget, so it looks like it's homeassistant + docker in this case.

The container is (as documented) in host network mode.

Any ideas to fix this?

@homeassistant

fatherlinux, to linux
@fatherlinux@noc.social avatar

I envision an image-based workflow, similar to Project Bluefin, as the future of operating systems https://bit.ly/49wy4dG

sergio_101, to mastodon
@sergio_101@mastodon.social avatar

So, I am setting up my instance of #mastodon in a #docker #container , and am having issues getting the #rails app to authenticate with the postgres database. hmmm..

https://github.com/mastodon/mastodon/discussions/29901

Floppy, to random
@Floppy@mastodon.me.uk avatar

#Docker / GHCR experts: how accurate is the count of pulls from GitHub's Container Registry as a measure of number of real users? Would a single machine checking (say) hourly get counted as a single download, or 24 per day?

I ask because my numbers at https://github.com/manyfold3d/manyfold/pkgs/container/manyfold seem surprisingly high, like 500 in under 24 hours, which seems frankly unlikely. Obviously they're not all running it, but still.

Boosts welcome for reach!

dgoosens, to random
@dgoosens@phpc.social avatar

so
discovered that if you're using advanced #docker features in #phpstorm, you might want to make sure you are no longer using docker-compose (you should be using docker compose instead)

piece of cake to deal with this...

change the path to your docker compose executable (see screenshot) and you're all set

fluxwatcher, to linux
@fluxwatcher@mastodon.social avatar

Don't make your children's lives difficult by making them believe it's easy using Docker :thinkerguns:

#linux #docker

social, to random
@social@social.diva.exchange avatar

🎉 Fresh release (v2.51.0). Here: https://github.com/diva-exchange/i2p and here: https://github.com/PurpleI2P/i2pd

Available as here: https://hub.docker.com/r/divax/i2p

🙏 😀 to devs all over the world .

linuxmagazine, to Kubernetes
@linuxmagazine@fosstodon.org avatar

Have you seen the latest @adminmagazine focus guide? For a limited time, download this free digital special and go inside the Docker toolset. https://mailchi.mp/admin-magazine.com/docker-focus-guide

webology, to random
@webology@mastodon.social avatar
scottkeckwarren, to random
@scottkeckwarren@phpc.social avatar

#phpc checkout my recent interview with @tjlytle about some tips for #phptek2024 and his talk about #docker .

https://youtu.be/lmXVJL15n0Q

sfunk1x, to random

Had a serious database failure earlier due to cascading bad decisions. I started deleting a bunch of old data, rather than using . This led to a bunch of errors in the container.

I moved the database into bare metal using a with 32GB of RAM and 4 cores. I'm going to use this for other services too, and it was on my roadmap of transition. will be moving to that as well.

I'm still sorting out the fallout from this.

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

shyim, to php
@shyim@phpc.social avatar

I found finally a better way to build
#php Docker images, no compiling required, smaller image than alpine, but still glibc so no incompatibilities or performance problems with PHP. How? It's possible with Wolfi-OS by @chainguard_dev

https://shyim.me/blog/wolfi-os-for-php/
#docker

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