pranavv

write-up ·2020 ·in progress

Server Management

Running my own self-hosted servers and services since 2020.

Role
Sole administrator
Dates
2020-09-01 → present
Tags
LinuxDockerTailscaleCloudflare Tunnel

published·

Hey There! Second Blog of the Year!

I recently learnt Docker, which is a containerizing tool.If you don't know what docker is, it is basically like a VM, but, it is different from a VM in many ways. A VM Typically tricks the host into thinking it is running on the hardware, but, Docker seems to be more friendly with the host, by sharing the Kernel with the Host Machine. That is why containers are so fast to run, and take up less resources. Also, in a container, all the versions of the tools remain the same (unless you change them manually in the config), so a container creates an environment for you application, that will remain same, wherever you deploy it.

Let's say that, you have an application that needs a specific older version of "Node.js". Once you have made it, everything is working as expected, and, you upload it to Github. But then, Some people report that it is not working on their machines. That is where docker comes into play. Docker will basically create an environment of the older version of node, but, only for your project, without touching other dependencies on the host machine. This will allow any user, who is using docker, to pull your project, and run it, without installing / messing up anything else on their existing machine.

I have tried out docker, and, it is fantastic. I have containerized this Portfolio, then run it on my own servers now, and the experience was fantastic. First, we will need to create a Dockerfile (All Source code here) which will define the container image, from which the containers will be created. Here is the Dockerfile for this Portfolio:

FROM node:latest
WORKDIR /var/www/html
COPY . .
RUN npm install
EXPOSE 8080
CMD ["npm", "start"]

This Will Get the Latest Image of Node.js from the Docker Hub, Set the Working Directory to "/var/www/html", copy all required files (that includes HTML, CSS, and JS Files), install all libraries, then, open up port 8080 to allow traffic to and from that port, and then finally, run the command which serves my portfolio to port 8080. 

Further Steps might include learning Docker Compose, which will let you run multiple containers from one docker-compose.yml file, as you might require more than one container for a project, or, maybe your project will be split across many containers providing different services.

Now, let me share some more insights and experiences I've had with Docker: One thing that really blew my mind about Docker is how easy it makes scaling your application. Let's say your web app suddenly gets popular and you need to handle more traffic. With Docker, you can quickly spin up multiple containers of your app across different machines or even cloud services. It's like magic – you can go from one instance to ten or a hundred with just a few commands! Another cool feature I discovered is Docker volumes. These are like special folders that exist outside your container but can be accessed from inside it. This is super useful for things like databases or user-uploaded files that you want to persist even if the container is destroyed and recreated. It's like having a safety deposit box for your important data!

Oh, and speaking of databases, I tried running a MySQL database in a Docker container alongside my app. It was so much easier than installing MySQL directly on my machine. No more worrying about conflicting versions or messing up my system settings. I just pulled the official MySQL image, set a few environment variables, and boom – I had a fully functioning database ready to go!

I also experimented with Docker Networks. These allow containers to communicate with each other as if they were on the same network, even if they're on different machines. It's like creating a virtual LAN for your containers. This was super helpful when I was working on a microservices architecture – I could have different services in different containers all talking to each other seamlessly.

One more thing that I found really useful was Docker's layered filesystem. Each instruction in the Dockerfile creates a new layer, and Docker caches these layers. This means that if you make a small change to your app, Docker only rebuilds the layers that have changed, making the build process super fast. It's like having a time machine for your development process!

Finally, I want to mention how Docker has changed my development workflow. Now, instead of spending hours setting up my development environment on a new machine, I just pull my Docker image and I'm ready to go in minutes. It's made collaboration with other developers so much easier too – no more "but it works on my machine" problems!

In conclusion, Docker has not only simplified my deployment process but has also opened up a whole new world of possibilities in terms of how I structure and scale my applications. If you haven't tried it yet, I highly recommend giving it a go.

Comments

Loading comments…

All projects