Docker

How to Create File in Docker Container’s Shell

To develop software or any application, a Docker forum is used. This platform is specially designed for generating, testing, and deploying software with ease. Additionally, it is the standardized management service which is known as container. With the help of a container, users can perform different operations on the run time, such as creating, and modifying the file’s data and any more inside the container’s shell.

This tutorial will describe the method of generating files in a Docker container’s shell.

How to Create a File in Docker Container’s Shell?

To create a file in the Docker container’s shell, check out the below-provided procedure:

  • List the Docker images and choose one of them.
  • Generate a container by utilizing the docker run command.
  • Use the docker exec -it <container-name> command, and move inside the currently running container.
  • Make a new file using the echo command.

Step 1: List Images

Initially, list Docker images through the docker images command:

docker images

From the below-mentioned output, we have selected the test_img7 Docker images for further processing:

Step 2: Create Docker Container

Now, create a Docker container using the below-stated command:

docker run -d --name=cocoa_con test_img7:latest

Here:

  • -d option for running the container in the background.
  • –name option is used for adding the new container name.
  • cocoa_con is the container name.
  • test_img7 is out built Docker image.
  • latest is the image tag.

After executing the above-mentioned command, a new container has been created successfully:

Step 3: View Running Containers List

To check whether the container has been created or not, use the following command:

docker ps

As can be seen, the new container has been created and on a running state:

Step 4: Shelled to Container and Create File

Next, use the provided command and move inside the currently running container:

docker exec -it cocoa_con sh

After executing the above-stated command, you will be inside the particular running container. Then, use the echo command along with some content and file name with the extension. To exit the container, execute the exit command:

Step 5: Verification

To verify whether the particular file has been created or not, execute the docker exec command. After that, run the cat command along with the file name:

docker exec -it cocoa_con sh

cat file.txt

As you can see, the file has been created and exists inside the container:

Or

You can also verify the presence of the created file inside the container through the Docker Desktop. To do so:

  • Launch the Docker Desktop application.
  • Then, move to the Container tab.
  • After that, click on the name of the particular container name:

Next, move to the Files tab and locate the created file:

That’s all! You have learned about creating files inside the running Docker container’s shell.

Conclusion

To create a new file inside the running container’s shell, first, list the Docker images using the docker images command. Then, generate a Docker container. Next, use the docker exec -it <container-name> command, move inside the currently running container, and create a file with the help of the echo command. This guide described the easiest way to generate files in a Docker container’s shell.

About the author

Maria Naz

I hold a master's degree in computer science. I am passionate about my work, exploring new technologies, learning programming languages, and I love to share my knowledge with the world.