Showing posts with label DEVOPS. Show all posts
Showing posts with label DEVOPS. Show all posts

How to build and configure automatically custom Docker image using dockerfile on CentOS and RHEL 6/7 step by step guide



Build docker image from dockerfile | In this article we will learn how to build and configure automatically custom docker image using dockerfile on centos and rhel 7 system. Now we need to know the definition of dockerfile.

What is Dockerfile?

Dockerfile is a set of instruction which will be run to deploy an application.Docker image is created based on the instruction of docker file.

How to create a Dockerfile to build a docker image

First we need to create dockerfile which is filled by a set of instruction as below.

#touch dockerfile

Then edit the dockerfile and add the below lines.

FROM centos
MAINTAINER abc@email.com
RUN yum update
RUN yum install –y httpd
CMD "<your message>”

From the above line indicates that:

FROM : from which base image docker image will be created.
MAINTAINER: who is maintain the dockerfile.
RUN: run defined set of instruction which use to un the command and create the docker image.
CMD: message for the user.

Build docker image from dockerfile

To build  docker image from  dockerfile we need to execute the below command.

#docker build  -t centos-apache:v1 <dockefile location>

Sending build context to Docker daemon  3.072kB
Step 1/3 : FROM centos
latest: Pulling from library/centos
a02a4930cb5d: Pull complete
Digest: sha256:184e5f35598e333bfa7de10d8fb1cebb5ee4df5bc0f970bf2b1e7c7345136426
Status: Downloaded newer image for centos:latest
 ---> 1e1148e4cc2c
Step 2/3 : MAINTAINER abc@email.com
 ---> Running in 0ab288df423f
Removing intermediate container 0ab288df423f
 ---> a6f733efac95
Step 3/3 : RUN yum install -y  httpd
 ---> Running in 51fd64d94a5e
Loaded plugins: fastestmirror, ovl
Determining fastest mirrors
Installed:
  httpd.x86_64 0:2.4.6-88.el7.centos
Complete!
Removing intermediate container 51fd64d94a5e
 ---> 835398195c98
Successfully built 835398195c98
Successfully tagged centos-apache:v1
How to build and configure automatically custom Docker image using dockerfile on CentOS and RHEL 6/7  step by step guide,Build docker image from docker file,dockerfile
Build docker image from dockerfile
From the above command indicates that:

-t: mentin the tag to the image
Centos-apache: centos-apache is the image name .
V1: v1 is the tag of the image.
<dockerfile location>: location of the docker file.

After executing the above command we verify the images using the below command.

#docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
centos-apache       v1                  835398195c98        2 minutes ago       305MB

That’s all. If this article is helpful  to know about Dockerfile and Build docker image from docker file please share it!!!!




How to deploy and run application inside the isolated docker container on CentOS and RHEL 6/7 step by step guide


Deploy and Docker run image and save new image using Docker commit|In the previous article we learned  how to install Docker on centos and redhat linux system and learned use of docker container on centos and redhat linux system. In this article we will learn how to deploy and run an application inside the docker container.

Deploy and run an application inside Docker container

In the previous article we have installed centos docker image and here we will learn Docker run image (centos based docker container) where apache service will be installed . To do this we need to execute the below command.

[root@localhost ~]# docker run centos bash -c "yum install -y httpd"

--> Finished Dependency Resolution

Dependencies Resolved

================================================================================
Package             Arch          Version                    Repository   Size
================================================================================
Installing:
httpd               x86_64        2.4.6-88.el7.centos        base        2.7 M
Installing for dependencies:
apr                 x86_64        1.4.8-3.el7_4.1            base        103 k
apr-util            x86_64        1.5.2-6.el7                base         92 k
centos-logos        noarch        70.0.6-3.el7.centos        base         21 M
httpd-tools         x86_64        2.4.6-88.el7.centos        base         90 k
mailcap             noarch        2.1.41-2.el7               base         31 k

Transaction Summary
================================================================================
Install  1 Package (+5 Dependent packages)

Total download size: 24 M
How to deploy and run application inside the isolated docker container on CentOS and RHEL 6/7 step by step guide,Docker run image,Docker Commit
How to deploy and run application inside the isolated docker container on CentOS and RHEL 6/7 step by step guide

After that we need to execute the below command to know the container id.

#docker ps  -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS     PORTS               NAMES
8b451071cdd1        centos              "bash -c 'yum instal…"   3 minutes ago       Exited (0) 2 minutes ago                       agitated_hawking

Using this id we can stop , start and attach the container.



Docker Commit

We can save the new image using the container id . To save the new image we can execute the below command.To save the new image from container ID we use "Docker Commit" command.

#Docker commit <container ID> <New image neme>
# docker commit 8b451071cdd1 centos-apache
sha256:2f81cfc333d7cf8d61d3fbf21366ee1dd21f57c94bb5d7ca859ae4e99b9104c0

We can verify the new image name using the below command.

# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
centos-apache       latest              2f81cfc333d7        33 seconds ago      305MB

Map the host container port on docker

To map the host container port we can run the below command.

 # docker run -it -p 81:80  centos-apache /bin/bash
[root@276c18a6318d /]#

After that we press ctrl+p and ctrl+q to continue the process and type the curl command on the shell to get the apache page as below.

Note: 81 is the host port and 80 is the container port.

#curl http://localhost

That’s all. If this article is helpful to know about Docker run image and save new image using Docker commit please share it!

How to download and remove Docker Image and use Docker container on CentOS and RHEL 6/7 and Ubuntu 16.04 step by step guide


Download Docker Image from the Docker Hub and Docker Remove Image | In this article we will learn how to download docker image and after downloading the image start and run docker container and play with docker container to run and deploy an application and how to remove docker image.

How to download Docker Image

We can download Docker Image from the Docker Hub on your linux system using the below command. Suppose we are going to download centos image first we need to search the available images into the central docker repo using the below command.

[root@localhost ~]# docker search centos
How to download Docker Image and use Docker container on CentOS and RHEL 6/7 step by step guide,Docker Remove Image,Docker Hub
How to download Docker Image and use Docker container on CentOS and RHEL 6/7 step by step guide

Once getting the available images we can download the image using the below command.

[root@localhost ~]# docker pull centos
latest: Pulling from centos
e4b082fc6cdb: Pull complete
f016d310caa9: Pull complete
ab9a80ab07d0: Pull complete
Digest: sha256:38777a4106f0072649128ea4236241345a3ed21c55abbbd53bad5342549f6126
Status: Downloaded newer image for centos:latest

To list the available docker images we can execute the below command.

 [root@localhost ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
centos              latest              ab9a80ab07d0        6 weeks ago         201.8 MB


Docker Remove Image

If you want to remove docker image you can execute the below command.

[root@localhost ~]# docker rmi centos

If you want to remove docker image force fully then you need to use the below command.

[root@localhost ~]# docker rmi -f ab9a80ab07d0

How To Use Docker Container

 After downloaded the image we can start and run the docker container.  We can run the container using the below command. In this below command run the container using the downloaded image and get the downloaded centos release version.

[root@localhost ~]# docker run centos cat /etc/centos-release
CentOS Linux release 7.6.1810 (Core)

After running the docker container we need to know container id which is very useful to start and stop the container as well as you can use container name also to start and stop the container.

[root@localhost ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                CREATED              STATUS                          PORTS               NAMES
a4d4d2937780        centos              "cat /etc/centos-rel   About a minute ago   Exited (0) About a minute ago                       sick_yalow

And you can start the container again using the below command.
[root@localhost ~]# docker start <container ID>

How To Run interactive session for docker container

To run interactive session for a docker container we can use the below command.

[root@localhost ~]# docker run –it centos bash

If you type exit into the shell the container will be stop and if you don’t want to stop container process you can press ctrl+p and ctrl+q at a time.

You can attach the container using the below command

[root@localhost ~]# docker attach <container id>

You can check the container process using the below command.

[root@localhost ~]# docker ps

That’s all.If this article is helpful to know about Download Docker Image from the Docker Hub and Docker Remove Image please share it!




Please Donate To Bitcoin Address: [[address]]
Donation of [[value]] BTC Received. Thank You.
[[error]]

How to install Docker on CentOS 7 /RHEL 7/Ubuntu 16.04 step by step guide



Install Docker in Ubuntu 16.04 / 18.04 LTS /CentOS 7/ RHEL 7 |In this tutorial we learn what is docker and how to install Docker on CentOS 7,RHEL 7 and Ubuntu 16.04 / 18.04.

What is Docker?

Docker is container service management. Docker is open source lightweight tool which allow us to create ,ship and run an application on isolated container. Developing task became easy for developer for docker because they only focus on coding.   

Docker Engine is important component for docker which is building the images and creating the container.

How to install Docker on CentOS 7 /RHEL 7

Docker package is available in the centos extra repository. you can install Docker package using the below command.

[root@localhost ~]# yum install docker -y



Install Docker in Ubuntu16.04/18.04

Docker package is available in ubuntu by default. To install Docker In Ubuntu 16.04 / 18.04 LTS we need to execute below command .There is no need to install extra repository like centos and redhat and oracle also.



# apt-get install docker -y

After installed the Docker package we need to start the docker service and enable the service at startup mode execute the below command.


 [root@localhost ~]#systemctl start docker
[root@localhost ~]#systemctl enable docker

After doing the above steps we can check the docker service status using the below command.

[root@localhost ~]#systemctl status docker
● docker.service - Docker Application Container Engine
   Loaded: loaded (/usr/lib/systemd/system/docker.service; disabled; vendor preset: disabled)
   Active: active (running) since Thu 2019-01-17 22:18:43 IST; 12s ago
     Docs: http://docs.docker.com
 Main PID: 2324 (dockerd-current)
   CGroup: /system.slice/docker.service
           ─2324 /usr/bin/dockerd-current --add-runtime docker-runc=/usr/lib...
           └─2328 /usr/bin/docker-containerd-current -l unix:///var/run/docke...

To know the Docker version and info

To know the docker version and docker info we need to execute the below command.
[root@localhost ~]#docker version
How to install Docker on CentOS 7 and RHEL 7  step by step guide,Install Docker in Ubuntu16.04
How to install Docker on CentOS 7 /RHEL 7/Ubuntu 16.04
And get the docker info run the below command.

[root@localhost ~]#docker info
Containers: 0
 Running: 0
 Paused: 0
 Stopped: 0
Images: 0
Server Version: 1.13.1
Storage Driver: overlay2
 Backing Filesystem: xfs
 Supports d_type: true
 Native Overlay Diff: true
Logging Driver: journald
Cgroup Driver: systemd
Plugins:
 Volume: local
 Network: bridge host macvlan null overlay
Swarm: inactive
Runtimes: docker-runc runc
Default Runtime: docker-runc
Init Binary: /usr/libexec/docker/docker-init-current

Verify Docker working or not on CentOS 7 and RHEL 7 and Ubuntu 16.04 /18.04

To verify Docker is working or not we can run test container image using the below command  and if you will get this below message then docker works correctly on your system.
[root@localhost ~]# docker run hello-world
Hello from Docker!
This message shows that your installation appears to be working correctly.

To get full list of docker command we can execute the command on the terminal.
[root@localhost ~]# docker
That’s all.If this article is helpful to know about Install Docker in Ubuntu16.04 / 18.04 ,CentOS 7/ RHEL7 please share it!!!

Yum module in Ansible




Yum module in Ansible | Normally to install ,upgrade or remove a package we use yum command  in linux operating system .Similarly when we are going to install a package using Ansible we use yum module. Through this module we can install, upgrade and remove our required packages.

How to update all packages in ansible

To update all packages at a time we use ‘*’ symbol in the name parameter in Ansible and you must mention the state to latest to update all packages.
- hosts: node1
  tasks:
  - name: Upgrade all yum packages 
    yum:
      name: "*"
      state: latest

If you want to update all packages except some package you must specify ‘exclude’ parameter which contains which packages you don’t want to update.

In the below example we want to update all packages except php package.

- hosts: node1
  tasks:
  - name: Exclude some packages for upgrade
    yum:
      name: "*"
      state: latest
      exclude: php*

How to install a package in Ansible

You can install a package in Ansible using yum module.Under  this yum module there are two arguments one is name and another is state.
Name: name of the package which is to be installed.
State: state of the packages. By default state of the package set to present. when we are going to upgrade a package the state will be latest. when we are going to remove  a package the state will be absent.

For an example we install httpd package.


---
- hosts: node1
  tasks:
  - name: Install  package
    yum:
      name: httpd
      state: present

Install Package in Ansible
Install Package in Ansible



 How to install a specific version of package

To install specific version of package we have to mention the version of the the package in the name argument along with package name.

- hosts: node1
  tasks:
  - name: Install specific version of a package 
    yum:
      name: httpd<package version>
      state: present

How to install the latest version of package in Ansible

When we are going to install latest version of package we have to set the state to latest and in the name argument set the package name.
Name: name of the package which is to be installed.
State: state of the packages. By default state of the package set to present. when we are going to upgrade a package the state will be latest. when we are going to remove  a package the state will be absent.
For an example we install httpd package in latest version.
- hosts: node1
  tasks:
  - name: Install latest yum package
    yum:
      name: httpd
      state: latest

How to install multiple packages in ansible

To install multiple package we use with_item module and state should be present.
You can set the state to installed instead of present.The functionality of the both is same.

In the below example we install lamp stack


- hosts: node1
  tasks:
  - name: yum
    yum:
      name: "{{ item }}"
      state: present
    with_items:
    - httpd
    - php
    - mysql

How to delete a  package in Ansible

You can remove a package in Ansible using yum module.Under  this yum module there are two arguments one is name and another is state.
Name: name of the package which is to be installed.
State: state of the packages. By default state of the package set to present. when we are going to upgrade a package the state will be latest. when we are going to remove  a package the state will be absent.

For an example we remove httpd package.
---
- hosts: node1
  tasks:
  - name: Install  package
    yum:
      name: httpd
      state: absent

If this article is helpful to know about  Yum module in Ansible please share this article.




Create Directory in Ansible

Directory  is very necessary for linux operation.You can create Directory in linux system using shell through command.But when we are going to create directory  large no of systems one after another  at a time we utilize  Ansible because it is extremely  easier to implement large no of systems at a same time. 

We are going to implement node1  machine though ansible.

In this tutorial we learn how to create directory in ansible,how to create multiples directories in Ansible,how to create local directory in ansible,how to change permission of a directory in ansible and how to change permission of a directory recursively. 



    Create  Directory in Ansible

     You can create a directory using ansible file module.Here we have to defined two arguments one is path and other is state.Normally we create a directory using mkdir command. When we are going to create a directory using ansible  we set the state parameters to ‘directory’  .


    ---
    - hosts: node1
      tasks:
      - name: ansible create directory example
        file:
          path: /opt/testdir
          state: directory
     
    create directory in ansible
    create directory in ansible

    Create multiple directories in Ansible

    We can create multiple directories running ansible playbook using  with_items module.
    Let’s try an example
    We are going to create testdir1,testdir2,testdir3  under /opt
    ---
    - hosts: all
      tasks:
      - name: create multiple directories
        file:
          path: "{{ item }}"
          state: directory
        with_items:
          - '/opt/testdir1'
          - '/opt/testdir2'
          - '/opt/testdir3'



    Create local directory in Ansible

    Local_action statement is used in ansible when we are going to create a directory on the local machine as well as remote machine simultaneously .Directoy is created on the local machine before creating the remote maching when we execute the playbook.
    ---
    - hosts: all
      tasks:
      - name:  create local directory in ansible
        local_action:
          module: file
          path: /opt/localdir
          state: directory


    Change  Permissions of a Directory in Ansible

    In the above example we have created  a directory with default permission .Now we want to change the directory defaults permission.So, We have to focused one arguments that is mode .
    Now we are going to change the default permission of testdir directory which is located at /opt .
    We can use the octal form or symbolic form to define the mode agument.
    ---
    - hosts: node1
      tasks:
      - name: change permission of directory in ansible
        file:
          path: /opt/testdir
          state: directory
          mode: 0777


    Change  Permissions of Directory recursively

    The above example we have changed only permission of a directory from its default permission.Now we are going to change the change the permission of this directory recursively that means change the pemission of the directory as well as change the permission of those files and directories which is inside the parent directory.

    ---
    - hosts: node1
      tasks:
      - name: change recursive permission of directory in ansible
        file:
          path: /opt/testdir
          state: directory
          mode: 0777
          recurse: yes

     Delete Directory in Ansible

    We can delete a directory in Ansible to set the state value to ‘absent’.

    ---
    - hosts: node1
      tasks:
      - name: delete directory in ansible
        file:
          path: /opt/testdir
          state: absent

    If this article is helpful to know about create directory in ansible please share and subscribe this article.