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