1

0

I am trying to run this command and getting an error:

docker exec 19eca917c3e2 cat "Hi there" > /usr/share/ngnix/html/system.txt

/usr/share/ngnix/html/system.txt: No such file or directory

A very simple command to create a file and write in it, I tried echo and that one too didn't work.

1
  • (Do you mean to use a Dockerfile COPY directive to create the file in a reusable image?)
    – David Maze
    Jun 5, 2021 at 1:33

2 Answers 2

2

The cat command only works on files, so cat "Hi there" is incorrect.

Try echo "Hi there" to output this to standard out.

You are then piping the output to /usr/share/ngnix/html/system.txt. Make sure the directory /usr/share/ngnix/html/ exists. If not create it using

mkdir -p /usr/share/ngnix/html
0

I presume you are trying to create the file in the container.

You have several problems going on, one of which @Yatharth Ranjan has addressed - you want echo not cat for that use.

The other is, your call is being parsed by the local shell, which is breaking it up into docker ... "hello world" and a > ... system.txt on your host system.

To get the pipe into file to be executed in the container, you need to explicity invoke bash in the container, and then pass it the command:

docker exec 12345 /bin/sh -c "echo \"hello world\" > /usr/somefile.txt"

So, here you would call /bin/sh in the container, pass it -c to tell it a shell command follows, and then the command to parse and execute is your echo "hello world" > the_file.txt.

Of course, a far easier way to copy files into a container is to have them on your host system and then copy them in using docker cp: (where 0123abc is your container name or id)

docker cp ./some-file.txt 01234abc:/path/to/file/in/container.txt

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.