Docker 101

Docker 101

Why docker ?

Docker allows you to run processes in isolated environments. This means that you can run multiple images on the same machine without any conflicts. You don't need to worry about the images; you'll understand more in the same blog.

What is a container?

A container is a unit of software that includes code and all its dependencies, ensuring the application runs quickly and reliably in different environments. A Docker container image is a lightweight, standalone package with everything needed to run an application, including the code, runtime, system tools, libraries, and settings.
Everything that you need to run the project in your local container is in it.

Why container?

For example, you have a Mac setup yah you are a rich guy who has a Mac setup and your friend who has window poor guy, another friend who has Linux in that case running that project might be different for everyone, that's why docker comes with container thing it does not depend on a machine that you have the steps are same for all here the equality came.
Keep one thing in mind docker is not the only way to create a container.

Installation of the docker

installation process
You can install docker from the above link you have to make sure that docker is running into you locally you should have just run the command.

docker

After running this command you will get the below image-like things in your cmd

If you getting the same, congrats docker successfully install into your computer/mac/Linux

So after installing the docker, you have three things to understand.

Docker Engine

The Docker engine is a containerization tech that allows developers to package applications into containers, and you already know what the container is.

Docker CLI

The command line interface lets you talk to the docker engine and you perform
all the required tasks that you want to perform.

Docker Registry

A Docker registry is just like a hub where you can push your image just like you push your code into Git Hub.
Docker registry

Image in Docker

This is pretty much jargon that you hear from the first line of this blog now understand what actually images are in the docker,
A Docker image is a small, self-contained package that has everything needed to run software. This includes the code, runtime, libraries, environment settings, and config files. it is just like the codebase that you push on GitHub.

On the other hand
A container is a running version of an image. It includes the application or service and its dependencies, running in a separate environment.
for better understanding assume this is a node index.js that you run into locally while starting the project.

Port mapping

Port mapping helps you to run the images into specific ports as you see in the below command

docker run -d -p 27018:27017 mongo

Dockerfile

If you want to create an image from your own code, you can push it to dockerhub, you need to create a Dockerfile for your application.

A Dockerfile is a text document that contains all the commands a user could call on the command line to create an image.
You can see the below file which is a dockerfile.

WORKDIR -> Sets a working directory
RUN -> Executes any commands in a new layer on top of the current image and commits the results.
CMD -> Provides defaults for executing a container. There can only be one CMD instruction in a Dockerfile.
EXPOSE -> Informs Docker that the container listens on the specified network ports at runtime.
ENV -> Sets the environment variable.
COPY -> Allow files from the Docker host to be added to the Docker image

How to build the image

Now you have a docker file in your local you can build the image via this command.

docker build -t image_name .

Now if you try to look at your images, you should notice a new image created

docker images

After running this command, you will see an image called image_name. Congratulations, you have created your image! Now, you need to add this Dockerfile to your codebase so that others can run your project on their local machines with just one command. You should also add a .dockerignore file to prevent node_modules and .env files from being included in the image during the build process.
Now after creating the image you can run it via command

docker run -p 3000:3000 image_name

And if everything goes right then in that case in the localhost:3000 you will find your project running just like the below image in your case it could be different.

One more thing you have to pass the env for your project for running it locally, in that case, you can pass it into the command line there are multiple ways but you can follow this also.

docker run -p 3000:3000 -e DATABASE_URL="databseurl" image_name

The -e argument lets you send in environment variables to your node.js app

Here I have added more commands to play with you can try into your playground

// List all contents of a container folder
docker exec <container_name_or_id> ls /path/to/directory 

Running an Interactive Shell
docker exec -it <container_name_or_id> /bin/bash

The next thing you need to know is how to push this to Docker Hub. I'll update this section soon, so come back and read it again.

And yeah, happy coding!