Before moving further, we need to work on some basic part of Docker technology, that will be used for subsequent chapters to do practices.
Using the prebuilt images from the Docker Hub to Run Container
In Day1 introduction, we have seen definition of Docker Hub, which is images repository, from where we can pull / push images and use the same to run the container from that image.
We can see, Image can be directly downloaded from Docker Hub and then can be used to run container as below.

Let we create image and run container directly from Docker Hub. Open 2 terminals in VCS and Run commands in order from terminals as mentioned,
Terminal -1
Below command will pull python latest images from Docker Hub, but first it will search at local repository,
Once pull is complete we can test Python command to Print "Hello Programmer , Welcome to Python" as below.
Keep this tab open and move to Terminal -2 now
PS D:\Docker> docker run -it python Unable to find image 'python:latest' locally latest: Pulling from library/python d52e4f012db1: Pull complete 7dd206bea61f: Pull complete 2320f9be4a9c: Pull complete 6e5565e0ba8d: Pull complete d3797e13cc41: Pull complete 70f90dfe001b: Pull complete bd75605de417: Pull complete >>> #this command will print "Hello Programmer, Welcome to Python" >>> print ('Hello Programmer, Welcome to Python') Hello Programmer, Welcome to PythonIn this terminal if we execute below command, it will show the running container on Python image, which was pulled from Docker Hub.
Terminal -2
PS D:\Docker> docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
bd08e61f72fb python "python3" 6 seconds ago Up 5 seconds focused_engelbart
Let we move to Terminal-1 again and exit from interactive mode of Python application as below.
Terminal -1
Hello Programmer, Welcome to Python
>>> exit()In above practice we downloaded python3 image and started container in Interactive mode, then we executed few commands on python container and exited.
About the Dockerfile
A Dockerfile is a text document that contains commands that are used to assemble an image. We used Dcokerfile in our last practice where docker reads its content and creates a docker image.
This file is main component to prepare an Image for project.
Docker builds images automatically by reading the instructions from the Dockerfile
The docker build command is used to build an image from the Dockerfile
Some of the common instructions in a Dockerfile are:
• FROM: specifies the parent image from which you are building
• COPY: adds files from your current directory to the image
• RUN: executes a command during the image creation process
• CMD: specifies what command to run when the container is deployed
For more commands of Dockerfile follow Dockerfile refernece
Let's Create a Sample Project for Practicing Management Options.
To proceed further let we first have as Sample project which will be used for all testing and management scenarios.
This sample project is called as "Shopping list repostory" . We will input Items through box and that will be displayed over html page.
Below are outline for this project.
- We will use Node.js as a programming language, No need to worry about that as we need to understand only working of Docker technology.
- We will create a Dockerfile and that will be used to create an image file.
- Define all parameters and commands in Dockerfile which will be using all required files as per project.
Dockerfile is heart of Docker technology, and this is as below.
Dockerfile# Set the baseImage to use for subsequent instructions.# If image not avaialable Locally , it will be pulled from DockerHub# FROM must be the first instruction in a DockerfileFROM node# Set the working directory in container for any subsequent ADD, COPY, CMD, ENTRYPOINT,# or RUN instructions that follow it in the Dockerfile.WORKDIR /app# Copy files or folders from source to the dest path in the image's filesystem# Here it's copying from Local Machine to ConaitnerCOPY . /app# Execute any commands on top of the current image as a new layer and commit the results.# In this example Node.js getting installed in containerRUN npm install#Define the network ports that this container will listen on at runtime.EXPOSE 80# Provide defaults for an executing container.# If an executable is not specified, then ENTRYPOINT must be specified as well.# There can only be one CMD instruction in a Dockerfile.# Here CMD tells to Execute contents of server.js file using Node.jsCMD ["node", "server.js"]
server.jspackage.json file is used for assisting extra library and packages for node program.const express = require('express');const bodyParser = require('body-parser');const app = express();let ItemList = '*****************';app.use(bodyParser.urlencoded({extended: false,}));app.use(express.static('public'));app.get('/', (req, res) => {res.send(`<html><head><link rel="stylesheet" href="styles.css"></head><body><section><h2>Shopping Item List</h2><h3>${ItemList}</h3></section><form action="/store-item" method="POST"><div class="form-control"><label>Input Item List</label><input type="text" name="Item"></div><button>Add Items</button></form></body></html>`);});app.post('/store-item', (req, res) => {const enteredItem = req.body.Item;console.log(enteredItem);ItemList = ItemList+ "<br/>"+ enteredItem ;res.redirect('/');});app.listen(80);
package.jsonBelow file is stored in public folder in local machine with same path as above files , this file is used for formatting all HTML content.{"name": "docker-complete","version": "1.0.0","description": "","main": "server.js","author": "N","license": "MIT","dependencies": {"express": "^4.17.1","body-parser": "1.19.0"}}
public/style.csshtml {font-family: sans-serif;}body {margin: 0;}section,form {padding: 1rem;border-radius: 12px;box-shadow: 0 2px 8px rgba(0, 0, 0, 0.26);margin: 2rem auto;max-width: 40rem;}.form-control {margin: 0.5rem 0;}input {font: inherit;}input,label {display: block;}label {font-weight: bold;margin-bottom: 0.5rem;}button {background-color: #2f005a;border: 1px solid #2f005a;color: white;cursor: pointer;padding: 0.5rem 1.5rem;}button:hover,button:active {background-color: #50005a;border-color: #50005a;}
PS D:\Docker> docker build . [+] Building 7.5s (9/9) FINISHED => [internal] load .dockerignore 0.0s => => transferring context: 2B 0.0s => [internal] load build definition from Dockerfile 0.1s => => transferring dockerfile: 128B 0.0s => [internal] load metadata for docker.io/library/node:latest 2.1s => [1/4] FROM docker.io/library/node@sha256:57391181388cd89ede79f371e09373824051eb0f708165dcd0965f18b36 0.0s => [internal] load build context 0.1s => => transferring context: 1.16kB 0.0s => CACHED [2/4] WORKDIR /app 0.0s => [3/4] COPY . /app 0.0s => [4/4] RUN npm install 4.7s => exporting to image 0.3s => => exporting layers 0.3s => => writing image sha256:37ec243acddd323a4772d06c6e6a6fb48410bc42d4e4f7d4560c4e1060f6c565 0.0s PS D:\Docker>
PS D:\Docker> docker images REPOSITORY TAG IMAGE ID CREATED SIZE <none> <none> 37ec243acddd 6 seconds ago 1.1GB
PS D:\Docker> docker run -p 3000:80 37ec243acddd
PS D:\Docker> docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 835b3ea24ee6 37ec243acddd "docker-entrypoint.s…" About a minute ago Up About a minute 0.0.0.0:3000->80/tcp exciting_nash PS D:\Docker>
PS D:\Docker> docker stop exciting_nash exciting_nash PS D:\Docker> docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 835b3ea24ee6 37ec243acddd "docker-entrypoint.s…" 11 minutes ago Exited (137) 4 seconds ago exciting_nash PS D:\Docker>
No comments:
Post a Comment