educative.io

Can't execute the command in the docker host

Hi There,

I was trying to follow the course, however I had the problem when trying to copy the updated app file to the volume’s mount point in my Docker host.

$ cp ~/counter-app/app.py /var/lib/docker/volumes/counter-app_counter-vol/_data/app.py

I can’t perform the copy operation because the file or directory is not there.


Course: Docker Deep Dive: Advanced Techniques - Learn Interactively
Lesson: Deploying an App

Hi @wahyu_sumartha_priya !!
It seems like you’re encountering an issue where the directory or file you’re trying to copy doesn’t exist. Let’s troubleshoot this step by step:

  1. Check Directory Existence: First, ensure that the directory /var/lib/docker/volumes/counter-app_counter-vol/_data/ exists on your Docker host. You can do this by running:

    ls -l /var/lib/docker/volumes/counter-app_counter-vol/_data/
    

    If the directory doesn’t exist, then it’s likely that the volume hasn’t been created properly or there’s an issue with the volume configuration.

  2. Verify Volume Existence: Confirm that the Docker volume counter-app_counter-vol exists by running:

    docker volume inspect counter-app_counter-vol
    

    Check the output to ensure that the volume exists and that its configuration matches what you expect.

  3. Check File Existence: If the directory exists and the volume is configured correctly, then check if the app.py file exists in the source directory (~/counter-app/). Run:

    ls -l ~/counter-app/app.py
    

    Ensure that the file exists and that you have the correct path.

  4. Permissions: Make sure you have the necessary permissions to access the files and directories you’re trying to work with. You may need to use sudo or ensure that your user has the appropriate permissions.

  5. Alternative Approach: Instead of directly copying the file to the volume’s mount point, you can also try mounting the directory containing app.py directly into the container. Modify your docker-compose.yml file to mount the directory as a volume:

    volumes:
      - ~/counter-app:/code
    

    Then, restart your containers using docker-compose up -d and see if the changes are reflected.

By going through these steps and troubleshooting each point, you should be able to identify and resolve the issue with copying the updated app.py file to the volume’s mount point. If you continue to encounter issues, please provide more details or error messages for further assistance.
Happy learning :blush:

Hi @Javeria_Tariq

Yes, Indeed the mountpoint directory does not exist in the Docker host.

By the way, I am using docker in Mac OS. It looks like Docker in Mac OS is runs in virtual machine, therefore I can’t access the mountpoint directory. Apparently it was like that by the design.

@wahyu_sumartha_priya Yes, you are correct. Docker Desktop for Mac runs Docker inside a lightweight virtual machine (VM) to provide the Docker runtime environment on macOS. Because of this design, you can’t directly access the mountpoint directory on the Docker host from macOS.

In this case, to update the file within the Docker volume while using Docker Desktop for Mac, you have a few options:

  1. Update the File Inside the Container: Instead of updating the file on the Docker host and copying it to the volume, you can access the container directly and update the file inside the container. You can use the docker exec command to access a running container and edit the file using a command-line text editor like vi or nano. For example:

    docker exec -it <container_name_or_id> vi /code/app.py
    

    This command will open the app.py file inside the container using the vi editor, allowing you to make changes directly.

  2. Use Docker Bind Mounts: Instead of using volumes, you can use bind mounts to map a directory on your host system (macOS) directly into the container. With bind mounts, changes made on the host are immediately reflected inside the container, bypassing the need to copy files to volumes. You can modify your docker-compose.yml file to use bind mounts instead of volumes. For example:

    services:
      web-fe:
        <Snip>
        volumes:
          - ~/counter-app:/code
    

    This configuration will mount the ~/counter-app directory on your macOS host into the /code directory inside the container.

Choose the method that best fits your workflow and environment. If you have any further questions or need assistance, feel free to ask!