0

i am in lots of confusion to how to achieve the following flow

i've two containers nginx and asset (will have only bundled asset)

so there cab be 2-3 nginx instance and few asset instances.

So from my local machine or build server i'll build assets using grunt and that bundled asset will be inside of image.

so if i use volumes for bundled asset how it will be pushed along side image.

or if i use no volumes then how nginx will get mount path from asset image or (stopped or running) container.

dev.meghraj
  • 8,542
  • 5
  • 38
  • 76
  • How large are your assets and do you share the same set of assets across multiple docker image versions? – Ashan Apr 24 '17 at 15:42
  • Source is big but after grunt there are 10-12 webpack bundle files and some images, css etc – dev.meghraj Apr 24 '17 at 15:44
  • So these assets you are planning to server are web content (js/css) bundled with webpack? – Ashan Apr 24 '17 at 15:53
  • Yes...but I want them as sperate container or image not in nginx image – dev.meghraj Apr 24 '17 at 15:54
  • Any reason not to include them inside nginx image? creating unique images and tags for different builds – Ashan Apr 24 '17 at 16:06
  • Nginx is different kind of thing and the asset we are we pushing is different so why should merge with nginx? Need flexibility not simplicity – dev.meghraj Apr 24 '17 at 16:09
  • Its a choice whether to include it inside nginx or in another container, but from docker perspective, it will be advantages, if you can keep multiple images (tags) for different versions of assets so that you can rollback to any version or test an older version & etc. This will be also helpful, if you scale out your containers across multiple hosts. – Ashan Apr 24 '17 at 16:11
  • Thats why I want my asset as sperate image and I may have another asset image that will serve other app's asset bundle. – dev.meghraj Apr 24 '17 at 16:13

2 Answers2

0

There are two main ways.

  1. You add your assets to your nginx image. In that case, you simply create a Dockerfile and COPY your local assets to a location on the nginx image. Eg:

    FROM: nginx
    COPY: myassets/dir/*  /var/lib/html
    

    This is the simplest way to do it.

  2. Mount a volume

    If you need the same assets to be shared between containers, then you can create a volume and mount this volume in your nginx container. The volume needs to be created before you try to create your nginx containers.

    docker volume create myassets
    

    The next step is to copy your files to that newly created volume. If your docker host is local (e.g.: VirtualBox, Docker for Mac or Windows, Vmware Fusion, Parallel) then you can mount a local dir with your assets an copy them to the volume. Eg:

    docker run --rm -v /Users/myname/html:/html -v myassets:/temp alpine sh -c "cp -rp /html/* /temp"
    

    If your docker host is hosted elsewhere (AWS, Azure, Remote Servers, ...) then you can't rely on mounted local drives. You'll need to remote copy the files.

    docker run -d --name foo -v myassets:/temp alpine tail -f /dev/null
    docker cp myassets/* foo:/temp
    docker rm -f foo
    

    This creates a container named foo which keeps running (tail -f) in the background -d. We then docker copy files into it at the location where the myassets volume is mounted, and then kill the container when done.

    When you mount your volume on a nginx container, it will overwrite whatever is in that container's location.

    docker run -d -v myassets:/var/lib/html nginx
    
Bernard
  • 16,149
  • 12
  • 63
  • 66
  • My question is regarding docker-compose – dev.meghraj Apr 24 '17 at 15:45
  • Volumes-from option is available if I want to run them separately. I want an example of docker-compose.yml – dev.meghraj Apr 24 '17 at 15:46
  • `volume_from` is deprecated as of docker compose v3, that's why I'm not including it. – Bernard Apr 25 '17 at 01:35
  • 1
    @dev.mraj Creating and initialising the myassets volume must be done before you do `docker compose up -d`. Currently, there isn't a way to specify a one off "job" in docker compose (it's been a common request for years now). So it's simpler to just put a few lines above in a shell scripts or Makefile that you run prior to setting up the rest of your services using docker compose. – Bernard Apr 25 '17 at 01:41
0

You can create multi container docker environments for each app with docker-compose, where multiple asset docker images are built, which is mounted as data volumes for the nginx container.

Refer the docker-compose reference for data volume mounting and stackoverflow question for mounting directory from one container to another.

Community
  • 1
  • 1
Ashan
  • 18,898
  • 4
  • 47
  • 67