Dockerization
I have been using Dockers for a while now, in almost every project that I have made. Here is a gist of what I have learned so far!
What are Dockers?
Docker is a software platform that allows you to build, test, and deploy applications quickly.
Firstly, how I came across Dockers?
I was working on a backend project and I had to deploy it on a Digital Ocean droplet. I was honestly tired of setting up the project and installing all dependencies again, this is when i came across Dockers.
What are containers and how are they different from VMs?
A container is a software code package containing an application’s code, its libraries, and other dependencies. Containerization makes your applications portable so that the same code can run on any device. Thus, processes running inside containers are visible from the host system (given enough privileges for listing all processes).
A Docker packages an application and all its dependencies in a container that can only run on a Linux Server. Thus, dockers are also called containers.
Instead of setting up the project from scratch, and installing all dependencies, Docker bundles up the code along with its dependencies into autonomous container units.
Whereas, a virtual machine is a digital copy of a physical machine. Everything running inside the VM is independent of the host operating system, or hypervisor. The virtual machine platform starts a process (called virtual machine monitor, or VMM) in order to manage the virtualization process for a specific VM, and the host system allocates some of its hardware resources to the VM.
A hypervisor(sometimes also referred to as Virtual Machine Monitor) is software that creates and runs virtual machines (VMs). It isolates the hypervisor operating system and resources from the virtual machines and enables the creation and management of those VMs.
How Components does a Docker need?
- Daemon — which is used to build, run, and manage the container.
- High-level API — which allows the user to communicate with the Daemon
- CLI — an interface we use to make this all available.
Steps for Dockerization
These are the steps required to set up dockers in WSL.
Make sure your WSL is upgraded to version 2.
If you do not have docker installed, Try installing using Docker Desktop as
Here is a list of basic commands to make sure the docker is up and running
- To start the docker daemon
sudo dockerd
2. To check the current version of the docker
docker version
Incase you get any permission denied error, try running the same command using sudo
3. To pull an image
A Docker image is a read-only template that contains a set of instructions for creating a container that can run on the Docker platform.
Here ‘hello-world’ is a sample docker image. Using this command, we pull an image from the Docker Hub, you can list all available images using
docker image ls
docker pull hello-world
docker pull redis
4. To check the list of containers that are already running on your system
docker ps
5. To check the list of all containers
docker ps -a
6. To containerize an image
docker run redis
7. To remove an image
docker rmi -f redis
8. To pull a specific image version from the docker hub and containerize it
docker run redis:4.0
9. To run docker in detached mode
You can run a container in the background using detcahed mode, this ensures that the applications that are running inside your container are able to run unattended.
docker run -d redis:4.0
10. We can use this id to start and stop a docker
You can see the <id> by running “docker ps” command
docker start <id>
docker stop <id>
11. Port Binding
docker run -p6000:6379 redis
12. To delete an image, we need to stop the container and remove its container first
docker ps
docker stop <Container_Id>
docker rm <Container_Id>
docker image rm <Image_Id>
This is how a sample docker file should look like —
Include this Dockerfile into your project directory
Steps
- Build the image
docker build . -t <your username>/<name-of-node-application>
2. Incase of network timeout error
docker build . -t <your username>/<name-of-node-application> --network=host
To see the images listed by Dockers
docker images
Run the image
docker run \
-it \
--rm \
-v ${PWD}:/app \
-v /app/node_modules \
-p 3001:3000 \
-e CHOKIDAR_USEPOLLING=true \
<your username>/<name-of-node-application>
Print app output
docker logs <container id>
To get container-id
docker ps
To call app using curl
curl -i localhost:49160
You can check out how to integrate the sample react app and sample node js with docker here!
Thank you for reading this blog! I will be posting more on Dockerization very soon ❤