Friday, July 5, 2019

Docker Image creation using Dockerfile

Dockers


Dockerfile :

     Dockerfile is used to build the new docker image

      Structure and Different sections and/or commonly used commands in Dockerfile are :
  1.       Base Image [FROM]
  • contains the Base Image that is used (downloaded from Repository) to create the new Image. The Base Image is referred in this section as follows:
  • FROM       baseImageFullPath:Tag 
  • where baseImageFullpath includes the repositoryname and ImageName
  1.       Arguments [ARG]
  • We can supply the set of user defined arguments that are useful in building different versions of Images using one uniform dockerfile
  • For example, We can have an argument like VERSION, that can be used to build an Image from different base versions of the baseImage . Similarly we can have arguments to pass the PROXY SERVER , INSTALLATION DIRECTORY, BIN DIRECTORY e.tc
  • Arguments are mentioned in dockerfile as : ARG [ArgumentName] 
  • Arguments values are passed to the dockerfile while building the Image using the docker build command as docker build --build-arg ArgumentName=ArgumentValue. --build-arg ArgumentName2=ArgumentValue2 ....
  1.       Environments [ENV]:
    • Environment variables are the variables that can be available as environmental variables when the Image was loaded and executing 
    • Environmental variables are defined in Dockerfile as : ENV EnvironmentalVariableName=Value 
    •  
  2.       Installation new Packages  or Softwares into the Image [RUN, COPY, ADD, ...]
    • Depending upon the requirements, we might need to add new packages (or) code (or) data into the new Image
    • RUN command is used to execute any unix command in Image creation . For example if we want to install a new python package, then we can use RUN pip install [packageName]. Similarly if we want download , untar and install any new software from the internet, then we can the respective commands using RUN as RUN curl -O -L https://......./ && tar -xvf <> ...
    • NOTE : to reduce the size of the final Image, it is advisable to combine multiple RUN command usages in consecutive commands into single RUN command. This will reduce the intermediate cache and layers created for each RUN command 
    • Similar to RUN command, there is COPY and ADD commands that can be used to copy files from local machine to the Image
  3.       Clean up 
    • Once the required packages are installed , it is good to remove the temporary and intermediate files created during the Image creation . Depending upon the Image OS type we can use the required cleanup commands like yum remove or rm
    •   
The following sections and steps are used w.r.t to setup the starting environment and commands in the Image
  1.       Working Dir [WORKDIR] : specifies the Current Working Directory , when the Image is started to execute
  2.       startup command [CMD]: Startup command
  3.       Entry Point [ENTRYPOINT] : Specifies the  command or script that will be executed while the Image is getting executed as part of the startup command
  4.       Ports expose [EXPOSE] : Specifies any ports that are to exposed to the outside


Useful Docker Commands


1. To list all docker containers in the system

docker images

2. To list all Images that are currently loaded and executing

docker ps 

3. tagging one docker with some other name

docker tag

4. To know more details (like tags, base, ...) and layers in the docker Image

docker inspect

5. To Pull an Image from the repository to local system

docker pull 

6. To Push an Image to the repository

docker push

7. To use (enter into ) the already executing docker Image
    docker exec -it 

8. To user ( enter into ) the docker that is not in execution
     docker run -it   




9. To copy the files to or from the Docker Container
     step1 : get the docker container id using the docker ps command
     Step2 : using the docker cp command copy the file or directories to/from the container and to the local file-system , using the docker container id as the host-machine
              eg: copying to the container
                       docker cp file-name docker-container-id:/destination-path
                    copying from the container
                       docker cp docker-container-id:/src-path/file destination-path