31

This question is coming from an issue on the Docker's repository:
https://github.com/docker/compose/issues/942

I can't figure it out how to create a data container (no process running) with docker compose.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
ivan
  • 550
  • 2
  • 5
  • 12
  • As of Docker 1.9, data-only containers can replaced with named containers https://github.com/docker/docker/issues/17798 – Peeter Kokk Jul 12 '16 at 10:24
  • In case you want to use named volumes to specify host mount point, see this: https://stackoverflow.com/questions/35841241/docker-compose-named-mounted-volume – Peeter Kokk Jul 12 '16 at 12:47

4 Answers4

24

UPDATE: Things have changed in the last years. Please refer to the answer from @Frederik Wendt for a good and up-to-date solution.

My old answer: Exactly how to do it depends a little on what image you are using for your data-only-container. If your image has an entrypoint, you need to overwrite this in your docker-compose.yml. For example this is a solution for the official MySql image from docker hub:

DatabaseData:
  image: mysql:5.6.25
  entrypoint: /bin/bash

DatabaseServer:
  image: mysql:5.6.25
  volumes_from:
    - DatabaseData
  environment:
    MYSQL_ROOT_PASSWORD: blabla

When you do a docker-compose up on this, you will get a container like ..._DatabaseData_1 which shows a status of Exited when you call docker ps -a. Further investigation with docker inspect will show, that it has a timestamp of 0. That means the container was never run. Like it is stated by the owner of docker compose here.

Now, as long as you don't do a docker-compose rm -v, your data only container (..._DatabaseData_1) will not loose its data. So you can do docker-compose stop and docker-compose up as often as you like.

In case you like to use a dedicated data-only image like tianon/true this works the same. Here you don't need to overwrite the entrypoint, because it doesn't exist. It seems like there are some problems with that image and docker compose. I haven't tried it, but this article could be worth reading in case you experience any problems.

In general it seems to be a good idea to use the same image for your data-only container that you are using for the container accessing it. See Data-only container madness for more details.

Jan Suchotzki
  • 1,406
  • 12
  • 24
  • I don't understand. Volume is a directory in the host filesystem that gets mapped into a container, right? Where is the volume here? – CrabMan Dec 21 '15 at 18:19
  • 1
    @crabman the [MySQL image](https://github.com/docker-library/mysql/blob/2e80e5ff6aa7d3a09723ad40f5954a0563dbac29/5.7/Dockerfile) has a volume specified. [volumes_from](https://docs.docker.com/compose/compose-file/) tells docker to map that volume to the DatabaseData container instead. – JayChase Jan 26 '16 at 03:34
  • @CrabMa, look inside the container which has volumes_from option (in your case Mysql container) / run # docker mysql_container inspect and see block Mounts [{Source: ""}]. "source" it is your path on host to container data – yuklia May 09 '16 at 18:27
3

The other answers to this question are quite out of date, and data volumes have been supported for some time now. Example:

version: "3.9"
services:
  frontend:
    image: node:lts
    volumes:
      - myapp:/home/node/app
volumes:
  myapp:

See https://docs.docker.com/storage/volumes/#use-a-volume-with-docker-compose for details and options.

Fredrik Wendt
  • 988
  • 1
  • 10
  • 17
2

A data only container (DOC) is a container that is created only to serve as a volume provider. The container itself has no function other than that other containers can mount it's volume by using the volumes_from directive.

The DOC has to run only once to create the volume. Other containers can reference the volumes in it even if it's stopped.

The OP Question: The docker-compose.yml starts the DOC every time you do a docker-compose up. OP asks for an option to only create container and volume, and not run it, using some sort of an create_only: true option.

Philipp Kyeck
  • 18,402
  • 15
  • 86
  • 123
Rob van Laarhoven
  • 8,737
  • 2
  • 31
  • 49
0

As mention in the issue from the OP's question:

  • you either create a data container with the same name as the one specified in the docker-compose.yml, and run docker-compose up --no-recreate (the one specified in docker-compose.yml won't be recreated).
  • or you run a container with a simple command which never returns.
    Like: tail -f /dev/null
Philipp Kyeck
  • 18,402
  • 15
  • 86
  • 123
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250