| by Arround The Web | No comments

YAML Comments

In this article, we will be discussing the concept of creating comments in a YAML file. YAML allows single-line comments with the help of the “#” character. We can use the single-line comment multiple times to make a block comment paragraph in the YAML file. Now, a YAML file is a configuration support file for storing data for other programs like Ruby and Dockers. For this article, we used the YAML file as a support file for Dockers. The Docker-compose supports files with a “.yml” file. In these files, the configuration of a Docker image file is stored. The pre-requisite for the implementation is to install Dockers and Docker-compose through the Ubuntu terminal.

Example # 01: Single-Line Comment in a YAML File in Ubuntu 20.04

In this instance, we will form a YAML file with a single-line comment. We will be making a compose file for the docker image file in the “.yml” format. This file will be created in a directory of our Ubuntu operating system.

We will start with creating a directory on our desktop using the “mkdir” command. In this illustration, we will be naming our script “app1”. In this directory, we will make a Docker image file and a Docker-compose YML file. We will use the following commands to create a directory and then open a compose file in a similar directory:

The “mkdir” instruction will create a directory, and the cd instruction will direct us into the directory. To generate a YAML file in the same directory, we’ll use the “nano compose.yml” command. After this, if we locate and open our directory, we will see the files we created in our current directory, as shown below:

We can open the file on the terminal and in the text editor, where we can edit the file. In this example, we have used the YAML file as the compose support file for the Docker image file, and the script used in the YAML file defines the general flow of the Docker file.

As we can see in the previous snippet, the file has several different definitions of variables like version, services, and other configurational files. In the tenth line, we have added a single line comment using the “#” character, and this comment is used to define the network property of the Docker image.

Example # 02: Multiline Comment in a YAML File in Ubuntu 20.04

We’ll make a YAML file with a multiline comment in this example. We will create a compose file in the “.yml” format for the Docker image file. This file will be generated in the Ubuntu operating system’s directory.

We will create a directory on our desktop using the “mkdir” command. In this example, we’ll call our script “app1.” We will form a Docker image file and a Docker-compose YML file in this relevant directory. The following instructions will be used to make a directory and open a compose file in a similar directory:

The “mkdir” instruction creates a directory, and the “cd” instruction navigates to it. We’ll use the “nano compose.yml” command to create a YAML file in the same directory. After that, if we find and open our directory, we will see that the files we generated are still present, as seen below:

In this case, the YAML file was used as the compose support file for the Docker image file, and the script in the YAML file defined the overall flow of the Docker file. The file will open directly on the terminal, and we can also edit the YAML file in the text editor.

As seen in the previous clip, the file contains multiple distinct definitions of variables, such as version, services, and other configuration files. We’ve inserted many lines as comments using the “#” character in the tenth line, and these comments are used to describe the network attribute of the docker image.

Example # 03: Single and Multiline Comments in the Same YAML File in Ubuntu 20.04

In this example, we’ll create a YAML file with a multiline and a single line distinct comment. For the docker image file, we will build a compose file in “.yml” format. This file will be produced in the directory of the Ubuntu operating system.

We’ll first use the “mkdir” command to create a directory on our desktop. In this example, we’ll refer to our script as “app1.” In this directory, we will build a Docker image file and a Docker- compose YML file. The following steps will be used to make a directory and open a compose file in the same directory.

The command “mkdir” creates a directory, and the command “cd” directs it. To generate a YAML file in the same folder, we’ll use the “nano compose.yml” command. If we locate and open our directory, we will notice that the files we made are still present, as shown below:

The YAML file was utilized as the compose support file for the docker image file in this example, and the script in the YAML file established the general flow of the Docker file. The file will open instantly in the terminal, but we can modify it in the text editor.

The file contains numerous separate definitions of variables, such as version, services, and other configuration files, as seen in the previous clip. We’ve used the “#” character to insert multiple lines as comments in the tenth line, and these comments are used to specify the network property of the docker image. Following that, we used the same method to add a single line remark to the definition of volume variable.

Conclusion

In this tutorial, we have discussed the concept of comments in YAML. We used the conventional method to add comments in a YAML file using the “#” character. As YAML allows single-line comments, we used it to imitate a block comment. Here, we used multiple single-line comments together and created a block comment. We implemented this concept in the Ubuntu 20.04 environment.

Share Button

Source: linuxhint.com

Leave a Reply