In daily life, you may have encountered many situations where you get confused about choosing different options and opportunities. In that situation, you may think about the circumstances of choosing one and the result of the other; that would be like the “-if-else” statement used in many programming languages. The docker platform of C# also allows us to use such conditions in a docker file. This can only be done by creating symlinks in docker via the docker images.

We have written this article for all our new users who doesn’t know anything about using conditions in the docker file.

So, let’s get started.

Open the shell application of Ubuntu 20.04 first. If your system has some old version of docker already installed on it, then you have to make sure that it must be removed. To remove it from our Ubuntu 20.04 Linux system, we have been utilizing Ubuntu’s apt package in the “remove” instruction. On execution of this query, the Ubuntu system has started getting information about the docker.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/04/echo/condition-in-dockerfile-01.png" data-lazy- height="48" src="data:image/svg xml,” width=”622″>

After getting all the required information about docker packages, it has been showing us the amount of disk space it will release after the removal of those packages. Along with that, it requires your affirmation for the removal process of docker to avoid any inconvenience in the near future. You just need to tap “Y” to continue this process.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/04/echo/condition-in-dockerfile-02.png" data-lazy- height="40" src="data:image/svg xml,” width=”492″>

The old docker will be removed completely and your system needs to get updated now. To update and upgrade, Linux has provided us with the “apt” package in this regard. Thus, we have utilized it on our shell with the “update” and “upgrade” instructions shown in the below image. Our system got updated and upgraded in a while.

$ sudo apt-get update && sudo apt-get upgrade

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/04/echo/condition-in-dockerfile-03.png" data-lazy- height="36" src="data:image/svg xml,” width=”624″>

Now, you can use the snap package or apt package to install the newest version of the docker input and output interface. We have been using the apt package here to install it using the “install” instruction followed by the “docker.io” package name.

$ sudo apt-get install docker.io

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/04/echo/condition-in-dockerfile-04.png" data-lazy- height="38" src="data:image/svg xml,” width=”490″>

The command got executed and it started getting information regarding docker from in and out. Within its processing, it will asks you to affirm the installation process on Ubuntu 20.04. You have to press “Y” to continue installing it other press “n” to stop it as shown in the image.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/04/echo/condition-in-dockerfile-05.png" data-lazy- height="34" src="data:image/svg xml,” width=”520″>

After the docker has been installed, we have loaded all the images of docker in our system using the “docker pull” instruction that has not been mentioned to prevent complexities. We executed the images to create containers and this is what we got after using the “docker ps –a” instruction on the shell. It shows that three containers are available for use.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/04/echo/condition-in-dockerfile-06.png" data-lazy- height="136" src="data:image/svg xml,” width=”596″>

You need to create a simple bash file via the “touch” instruction below.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/04/echo/condition-in-dockerfile-07.png" data-lazy- height="20" src="data:image/svg xml,” width=”332″>

Add some if-then bash statement in it that runs some echo statement using the “CMD” keyword after authenticating the user. Save your file for execution.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/04/echo/condition-in-dockerfile-08.png" data-lazy- height="92" src="data:image/svg xml,” width=”554″>

Before using the containers, we will be creating symlinks using the docker file. We have been creating a new directory “docker” in our main home folder of the system using the “mkdir” instruction shown in the image. The list command has been used to display the before and after results of using the “mkdir” on the shell. The directory “docker” has been created.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/04/echo/condition-in-dockerfile-09.png" data-lazy- height="128" src="data:image/svg xml,” width=”468″>

We have been moving within the newly created directory “mkdir” using the “cd” instruction and creating a new docker file named “dockerfile” with the use of the “touch” instruction. We have been opening the newly made docker file in the “Vim” editor to create some symlink.

$ cd docker


$ ls


$ vim docker

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/04/echo/condition-in-dockerfile-10.png" data-lazy- height="76" src="data:image/svg xml,” width=”388″>

You have to add the shown below lines on the docker file as the execution data. It contains the main source i.e. “FROM Ubuntu”, the maintainers “simpli” which will be a symlink in our case, and the “RUN” variable shows the command to be executed. It contains a simple “if-then” statement to run the “test.sh” file.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/04/echo/condition-in-dockerfile-11.png" data-lazy- height="144" src="data:image/svg xml,” width=”468″>

Save this file and quit the editor. Now, you need to build the symlink using the “docker build” instruction along with the path to the docker file. The symlink has been ready for building.

$ sudo docker build /home/kalsoom/docker

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/04/echo/condition-in-dockerfile-12.png" data-lazy- height="184" src="data:image/svg xml,” width=”548″>

After a while, the symlink will be built using the docker file.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/04/echo/condition-in-dockerfile-13.png" data-lazy- height="136" src="data:image/svg xml,” width=”504″>

Now, it’s time to link this symlink with a symlink image. For that, you need to use the docker build command along with the path to the file followed by the “-t” flag and the name of an image to be created (i.e. “simpli_image”). Using this instruction, the symlink docker file has been successfully linked to a symlink image.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/04/echo/condition-in-dockerfile-14.png" data-lazy- height="246" src="data:image/svg xml,” width=”624″>

Now, when we check for the images using the docker images instruction, we got the new image listed at the top.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/04/echo/condition-in-dockerfile-15.png" data-lazy- height="144" src="data:image/svg xml,” width=”576″>

On running the “docker run” instruction with the image “simpli_image”, the line in the “test.sh” file got executed. This is because of the use of symlink used in the docker file.

$ sudo docker run simpli_image

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/04/echo/condition-in-dockerfile-16.png" data-lazy- height="38" src="data:image/svg xml,” width=”468″>

Conclusion

This is about the use of conditions in a docker file of the docker platform. We have started with the installation of docker on Ubuntu 20.04. After that, we have created a docker file and added a symlink to it. The “if-then” condition has been used in the docker file and the bash file. Upon running the symlink built by a docker file, we have the message displayed that is a text of bash file while the dicker file symlink has been executed.

About the author

<img alt="" data-lazy-src="https://secure.gravatar.com/avatar/d014e3711df41253029f4d4199698df8?s=112&r=g" data-lazy- height="112" src="data:image/svg xml,” width=”112″>

Kalsoom Bibi

Hello, I am a freelance writer and usually write for Linux and other technology related content