0

I have a python script which changes the first line of a file, and it works locally but not when running the docker container.

Directory structure

my_workdir
├─── Dockerfile
├─── docker-compose.yml
├─── script.py
└─── data
    └─── r2.txt

this is my python code

a_file = open("data/r2.txt", "r")
list_of_lines = a_file.readlines()
list_of_lines[0] = "1\n"
a_file = open("data/r2.txt", "a")
a_file.writelines(list_of_lines)
a_file.close()

it basically changes the first line inside "r2.txt" to "1" but when I check the txt file is not changing it. I was reading that you need to mount a volume so I added the following to my docker-compose.yml file

version: "3"
services:
  app:
    image: imagen-c1
    container_name: c1
    privileged: true
    deploy:
      replicas: 1
      update_config:
        parallelism: 1
        delay: 5s
      restart_policy:
        condition: on-failure
    build: .
    environment:
      - DISPLAY=${DISPLAY}
      - LEGACY_IPTABLES=true 
    volumes:
      - /tmp/.X11-unix:/tmp/.X11-unix
      - ./:/data #HERE IS THE VOLUME

this is my dockerfile

FROM python:3.9-alpine

RUN pip install selenium

RUN apk add chromium
RUN apk add chromium-chromedriver


ENV CHROME_BIN=/usr/bin/chromium-browser \
    CHROME_PATH=/usr/lib/chromium/
ENV HOME /home/user-machine
    
RUN pip install webdriver_manager
WORKDIR ./
COPY ["script.py", "data/", "./"]
CMD python3 script.py

maybe I'm not doing it correctly because the file remains the same.

Camilo
  • 419
  • 1
  • 4
  • 9
  • 1
    You question is missing important information. What does your `docker-compose.yml` look like? What about your Dockerfile? Your Python code is looking for the `data` directory as a subdirectory of its current directory; are you sure your cwd is what you think it is? – larsks Sep 16 '21 at 20:44
  • sorry, I updated the contents – Camilo Sep 16 '21 at 20:56

1 Answers1

0

I think there are 2 problems in your current code.

  1. COPY ["script.py", "data/", "./"]

    inside_container_root
    ├─── script.py
    └─── r2.txt
    

    Above is what will be outcome, according to this SO answer

    Try do ls -l to see if this is really the case:

    docker build -t my_image
    docker run my_image ls -l
    

    This might be what you wanted:

    COPY script.py ./
    ADD data data
    

    ADD can copy directories

  2. I think file data/r2.txt will be read-only because it is a file inside the image. Once you resolved (1), your script.py should hit error saying you are trying to write a read-only file. I think the whole data/ directory is not writable after image is created. The updated file needs to be a folder not existing in the image. Like data_out. If you do not need the contents of your new file to be used outside the container, you do not need to mount volume. Mounting volume is to enable retrieval of file from outside the container. To get a writable directory, just make sure that writeable directory does not exist in the image.

     a_file = open("data_out/r2.txt", "a")
    

    You might want something like this.

Andrew NS Yeow
  • 341
  • 2
  • 8
  • I tried doing it outside the folder but I got the same result, it writes on the container's filesystem but not in the original file. – Camilo Sep 16 '21 at 22:01
  • The original file became _read-only_. We will not be able to write to it. I think it is existing design of **docker**. Can you explain why you want to overwrite an existing file? One idea, use `r2-template.txt` as your input file. Then `r2.txt` as your output file. Does this work for your use-case? – Andrew NS Yeow Sep 16 '21 at 22:07
  • 1
    I want to overwrite because i need to keep changing it, but since it is not keeping the changes I thought I need a way to write on the original. I will try what you say about the template hoping it works, my main goal is to keep the state of the file after changing it even if the container restarts – Camilo Sep 16 '21 at 22:13
  • @Chaos_Is_Harmony that has nothing to do with my problem. I need to edit the content of a txt file and keep those changes, not the name of the file, the content of the file – Camilo Sep 17 '21 at 03:16