DEV Community

Mehul Prajapati
Mehul Prajapati

Posted on

Add new Volumes to a running container in docker-compose

Consider a situation where you have a set of services running together with docker-compose. Now, For one of your services, You want to have one directory of service to be exported and synced with the host. In other terms, you want to have volume for that directory. ( e.g Downloads, Uploads).

This post shows how to add volume to the running container without letting other services of the network going down.

Steps

1. Copy contents of directory from container
2. Create a new image from the running container (Commit)
3. Stop and Delete running container
4. Create a container from the image and specify volume and Network

Step 1 - Copy

If path, where we are going to add volume, is not empty, then make sure to copy the content to the host system, As adding a volume will overwrite container data at that location.

List the containers and copy the id of running container.

docker ps 
Enter fullscreen mode Exit fullscreen mode

Copy the contents of the directory from container, At which volume is going to be mounted.

docker cp <container-id>:<container path to directory> $(pwd)
Enter fullscreen mode Exit fullscreen mode

Step 2 - Create new Image

This step will create an image from the running container. So the state of running service is preserved.

docker commit <container-id> <new-image-name>
Enter fullscreen mode Exit fullscreen mode

Step 3 - Delete container

Delete the running container, so new container with added volume will take this place.

docker stop <container-id> && docker rm <container-id>
Enter fullscreen mode Exit fullscreen mode

Step 4 - Create a new container

Now, We have an image (snapshot) of container available, Which we can use to create another container and add a new volume.

Before proceeding, One important thing to notice is that Our services are running together with docker-compose. They can communicate with each other through the network created by docker-compose. Now if we create and run the new container. This container will not able to access other services as a new container is not part of the network. So while creating new container, we have to add this container in the existing docker-compose network.

1. List out the list of docker-compose networks and copy network name
This will list all the networks, list will include some default networks and also for your service. Find out relevant network and copy network name.

docker network ls
Enter fullscreen mode Exit fullscreen mode

Get more details about the network by running docker inspect NETWORK_ID

2. Create a new container and run in the existing network

docker run -d \
        -v <copied-directory>:<path to volume> \
        --network=<network-name> \
        image-name
Enter fullscreen mode Exit fullscreen mode

copied-directory - Path to directory copied in step 1
network-name - Network name identified in step 4.1
image-name - Image name created in step 2

Done.
New service is up in docker-compose network with new volume.

Top comments (0)