Docker - 编辑容器内部文件

 

使用 -v 命令

run的时候,增加 -v 命令,将容器内部的文件夹映射到主机,主机的文件发生变化之后,容器内的文件也会发生对应的变化。

样例:

将容器 /usr/local/tomcat/webapps 目录下的内容,映射到主机的 root 文件夹下

docker run -d -p 8080:8080 --name tomcat_hello -v /root/tomcat/webapps:/usr/local/tomcat/webapps tomcat:8


复制文件到容器内部

docker cp [file_path] [container_id]:[target_path]

样例:

将test文件复制到容器的根目录
docker cp test.txt bc02ce9f6363:/

使用 exec 命令进入到容器内部

以 tomcat 容器为例

docker exec -it bc02ce9f6363 /bin/bash

需要注意,不是所有容器都能使用 /bin/bash ,使用 exit 命令退出容器。

 

下图内容:将 test.txt 文件复制到容器内部,并且使用 exec 进入到容器内部。

 


更多exec可用的命令

docker exec -help

-d, --detach                Detached mode: run command in the background
--detach-keys string Override the key sequence for detaching a container
-e, --env list Set environment variables (default )
--env-file list Read in a file of environment variables
-i, --interactive Keep STDIN open even if not attached
--privileged Give extended privileges to the command
-t, --tty Allocate a pseudo-TTY
-u, --user string Username or UID (format: "<name|uid>[:<group|gid>]")
-w, --workdir string Working directory inside the container

翻译:
-d, --detach 离模式:在后台执行命令
--detach-keys string 覆盖分离容器的键序列
-e, --env list 设置环境变量(默认)
--env-file list 读取环境变量的文件
-i, --interactive 保持STDIN打开,即使没有附加
--privileged 赋予该命令扩展权限
-t, --tty 分配一个伪tty
-u, --user string 用户名或UID(格式:"<name| UID >[:<group|gid>]")
-w, --workdir string 容器内的工作目录

posted on 2023-05-26 15:41  疯狂的妞妞  阅读(353)  评论(0编辑  收藏  举报

导航