Showing posts with label Docker. Show all posts
Showing posts with label Docker. Show all posts

Saturday, July 25, 2020

How to Build Apache Web Server Docker Image

Description: Here I have explained, how to build custom Apache Web Server Docker Image and create docker container using image.

Procedure:
  • First we create docker file to build Apache Web Server in Ubuntu. Docker file is like text file without having any extension
# mkdir /DockerDir [create directory to save file]

  • create index.html file under /DockerDir directory 
# vi /DockerDir/index.html


<!DOCTYPE HTML>
<html>
<body>

<h1> Server Techno Lab</h1>
<h2> https://servertecholab.blogspot.com/</h2>
</body>
</html>
</div>

  • Create Docker file for Webserver and copy index.html to docker image and run that URL on port 80
# vi /DockerDir/dockerfile

FROM centos:latest
MAINTAINER NewstarCorporation
RUN yum -y install httpd
COPY index.html /var/www/html/
CMD [“/usr/sbin/httpd”, “-D”, “FOREGROUND”]
EXPOSE 80
  1. FROM = container image name : tag
  2. MAINTAINER = Tag of Image
  3. RUN =  If you want to Run any command in container
  4. COPY = To copy files 
  5. CMD = Run command using bash 
  6. EXPOSE = Expose port number for service
  • Run Docker Build command to build docker file
# docker build -t="mywebserver" .


  • You can find your docker image using docker images command 
# docker images 


# docker run –d –p 80:80 mywebserver  

-d = Docker container deteched and runninin background
-p = Map Port number with container port apache will run on port 80

  • Finally you can browse server IP Address and default page of apache

Thursday, July 16, 2020

How save and load docker images to offline servers

Description: It is difficult to provide internet in all servers in production environment. So without internet you can not pull image from docker hub. So in such cases you can create tar file of docker image and load it from tar file. 

Procedure:  In example I have 2 server AppServer1 and AppServer2. AppServer1 have internet and AppServer2 is behind the firewall without internet. So first we save docker image on AppServer1 and then copy to AppServer2. After copy to AppServer2 we load it from there. 

  • On AppServer1, we will pull httpd image from docker using below command
    AppServer1> docker pull httpd:latest
  • Once image pull from docker hub we save it to .tar.gz file in /images directory
    AppServer1> docker save httpd:latest > /images/httpdlatest.tar.gz
  • Now you need to copy httpdlatest.tar.gz file from AppServer1 to AppServer2  on /images location using SCP or any other way.
  • Once Copy process completed you need to load image from AppServer1 using below command
    AppServer2> docker load -i  /images/httpdlatest.tar.gz 
  • You can find the images using docker images command
    AppServer2> docker images
  • You can run image using below command in AppServer2
    AppServer2> docker run -d httpd:latest
    

Tuesday, July 14, 2020

How to Setup Apache Web Server in a Docker Contaier

Description: Here I have explain how to setup Simple Apache Web Server in Docker Container. 

Procedure:

  • First install Docker on Centos and Ubuntu. Below command will download and run script that will add the Docker Repository and install package.
# curl -fsSL https://get.docker.com | sh

  • After installation start and check status of Docker service using below command
# systemctl start docker
# systemctl status docker
  • Now you can run docker command given in this URL
Setting UP Container:  There are many container for same technology, So in this blog we are using Apache 2.4 and named it to ServerTechLab-Apache. Once we get image we setup apache on 8080 port on our IP address to port 80 on container. We will setup simple page on /home/ServerTechLab. 

To setup this we will map /home/ServerTechLab [On Local Server] to /usr/local/apache2/htdocs [On container Machine]

# docker run -dit --name ServerTechLab-Apache -p 8080:80 -v /home/ServerTechLab/:/usr/local/apache2/htdocs/ httpd:2.4

  • If you have Selinux running on Server then you will get 503 authorization error. To Over come this issue you need to use :Z
# docker run -dit --name ServerTechLab-Apache -p 8080:80 -v /home/ServerTechLab/:/usr/local/apache2/htdocs/:Z httpd:2.4
  • You will find docker container is running check by using below command.
# docker ps
  • Create test html page on /home/ServerTechLab and browse it by http://IP_Addr:8080/test.html
  • If you want to stop and remove container, you can do it by using below commands
# docker stop ServerTechLab-Apache
# docker rm ServerTechLab-Apache         

Friday, July 10, 2020

Useful Docker Commands

Below are some docker commands with examples.

  • docker run – Runs a command in a new container.
  • docker start – Starts one or more stopped containers
  • docker stop – Stops one or more running containers
  • docker build – Builds an image form a Docker file
  • docker pull – Pulls an image or a repository from a registry
  • docker push – Pushes an image or a repository to a registry
  • docker export – Exports a container’s filesystem as a tar archive
  • docker exec – Runs a command in a run-time container
  • docker search – Searches the Docker Hub for images
  • docker attach – Attaches to a running container
  • docker commit – Creates a new image from a container’s changes
  • docker rm - Remove container
  • docker images - List images
  • docker rmi imageid  -Remove Image
RUN Command Examples:
  • docker run --rm [IMAGE] – removes a container after it exits.
  • docker run -td [IMAGE] – starts a container and keeps it running.
  • docker run -it [IMAGE] – starts a container, allocates a pseudo-TTY connected to the container’s stdin, and creates an interactive bash shell in the container.
  • docker run -it-rm [IMAGE] – creates, starts, and runs a command inside the container. Once it executes the command, the container is removed.

Docker Introduction and comparison with Virtual Machine

What is Docker: Docker is a set of platform as a service (PaaS) products that uses OS-level virtualization to deliver software in packages called containers. Docker is lighter version of virtual machine. To prevent dependency issue of development code to staging or production environment.
Image result for docker definition

Comparison Docker with Virtual Machine


A Practical Guide to Choosing between Docker Containers and VMs 

  • Docker is container based technology and containers are just user space of the operating system. At the low level, a container is just a set of processes that are isolated from the rest of the system, running from a distinct image that provides all files necessary to support the processes.
  • A Virtual Machine, on the other hand, is not based on container technology. They are made up of user space plus kernel space of an operating system. Under VMs, server hardware is virtualized. Each VM has Operating system (OS) & apps. It shares hardware resource from the host