I-dont-know
I-dont-know

Reputation: 1

Docker Mounted Volumes to facilitate development

I'm using Docker Desktop on a Windows Enterprise 11 host machine to copy java source files from the host to an docker image; I then want to run the image in a container where the java source files from the image can be made available (and persisted) in a mounted host directory (volume), super easy use case, but its not working.

  1. The Dockerfile makes the "code-in-container" directory (for image) and copies java source files and sub-directories from "code-on-host" (local host) to "code-in-container" (on the image), a text file is also copied to "code-in-container", the image is built on a Linux Alpine base image.

    FROM alpine:latest
    RUN mkdir -p /code-in-container
    COPY /code-on-host /code-in-container/
    COPY file-on-host.txt /code-in-container
    
  2. The docker-compose creates a service based on the Dockerfile referenced (and outlined above), sets up port forwarding from host -> container, and then creates a binding mount called "some-host-directory" (where the ./ provides the context of where "some-host-directory" should be created on the host, this is based on where the compose-file and Dockerfile are hosted), the compose-file then maps the bind mount to the "/code-in-container" directory within the running container (image). The stdin_open and tty is used to keep the container running so the mount can be established and I can access the shell of the running container.

host directory

version: '3'
services:
  my_app:
    build:
      context: .
      dockerfile: Dockerfile
    ports:
      - "3001:3001"
    volumes:
      - ./some-host-directory:/code-in-container
    stdin_open: true
    tty: true

I use docker compose up -d to build image and run it in a docker container, the "code-on-host" contains source files and sub-directories, however, when the image is built and run the "some-host-directory" and "code-in-container" directories are empty.

When I build just the docker image using

docker build -t my-dev-image:1.0 .

(which uses the Dockerfile from the current folder in terminal) and then run the the image using

docker run -d my-dev-image:1.0

where I added CMD ["tail", "-f", "/dev/null"] to keep the container running when its run, then, I can see the "code-in-container" directory available in the container when I access the running containers shell using

docker exec -it <container_id/bin/sh> /bin/sh

Can anyone help explain what I'm missing when I run the "docker inspect <container_id>" I see the following in the "Mount" section:

    "Mounts": [
        {
            "Type": "bind",
            "Source": "C:\\PathTo\\Host\\Directory\\code-on-host",
            "Destination": "/code-in-container",
            "Mode": "rw",
            "RW": true,
            "Propagation": "rprivate"
        }
    ],

But I don't see a reference to a "Type": "volume", unsure if that is significant, but would expect a volume reference to exist.

Cheers.

I have tried build the just the image from Dockerfile and running that image in Docker Desktop, I have tried using docker-compose to build the image and mount the image directory to the host directory specified as a volume in docker-compose file. Again all to no avail. I'm wondering if the challenge is because the image is built and used locally, and it should be in a docker image repository and accessed that way from the docker-compose file. I also tried to use a Windows Linux Subsystem to generate the image and run it using a docker-compose file.

Upvotes: 0

Views: 139

Answers (0)

Related Questions