DevOps Classroom Series – 31/Oct/2020

Building Docker Images

  • Building Docker Images can be done in two ways
    • Create a container, do the manual installation & create a docker image from container.
      • This approach is not sensible as the image creation process is manual & cannot be version controlled
      • Changes are difficult to handle
    • Create a docker container by writing a Dockerfile
      • This approach helps in maintaining versions of every change which you do
      • Changes will be simple as it all about changing one file & create a new version
  • Dockerfile is a simple plain text with a set of INSTRUCTIONS
  • Workflow: Preview
  • Basic Steps involved in building a docker image
    • choose the right base image
    • add steps to install/configure your application
    • add information of ports required etc
    • add step which should be executed to run your application
  • This is exactly what we do to in terms of Dockerfile. In Dockerfile we have lot of instructions. Each instruction performs some activity. So lets look at Dockerfile instructions
  • Docker image has the following naming conventions <image-name>:<tag>
  • Refer Here for the official documentation.
  • Lets try to create a simple dockerfile
  • Refer Here for changes
  • Lets try to build a image Preview Preview
  • Now lets try to build our docker image with name my-helloworld Preview Preview
  • When we build docker image an image id, image name & image tag are associated with a image
  • Now lets use an instruction LABEL to add metadata Refer Here for the changeset Preview Preview Preview Preview
  • The inspection result is
[
    {
        "Id": "sha256:ba68f57b7991745a56bbc696ab0f15aa81e7e712dbf5702ebc67044e41f8dd73",
        "RepoTags": [
            "my-helloworld:latest"
        ],
        "RepoDigests": [],
        "Parent": "sha256:9932ce15331bc6adaef6ccf95a8ce0fdaa389714097b2adad4e68ee2dae76294",
        "Comment": "",
        "Created": "2020-10-31T13:24:43.128925375Z",
        "Container": "3fe99f6b027b8d97102f981384539046432bd6c09599353de86088896b569306",
        "ContainerConfig": {
            "Hostname": "3fe99f6b027b",
            "Domainname": "",
            "User": "",
            "AttachStdin": false,
            "AttachStdout": false,
            "AttachStderr": false,
            "Tty": false,
            "OpenStdin": false,
            "StdinOnce": false,
            "Env": [
                "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
            ],
            "Cmd": [
                "/bin/sh",
                "-c",
                "#(nop) ",
                "LABEL organization=learning thoughts"
            ],
            "ArgsEscaped": true,
            "Image": "sha256:9932ce15331bc6adaef6ccf95a8ce0fdaa389714097b2adad4e68ee2dae76294",
            "Volumes": null,
            "WorkingDir": "",
            "Entrypoint": null,
            "OnBuild": null,
            "Labels": {
                "author": "khajaibrahim",
                "organization": "learning thoughts"
            }
        },
        "DockerVersion": "19.03.13",
        "Author": "",
        "Config": {
            "Hostname": "",
            "Domainname": "",
            "User": "",
            "AttachStdin": false,
            "AttachStdout": false,
            "AttachStderr": false,
            "Tty": false,
            "OpenStdin": false,
            "StdinOnce": false,
            "Env": [
                "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
            ],
            "Cmd": [
                "/hello"
            ],
            "ArgsEscaped": true,
            "Image": "sha256:9932ce15331bc6adaef6ccf95a8ce0fdaa389714097b2adad4e68ee2dae76294",
            "Volumes": null,
            "WorkingDir": "",
            "Entrypoint": null,
            "OnBuild": null,
            "Labels": {
                "author": "khajaibrahim",
                "organization": "learning thoughts"
            }
        },
        "Architecture": "amd64",
        "Os": "linux",
        "Size": 13336,
        "VirtualSize": 13336,
        "GraphDriver": {
            "Data": {
                "MergedDir": "/var/lib/docker/overlay2/cb7f79e0d542a174d630cb7517f46775b6e117f312c9e5b6a6aca1c469964a43/merged",
                "UpperDir": "/var/lib/docker/overlay2/cb7f79e0d542a174d630cb7517f46775b6e117f312c9e5b6a6aca1c469964a43/diff",
                "WorkDir": "/var/lib/docker/overlay2/cb7f79e0d542a174d630cb7517f46775b6e117f312c9e5b6a6aca1c469964a43/work"
            },
            "Name": "overlay2"
        },
        "RootFS": {
            "Type": "layers",
            "Layers": [
                "sha256:9c27e219663c25e0f28493790cc0b88bc973ba3b1686355f221c38a36978ac63"
            ]
        },
        "Metadata": {
            "LastTagTime": "2020-10-31T13:24:43.139925002Z"
        }
    }
]
  • Now lets try find history of image Preview

Lets try to build a gameoflife docker image

  • Select base image => tomcat with version 8 of java
  • download the file from url into webapps folder
  • This application runs on 8080 port
  • running this app is running tomcat
  • Now lets create a Dockerfile Refer Here for the changes Preview Preview
  • Lets try to create continer with gameoflife image docker container run -d -P gameoflife:1.0 Preview Preview

Lets try to build one more image for spring pet clinic

  • This application just requires java to be present.
  • Copy the jar file and when you want to run the application execute command java -jar <package>.jar
  • Over here we need to worry about how to start the application.
  • Now lets look at steps
    • Try to find a image with java 8 installed
    • Copy the jar file into some path
    • Expose port 8080
    • While starting container execute the command java -jar
  • Lets use FROM, LABEL instructions as usual for copying the jar file lets use wget instruction and for running the command during container starts lets use CMD instruction
  • Refer Here for the changes

From the above 3 examples (Lessons Learnt)

  • While building images We need to consider
    • picking right base image
    • exposing the ports which application uses
    • commands to install/configure the application
    • commands to start the application

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

About learningthoughtsadmin