Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Migrating existing Docker containers to Docker Compose

I have several running containers that were started by classic Docker commands. These containers are using a Docker network that was also created "by hand":

docker network create simple-network

docker run -d \
  --name docker-registry \
  --net=simple-network \
  -p 5000:5000 \
  -v [...] \
  registry:2.3.0

docker run -d \
  --name docker-registry-web \
  --net=simple-network \
  -p 8080 \
  -v [...] \
  hyper/docker-registry-web

docker run -d \
  --name nginx \
  --net=simple-network \
  -p 80:80 \
  -p 443:443 \
  -v [...] \
  nginx:1.9.8

I would like to use Docker Compose to run these containers as it would be easier to manage.

Is it possible to migrate these containers to Docker Compose like the following one or it's only possible to use compose with fresh new containers?

I would like to use the same volumes during this migration! Downtime is not a problem.

docker-compose.yml

version: '2'

docker-registry:
  image: registry:2.3.0
  ports:
    - "5000:5000"
  volumes: 
    - [...]

docker-registry-web:
  image: hyper/docker-registry-web
  expose:
    - "8080"
  volumes: 
    - [...]

nginx:
  image: nginx:1.9.8
  ports:
    - "80:80"
    - "443:443"
  volumes: 
    - [...]
like image 881
Cedric Thiebault Avatar asked Feb 09 '16 16:02

Cedric Thiebault


People also ask

Is docker compose obsolete?

Deprecated in Release: v20.10 Following the deprecation of Compose on Kubernetes, support for Kubernetes in the stack and context commands in the docker CLI is now marked as deprecated as well.

Is docker compose the same as docker compose?

The key difference between docker run versus docker-compose is that docker run is entirely command line based, while docker-compose reads configuration data from a YAML file. The second major difference is that docker run can only start one container at a time, while docker-compose will configure and run multiple.

Can docker containers be moved?

When migrating docker containers, we must save the operating container as an image, transmit it to the new server, then load the docker image as a brand new container. Another method of migrating docker containers is to export and import docker containers.


2 Answers

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.

like image 162
dnephin Avatar answered Oct 10 '22 02:10

dnephin


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

like image 33
user8675309 Avatar answered Oct 10 '22 02:10

user8675309