Forum

Notifications
Clear all

Devops-03-Oct-2024

0
Topic starter

Port mapping:
=============
Port mapping enables access to application running inside containers from outside world (i.e internet or browser)
Port mapping also called as port forwarding
Port mapping is needed for Application which needs to be accessed from GUI / browser
eg:- Tomcat, Jenkins etc...

syntax: -p <portNumber_in_dockerHost>:<portNumber_in_container>

How to use Port mapping / port forwarding ?

eg: start a docker container using tomcat image(tomee) with container_name as my_tomcat_container & port forwarding use 7070 port of docker host.

docker run --name my_tomcat_container -p <portNumber_in_dockerHost>:<portNumber_in_container> tomee

docker run --name my_tomcat_container -p 7070:8080 tomee

http://<publicip>:<portNumber_in_dockerHost>
http:// 3.110.213.91:7070

 

how to check ip address

Scenario 2:
-------------------------
Start jenkins as a container in detached mode , name is as "devserver_container", perform port mapping

docker run --name devserver_container -p <dockerhostPort>:<containerportnumber> -d jenkins/jenkins

docker run --name devserver_container -p 4040:8080 -d jenkins/jenkins

To access home page of jenkins ( In browser) ==> http://public_ip_of_dockerhost:4040

 

Scenario 3: Start a container centos_container along with downloanding image simeltaneously
 
To start centos as container, if centos image ifnot downloaded earlier, then docker will pull that from dockerHub immediately & it will start container
 
 
# docker run --name centos_container  -it  centos
 
 
#  exit  ( To come back to dockerhost )
 
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 
 
example: To start a container with terminal attached inside the container
 
syntax: docker run --name <container_name> -it <imagename>
        docker run --name c1 -it amazonlinux
 
# above command apart from creating container c1, it will also open terminal (-it) inside the c1
  
to come out of this container c1 back to dockerhost we have 2 options
      1. exit --> container will gets moved into exited state
      2. ctrl p + q --> container will be in running state  
 
 
 
 
Assignment :
-------------------
Note: before starting any assignments clear prevous images & containers
- create a container, container name ==> appserver_container, imagename ==> nginx (default port of nginx is 80) , run in detached mode with port mapping to 5555 on docker host  & access nginx from browser
 
- create a container, container name ==> grafana_container, imagename ==> grafana/grafana (default port of grafana is 3000) run in detached mode with port mapping to 6666 on docker host  & access grafana from browser
 
- create a container, container name ==> atlassian/jira-software (default port of jira is 8080 ) run in detached mode with port mapping to 7777 on docker host & access jira from browser
 
 
 
- create a amazonlinux container with interactive mode & create a environment variable & print variable value inside the container
 
docker run --name <conatiner_name>  -it amazonlinux
 
 
 
What are docker tags?
- Tags contains information about version of a docker image
   syntax <imagename>:<tagname>
  
  eg:
     1. if you run below command docker will take tag as default tag(i.e latest) even if you dont mention the tag
    docker pull amazonlinux
     
     2. if we want to download specific version of image we can mention tag along with image name
to download docker image with tag 2, we can run below command
     docker pull amazonlinux:
 
Agenda:
=======
- Tags in docker images
- commands for checking information / details of containers
  logs, inspect
- commands for Accessing running containers
  attach, exec
 
 
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 
 
 
 
Important feature of docker containers:
---------------------------------------
 
1. docker containers works on process isolation.
    (i.e, files & process running inside one container will be not be known to DOCKER HOST or any another containers)
    
2. when a docker container is deleted ==> all files / data inside the container will also get deleted by default 
 
(to be continued.....)
 
 
Note:
-----
To come-out from running container without exiting
press Ctrl +p  Ctrl +q  ==> it will run your container in background without stopping exit from container
 
 
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 
commands for checking information / details of containers:
----------------------------------------------------------
 
 - To see the logs generated by a container 
    docker logs <container_name/container_id> 
 
 
 - To get detailed info about a container 
    docker inspect <container_name/container_id>
              (or)
    docker inspect <image_name/image_id>
 
 
 
 Note:  
      docker inspect provides information about the specified container, such as its ID, name, configuration, network settings, volume mounts, and much more.This information can be helpful for troubleshooting, understanding how a container is set up.
 
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 
 
 
 
 
How to login into docker container (container which is running in background) ?
----------------------------------------------------------------------
**IMPORTANT COMMANDS**
 
There are only 2 ways to login into already running container
 
1. Using docker attach.
   -------------------------
    docker attach <container_name/container_id>
 
2. Using docker exec command.
   -------------------------
   docker exec -it <container_name/container_id> /bin/bash   
 
   docker exec command with the -it flags to open an interactive terminal inside the specified container. The /bin/bash at the end specifies the shell to use.
      
     Eg: 
      1. To create a 3 file inside container 
docker exec -it <container_name/container_id> touch file1 file2 file3
                    (or)
         docker exec <container_name/container_id> touch file1 file2 file3    
              
      2. To list files inside the container 
         docker exec -it <container_name/container_id> ls -lrt
 
 
Can we execute the commands outside the containers From docker host?
--------------------------------------------------------------------
Yes, we can do that using docker exec commands,
 
examples:
1. Run a Command to List files in the "/tmp" directory of a container
   docker exec -it <container_name/container_id> ls /tmp
 
2. Run a command (e.g., ps aux) inside a container:
   docker exec -it <container_name/container_id> ps aux
 
 
3. Run a Command to install git inside the container
   docker exec -it <container_name/container_id>
 
 
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 
 
 
Types of Docker Images:
------------------------
we can get docker images in 2 ways 
- Pre-defined images ==> images which we download from dockerHub
- Custom docker images ==> custom images created by devops engineers / developers of any company
 
 
What are the ways through which we can create docker images?
-----------------------------------------------------------------------------------
 
There are three ways through which we can create docker images.
 
 1. We can download any docker image directly from docker hub (using docker pull command) 
    docker images in dockerhub  are prepared & maintained by docker company and docker community.
 
 2. We can create our own docker images form our own docker containers. 
       i.e. first we create container form base docker image taken form docker hub and then by going inside container, we can install all required softwares and then create docker image from our own docker container.
  
 3. We can create docker image from a docker file.  It is the most preferred way of creating docker images.
 
 
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
genda:
=======
- Ways of creating docker Images
- Creating image from a running conatiner
- Creating image from Dockerfile

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

Docker Images:
--------------
we can get docker images in 2 ways
- Pre-defined images ==> images which we download from dockerHub
- Custom docker images ==> custom images created by devops engineers / developers of any company

What are the ways through which we can create docker images?
-----------------------------------------------------------------------------------

There are three ways through which we can create docker images.

1. We can download any docker image directly from docker hub (using docker pull command)
docker images in dockerhub are prepared & maintained by docker company and docker community.

2. We can create our own docker images form our own docker containers.
i.e. first we create container form base docker image taken form docker hub and then by going inside container, we can install all required softwares and then create docker image from our own docker container.

3. We can create docker image from a docker file. It is the most preferred way of creating docker images.

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

Creating a custom image from any running container?
---------------------------------------------------
- create a container

- do your customizations
customizations ==> install any softwares (or) add files / directories

- docker diff <ContainerID or ContainerNAME>
docker diff commmand shows custom changes we have made in the container incomparison with original image.

- docker commit <container_name> <new_image_name>
docker commit command will create image from the containername specified

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

Dockerfile:
===========
- A text file which uses predefined keywords or instructions to build a docker image.

- Automation of docker image creation
• FROM
• RUN
• CMD

Steps involved in creation from docker image to running container:
------------------------------------------------------------------
Step 1: create a file named Dockerfile
Step 2: Add instructions in Dockerfile
Step 3: Build Dockerfile to create image
Step 4: Run image to create container

Using docker file
--------------------
This is a simple text file, which uses predefinied key words for creating customized docker images.

Key words or insrtuctions used in docker file ( case sensitive )

1) FROM -- specify the base image for building a new image.

2) MAINTAINER -- This represents name of the organization or the author who created this docker file.

3) RUN -- Used for running linux commands while creating image.
It is generally helpful for installing the software in the container.

4) CMD -- This is used to specify the initial command that should be executed when the container starts.

*Important_interview_Question**
What is difference between RUN & CMD in dockerfile?
----------------------------------------------------
- RUN ==> RUN instructions will get executed while image is getting created
- CMD ==> CMD instructions will get executed as initial command (first command) when a container gets created

Scenario 1:
-----------
Create a docker iamge named myimage & tag as v1, take ubuntu as the base image and specify the maintainer as bharath. Construct an image from the dockerfile.

vi Dockerfile

FROM ubuntu
MAINTAINER bharath

inorder to create a image from dockerfile, we have to use docker build command like below

docker build -t <image_name>:<tag_name> .

docker build -t myimage:v1 .

Note:
-----
. (dot) ==> dot in docker build command indicates the Dockerfile present in current directory
its always a best practice to keep Dockerfile in seperate directory
# in dockerfiles indicates comment, these lines will be skipped from execution

Scenario 2:
-----------
Create a docker image named helloimage with tag as v3, take amazonlinux as the base image, create a file /etc/hellofile which has content "hello everyone".

vi Dockerfile

FROM amazonlinux
MAINTAINER bharath
RUN echo "hello everyone" > /etc/hellofile

docker build -t <image_name>:<tag_name> .

#create a container from created image & go inside the container & cat /etc/hellofile to verify the content

Scenario 3:
-----------
Create a docker iamge named amazonlinuxgit with tag as 2024, take amazonlinux as the base image, i want git to be available in my container, Construct an image from the dockerfile.

vi Dockerfile

FROM amazonlinux
RUN yum update -y
RUN yum install git -y

inorder to create a image from dockerfile, we have to use docker build command

docker build -t amazonlinuxgit:2024 .

#observation ==> create conatiner, login & verify the git installation

Scenario 3:
-----------
Create a docker iamge named amazonlinuxgit with tag as 2024, take amazonlinux as the base image, i want git to be available in my container, Construct an image from the dockerfile.

vi Dockerfile

FROM amazonlinux
RUN yum update -y
RUN yum install git -y

inorder to create a image from dockerfile, we have to use docker build command

docker build -t amazonlinuxgit:2024 .

#observation ==> create conatiner, login & verify the git installation

Scenario 4:
-----------
create an image named importantImage with tag as v1
take ubuntu as the base image, Whenever i start my container, i want date command / program to get executed

vi Dockerfile

FROM ubuntu
MAINTAINER vijay
#run instructions will always get executed at the time of image creation
#cmd instructions will always get executed as first command, whenever we create a container from this image
CMD date

docker build -t importantimage:v1 .

ubuntu@ip-172-31-45-143:~$ docker run --name c5 -it importantimage:v1
Mon Oct 30 03:52:09 UTC 2023
ubuntu@ip-172-31-45-143:~$

Observation:
eventhough we have specified docker run command with -it container doesent open interactive terminal, container just executed date command as mentioned in CMD instruction of Dockerfile & container gets moved into exited state.
what is the reason behind this?

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

Key words used in docker file.......< continued....>

5) ENTRYPOINT -- used to specify the default process that should be executed when container starts.
It can also be used for accepting arguments from the CMD instruction.

6) USER -- used to specify the default user who should login into the container.

7) WORKDIR -- Used to specify default working directory in the container

8) COPY -- Copying the files from the host machine to the container.

9) ADD -- Used for copying files from host to container, it can also be used for downloading files from remote servers.

10) ENV -- used for specifying the environment variables that should be passed to the container.

11) EXPOSE -- Used to specify the internal port of the container

12) VOLUME -- used to specify the default volume that should be attached to the container.

13) LABEL -- used for giving label to the container

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

Assignament:
------------
- create different images with different Base image flavor.
- investigate, why container stopped automatically after using CMD Instruction in senario4 ?
- difference between && (and operator) and || (or operator) used in linux.

 
AGENDA:
-------
- Dockerfile instructions
- CMD vs ENTRYPOINT
- Registry in dockerhub
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 
 
 
ex1: create an image named ubuntujava with tag as v1 
     take ubuntu as the base image, update apt package & install java (or default-jre) 
     Whenever i start my container, echo comamnd to get executed 
 
  FROM ubuntu 
  RUN apt update -y
  RUN apt install default-jre -y
  CMD ["echo","i have created image / container with java installed"] 
 
#observation: build the image & run container, once container gets started observe the CMD instruction gets executed (echo command)
 
Note:
-----
as best practice in CMD & ENTRYPOINT instructions use them inside square brackets with comma seperation, 
i.e exec form ==> ["<commands>"] 
ex: ["echo","hello world"]
 
 
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 
Instructions in Dockerfile (continued....)
-------------------------------------------
 
8) COPY --> Copying the files from the dockerhost machine to the container.
   syntax: COPY <files/Directory_name_in_dockerhost> <path_inside_container>
   
9) ADD  --> Used for copying files  from host to container, it can also be used for downloading files from remote servers.
   synatx: ADD <files/Directory_name_in_dockerhost> <path_inside_container>
 
 
 
 
What is difference between COPY & ADD instruction in Dockerfile?
-----------------------------------------------------------------
Both COPY & ADD instructions are used to copy files/Directories from DockerHost to Container.
ADD can also be used to download file (like wget command in linux) & also ADD can untar/unzip file inside containers,
 
 
 
 
10) ENV  -->  used for specifying the environment variables that should be passed to the container.
 
  ENV <Variable_name> <variable_value>
  
  ENV sportsman viratkohli
 
  inside container ==>  echo $sportsman
  
11) EXPOSE -- Used to specify the port on which we want to run our container
 
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 
 
 
 
 
What is difference between CMD &  ENTRYPOINT instruction in Dockerfile:
------------------------------------------------------------------------
 
CMD ==> we will define the commands that needs to be executed when a container starts.
ENTRYPOINT ==> we will define the commands that needs to be executed when a container starts.
 
 
**important_interview_question*************
 
Difference between CMD & ENTRYPOINT ?
-------------------------------------
 
CMD sets the default commands that needs to be executed when a container starts.
CMD command/instruction can be easily overridden while creating a container with different command.
whereas ENTRYPOINT command/instruction can't be overridden while creating a container.
 
Most of Docker containers by default have ENTRYPOINT ==> which is ==> /bin/sh -c
 
ENTRYPOINT generally used if we want to container as an executable ( like only purpose of running container is to run a script only)
 
 
 
Can we have both CMD & ENTRYPOINT in docker file? 
-------------------------------------------------
Yes we can have both in a Dockerfile. but CMD instructions will be passed as an arguments for ENTRYPOINT.
 
 
ex: usecase1 of using both CMD & ENTRYPOINT?
    FROM ubuntu
    CMD ls
    ENTRYPOINT ["echo", "Helloworld"]
 
    build above Dockerfile, run container ==> container Will be executed as below ===> Helloworld ls 
 
 
 
 
 
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 
 
 
Scenario 1:
-----------
Create a dockerfile by taking 
    - ubuntu as the base image 
    - create a user as bharath & make him as default user after logging in container
    - default working directory as opt  
Construct an image from the dockerfile.
 
vi Dockerfile
 
FROM ubuntu
RUN useradd bharath
USER bharath
WORKDIR opt
 
 
docker build -t <image_name> .
 
create conatiner & verify
 
Scenario 2:
------------
Create a dockerfile by taking 
    - alpine as the base image 
    - copy samplefile to docker container to /opt directory
    Construct an image from the dockerfile.
--> create a file called samplefile in dockerHost 
 
vi Dockerfile
   FROM alpine
   COPY ./samplefile /opt
 
docker build -t <image_name> .
 
create container & verify
 
 
 
Scenario  3:
------------
Create a dockerfile by taking 
- busybox as the base image 
- download maven installation file to docker conatiner /opt directory
Construct an image from the dockerfile.
 
FROM centos
 
 
Note:
-----
Alpine & busybox are ligh weight (smaller sized) docker images which will have all linux utilities
 
 
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 
 
 
How long will a docker container run?
--------------------------------------
Every docker image come with default process. 
As long as default process is running, the container will be running condition. The moment, the default process execution is completed, the container will get itself moved to exited / stopped state.
 
(default process ==> whatever mentioned in CMD instruction in Dockerfile) 
in already running containers, we can check default process using docker ps command & observe under COMMAND sections  
 
 
 
Practicals & observations on understanding default process in containers:
-------------------------------------------------------------------------
 
scenario 1:
-----------
  Create Dockerfile with below mentioned instructions
 
FROM ubuntu
CMD ["date"]
 
build this Dockerfile & create a conatiner from it & observe that conatiner has moved in to exited state.
 
Reason? ==> container has exited, because it has completed running its default process (whatever mentioned in CMD instruction i.e date command )
 
 
For all linux based containers( ubuntu,centos,amazonlinux.....) , the default process is shell process for that Dockerfile will look like
 
FROM ubuntu
CMD ["/bin/bash"]
 
/bin/bash or bash -- is nothing but the terminal.
Hence we are able to enter -it mode  in ubuntu/centos or any OS based containers.
 
 
 
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 
Assignment:
-----------
1. see how copy files from dockerhost to already running container(docker cp /home/user/documents/example.txt d5a8b8a2c46f:/app)
 
2. To deploy Acecloudacademy project application as a container 
   - launch ec2 instances change security group settings (inbound rules set to all traffic & anywhere)
   - install git, java, maven
   - clone Acecloudacademy project repo ( git clone https://github.com/Acecloudacademy/maven-repo.git)
   - build using maven to generate war file
 
   after generating war file from above steps
   Write a Docker file with tomee as a base image
   COPY generated war file to webapps directory inside the container & expose on port==> EXPOSE 8080
   build Dockerfile to create image & run container so that we can access ===> Acecloudacademy web applicatiion.
 

 

 

© Copyright 2024, All rights reserved by HeyCloud Innovations LLP | designed by ColorWhistle | Privacy Policy | Terms and Conditions