Search code examples
dockerdocker-composedocker-volume

Docker-compose volume not storing files


I'm having trouble demonstrating that data I generate on a shared volume is persistent, and I can't figure out why. I have a very simple docker-compose file:

version: "3.9"

# Define network
networks:
    sorcernet:
        name: sorcer_net

# Define services
services:
    preclean:
        container_name: cleaner
        build:
            context: .
            dockerfile: DEESfile
        image: dees
        networks:
            - sorcernet
        volumes:
            - pgdata:/usr/share/appdata
        #command: python run dees.py

    process:
        container_name: processor
        build:
            context: .
            dockerfile: OASISfile
        image: oasis
        networks:
            - sorcernet
        volumes:
            - pgdata:/usr/share/appdata

volumes:
    pgdata:
        name: pgdata

Running the docker-compose file to keep the containers running in the background:

vscode ➜ /com.docker.devenvironments.code $ docker compose up -d
[+] Running 4/4
 ⠿ Network sorcer_net   Created
 ⠿ Volume "pgdata"      Created
 ⠿ Container processor  Started
 ⠿ Container cleaner    Started

Both images are running:

vscode ➜ /com.docker.devenvironments.code $ docker image ls
REPOSITORY      TAG          IMAGE ID       CREATED          SIZE
oasis          latest      e2399b9954c8    9 seconds ago   1.09GB
dees           latest      af09040befd5   31 seconds ago   1.08GB

and the volume shows up as expected:

vscode ➜ /com.docker.devenvironments.code $ docker volume ls
DRIVER    VOLUME NAME
local     pgdata```

Running the docker container, I navigate to the volume folder. There's nothing in the folder -- this is expected.

vscode ➜ /com.docker.devenvironments.code $ docker run -it oasis
[root@049dac037802 opt]# cd /usr/share/appdata/
[root@049dac037802 appdata]# ls
[root@049dac037802 appdata]# 

Since there's nothing in the folder, I create a file in called "dog.txt" and recheck the folder contents. The file is there. I exit the container.

[root@049dac037802 appdata]# touch dog.txt
[root@049dac037802 appdata]# ls
dog.txt
[root@049dac037802 appdata]# exit
exit

To check the persistence of the data, I re-run the container, but nothing is written to the volume.

vscode ➜ /com.docker.devenvironments.code $ docker run -it oasis
[root@1787d76a54b9 opt]# cd /usr/share/appdata/
[root@1787d76a54b9 appdata]# ls
[root@1787d76a54b9 appdata]# 

What gives? I've tried defining the volume as persistent, and I know each of the images have a folder location at /usr/share/appdata.


Solution

  • If you want to check the persistence of the data in the containers defined in your docker compose, the --volumes-from flag is the way to go

    When you run

    docker run -it oasis
    

    This newly created container shares the same image, but it doesn't know anything about the volumes defined.

    In order to link the volume to the new container run this

    docker run -it --volumes-from $CONTAINER_NAME_CREATED_FROM_COMPOSE oasis
    

    Now this container shares the volume pgdata.

    You can go ahead and create files at /usr/share/appdata and validate their persistence