Docker basics - create image and deploy to docker hub from development machine 

Docker is a platform or environment that are used to build, run, and deploy the app consistently. It packages all the software and binaries into an image and that ensure the same application behavior in the development environment also will be same in any other environment like staging production etc. once we deploy the docker images.

A Docker is a platform, which bundles all resources, binaries, run time environment, and all dependencies that require to run the application same as development environment to any other environment, which runs Docker engine.

 

This makes much easier to start up with the system to deploy and run the application, without worrying of installation and configuration the environment. Another benefit is, docker allow to run different versions of application, that are packaged with the different software configuration, and easier to up and remove any time we wish. And docker clean the all necessary dependencies for us.

 

This makes much easier the entire process of managing running and deploying any application in any environment, where docker binaries exist.


To make Docker running on the development machine.

Download and install the Docker for the respective Operating system from https://www.docker.com/products/docker-desktop 

Also check the best possible way to learn more in Docker with

Docker hub https://hub.docker.com/ , a docker repository to keep docker images.

Docker playground https://www.docker.com/play-with-docker , https://labs.play-with-docker.com/ to create a virtual machine with a pre setup docker environment



Once a docker installed in to the development environment, check its version to confirm by type the below command in the terminal.

docker version



For this example will create a basic app which console log the Hello Docker message. Will use NodeJS distribution on alpine Linux. Alpine is a smaller and lightweight distribution of Linux.


mkdir hello-app

cd hello-app

touch app.js Dockerfile


Add the content in app.js

console.log("Hello Docker");


Add the content in Dockerfile

FROM node:alpine

COPY . /app

WORKDIR /app

CMD node app.js



Now, that we have app.js and Dockerfile ready. Dockerfile is having the instruction to docker to follow and perform all the steps that define. Install node from alpine distribution, copy all the file from the current directory and move to app directory. Change the current working directory to /app and execute the command to run the app.js file.


Once this is done, we can create or build a docker image which package app.js and Dockerfile

Before that make sure you have docker credential , if not then signup https://hub.docker.com/signup .

Using docker credential we then back in terminal login to Docker

Can also create a repository in docker hub or , we can create locally and push that.


Here will create locally and distribute on the docker hub.


docker login

This will ask for docker username and password.



On successful login

docker build -t avinashau/hello-docker:latest .

-it represent the interactive flag

avinashau is the username

hello-docker is the build or package name

latest is the tag for this package

make sure to put period symbol at the last, that denote the Dockerfile exist directory. Here in this case the current directory.


Now, once the docker is build , can list the docker images

For this there are two command

docker image ls

docker images




Can run and execute the docker

docker run -it avinashau/hello-docker:latest


To push the docker image to docker hub

docker push avinashau/hello-docker:latest



Then navigate to https://hub.docker.com/ and can see a new docker image listed which we just pushed.



For re-tagging an existing local image docker tag <existing-image> <hub-user>/<repo-name>[:<tag>]

More on Docker command : https://motionknowledge.blogspot.com/2021/07/docker-basic-commands.html