Reputation: 1224
I did docker-compose down
/docker container rm
and noticed that I lost all my data created in the container. Yes, I forgot to mount my local directory as a volume in the first place. ðŸ˜
To prevent this, on startup, I want to warn users that "the data will be non-persistent", if the local volume's not mounted.
Is there a way to detect from inside container whether a file or a directory is a mounted one via Docker?
I googled it but couldn't find a good way. And my current workaround is:
FROM alpine:latest
RUN \
mkdir /data && \
touch /data/.replaceme
...
ENTRYPOINT /detect-mount-and-start.sh
detect-mount-and-start.sh
checks if /data/.replaceme
exists. If so, it warns to mount a local volume and exits.
Are there a better way to detect it?
Note (2019/09/12): This container is not only used via docker-compose up
but docker run --rm
too. And the directory name in local are not specified. Meaning, it can be -v $(pwd)/mydata:/data
or something like -v $(pwd)/data_local:/data
, etc.
Note (2019/09/15): The situation is: I launched a container of Markdown Editor and created something like 100 of .md
files. Those files were saved on /data
on the root of the container. I should have mounted the volume like -v $(pwd)/data:/data
before everything. But I didn't ... and noticed it after removing the container. My bad I know.
Upvotes: 0
Views: 1904
Reputation: 574
docker-compose stop - doesn't destroy your containers
Then you can use:
docker-compose start
Upvotes: 0
Reputation: 49
I don't know if I understand your question, but when you use docker-compose down, depending on how you create your docker-compose.yml, it will destroy your data, see:
Will delete you data when you execute down:
version: '2'
  mysqldb:
    image: mysql: 5.7
Don't will delete when you execute down:
version: '2'
  mysqldb:
    image: mysql: 5.7
    volumes:
      - ./data:/var/lib/mysql
Don't will delete when you execute down:
version: '2'
  mysqldb:
    image: mysql: 5.7
    volumes:
      - data-volume:/var/lib/mysql
volumes:
  data-volume:
    external: true
PS: I do not have a rating to comment on your question, so I am answering.
Upvotes: 2
Reputation: 59896
The way you are doing may also work, but I was working with one of my client on a project in development environment, It was nodejs based application and they need to make sure the server.js
exist before starting the container and server.js
was expected from mount location so I came up with this approach, As I did not find a way to sense shared docker volume inside container.
Dockerfile
FROM alpine
run mkdir -p /myapp
copy . /myapp
copy entrypoint.sh /entrypoint.sh
RUN chmod +x /entrypoint.sh
ENTRYPOINT ["/entrypoint.sh"]
entrypoint.sh
#!/bin/sh
APP_PATH="/myapp"
files=$(ls /myapp/*.js)
echo "Files in Docker mount location: $files"
if [ -f "$APP_PATH/server.js" ] && [ -f "$APP_PATH/index.js" ]; then
echo "Starting container with host mount files"
echo "Starting server.js"
cd $APP_PATH;
node server.js
else
>&2 echo "Error: Pls mount the host location on /myapp path of the container i.e -v host_node_project:/myapp. Current files $(ls $APP_PATH)"
break
fi
build and run
docker build -t myapp .
docker run -it --rm --name myapp myapp
Upvotes: 1