import pathlib p = pathlib.Path("Dockerfile") t = p.read_text() old = 'CMD["/usr/local/bin/ds2api"]' new = 'COPY entrypoint.sh /usr/local/bin/entrypoint.sh\nRUN chmod +x /usr/local/bin/entrypoint.sh\nCMD["/usr/local/bin/entrypoint.sh"]' t = t.replace(old, new) p.write_text(t) print("Done")