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.
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.
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 …
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.
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).
what is a good based image for build my own container image for a #rustlang 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... #docker#podman
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:
I have an odd problem with #HomeAssistant in #Docker. In its docker container, the command wget does not resolve any domains. It just times out. Curl does resolve hosts correctly. Problem is: #HACS 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.
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..
#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?
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
Had a serious database failure earlier due to cascading bad decisions. I started deleting a bunch of old data, rather than using #tootctl. This led to a bunch of errors in the #pgsql#docker container.
I moved the database into bare metal using a #nuc 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. #nextcloud will be moving to that as well.
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
How to install PHP dependencies and extensions with Docker (kinsta.com)
Docker is a popular open-source platform for developing, shipping, and running applications. It encapsulates applications and their dependencies in