Shubham Ankit

Shubham Ankit

1567322313

Docker 101 - Creation to Deployment

What is Docker?

In simple terms, Docker is a tool that lets developers create, deploy and run applications in containers. Containerization is the use of Linux containers to deploy applications.

So why is Docker so great, and why should we as developers even bother learning it?

Now that we know why Docker is such a big deal, let's have it installed on our local machine.

Sign up for an account on Docker Hub, and download the free Docker Desktop application.

How is Docker different from traditional Virtual Machines?

A container runs natively on Linux and shares the kernel of the host machine with other containers. It runs a discrete process, taking no more memory than any other executable, making it lightweight.

By contrast, a virtual machine (VM) runs a full-blown “guest” operating system with virtual access to host resources through a hypervisor. In general, VMs provide an environment with more resources than most applications need.

When working with Docker, a Dockerfile defines what goes on in the environment inside your container. Access to resources like networking interfaces and disk drives is virtualized inside this environment, which is isolated from the rest of your system, so you need to map ports to the outside world, and be specific about what files you want to “copy in” to that environment. However, after doing that, you can expect that the build of your app defined in this Dockerfile behaves the same wherever it runs.

Docker Commands

To test that you have a running version of Docker, run the following command:

docker --version

To test that your installation is working perfectly, try running the simple Docker hello-world image:

docker run hello-world

If all is setup properly, you should see output similar to the following:

Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
ca4f61b1923c: Pull complete
Digest: sha256:ca0eeb6fb05351dfc8759c20733c91def84cb8007aa89a5bf606bc8b315b9fc7
Status: Downloaded newer image for hello-world:latest
 
Hello from Docker!
This message shows that your installation appears to be working correctly.
...

To see the hello-world Docker image that was downloaded to your computer. The Docker image listing command is helpful. Here it is:

docker image ls

Awesome! You've already started developing containerized applications with Docker. Here are some helpful basic Docker commands:

## List Docker CLI commands
docker
docker container --help
 
## Display Docker version and info
docker --version
docker version
docker info
 
## Execute Docker image
docker run hello-world
 
## List Docker images
docker image ls
 
## List Docker containers (running, all, all in quiet mode)
docker container ls
docker container ls --all
docker container ls -aq

Containerization makes CI/CD seamless. For example:

  • applications have no system dependencies
  • updates can be pushed to any part of a distributed application
  • resource density can be optimized.
  • With Docker, scaling your application is a matter of spinning up new executables, not running heavy VM hosts.

Let's build a Node.js web app using Docker!

The first thing we do is create a package.json file. We can do this quickly, by simply running the following command:

npm init -y 

This creates the file, above with certain essential fields already filled in, or left blank.

Your file should look something like this:

{
  "name": "app-name",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC"
}

Next, we install express.js - which according to the official website, is a "Fast, unopinionated, minimalist web framework for Node.js".

We do this by running the following command in a terminal:

npm install express --save 

The command above adds the express.js framework to our application, with the --save flag, acting as an instruction to the application, to use express.js as a dependency.

Now, go into your package.json, and change the "main": "index.js" key-value pair to the following:

"main": "app.js" 

Next, create a .gitignore file using the following command:

touch .gitignore 

Then add the following line:

node_modules/ 
This prevents the node_modules folder which is essential to node.jsdevelopment from being tracked by git.

Now add the following code to the app.js file:

const express = require('express');
 
const app = express();
 
const PORT = 8080;
const HOST = '0.0.0.0';
 
app.get('/', (req, res) => {
  res.send(
    `
    <h1>Home</h1>
    <p>Docker is awesome!</p>
    <a href="/more" alt="Next Page">Next Page</a>
    `
  )
});
 
app.get('/more', (req, res) => {
  res.send(
    `
    <h1>Page Two</h1>
    <p>Node.js is pretty great too!!</p>
    <a href="/" alt="Back Home">Back Home</a>
    `
  )
});
 
app.listen(PORT, HOST);
console.log(`Running on https://${HOST}:${PORT}`);

To have this run on your local machine, run the following command, in the application folder:

npm start 
You will find the application running at http://0.0.0.0:8080/

Awesome!

Into the Dockerverse

Now create a Dockerfile with the following command:

touch Dockerfile 

Then place in the following code:

# An official Docker image for Node.js
FROM node:10-alpine
 
# Working directory for the containerized application
WORKDIR /src/app
 
# This copies significant package.json files to the current directory
COPY package*.json ./
# Install essential Node.js dependencies
RUN npm install
 
COPY . .
 
# Opens up this port on the Docker container
EXPOSE 8080
 
# This starts the Docker application
CMD [ "npm", "start" ]

The comments above, attempt to explain what each Dockerfile command does.

Also, add a dockerignore file, to prevent the containerization of certain components of the application.

Place this inside of the file created:

node_modules
npm-debug.log
Dockerfile*
docker-compose*
.dockerignore
.git
.gitignore
README.md
LICENSE

How to Deploy!

The <image-name> is the name you assign to your Docker app, and <tag> is essentially just a version indicator for your Docker image.

  • docker build -t image-name:tag .

Run this to access your Docker account from your terminal.

  • docker login

Create a repository on Docker Hub.

Tag <image> for upload to registry.

  • docker tag <image-name> username/repository:tag

Upload tagged image to the registry.

  • docker push username/repository:tag

Run the deployed Docker container on your local machine, by connecting PORTS - targeting the exposed 8080, and assigning it to 10203 on your machine.

  • docker run -p 10203:8080 username/repository:tag


That's it! You have built and deployed a containerized Node.js web application.

All the code above can be found in this Github repository.

Originally published by  Ninte at  blog.ninte.dev

=============================

Thanks for reading

If you liked this post, share it with all of your programming buddies!

Follow me on Facebook | Twitter


Further reading

☞ The Complete Node.js Developer Course (3rd Edition)

☞ Angular & NodeJS - The MEAN Stack Guide

☞ NodeJS - The Complete Guide (incl. MVC, REST APIs, GraphQL)

☞ Best 50 Nodejs interview questions from Beginners to Advanced in 2019

☞ Node.js 12: The future of server-side JavaScript

☞ Docker for Absolute Beginners

☞ How to debug Node.js in a Docker container?

☞ Docker Containers for Beginners

☞ Deploy Docker Containers With AWS CodePipeline

 

#docker #node-js #web-development

What is GEEK

Buddha Community

Docker 101 - Creation to Deployment
Iliana  Welch

Iliana Welch

1595249460

Docker Explained: Docker Architecture | Docker Registries

Following the second video about Docker basics, in this video, I explain Docker architecture and explain the different building blocks of the docker engine; docker client, API, Docker Daemon. I also explain what a docker registry is and I finish the video with a demo explaining and illustrating how to use Docker hub

In this video lesson you will learn:

  • What is Docker Host
  • What is Docker Engine
  • Learn about Docker Architecture
  • Learn about Docker client and Docker Daemon
  • Docker Hub and Registries
  • Simple demo to understand using images from registries

#docker #docker hub #docker host #docker engine #docker architecture #api

Docker Architecture Overview & Docker Components [For Beginners]

If you have recently come across the world of containers, it’s probably not a bad idea to understand the underlying elements that work together to offer containerisation benefits. But before that, there’s a question that you may ask. What problem do containers solve?

After building an application in a typical development lifecycle, the developer sends it to the tester for testing purposes. However, since the development and testing environments are different, the code fails to work.

Now, predominantly, there are two solutions to this – either you use a Virtual Machine or a containerised environment such as Docker. In the good old times, organisations used to deploy VMs for running multiple applications.

So, why did they started adopting containerisation over VMs? In this article, we will provide detailed explanations of all such questions.

#docker containers #docker engine #docker #docker architecture

Paris  Turcotte

Paris Turcotte

1618003500

How to Deploy AI models ? Deploying Web-application on Heroku via Docker

This Part is the continuation of the Deploying AI models Part-3 , where we deployed Iris classification model using Decision Tree Classifier. You can skip the training part if you have read the Part-3 of this series. In this article, we will use Flask as the front end to our web application to deploy the trained model for classification on Heroku platform with the help of docker.

Note: If you have followed my Model Deployement series from starting you can skip the section 1.

A_rticle: _Deploying AI models Part-3

1.Iris Model Web application using Flask.

1.1. Packages

The following packages were used to create the application.

1.1.1. Numpy

1.1.2. Flask, Request, render_template from flask

  1. 1.3. Dataset, Ensemble, Model Selection from sklearn

1.2. Dataset

The dataset is used to train the model is of iris dataset composed of 4 predictors and 3 target variable i.e. Classes

Predictors: Sepal Length, Sepal Width, Petal Length, Petal Width

_Target _: Setosa [0], Versicolor[1], Verginica[2]

Dataset Shape: 150 * 5

#docker-image #artificial-intelligence #deployment #machine-learning #docker

Docker manifest - A peek into image's manifest.json files

docker manifest – An experimental feature !

The image manifest provides a configuration and a set of layers for a container image.

This is an experimental feature. To enable this feature in the Docker CLI, one can edit the config.json file found in ~/.docker/config.json like :

{
        "auths": {
                "https://index.docker.io/v1/": {
                        "auth": "XXXXXXX"
                }
        },
        "HttpHeaders": {
                "User-Agent": "Docker-Client/19.03.8 (linux)"
        },
        "experimental": "enabled",
        "debug": true
}

What is ‘docker manifest’ ?

The docker manifest command does not work independently to perform any action. In order to work with the docker manifest or manifest list, we use sub-commands along with it. This manifest sub-command can enable us to interact with the image manifests. Furthermore, it also gives information about the OS and the architecture, that a particular image was built for.

A single manifest comprises of information about an image, it’s size, the layers and digest.

A manifest list is a list of image layers (manifests) that are, created by specifying one or more image names. It can then be used in the same way as an image name in docker pull and docker run commands.

Commands to get started with :

After enabling this feature, one would be able to access the following command :

docker-manifest-enter image description here

These commands are easy to use. It basically avoids the need for pulling and running and then testing the images locally, from a docker registry.

Next, to inspect an image manifest, follow this syntax,

 docker manifest inspect image-name

enter image description here

.

#devops #docker #devops #docker #docker learning #docker-image

Iliana  Welch

Iliana Welch

1597368540

Docker Tutorial for Beginners 8 - Build and Run C++ Applications in a Docker Container

Docker is an open platform that allows use package, develop, run, and ship software applications in different environments using containers.
In this course We will learn How to Write Dockerfiles, Working with the Docker Toolbox, How to Work with the Docker Machine, How to Use Docker Compose to fire up multiple containers, How to Work with Docker Kinematic, Push images to Docker Hub, Pull images from a Docker Registery, Push stacks of servers to Docker Hub.
How to install Docker on Mac.

#docker tutorial #c++ #docker container #docker #docker hub #devopstools