Table of contents
1.
Introduction
2.
Salt States and Commands in Docker Container
2.1.
Step 1 - Installation of Docker
2.2.
Step 2 - Installation of Salt
2.3.
Step 3 - Creation using Salt States
2.4.
Step 4 - Executing Command in the Container
3.
Frequently Asked Questions
3.1.
What is Jinja in DBT?
3.2.
Is Jinja2 a library?
3.3.
What is the difference between Jinja and Jinja2?
3.4.
What is Jinja salt?
3.5.
What is Salt software used for?
4.
Conclusion
Last Updated: Mar 27, 2024
Medium

Running Salt States And Commands In Docker Containers

Author Amit Singh
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Have you worked on the deployment of complex IT Systems? Have you ever worked on the automation of the configuration of IT Systems?

running salt states and commands

This article focuses on one of the automation engines, i.e., Salt Project. We will study the Salt States and Commands in detail. We will try to understand how to use the Salt states and the Salt States inside the Docker container as well.

Salt States and Commands in Docker Container

The Salt version 2016.11.0 was an essential update in the Salt. 

  • It introduced the feature of executing Salt States. It also provides the feature of Salt remote execution commands which can be done directly inside the container of the Docker. 
     
  • With this addition, you can now use the Salt States to deploy containers other than new ones. This feature allows Salt to audit and modify running containers without having to run a Salt Minion within the container. It has many applications, like getting operating data from containers and doing security audits on active containers.
     
  • This feature is quite simple and straightforward. You can use this feature using a running Salt Minion, Salt SSH, or the Salt Call Command. In this article, we will use the Salt Call command. This is because you can translate all the salt commands directly to salt-ssh and salt. 
     

Now, let's see how to use this feature in more detail. 

Step 1 - Installation of Docker

The first step is crucial step when you want to work on Docker. You will have to install Docker to use its features. You can download the setup from the official site. After the proper download, you can install it.

After the complete installation, you must have the docker-py library as well. You must have the docker-py library in case you want to work with the Docker Integration. You can do it by using pip or using your system package manager. You can use the following command:

pip install docker-py

Step 2 - Installation of Salt

The next is also the most crucial step. The second step is to download and install the Salt.

We will work with the Salt Call. The Salt Call is available in the salt-minion package of salt.

Step 3 - Creation using Salt States

In the third step, we will work with the Salt states, and for that, we will need them. 

For example, here, we can use a very basic state which will be responsible for the installation of vim. 

We can do anything that the Salt states can do. 

Let's try to understand this with an example. 

In this example, we will simply create a small state file first. You must create this file in /srv/salt/vim.sls.

The next step is to run the docker.sls_build command. For this, execute the following command:

salt-call --local dockerng.sls_build test_cn base=my_base_image mods=vim


After the above steps, we will have a fresh image named test_cn. We can work with this image. And vim has been installed. 

Step 4 - Executing Command in the Container

Now, you can use Salt to run the remote execution functions. You can use these functions in the container. In case you want to run the function, you can use a different simple salt-call command. You can use the following commands:

salt-call --local dockerng.call test_cn test_cn.version
salt-call --local dockerng.call test_cn network.interfaces
salt-call --local dockerng.call test_cn disk.usage
salt-call --local dockerng.call test_cn pkg.list_pkgs
salt-call --local dockerng.call test_cn service.running httpd
salt-call --local dockerng.call test_cn cmd.run 'ls -l /etc'

Frequently Asked Questions

What is Jinja in DBT?

In the data build tool (dbt), SQL can be combined with Jinja. Jinja is a templating language. Your dbt project into a programming environment using Jinja for SQL. It allows you to do things that aren't usually possible in SQL. For example, with Jinja, you can use control statements in SQL.

Is Jinja2 a library?

Jinja is a library for Python. It is designed to be fast, flexible, and secure. Suppose you have worked with other text-based template languages, such as Django or Smarty. Then you will find it super easy to work with.

What is the difference between Jinja and Jinja2?

Jinja 2 has a similar syntax as Jinja 1. The difference is that around the argument list, macros now require parentheses. Also, Jinja 2 allows dynamic inheritance and dynamic includes. The old helper function rendertemplate is invalid now and included used instead.

What is Jinja salt?

Jinja is the default templating language. Jinja supports a secure, sandboxed template execution environment. It is the advantage used by Salt. Salt highly recommends the usage of jinja/jinja |yaml files.

What is Salt software used for?

SALT stands for Systematic Analysis of Language Transcripts. It is software that standardizes the process of transcribing, eliciting, and analyzing language samples. It includes standard reports, a transcription editor, and reference databases for comparison with typical peers.

Conclusion

In this article, we have studied one of the automation engines, i.e., Salt Project. We have studied the Salt States and Commands in detail. We have also tried to understand how to use the Salt states and the Salt States inside the Docker container as well.

We hope that this article has provided you with the help to enhance your knowledge regarding the Salt Project and if you would like to learn more, check out our articles on salt-mine and salt-event-system.

Refer to our guided paths on Coding Ninjas Studio to learn more about DSA, Competitive Programming, JavaScript, System Design, etc. Enroll in our courses and refer to the mock test and problems available; take a look at the interview experiences and interview bundle for placement preparations.

Do upvote our blog to help other ninjas grow.

Merry Learning!

Live masterclass