Zuriar
Zuriar

Reputation: 11754

how to create data volume from image for use in nginx container

I have an image (not container) which has the data baked in at /store. The image does not do anything else, it is only a vessel for holding the data in. I want to create a docker-compose file so that my nginx container has access to everything in /store at /usr/share/nginx/html

Do I need to make an intermediary container first? I am not sure how the docker-compose file would look. thanks

Upvotes: 1

Views: 3909

Answers (4)

BMitch
BMitch

Reputation: 264306

This is a quick step process:

docker run -v nginx-store:/store --rm store true

docker run -v nginx-store:/usr/share/nginx/html -d --name nginx nginx

The first run creates a named volume nginx-store from the contents of your store image (this happens any time you mount an empty volume in a container), and immediately exits and deletes the container.

The second run uses that named volume with the future nginx containers. To modify the nginx-store volume in the future, you can run any side container that mounts it with a similar -v nginx-store:/target flag.

Upvotes: 2

Nik
Nik

Reputation: 438

I'd suggest you to consider bake /store inside your nginx container. It will reduce number of mounted volumes and thus simplify overall structure. And maybe improves performance.

You could do it in several ways:

  1. Use you data image as base for your nginx image. In this case you will need to write Dockerfile for nginx but it's not very difficult.

  2. You can extract /store from your data image by creating container with true endpoint and docker cp desired data. Then copy it to your nginx image.

Upvotes: 0

Mark O'Connor
Mark O'Connor

Reputation: 77991

You could try using the local-persist volume plugin as follows:

version: '2'
services:
  web:
    image: nginx
    volumes:
      - data:/usr/share/nginx/html 
volumes:
  data:
    driver: local-persist
    driver_opts:
      mountpoint: /data/local-persist/data

Obviously other volume plugin types might offer more flexibility.

https://docs.docker.com/engine/extend/plugins/

Upvotes: 0

The best way for managing that would probably do use a docker volume to store you're /store datas. You can do it once by creating a container from that image, mount an empty docker volume in it, and then copy the content of /store in the external docker volume.

If you still need to use the /store from an existing image you will need to instanciate a container from if and retrieve the exposed volume from your nginx container. (using volume_from). In this case both containers would need to be on the same host.

Upvotes: 1

Related Questions