Docker学习-常用命令(2)

进入当前正在运行的容器:

#我们通常容器都是后台运行的,需要进入容器,修改一些配置

#方式1: docker exec
-it 容器id /bin/bash #进入容器后开启一个新的终端,可以在里面操作【常用】 #方式2: docker attach 容器id #进入容器正在执行的终端,不会启动新的线程

复制文件:
docker cp 容器id:/文件所在容器目录/文件名 /目标目录

#进入docker容器内部
[root@localhost gsl]# docker attach d03a1d206464
[root@d03a1d206464 /]# cd /home
bash: $'cd343': command not found
[root@d03a1d206464 /]# cd home
[root@d03a1d206464 home]# ls

#在容器内创建一个文件
[root@d03a1d206464 home]# touch test.java
[root@d03a1d206464 home]# ls
test.java
[root@d03a1d206464 home]# exit
exit
[root@localhost gsl]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@localhost gsl]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e7d2c5417147 centos "/bin/sh -c 'while t…" 36 minutes ago Exited (137) About a minute ago serene_khorana
d03a1d206464 centos "/bin/bash" 45 minutes ago Exited (0) 19 seconds ago quizzical_shtern
5d5dec77f40a centos "/bin/bash" 48 minutes ago Exited (0) 48 minutes ago hopeful_liskov
8ee4effed193 centos "/bin/bash" 50 minutes ago Exited (0) 50 minutes ago epic_hertz
f3504e169f27 centos "/bin/bash" 52 minutes ago Exited (0) 52 minutes ago cool_cerf
01913d634873 centos "/bin/bash" 59 minutes ago Exited (0) 58 minutes ago nostalgic_morse
8f87f9b89d52 centos "/bin/bash" About an hour ago Exited (0) 51 minutes ago frosty_noyce

#将这个容器中的文件拷贝到外面的主机上
[root@localhost gsl]# docker cp d03a1d206464:/home/test.java /home


[root@localhost gsl]# ls
omnetpp-5.6.1 omnetpp-5.6.1-src-linux.tgz qt-opensource-linux-x64-5.9.9.run redis-2.8.17 redis-2.8.17.tar.gz
[root@localhost gsl]# cd home
bash: cd: home: 没有那个文件或目录
[root@localhost gsl]# cd /home
[root@localhost home]# ls
gsl test.java

[ 版权声明 ]: 本文所有权归作者本人,文中参考的部分已经做了标记! 商业用途转载请联系作者授权! 非商业用途转载,请标明本文链接及出处!
原文地址:https://www.cnblogs.com/gslgb/p/14614256.html