Skip to main content

Learn Docker Beginner


If you are in Software industry then you must use this phrase to prove yourself.






But I m sure after reading this tutorial you won't be able to say this again.






1. What is Docker Docker is a software platform designed to make it easier to create, deploy, and run applications by using containers. It allows developers to package up an application with all the parts it needs in a container, and then ship it out as one package
2. Virtual Machines vs. Docker Virtual machines have a full OS with its own memory management installed with the associated overhead of virtual device drivers. Every guest OS runs as an individual entity from the host system.

On the other hand Docker containers are executed with the Docker engine rather than the hypervisor.








3. Introduction to Dockerfiles, images and containers

A Dockerfile is a text file that Docker reads in from top to bottom. It contains a bunch of instructions which informs Docker HOW the Docker image should get built.

A Docker image gets built by running a Docker command (which uses that Dockerfile)

A Docker container is a running instance of a Docker image

If you are too much into object oriented design then you can assume Docker image as a class, where as a Docker container is an instance of that class.


4. The Docker Hub

Docker Hub is a cloud-based repository in which Docker users and partners create, test, store and distribute container images which you can get from here.

https://hub.docker.com/


Create and Run docker image

prerequisite : Must have docker installed in system

https://librakblog.blogspot.com/2019/01/install-docker-on-ubuntu.html


In this scenario, you'll learn how to create a Docker Image for running a static HTML website using Nginx.
The scenario will explain how to build a Docker Image running Nginx with your HTML site.
The aim is to help you understand how to create and run Docker Images created by yourself.

create folder Docker and add file hello.html file into that.


Hello.html
<h1>Hello World</h1>

add one more file name Dockerfile make sure file name should be like this only without ant extension.


Open terminal and reach at folder Docker

vikash@vikash-pc:~/D-Drive/RKS_DOCS$ cd /home/vikash/D-Drive/RKS_DOCS/Docker/
vikash@vikash-pc:~/D-Drive/RKS_DOCS/Docker$ ls -lrt
total 8
-rw-rw-r-- 1 vikash vikash 20 Jan 27 16:03 Hello.html
-rw-rw-r-- 1 vikash vikash 49 Jan 27 16:04 Dockerfile

Step 1 - Create Dockerfile


Copy the below content in your Dockerfile for building your image.

Copy to EditorFROM nginx:alpine

COPY . /usr/share/nginx/html


The first line defines our base image.

The second line copies the content of the current directory into a particular location inside the container.



Step 2 - Build Docker Image
The Dockerfile is used by the Docker CLI build command. The build command executes each instruction within the Dockerfile. The result is a built Docker Image that can be launched and run your configured app. The build command takes in some different parameters.
The format is

docker build -t <build-directory>

The -t parameter allows you to specify a friendly name for the image and a tag, commonly used as a version number. This allows you to track built images and be confident about which version is being started.

sudo docker build -t webserver-image:v1 .


Result:


vikash@vikash-pc:~/D-Drive/RKS_DOCS/Docker$ sudo docker build -t webserver-image:v1 .
[sudo] password for vikash: 
Sending build context to Docker daemon  3.072kB
Step 1/2 : FROM nginx:alpine
alpine: Pulling from library/nginx
cd784148e348: Pull complete 
6e3058b2db8a: Pull complete 
7ca4d29669c1: Pull complete 
a14cf6997716: Pull complete 
Digest: sha256:385fbcf0f04621981df6c6f1abd896101eb61a439746ee2921b26abc78f45571
Status: Downloaded newer image for nginx:alpine
 ---> 315798907716
Step 2/2 : COPY . /usr/share/nginx/html
 ---> 6c54defc9f09
Successfully built 6c54defc9f09
Successfully tagged webserver-image:v1

As you can see in above output that above command executes all given steps in Dockerfile one by one.

You can view a list of all the images on the host using



sudo docker images



Output:

vikash@vikash-pc:~/D-Drive/RKS_DOCS/Docker$ sudo docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
webserver-image     v1                  6c54defc9f09        2 minutes ago       17.8MB
hello-world         latest              fce289e99eb9        3 weeks ago         1.84kB
nginx               alpine              315798907716        4 weeks ago         17.8MB
The built image will have the name webserver-image with a tag of v1.

Step 3 - Run


Launch our newly built image providing the friendly name and tag. As it's a web server, bind port 80 to our host using the -p parameter.

docker run -d -p 80:80 webserver-image:v1

Once started, you'll be able to access the results of port 80 via

http://localhost/Hello.html


You now have a static HTML website being served by Nginx.




Comments

Popular posts from this blog

Extent report plugin for cucumber framework

Extent Reports  are the most popular  reporting  used with Selenium. ExtentReport API makes our life easy to generate interactive  report  with simple configuartions. It supports almost all Java and .NET test frameworks such as TestNG , JUnit , NUnit etc Here we are discussing about  a plugin which is build on  Extent Report specially for Cucumber. This plugin is used to simple out the implementation of  Extent Report  in  Cucumber Framework .  We are creating a maven project to implement the integration of our plugin with cucumber 1. Create new maven project in any tool eclipse/sts/intellij 2. Open pom.xml and update below entries. Step 1 : Add Cucumber Extent Reporter library to Maven Project Add  cucumber-extentsreport <dependency>      <groupId> com.vimalselvam </groupId>      <artifactId> cucumber-extentsreport </artif...

java: You aren't using a compiler supported by lombok, so lombok will not work and has been disabled.

  In order to make projects compile with the existing builds of Lombok processor, as a workaround you can use the flag -Djps.track.ap.dependencies=false which should be added to File | Settings | Build, Execution, Deployment | Compiler | Build process VM options field. This will disable collection of dependencies specified by an annotation processor when Filer methods are called

Execution default of goal org.springframework.boot:spring-boot-maven-plugin:1.2.3.RELEASE:repackage failed: Unable to find main class

Solutions:  Solution 1 : You needed to change the packaging parameter to jar from pom. Also, the repositories , pluginRepositories , the maven-compiler-plugin and the spring-boot-maven-plugin's version and executions weren't needed. Solution 2:  Try mvn install and see if it works Solution 3: Preview: <properties> <!-- The main class to start by executing java -jar --> <start-class> com.mycorp.starter.HelloWorldApplication </start-class> </properties> Solution 4: Enable the main() method in your Application.java. Configure spring-boot-maven-plugin to specify the class with the main class (Spring should find it anyway if you have one, but good to be explicit): Preview: <plugin> <groupId> org.springframework.boot </groupId> <artifactId> spring-boot-maven-plugin </artifactId> <version> ${spring-boot-version} </version>...