Docker Fundamentals
Tags: docker, devops, architecture
Published: 2021-05-30 12:48:58

---

since you landed here you definitely want to know what docker is?



**We'll start off by telling the problem statement**: let's say we want to use create-react-app where the OS is not compatible of node to install `npm install -g create-react-app` 😿 , you can achieve this by using Docker Image. Hurray!!!! Interesting. isn't it? 🤔



WHAT is docker? simply you can think off like a Sea Port harbour docker 🚢 where we have containers to be exported into other places across the world and container contains the goods and materials. Similarly, our docker also has containers but here we call those goods and materials as an Image, just a technical term. so all the images need a place to store we call it Docker Hub. just like a GitHub is the place to store code as simple as that.



Now, How does docker solve our problem, Though the OS doesn't support node what if the docker image provides you with all the dependencies to install create-react-app… WOW, that's a solid idea !!!!! 💡 so Image is independent of the OS. Moving forward I'll show how to create an image and push it to docker hub. Here is the architecture diagram of a docker

Without any delay, let's go ahead create our first image and store it in docker hub.
Install docker from [here](https://docs.docker.com/engine/install/).

Create a folder called docker-sample. or fork the repo from [here](https://github.com/SamalaSumanth0262/Docker-example.git)

``` mkdir docker-sample && cd docker-sample && mkdir public && cd public && vim index.html ```
and write some HTML code you can refer [here](https://github.com/SamalaSumanth0262/Docker-example/blob/master/public/index.html) 
3. now vim Dockerfile .
4. let's start adding the code to the file. FROM node:alpine , this line will fork the base node repo where our application code gets run. By doing this step we've just got a copy of machine and does not contain any application code
5. Add `COPY . /usr/src` , we copy all the files from the current directory and paste in /usr/src where usr stands for a universal system resource.
6. Add `WORKDIR /usr/src` , we tell the container that usr/src in the source of my application code.
7. Add RUN npm install, this will install packages in `package.json` and also we have in-built npm installed in node:alpine.
8. Add `CMD ['npm', 'run', 'start']` , RUN is a block command where the execution blocks but here we want to run a command asynchronously so we use CMD.
9. Add `EXPOSE 80` , we are telling docker once the image runs in the container we expose port number 80.
Finally, your Dockerfile should look like this.

Now then we have copied the code and we set the working directory and all set except we are left with building the image, we run this command now `docker build . -t docker_id/name_of_the app` in my case its `docker build . -t 14bee0262/docker-sample` where docker_id is the user_id of docker hub.
Before uploading to docker hub we need to really check in our local whether the container is running image or not? we can run the image by `docker run --restart always -d -p 80:80 14bee0262/docker-sample` you should see the below image in your localhost …taaadaaa!!!! 
- restart => whenever there's is an exception docker restarts the image.
- d is for telling the docker to restart the daemon.
- p is the port number binded_container_port:exposed_port 
Now you can push this image by docker push user_id/name_of_image in my case its docker push 14bee0262/docker-sample and my image is here. you can check in your docker hub dashboard. Now where ever you are in the world whatever the OS configuration is you can just pull the image and run it and your server is UP Cheeeeerrrss!!!! 🍹 🍹 😄 😄. Anyways  Documentation is available for much more information . PEACE !!!!!!!!
PS: Any Suggestions, Any comments are heartfully welcome.