1

Flow of the python script:

  • I want to run docker image from python script.
  • After running docker image, I need to execute a shell script which creates a tar file inside docker container.
  • I need to copy that tar file to host machine from docker container.
  • and then python script should continue with some stuff to be executed on host machine.

Using docker-py module, I was able to do following:

 pip install docker-py

    docker version
    Client:
     Version:      1.12.1
     API version:  1.24
     Go version:   go1.6.3
     Git commit:   23cf638
     Built:        Thu Aug 18 05:22:43 2016
     OS/Arch:      linux/amd64

    Server:
     Version:      1.12.1
     API version:  1.24
     Go version:   go1.6.3
     Git commit:   23cf638
     Built:        Thu Aug 18 05:22:43 2016
     OS/Arch:      linux/amd64

    >>> import docker

    >>> c = docker.Client(base_url='unix://var/run/docker.sock',version='1.12',timeout=10)

    >>> c.images()
    [{u'Created': 1476217543, u'Labels': {}, u'VirtualSize': 5712315133, u'ParentId': u'sha256:1ba2be8d70b6ede3b68b1af50759e674345236dd952225fcbfbcc1781f370252', u'RepoTags': [u'ubuntu14.04_64:latest'], u'RepoDigests': None, u'Id': u'sha256:1c8ced0fb34d776adafaed938d41a69e3bab87466beaa8752d49bde0d81230c5', u'Size': 5712315133}]

    >>> ctr = c.create_container('ubuntu14.04_64:latest') 

    >>> c.start(ctr)

    docker  ps
    CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
    d685766385e7        ubuntu14.04_64:latest              "bash"              16 hours ago        Up 16 hours                             focused_fermi

    docker images
    REPOSITORY                   TAG                 IMAGE ID            CREATED             SIZE
    ubuntu14.04_64               latest              1c8ced0fb34d        21 hours ago        5.712 GB

I see docker image and container running on host, but now if I want to run shell script inside docker container, how can I do that? after that I need to copy tar from container to host also. Can someone suggest how to do this?

2 Answers2

1

This is probably the sequence of commands you want (borrowing from this answer):

docker create --name tmp -it ubuntu14.04_64:latest
docker start tmp
docker exec tmp tar czvf tmp.tgz etc/os-release
docker stop tmp
docker cp tmp:tmp.tgz tmp.tgz
docker rm tmp

Look through this documentation for the equivalent commands with docker-py.

Community
  • 1
  • 1
Luke Yeager
  • 1,400
  • 1
  • 17
  • 30
  • I have gone through the documentation but couldn't find how can run shell script from container and copy tar from container to host using python. –  Oct 12 '16 at 20:15
  • Using exec shouldn't be necessary here, just set the CMD to the script you want to run. – dnephin Oct 13 '16 at 19:14
1

If you want to run a script in a container you should create a Dockerfile which contains that script. An example might look something like this:

FROM ubuntu14.04_64:latest
COPY ./script.sh /code/script.sh
CMD /code/script.sh -o /target/output.tar.gz

Then your python script would look something like this:

#!/usr/bin/env python
import docker

c = docker.from_env()
c.build('.', image_name)
ctr = c.create_container(image_name, volumes='./target:/target')
c.start(ctr)

# the tarball is now at ./target/output.tar.gz, copy it where you want it
dnephin
  • 25,944
  • 9
  • 55
  • 45
  • Thanks for the suggestion, but I need to mount a directory to container from host, so I can't use "CMD" to run script while building docker, I need to run the script while running docker. I tried setting entrypoint in my Dockerfile, I changed workdir in Dockerfile, even though my script is in same path as workdir, I am getting error : no such file or directory. –  Oct 13 '16 at 19:23
  • If somehow I am able to run script by keeping it as an entrypoint in Dockerfile, I will be able to generate tar file in container. –  Oct 13 '16 at 19:31
  • How can I copy tar file which gets generated inside container using python script? –  Oct 13 '16 at 19:31
  • 1
    `CMD` works like `ENTRYPOINT`, it runs when the container starts, not during build. Don't worry about trying to copy the file out. Use a "host bind mount" (aka bind volume) so that the `/target` path in the container is actually a directory on the host. – dnephin Oct 13 '16 at 19:33
  • I have added "CMD ./mydir/myshell.sh /repo" in my CMD where "/repo" is mounted from host to container. I ran docker, but dint see tar file getting created from above CMD. This script takes around 3 minutes to generate tar, do I need to include any other commands in Dockerfile? –  Oct 13 '16 at 19:38
  • You can print the logs to see what's going on, see https://docker-py.readthedocs.io/en/stable/api/#logs – dnephin Oct 13 '16 at 19:39
  • I want to try this out manually first, I have kept this in my Dockerfile "CMD ./mydir/myshell.sh /repo | tee /logs/my-install-cmd.log" but not sure where to check this logs? –  Oct 13 '16 at 19:52
  • Thank you for all the suggestions! –  Oct 13 '16 at 22:33