Docker

Condition in Dockerfile

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.

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.

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

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

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.

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.

$ sudo docker ps -a

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

$ touch test.sh

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.

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.

$ ls
$ mkdir docker
$ ls

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

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.

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

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

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.

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

$ sudo docker images

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

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

Kalsoom Bibi

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