Migrating existing Docker containers to Docker Compose

To re-use the existing network you can use

networks:
  default:
    external:
      name: simple-network

To re-use the volumes, it depends on what kind of volumes they are. If they are named volumes you can do something similar:

volumes:
  data:
    external:
      name: the_name_of_the_volume

You would then use volumes: [data] in a service to use it.

If they are host volumes then it is really easy, just use the same config.

If they are unnamed volumes you can use the volume id as an external volume (the same way you would use a named volume).

Re-using the containers isn't going to be possible. You can have Compose take over containers created by docker, but they need to have the correct labels on them. The easiest way to find the labels is to docker inspect a container created by compose to see the key/values. Since you have to re-create a container anyway to apply labels, it's probably easier to just stop them and up compose.


Update to part of dnephin's example of reusing named volumes, in case it helps the next person:

It looks like in the volumes section of docker-compose.yml you aren't supposed to use name: name_of_the_volume anymore, just define them using the name e.g.

volumes:
  app-mongo-data:
    external: true
  app-mongo-config:
    external: true

then reference them in your services by (in this example) app-mongo-data or app-mongo-config like this:

version:'3'
services:
    app-db:
        image: mongo:latest
        expose:
          - "27017"
        volumes:
          - app-mongo-data:/data/db
          - app-mongo-config:/data/configdb

Might help with the networking side of the question (though not what was asked): the docker-compose.yml the above lines are from lets me reference my app's database using

app-db:27017

since compose includes a name resolver to listed services and the port was exposed to all services started by this compose.

See 'external' section