Mega Menu

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

Tuesday, April 21, 2020

Docker Engine

Docker Engine comprises of three components -
   
        Docker Daemon - background process that manages docker object such as network, storage, containers, volumes.

        REST API - API interface that can provide instructions to Docker Daemon.

        Docker CLI - Command line interface that can provide instructions to Docker Daemon.


              


It is not always mandatory to have Docker CLI on the same machine of th Docker Engine. Below is an example of remote Docker Engine -


              


Command to use remote Docker Engine :

        docker -H={remote-docker-engine-host}:{port}











Docker Compose

Steps:
  • Install docker compose
  • Create docker-compose YAML file
  • Execute docker compose

docker-compose.yml

     redis:
       image: redis
       webapp:
         image: mywebapp
         ports:
           - 5000:80
         links:
           - redis 


Script:

     docker-compose up

                        container names will be prefixed with the directory name from which this script is being executed.


Docker - Command and Entrypoint

If a process inside the container is dead, the container exits and will no longer be running. 

Command is a process that is specified in the DockerFile, which should be executed when a Docker image is run.
The arguments will have to be hard-coded within the Dockerfile.

Docker File:

    FROM ALPINE

    CMD ["sleep","10"]    OR     
    CMD sleep 10
   
Docker Run:   

    docker run alpine
            This will execute
                    docker run alpine sleep 10
               
Entry point is the command that executes at the start of the container. The argument passed as input while running the container will be passed as input to the entry point command.
This will allow the command arguments to be dynamic, rather than hard-coded in the DockerFile
       
Docker File:

    FROM ALPINE

    ENTRYPOINT ["sleep"]
   
Docker Run:   

    docker run alpine 15
            This will execute
                    docker run alpine sleep 15
               
               
To set a default value to an EntryPoint command,

Docker File:

    FROM ALPINE

    ENTRYPOINT ["sleep"]

    CMD ["5"]   

   
Docker Run:   

    docker run alpine
            This will execute
                    docker run alpine sleep 5
               
    docker run alpine 20
            This will execute
                    docker run alpine sleep 20


To override the entrypoint command runtime, pass an entrypoint argument while running a docker container -

Eg:
         docker run --entrypoint sleep2 alpine 10   
                ----- this executes sleep2 process instead of sleep which is in the dockerfile


Docker Environment Variables


Find all available environment variables
    
         docker inspect {imageName}
   

Set environment variables while running the image

           docker run -e {ENV_VAR_NAME}={VALUE} {IMAGE_NAME}

Docker - Build and publish custom image to repository

Key Steps Involved -
  • Create a DockerFile
  • Build docker image with appropriate tag
    • docker build . -t rajesh.sripathi/test-web-app
  • Login to docker hub
    • docker login
  • Push the Image
    • docker push test-web-app

By default the image gets pushed to a public repository. To push it to private repository,
    •     Login to the hub, and create the repository first, and mark it as private. Then when you push to that repo, it will be private.

Docker Port Mapping Basic Scenario

Port mapping can be used to map a port on localhost with that of the port of docker container on which the application is running. Below RUN command can be used for that

             docker run -p 8001:8080 -p 50001:50000 jenkins
                                            where 8001 and 50001 are the localhost ports and 8080, 50000 are ports of docker image.


Running a Jenkins Container -

   docker run jenkins
        Once the download is complete, we should be able to access Jenkins through browser. To do this, you need to find the port of the jenkins image using 'ps' and ip using 'inspect'
   
          docker ps       
   

    docker inspect {containerId}
   
This can be simplified using the port mapping.
   
   
Detailed steps for using Jenkins Image     -     https://hub.docker.com/_/jenkins/

Wednesday, January 08, 2020

Docker - Build Simple Node JS Webapp docker container

#1 Create the below docker file

 

#2 Create the dependency files

    package.json
    index.js

package.json

   

index.js


 

#3 Build the container

Build the container from the location where all the above 3 files are placed.

 docker build -t testdocker/myfirstweb:v1 .

 

#4 Port binding

 Through the index.js file, we have configured nodeJS to listen to port 8080 i.e; the port 8080 on the container has the app deployed.
To access this port from the local machine, there should be a binding from local machine to docker container, so as to allow the web browser on local machine to access the port 8080 of docker container.
This is done with the help of docker run command.

syntax: docker run -p 8080:8080 testdocker/myfirstweb:v1


#5 Access the test Url from web browser
    http://localhost:8080
         this should print the message
                      Hello My First Web App Container

Screenshots of terminal window and web browser-