Docker

How to Use Tmpfs and its Function in Docker?

Tmpfs is a feature of Docker that allows users to create temporary file systems in memory or on disk that are not persisted after the container is stopped. Tmpfs mounts are useful for storing sensitive data that does not need to be shared with other containers or the host machine. They can also improve the performance of applications, enhance security, and reduce disk storage.

This article will demonstrate the use of tmpfs mount and its function in Docker.

How to Use Tmpfs and its Function in Docker?

The tmpfs mount can be used in a container using different flags, such as:

  • “–tmpfs” Flag
  • “–mount” Flag

Use Tmpfs Mount in a Container Through “–tmpfs” Flag

To create a tmpfs mount in a particular container, execute the below-provided command:

docker run -it --name cont1 --tmpfs /home -d nginx:latest

Here:

  • -it” flag is utilized to interact with the container’s command prompt.
  • –name” option defines a container name.
  • cont1” is the container’s name.
  • –tmpfs /home” flag mounts a “tmpfs” file system at the “home” directory inside the container.
  • -d” option runs a container in a detached mode.
  • nginx:latest” is the Docker image to use for the container:

This command has created a “tmpfs” mount at the “/home” directory inside the “cont1” container.

Verification
Run the following command to verify whether the container has been created using a “tmpfs” mount or not:

docker ps -a

The “cont1” container has been created successfully using the “–tmpfs” flag.

Use Tmpfs Mount in a Container Through “–mount” Flag

To create a tmpfs mount in a container, use the “–mount” flag and specify the type and destination:

docker run -it --name cont2 --mount type=tmpfs,destination=/home -d nginx:latest

Here the “–mount type=tmpfs,destination=/home” option mounts a temporary file system “tmpfs” to the “/home” directory inside the container:

This command has created a “tmpfs” mount at the “/home” directory inside the “cont2” container.

Verification
Execute the provided command to ensure that the container has been created using the “–mount” flag:

docker ps -a

According to the above output, the “cont2” container has been created successfully using the “–mount” flag.

Conclusion

In Docker, the tmpfs function allows users to create a temporary file system that resides in memory instead of on disk. It helps in improving the performance, security, and efficiency of Docker containers. The tmpfs mount can be used in a particular container using the “–tmpfs” flag or the “–mount” flag along with the “type=tmpfs” and “destination” options. This write-up explained the usage and function of tmpfs mount in Docker.

About the author

Laiba Younas

I have done bachelors in Computer Science. Being passionate about learning new technologies, I am interested in exploring different programming languages and sharing my experience with the world.