Docker-容器使用 (2)

将宿主机与容器之间复制文件/文件夹

docker container cp [OPTIONS] CONTAINER:SRC_PATH DEST_PATH|- docker cp [OPTIONS] SRC_PATH|- CONTAINER:DEST_PATH or docker cp [OPTIONS] CONTAINER:SRC_PATH DEST_PATH|- docker cp [OPTIONS] SRC_PATH|- CONTAINER:DEST_PAT Options: -a, --archive Archive mode (copy all uid/gid information) -L, --follow-link Always follow symbol link in SRC_PATH

将宿主机的test.py文件拷贝到tomcat容器的webapps目录下

docker cp test.py ec4b4f0fd45c:/usr/local/tomcat/webapps create

创建一个新的容器

docker container create [OPTIONS] IMAGE [COMMAND] [ARG...] or docker create [OPTIONS] IMAGE [COMMAND] [ARG...]

参数有点多,这里就不列出来了,有兴趣可以使用--help查看。

docker create -it -p 8089:8080 77fc61f55a0c

创建的新容器,默认是未启动的,可以使用start命令启动。

docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES e5e2f9915c42 77fc61f55a0c "catalina.sh run" 7 seconds ago Created frosty_ellis ec4b4f0fd45c 4b "catalina.sh run" 37 minutes ago Up 37 minutes 0.0.0.0:8088->8080/tcp, :::8088->8080/tcp fervent_goldberg docker start ec docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES e5e2f9915c42 77fc61f55a0c "catalina.sh run" 42 seconds ago Up 2 seconds 0.0.0.0:8089->8080/tcp, :::8089->8080/tcp frosty_ellis ec4b4f0fd45c 4b "catalina.sh run" 38 minutes ago Up 38 minutes 0.0.0.0:8088->8080/tcp, :::8088->8080/tcp fervent_goldberg diff

检查容器文件系统上文件或目录的更改

docker container diff CONTAINER docker diff CONTAINER

这个命令比较简单,没有其他参数。

docker diff e5e2f9915c42 C /usr C /usr/local C /usr/local/tomcat C /usr/local/tomcat/logs C /usr/local/tomcat/logs/catalina.2021-08-01.log C /usr/local/tomcat/logs/localhost.2021-08-01.log C /tmp C /tmp/hsperfdata_root C /tmp/hsperfdata_root/1 exec

进入一个正在运行的容器

docker container exec [OPTIONS] CONTAINER COMMAND [ARG...] or docker exec [OPTIONS] CONTAINER COMMAND [ARG...] Options: -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 --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

通常使用-i -t参数,进入容器

docker exec -it e5 /bin/bas root@e5e2f9915c42:/usr/local/tomcat# ls

看到root后面跟着容器ID,则表示进入到容器了,如果想退出容器,使用exit命令,就可以退出容器。

export

将容器导出为本地文件

docker container export [OPTIONS] CONTAINER or docker export [OPTIONS] CONTAINER Options: -o, --output string Write to a file, instead of STDOUT

这个必须要带参数

docker export -o "tomcat_user_v1.gz.tar" e5

输出的文件路径支持绝对路径,如果不填绝对路径,则默认就是在当前执行导出命令的目录下,使用ll或ls查看文件。

-rw-------. 1 root root 588863488 Aug 1 11:51 tomcat_user_v1.gz.tar

从结果上看出已经导出成功。

inspect

查看一个或多个容器详细信息

docker container inspect [OPTIONS] CONTAINER [CONTAINER...] Options: -f, --format string Format the output using the given Go template -s, --size Display total file sizes

命令:docker container inspect e5e2f9915c42,输出的内容太多了,就不贴出来了,可以自行验证查看。

kill

杀死一个或多个正在运行的容器

docker container kill [OPTIONS] CONTAINER [CONTAINER...] or docker kill [OPTIONS] CONTAINER [CONTAINER...] Options: -s, --signal string Signal to send to the container (default "KILL")

命令操作:

[root@localhost ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES e5e2f9915c42 77fc61f55a0c "catalina.sh run" 21 minutes ago Up 20 minutes 0.0.0.0:8089->8080/tcp, :::8089->8080/tcp frosty_ellis ec4b4f0fd45c 4b "catalina.sh run" 59 minutes ago Up 59 minutes 0.0.0.0:8088->8080/tcp, :::8088->8080/tcp fervent_goldberg [root@localhost ~]# docker kill e5 e5 [root@localhost ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES ec4b4f0fd45c 4b "catalina.sh run" 59 minutes ago Up 59 minutes 0.0.0.0:8088->8080/tcp, :::8088->8080/tcp fervent_goldberg logs

获取容器日志

docker container logs [OPTIONS] CONTAINER or docker logs [OPTIONS] CONTAINER Options: --details Show extra details provided to logs -f, --follow Follow log output --since string Show logs since timestamp (e.g. 2013-01-02T13:23:37Z) or relative (e.g. 42m for 42 minutes) -n, --tail string Number of lines to show from the end of the logs (default "all") -t, --timestamps Show timestamps --until string Show logs before a timestamp (e.g. 2013-01-02T13:23:37Z) or relative (e.g. 42m for 42 minutes)

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/zyssyy.html