Configuration Management With Docker

Configuration Management With Docker

There are two Important things to keep in mind:-

  • Container Will always launch from the Image

  • Whatever is inside the image, will also be in the container.

There are three ways of Configuration Management:-

  • Manual

  • Automation

  • Pre-Backed or Custom

In the Manual, we Create the container from the image by ourselves, completely human & it takes so much time.


In Automation We have two ways:-

Imperative:-

  • What to do

  • How to do

  • eg. Shell Scripting

Declarative:-

  • Only What to do

  • eg. Ansible


In Pre-Backed or custom images we have:-

  • Commit ( Looks Like Manual Way )

  • Dockerfile ( It is Automated Way )

Challenges in both Manual & Automation:-

- Installing Software & Libraries will take so much time.


Why We Need Custom Docker Images:-

  • If we want the same configurations in a container again & again

  • We want to save the time

  • Where you see a pattern


Custom Images:-

Commit:-

Step 1:- Create a container

docker run -it --name=cos1 redhat/ubi8

Step 2:- Install things you want in your image

yum install net-tools -y
yum install python39

Step 3:- Commit that container to a custom image, with this format

docker commit “container in which you did configurations” “New image name which you want to create”:version or tag

docker commit cos1 abc:v1

Step 4:- Run a container from that image

docker run -it abc:v1

In this container, we will be having Python & ifconfig commands pre-installed.

Disadvantages of Commit:-

  1. It Is Manual.

  2. The image will be updated in the future, so again we have to do everything from scratch

  3. In the future, we want the latest version of Python ( py 3.5 → py3.9 )


Dockerfile:-

Prerequisites:-

  1. You Should Know the requirement

  2. You Should know how to solve it ( The Steps )

  3. Create Custom images in an automated way.

Now Repeating The same steps of commit:-

Step 1:- Mention from which image you want to run the container

FROM redhat/ubi8
# docker run -it redhat/ubi8

Step 2:- Install Things you want in your Custom Image

RUN yum install python39
RUN pip3 install flask

Step 3:- Change the Working Directory ( all code will be stored & run in this folder )

WORKDIR /code

Step 4:- Copy the program file from the docker host to the image or from the source to the destination

COPY app.py /code/app.py
# app.py and Dockerfile should be in same folder or you define the path of app.py
# /code is already defined in WORKDIR, that's why we write only /app.py

Step 5:- Run the app.py file inside the container.

CMD ["python3", "app.py"]
# ["executable", "param1", "param2"..]

Now Save & Exit, and Dockerfile is created & you can create an image from this file with your desired configurations.

This is the Final Dockerfile:-

FROM redhat/ubi8
RUN yum install python39 -y
RUN pip3 install flask
WORKDIR /code
COPY app.py app.py
CMD ["python3", "app.py"]

Run the Dockerfile:-

docker build -t lwxyz:v1 .
# -t=target  #lwxyz:v1=image name you want to give 
# . means the location of folder where the Dockerfile is

You have to use the non-interactive command in Dockerfile if you want Automation.

RUN command runs at Buildtime & CMD Command runs at Runtime, What does that mean?

Whatever You want to put inside the image like configurations, code, and settings, use the RUN command,

Whatever you want to put inside the container, The commands you want to run automatically after the container launch. Then use the CMD command.

We can’t run more than one CMD command in one Dockerfile, because we follow 1 O.S for one App

If We write 2 CMD then CMD’2 will overwrite the CMD’1 & CMD’1 will not run only 2 will.