Docker Basics(share the same environment)

Anil Verma
2 min readApr 14, 2022

Create your own image and share it using dockerHub or with private repos.

Docker- Docker is a container technology for creating and managing containers.

Image- An image is a template or blueprint for container. it contains the source code and environment.

Container- It is a running instance of an image.

Why Docker- Containers are a standardized unit of software that allows developers to isolate their app from its environment, solving the “it works on my machine” headache.
Means — you can share the same environment with all team members without any headache. it will be not a case where I have a different version of node.JS in my machine and you have a different version of Node.js and the code is working on the machine of one folk but not working on the machine of another folk.

Installing Docker

  1. Visit docker.com
  2. inside docker →products →docket desktop
  3. download docker desktop as per your OS
  4. install it on your local machine

Creating Container

Step 1- Create Dockerfile file in your source directory

Step -2 put the following code in Dockerfile file

FROM nodeWORKDIR /appCOPY package.json ./ RUN npm installCOPY . ./#first dot(.) will tel all folder subfolder should get copied in image
#second dot(.) path inside image where all files will be put
#optional but good for documenation purposeEXPOSE 3000 CMD [ "node", "app.js" ]

Step 3

execute below command to create a imagedocker build .#if dockerfile is inside any other folder the put path of that folder instead of dot(.)it will return an imageId

Step 4 Run Image

docker run -p portonLocal:portInsideContainer imageIdlikedocker run -p 3000:3000 imageId

Step 5 Access App in local
In browser type localhost:3000
You can access the application.

Note — till this, it is very basic but it will help you to build an idea and thought around docker.

Source code — https://github.com/anilvermaspeaks/docker

Few Commands

Show all running containers-
docker ps

Show all containers-
docker ps -a

Show all images-
docker images

Thanks for learning 👏👏👏

--

--

Anil Verma

Hi there 👋, I am Anil Verma, a full stack web developer, and JavaScript enthusiast. 👥 Ask me anything about web development. web- https://anilvermaspeaks.in/