2

I've build a docker container from a dockerscript, e.g.

user@servername:~$ echo "FROM ubuntu:latest" > dscript.txt
user@servername:~$ 
user@servername:~$ cat dscript.txt 
FROM ubuntu:latest
user@servername:~$ docker build -t ubu.image - < dscript.txt
Sending build context to Docker daemon  2.048kB
Step 1/1 : FROM ubuntu:latest
 ---> 0ef2e08ed3fa
Successfully built 0ef2e08ed3fa

And I can run it by using the image name I've given, i.e. ubu.image:

user@servername:~$ docker run -it ubu.image
root@c00379445b9d:/# exit
exit
user@servername:~$ docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
user@servername:~$ sudo docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES

But to export it I have to use the commit hash:

user@servername:~$ docker export c00379445b9d > ubu.image.tar

I've tried using the container name instead of the hash but it throws an error:

$ docker export ubu.image > ubu.image.tar
Error response from daemon: No such container: ubu.image

The question is:

  1. Is there a way to find out the latest commit hash of the container that's needed to export the container as a .tar file?

  2. Without the latest commit hash, is it possible to export the container as .tar file? Is so, how?

1 Answer 1

2

The ubu.image you used is image name, not container name, so you can't use it in argument to docker export.

If you know the container name, then you can use it directly to export:

docker export <container name>

or getting its hash:

docker ps -aqf 'name=<container name>'

Without container name, you can get all list of containers with the given image name:

docker ps -aqf 'ancestor=<image name>'

Beware that it will return all containers built from the given image.


A better approach, is setting your own container label, so that you can use it to filter the container:

$ docker run -l foo=bar alpine sh -c 'echo 1'
1
$ docker run -l foo=baz alpine sh -c 'echo 1'
1
$ docker ps -aqf 'label=foo=bar'
d43aa7d7c854
7
  • To get the container name, do I need to first commit the image and the push to docker hub?
    – alvas
    Commented Apr 7, 2017 at 9:36
  • Ah the label trick is good =)
    – alvas
    Commented Apr 7, 2017 at 9:36
  • Hmmm, the --label trick didn't work on docker build. Does docker ps -aqf work when you use docker build instead of docker run? It returned nothing for me =(
    – alvas
    Commented Apr 7, 2017 at 9:52
  • @alvas you misunderstood the concept of image and container. docker ps only list and filter containers, it has nothing to do with image. You build the image, the run a container base on that image. The docker export is also container operation, not image.
    – cuonglm
    Commented Apr 7, 2017 at 9:56
  • 1
    @alvas you can filter image by label
    – cuonglm
    Commented Apr 7, 2017 at 10:12

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .