I have the following code
import subprocess
import docker
import unittest
class DockerContainerHost(object):
def execute(self, command):
self._container.exec_run(command)
class Tester(unittest.TestCase):
def test_twoHostsOneFile(self):
with DockerContainerHost() as host1, DockerContainerHost() as host2:
host1.execute('echo content > /tmp/foo')
if __name__ == '__main__':
unittest.main()
The thing is that everything works fine, but the host1.execute('/bin/echo content > /tmp/foo')
line doesn't create the file /tmp/foo
. When I tried to use touch /tmp/foo
command it worked, but I need to create the file with content. How to make it work? Thanks.
CodePudding user response:
Python and docker exec
are not running a shell, and the redirect in 'echo content > /tmp/foo'
is expecting a shell to parse the >
. You can specify running this with a shell using the following syntax:
'/bin/sh -c \'echo content > /tmp/foo\''
Note that this will parse the command inside the container, so the file will be created inside that container.