Xavi Montero
Xavi Montero

Reputation: 10664

"docker container create" with "nothing" inside. Ie: An empty / void / null / scratch container

It might be helpful to create a container that has nothing. Nothing means nothing.

Use case

In this answer https://stackoverflow.com/a/55683656/1315009 a container is created without ever starting it just to instantiate a volume and copy contents into the volume. Then the container is removed.

The example instantiates a busybox. Nevertheless the busybox contents are never used. So I tested it with the hello-world and it works as well, thus reducing from 1.22MB to 13.3kB.

At the time being, pulling "scratch" fails:

$ docker pull scratch
Using default tag: latest
Error response from daemon: 'scratch' is a reserved name

So... Question

How can I create a container with an image that has "nothing" inside?

I mean similar to docker create hello-world but without the hello-world binary.

Upvotes: 0

Views: 1036

Answers (2)

Xavi Montero
Xavi Montero

Reputation: 10664

Inspired on @BMitch's answer, the solution is that the Dockerfile can not only contain the FROM sentence.

It compiles, but fails when doing a docker create, the engine complains because it does not have a command even if it's a create and not a run.

The complete Dockerfile is this one:

FROM scratch
CMD ""

You can build it (in my case I named the image xavi-scratch) and then see the image is 0 bytes of data:

$ docker image ls | grep xavi-scratch
xavi-scratch    latest    bee1419a6d83    N/A    0B

image contains 0 bytes of data

I then create a container without running it:

docker create -v ${PWD}/whatever-dir:/data --name test-scratch xavi-scratch

There's a full session here testing it serves the purpose:

  1. Setup 2 empty dirs a and b and create one file in a.
  2. Check b is empty
  3. Create this container from the empty image, mounting b.
  4. Copy the file from a into the container
  5. Check b contains the file copied via the container.

Test

Upvotes: 0

BMitch
BMitch

Reputation: 263479

A container is defined from an image and includes the command to run inside the container. A container that is just scratch has no commands to run and is therefore not a properly defined container. You can build an image that is nothing more than:

FROM scratch

However, you will quickly run out of things to do with the resulting image. If you need to manage a named volume, that volume will be attached to a container, and that container will use an image, so it's easiest to use that image directly.

Upvotes: 1

Related Questions