01. Docker Basics

Parmeshwar C
5 min readJan 16, 2021

With Docker, developers can build any app in any language using any tool-chain. “Dockerized” apps are completely portable and can run anywhere on OS X and Windows laptops, QA servers running Ubuntu in the cloud, and production data center VMs running Red Hat.

Developers can get going quickly by starting with one of the 1,00,000+ apps available on Docker Hub. Docker manages and tracks changes and dependencies, making it easier for sysadmins to understand how the apps that developers build work. And with Docker Hub, developers can automate their build pipeline and share artifacts with collaborators through public or private repositories.

Docker helps developers build and ship higher-quality applications, faster.— What is Docker

Need for Docker

  1. Many times we have observed that when a new developer joins our team then he took almost half a day to set up his development environment. Similarly, when we want to run our application on some machine we need to take care of all those dependencies before running it.
  2. Most of the time we develop on some machine lets say (Windows machine) and when the time comes to deploy it on the Linux server then environment setup becomes another overhead. Sometimes we deliver our app and client faces a lot of problems to run our app on their machine.
  3. Yes, this issue we can solve using docker. You create an image and be assured to run it in any other environment where Docker is installed.

Components of docker:

Docker is becoming complex day by day because of cloud requirements then from where to start.

  1. Docker Host — This is an area inside any host machine where Docker is installed. Where all our containers will run or reside.
  2. Docker Hub — this is a repository of commonly used images. You can use their images or can create and push our image there.
  3. Dockerfile — This is a file in our application listing all those instructions to set up the environment for our application to run.
  4. Docker Image — An executable project holding our project executable along with all dependencies. This is a zipped format so that it can be delivered to other people easily over the internet.
  5. Docker Container — container is an area where our image got unzipped an ready to run
Docker architecture

Let’s try to understand from one end which is our application and we can go till the cloud.

  1. Dockerfile — This is the file we do add inside of the project root folder. Which holds the information about what project needs to run. Along with instructions so that along with our project build or source code other dependencies should be added in the final image.
FROM node:12RUN mkdir -p /app
WORKDIR /app
COPY package.json .
RUN npm install
COPY . .
EXPOSE 3000CMD [“npm”, “start”]Let’s understand each and every line.

FROM node:12 :

  1. this line informs the docker host that Take node:12 as the base image.
  2. We need to understand what is base image and from where to take it. There is docker-hub which is a repository holding such images. From where our docker host will pull it and install it.
  3. We are aware of operating systems like Linux and windows. Here our image is Ubuntu and node-12 installed in it. Yes, there is such a small OS. Actually, those are minimal versions of operating systems. You can say it is a Linux kernel. We would have taken Ubuntu as the base image also but then we have to install node in that. If there node preinstalled Ubuntu then let’s take it.

RUN mkdir -p /app

  1. We want docker to create an “app” folder. Our intention is to put our app related data there.

WORKDIR /app

  1. We are mentioning that the “app” directory will be my working directory now onwards.

COPY package.json .

  1. We are copying the package.json file in the current working directory that is “app” now. Why we are copying it. because the package.json file holds all those dependencies needed for the application. Using the next command we are going to install all those dependencies required for our application. But in your app, there are chances of not having such a file. Then whatever dependencies you need you can mention one by one in the Docker file.

RUN npm install

  1. Here we are instructing the docker host to install those packages which are mentioned in package.json.

COPY . .

  1. Here we are copying all our applications in that working directory. This is okay for development purposes but while making an image for deployment we need to copy only the build folder and required packages.

EXPOSE 3000

  1. This application runs on localhost:3000 but when this application will run on the docker host then the 3000 port of our machine and 3000 port of the docker container should be mapped so we are exposing our port so that the host machine should forward the request coming to 3000 port to our container.

CMD [“npm“, “start“]

  1. This is the command to run that image. When their time comes to run the image.

Docker Image

  1. We tried to understand what a docker file is and what it’s purpose. When we build this dockerfile using the docker build command then the docker image gets created. Which is an executable image or we can say it is our application with all dependencies bundled. (it is executable but still, it is not running). Then when and how it will run?. here Docker container comes in a picture.

Docker Container

  1. Container is a place where our image or application is being executed. Now we can interact with the application or we can see the application on the browser using localhost:3000.

Commands for image and container creation

  1. Assuming you have written Dockerfile. Now be inside the folder where Dockerfile resides and fire the below command from the terminal.
docker build -f Dockerfile -t <projectname> .
  1. -f: Here -f is from the file we can use — file also.
  2. -t: this is to tag name to our image.
  3. last . is for dockerfile path. “dot (.)” is used to mention as the current directory. If dockerfile is in another directory then you can mention that path.
  4. Example — docker build -f Dockerfile -t MyWebApp:1.0 .

Commands to run image and container

docker run — name auth-tool -p 3000:3000 MyWebApp:1.0
  1. — name: is for the name of the container
  2. -p 3000:3000 is for the port mapping. Where 3000 from the host machine to 3000 from the container gets mapped.

Unanswered questions

  1. How to handover that image to another person.
  2. How to check the contents of the container when it is running.how two containers will communicate in between.
  3. how two containers will communicate in between.
  4. What is the Docker hub? Who keep all those preinstalled images there. Can we upload our image? if yes, then how?
  5. What if my application has sub modules like client and server. or server and database.

We will answer all those questions in the next chapter with some hands-on.

--

--