How do I edit a file after I shell to a Docker container?

后端 未结 16 1770
你的背包
你的背包 2020-11-29 14:22

I successfully shelled to a Docker container using:

docker exec -i -t 69f1711a205e bash

Now I need to edit file and I don\'t have any edito

相关标签:
16条回答
  • 2020-11-29 15:07

    You can use cat if installed, with the > caracter. Here is the manipulation :

    cat > file_to_edit
    #1 Write or Paste you text
    #2 don't forget to leave a blank line at the end of file
    #3 Ctrl + C to apply configuration
    

    Now you can see the result with the command

    cat file
    
    0 讨论(0)
  • 2020-11-29 15:08

    You can open existing file with

    cat filename.extension
    

    and copy all the existing text on clipboard.

    Then delete old file with

    rm filename.extension
    

    or rename old file with

    mv old-filename.extension new-filename.extension
    

    Create new file with

    cat > new-file.extension
    

    Then paste all text copied on clipboard, press Enter and exit with save by pressing ctrl+z. And voila no need to install any kind of editors.

    0 讨论(0)
  • 2020-11-29 15:08

    After you shelled to the Docker container, just type:

    apt-get update
    apt-get install nano
    
    0 讨论(0)
  • 2020-11-29 15:11

    If you use Windows container and you want change any file, you can get and use Vim in Powershell console easily.

    To shelled to the Windows Docker container with PowerShell:

    docker exec -it <name> powershell

    • First install Chocolatey package manager

      Invoke-WebRequest https://chocolatey.org/install.ps1 -UseBasicParsing | Invoke-Expression;

    • Install Vim

      choco install vim

    • Refresh ENVIRONMENTAL VARIABLE You can just exit and shell back to the container

    • Go to file location and Vim it vim file.txt

    0 讨论(0)
提交回复
热议问题