4

I am new to Docker and have been trying to achieve the following, but cannot seem to get it working.

I want to create an image where to save some static files. Then, I'd like to access the folder with the static files from another container.

For this, first I have created a dockerfile with this info:

FROM alpine
WORKDIR /usr/local/apache2/htdocs
ADD static /usr/local/apache2/htdocs
VOLUME /usr/local/apache2/htdocs

I have then created an image from that dockerfile running the following command:

docker build -t customimage .

Then in my docker-compose.yaml I start two services like this:

version: '3.7'
services:
    custom:
        image: customimage:latest
        tty: true
    apache:
        image: httpd:latest
        volumes:
            - /usr/local/apache2/htdocs
        ports: 
            - "80:80"

After doing docker-compose up both services start, but I cannot access the volume from customimage from the apache container.

What am I missing here? I know I can use run commands, but I prefer to use docker-compose as I will be adding more services in the future.

Adrian
  • 198
  • 4
  • 16

1 Answers1

4

I suggest that you declare a docker volume to share between the two containers. One of the containers can then be prebuilt with some data in the volume path. The other container can then mount the same volume, and will see the same data.

One very important detail that needs to be observed here is - files in a volume will not be visible in the mounting container, if you bind it to a location in the filesystem of a container where other files already exists. It needs to be an empty location!

To make this work for you, you need to create a Dockerfile for your httpd image, because we need to clean the target location before the files in the volume that we will bind will be visible.

Solution

I suggest a project layout like this:

/
├─ data.Dockerfile
├─ docker-compose.yml
├─ httpd.Dockerfile
├─ index.html 

This is an example of a minmal website running in a httpd container, that is serving the website from a volume mounted from the host. A volume that is also mounted into a sidekick "data container", that is prepopulated with the website (a single index.html file, for the sake of keeping things brief)

The following is the content of the files:

data.Dockerfile

FROM alpine

# declare a volume at location /var/shared_volume
VOLUME /var/shared_volume
# copy your new great site into the volume location
COPY index.html /var/shared_volume/

# configure an entrypoint that does not terminate, to keep the container up
ENTRYPOINT [ "tail", "-f", "/dev/null" ]

httpd.Dockerfile

FROM httpd:latest

# remove any file and directory at the location where we wantt serve 
# the static content 
RUN rm -rf /usr/local/apache2/htdocs/*

# declare the path as a volume
VOLUME /usr/local/apache2/htdocs

docker-compose.yml

version: "3.7"

# declare the volume to share between the containers
volumes:
  shared_volume:

services:
  data:
    build:
      context: ./
      dockerfile: data.Dockerfile
    container_name: data
    volumes:
      - "shared_volume:/var/shared_volume"
  httpd:
    build:
      context: ./
      dockerfile: httpd.Dockerfile
    container_name: httpd
    volumes:
      - "shared_volume:/usr/local/apache2/htdocs"
    ports:
      - "80:80"

index.html

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <title>Is this page visible in your httpd container?</title>
  </head>
  <body>
    <h1>I sure hope you can see this page?</h1>
  </body>
</html>

The volume shared_volume will now be a docker volume that is managed on the host. Both containers will mount it to a path in their respective filesystem.

Run the example

Just docker-compose up, and when this is running visit http://localhost.

I have created a gist with the solution here.


Briefly on, mounting directly from one container to another

If you want to mount the directory directly from the "data container" into your apache container - then you will run into a bunch of issues. These are best explained in the answer to this question: Mounting nfs shares inside docker container

Andreas Lorenzen
  • 3,810
  • 1
  • 24
  • 26
  • Sorry for the late reply. I just tried this and it works the opposite way of what I need to do. Instead of showing the contents of the customimage container into the apache container, it shows the apache container files into the customimage container. – Adrian Mar 09 '19 at 08:35
  • You are right, I realized the problem the moment that I read your comment here - I have updated my answer, please review and try. – Andreas Lorenzen Mar 09 '19 at 10:32