Docker学习系列(一)——操作篇:常见操作

1、安装

主要参考:docker官方文档
https://docs.docker.com/engine/install/centos/
我使用的是centos,使用其他系统查找对应的系统的安装指导文档。
这里简单展示一下:

Docker 运行在 CentOS 7 上,要求系统为64位、系统内核版本为 3.10 以上。

To install Docker Engine, you need a maintained version of CentOS 7 or 8. Archived versions aren’t supported or tested.

1.1 安装准备

1.1.1 查看系统环境

[root@VM-0-13-centos ~]# uname -a
Linux VM-0-13-centos 3.10.0-1127.19.1.el7.x86_64 #1 SMP Tue Aug 25 17:23:54 UTC 2020 x86_64 x86_64 x86_64 GNU/Linux

是 OK的。可以按照流程安装。

1.1.2 卸载旧版本

如果你之前安装过 docker,请先删掉

[root@VM-0-13-centos ~]# sudo yum remove docker*Loaded plugins: fastestmirror, langpacks, product-id, search-disabled-repos, subscription-manager

This system is not registered with an entitlement server. You can use subscription-manager to register.

Resolving Dependencies
There are unfinished transactions remaining. You might consider running yum-complete-transaction, or "yum-complete-transaction --cleanup-only" and "yum history redo last", first to finish them. If those don't work you'll have to try removing/installing packages by hand (maybe package-cleanup can help).
--> Running transaction check
---> Package docker-ce.x86_64 3:20.10.3-3.el7 will be erased
---> Package docker-ce-cli.x86_64 1:20.10.3-3.el7 will be erased
---> Package docker-ce-rootless-extras.x86_64 0:20.10.3-3.el7 will be erased
--> Finished Dependency Resolution
docker-ce-stable/7/x86_64                                                                                                                                                                  | 3.5 kB  00:00:00     
epel/7/x86_64                                                                                                                                                                              | 4.7 kB  00:00:00     
epel/7/x86_64/updateinfo                                                                                                                                                                   | 1.0 MB  00:00:00     
epel/7/x86_64/primary_db                                                                                                                                                                   | 6.9 MB  00:00:00     
extras/7/x86_64                                                                                                                                                                            | 2.9 kB  00:00:00     
os/7/x86_64                                                                                                                                                                                | 3.6 kB  00:00:00     
updates/7/x86_64                                                                                                                                                                           | 2.9 kB  00:00:00     

Dependencies Resolved

==================================================================================================================================================================================================================
 Package                                                     Arch                                     Version                                           Repository                                           Size
==================================================================================================================================================================================================================
Removing:
 docker-ce                                                   x86_64                                   3:20.10.3-3.el7                                   @docker-ce-stable                                   115 M
 docker-ce-cli                                               x86_64                                   1:20.10.3-3.el7                                   @docker-ce-stable                                   156 M
 docker-ce-rootless-extras                                   x86_64                                   20.10.3-3.el7                                     @docker-ce-stable                                    24 M

Transaction Summary
==================================================================================================================================================================================================================
Remove  3 Packages

Installed size: 295 M
Is this ok [y/N]: y
Downloading packages:
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Erasing    : 3:docker-ce-20.10.3-3.el7.x86_64                                                                                                                                                               1/3 
  Erasing    : docker-ce-rootless-extras-20.10.3-3.el7.x86_64                                                                                                                                                 2/3 
  Erasing    : 1:docker-ce-cli-20.10.3-3.el7.x86_64                                                                                                                                                           3/3 
  Verifying  : docker-ce-rootless-extras-20.10.3-3.el7.x86_64                                                                                                                                                 1/3 
  Verifying  : 3:docker-ce-20.10.3-3.el7.x86_64                                                                                                                                                               2/3 
  Verifying  : 1:docker-ce-cli-20.10.3-3.el7.x86_64                                                                                                                                                           3/3 

Removed:
  docker-ce.x86_64 3:20.10.3-3.el7                               docker-ce-cli.x86_64 1:20.10.3-3.el7                               docker-ce-rootless-extras.x86_64 0:20.10.3-3.el7                              

Complete!

1.2 安装

# 根据你的发行版下载repo文件
wget -O /etc/yum.repos.d/docker-ce.repo https://mirrors.ustc.edu.cn/docker-ce/linux/centos/docker-ce.repo
# 把软件仓库地址替换为 中科大的,当然其他的也是可以的,不要用 官方的仓库,会很慢。。。
sed -i 's#download.docker.com#mirrors.ustc.edu.cn/docker-ce#g' /etc/yum.repos.d/docker-ce.repo
# 最后安装:
yum install docker-ce -y

1.3 察看是否安装成功

# 启动docker
[root@VM-0-13-centos ~]# systemctl start docker
# 查看docker版本信息,并可以确定安装情况
[root@VM-0-13-centos ~]# docker version
Client: Docker Engine - Community
 Version:           20.10.3
 API version:       1.41
 Go version:        go1.13.15
 Git commit:        48d30b5
 Built:             Fri Jan 29 14:34:14 2021
 OS/Arch:           linux/amd64
 Context:           default
 Experimental:      true

Server: Docker Engine - Community
 Engine:
  Version:          20.10.3
  API version:      1.41 (minimum version 1.12)
  Go version:       go1.13.15
  Git commit:       46229ca
  Built:            Fri Jan 29 14:32:37 2021
  OS/Arch:          linux/amd64
  Experimental:     false
 containerd:
  Version:          1.4.3
  GitCommit:        269548fa27e0089a8b8278fc4fc781d7f65a939b
 runc:
  Version:          1.0.0-rc92
  GitCommit:        ff819c7e9184c13b7c2607fe6c30ae19403a7aff
 docker-init:
  Version:          0.19.0
  GitCommit:        de40ad0
[root@VM-0-13-centos ~]# 

1.4 配置docker镜像加速

vi /etc/docker/daemon.json
{
  "registry-mirrors": ["https://registry.docker-cn.com"]
} 
# 编辑完之后,重新加载并重启docker
[root@VM-0-13-centos ~]# systemctl daemon-reload
[root@VM-0-13-centos ~]# systemctl restart docker

1.5 卸载 Uninstall Docker Engine

1.5.1 Uninstall the Docker Engine, CLI, and Containerd packages:

# 卸载依赖
$ sudo yum remove docker-ce docker-ce-cli containerd.io

1.5.2 Images, containers, volumes, or customized configuration files on your host are not automatically removed. To delete all images, containers, and volumes:

# 删除资源
$ sudo rm -rf /var/lib/docker

You must delete any edited configuration files manually.

2 Docker常见命令

详细可以参考:
https://docs.docker.com/reference/

2.1 帮助命令

docker version		# 显示docker的版本信息
docker info			# 显示docker的系统信息,包括镜像和容器的数量
docker 命令 --help		# 查看命令帮助

2.2 镜像命令

2.2.1 docker images 查看本地主机所有的镜像

[root@VM-0-13-centos ~]# docker images
REPOSITORY   TAG       IMAGE ID       CREATED      SIZE
nginx        latest    35c43ace9216   7 days ago   133MB
[root@VM-0-13-centos ~]#

解释:
- REPOSITORY 			镜像的仓库源
- TAG					镜像的标签
- IMAGE ID 				镜像的id
- CREATED				镜像的创建时间
- SIZE					镜像的大小

可选项:
		- a, --all				# 列出所有镜像
		- q,  --quite			# 只显示镜像的id

2.2.2 docker search 搜索镜像

[root@VM-0-13-centos ~]# docker search mysql
NAME                              DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
mysql                             MySQL is a widely used, open-source relation…   10545     [OK]       
mariadb                           MariaDB is a community-developed fork of MyS…   3937      [OK]       
...
ansibleplaybookbundle/mysql-apb   An APB which deploys RHSCL MySQL                2                    [OK]
widdpim/mysql-client              Dockerized MySQL Client (5.7) including Curl…   1                    [OK]
jelastic/mysql                    An image of the MySQL database server mainta…   1                    
monasca/mysql-init                A minimal decoupled init container for mysql    0     


可选项:
  -f, --filter filter   Filter output based on conditions provided
      --format string   Pretty-print search using a Go template
      --limit int       Max number of search results (default 25)
      --no-trunc        Don't truncate output
[root@VM-0-13-centos ~]# 

# 加过滤条件的例子
[root@VM-0-13-centos ~]# docker search mysql --filter=STARS=3000
NAME      DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
mysql     MySQL is a widely used, open-source relation…   10545     [OK]       
mariadb   MariaDB is a community-developed fork of MyS…   3937      [OK]  

2.2.3 docker pull下载镜像

[root@VM-0-13-centos ~]# docker pull --help

Usage:  docker pull [OPTIONS] NAME[:TAG|@DIGEST]

Pull an image or a repository from a registry

Options:
  -a, --all-tags                Download all tagged images in the repository
      --disable-content-trust   Skip image verification (default true)
      --platform string         Set platform if server is multi-platform capable
  -q, --quiet  
# 下载镜像docker pull 镜像名[:tag]
[root@VM-0-13-centos ~]# docker pull mysql
Using default tag: latest	# 如果不写tag,默认就是latest
latest: Pulling from library/mysql
45b42c59be33: Already exists 
b4f790bd91da: Pull complete 	# 分层下载 docker image的核心 联合文件系统
325ae51788e9: Pull complete 
adcb9439d751: Pull complete 
174c7fe16c78: Pull complete 
698058ef136c: Pull complete 
4690143a669e: Pull complete 
f7599a246fd6: Pull complete 
35a55bf0c196: Pull complete 
790ac54f4c47: Pull complete 
18602acc97e1: Pull complete 
365caa3500d0: Pull complete 
Digest: sha256:b1cc887ed32cc6c2f217b12703bd05f503f2037892c8bb226047fe5dff85a109	# 签名
Status: Downloaded newer image for mysql:latest
docker.io/library/mysql:latest	# 真实地址


docker pull mysql
# 等价于
docker pull docker.io/library/mysql:latest


[root@VM-0-13-centos ~]# docker pull mysql:5.7 		# 下载具体版本 
5.7: Pulling from library/mysql
45b42c59be33: Already exists 
b4f790bd91da: Already exists 
325ae51788e9: Already exists 
adcb9439d751: Already exists 
174c7fe16c78: Already exists 
698058ef136c: Already exists 
4690143a669e: Already exists 
66676c1ab9b3: Pull complete 
25ebf78a38b6: Pull complete 
349a839d5e27: Pull complete 
40b03e3e5980: Pull complete 
Digest: sha256:853105ad984a9fe87dd109be6756e1fbdba8b003b303d88ac0dda6b455f36556
Status: Downloaded newer image for mysql:5.7
docker.io/library/mysql:5.7


[root@VM-0-13-centos ~]# docker images
REPOSITORY   TAG       IMAGE ID       CREATED       SIZE
nginx        latest    35c43ace9216   7 days ago    133MB
mysql        5.7       5f47254ca581   2 weeks ago   449MB
mysql        latest    2933adc350f3   2 weeks ago   546MB

2.2.4 docker rmi删除镜像

docker rmi -f 容器id		# 删除指定的容器
docker rmi -f 容器id 容器id 容器id	# 删除多个容器
docker rmi -f $(docker images -aq)		# 删除全部的镜像
 [root@VM-0-13-centos ~]# docker images
REPOSITORY   TAG       IMAGE ID       CREATED       SIZE
nginx        latest    35c43ace9216   7 days ago    133MB
mysql        5.7       5f47254ca581   2 weeks ago   449MB
mysql        latest    2933adc350f3   2 weeks ago   546MB
[root@VM-0-13-centos ~]# docker rmi -f 35c43ace9216
Untagged: nginx:latest
Untagged: nginx@sha256:f3693fe50d5b1df1ecd315d54813a77afd56b0245a404055a946574deb6b34fc
Deleted: sha256:35c43ace9216212c0f0e546a65eec93fa9fc8e96b25880ee222b7ed2ca1d2151
[root@VM-0-13-centos ~]# docker images
REPOSITORY   TAG       IMAGE ID       CREATED       SIZE
mysql        5.7       5f47254ca581   2 weeks ago   449MB
mysql        latest    2933adc350f3   2 weeks ago   546MB

2.3 容器命令

Note:有了镜像才可以创建容器

2.3.1 新建容器并启动

docker run [OPTIONS] IMAGE [COMMAND] [ARG...]

参数说明:
--name="Name"			容器名字   name1, name2 ... 用来区分容器
-d									后台方式运行
-it									使用交互方式运行,进入容器看内容
-p									指定容器的端口  -p  8080:8080
				-p ip:主机端口:容器端口
				-p 主机端口: 容器端口 (常用)
				-p 容器端口
				容器端口
-P								随机指定端口


[root@localhost testdocker]# docker images
REPOSITORY   TAG       IMAGE ID       CREATED        SIZE
centos       latest    300e315adb2f   2 months ago   209MB

# 启动并进入容器
[root@localhost testdocker]# docker run -it centos /bin/bash

# 注意此处,已经进入到centos容器中了
[root@303f29a91107 /]# ls
bin  dev  etc  home  lib  lib64  lost+found  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var

# 从容器中退出返回主机
[root@303f29a91107 /]# exit
exit
[root@localhost testdocker]# 

2.3.2 列出所有运行的容器

docker ps

  -a, --all             Show all containers (default shows just running)
  -f, --filter filter   Filter output based on conditions provided
      --format string   Pretty-print containers using a Go template
  -n, --last int        Show n last created containers (includes all states) (default -1)
  -l, --latest          Show the latest created container (includes all states)
      --no-trunc        Don't truncate output
  -q, --quiet           Only display container IDs
  -s, --size            Display total file sizes
# 正在运行的容器
[root@localhost testdocker]# docker ps
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES
# 显示所有容器
[root@localhost testdocker]# docker ps -a
CONTAINER ID   IMAGE     COMMAND       CREATED         STATUS                     PORTS     NAMES
303f29a91107   centos    "/bin/bash"   9 minutes ago   Exited (0) 9 minutes ago             heuristic_franklin

# 显示最近的一个容器
[root@localhost testdocker]# docker ps -a -n=1
CONTAINER ID   IMAGE     COMMAND       CREATED          STATUS                      PORTS     NAMES
303f29a91107   centos    "/bin/bash"   15 minutes ago   Exited (0) 15 minutes ago             heuristic_franklin

2.3.3 退出容器

exit  # 停止容器运行并退出
ctrl + P + Q   # 容器不停止退出

2.3.4 删除容器

docker rm 容器id							# 删除所给id的容器(不能删除运行中的,要删除需要-f选项,强制删除)
docker rm -f $(docker ps -aq)		# 删除所有容器
docker ps -a |xargs docker rm -f 	# 删除所有容器
[root@localhost testdocker]# docker ps -aq
303f29a91107
[root@localhost testdocker]# docker rm -f $(docker ps -aq)
303f29a91107
[root@localhost testdocker]# docker ps -aq
[root@localhost testdocker]# 

2.3.5 启动和停止容器

docker start 容器id			#启动动容器
docker restart 容器id		#重启容器
docker stop 容器id			#停止当前正在运行的容器
docker kill 容器id			#强制停止当前容器

2.4 常用其他命令

2.4.1 后台启动

docker run -d 镜像名

[root@localhost testdocker]# docker ps -a
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES
[root@localhost testdocker]# docker run -d centos
a72e5614e1b9c8fa269b50108983469156b5f29f8e63754bc564b4cd2cd7771d
[root@localhost testdocker]# docker ps
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES
[root@localhost testdocker]# docker ps -a
CONTAINER ID   IMAGE     COMMAND       CREATED          STATUS                      PORTS     NAMES
a72e5614e1b9   centos    "/bin/bash"   17 seconds ago   Exited (0) 15 seconds ago             musing_cray

Note:docker容器使用后台运行,句必须有一个前台进程,docker发现没有应用就会自动停止!

2.4.2 查看日志

[root@localhost testdocker]# docker logs --help

Usage:  docker logs [OPTIONS] CONTAINER

Fetch the logs of a 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)
[root@localhost testdocker]# docker run -d centos /bin/bash -C "while true;do echo fancy;sleep 3;done"
a4ec12f65c4fc4c6f717ffd8d36bf692d61252cb16e511f9e1c9057676eb57d6
[root@localhost testdocker]# docker ps
CONTAINER ID   IMAGE     COMMAND       CREATED         STATUS         PORTS     NAMES
9e1e8fd0fd95   centos    "/bin/bash"   8 minutes ago   Up 8 minutes             elegant_feynman
[root@localhost testdocker]# docker run -d centos /bin/bash -c "while true;do echo fancy;sleep 3;done"
a322207f709ece46ee0e0e677133c39117f8ca8684af8415f9f1d3b834cd8102
[root@localhost testdocker]# docker ps
CONTAINER ID   IMAGE     COMMAND                  CREATED          STATUS         PORTS     NAMES
a322207f709e   centos    "/bin/bash -c 'while…"   12 seconds ago   Up 9 seconds             objective_edison
9e1e8fd0fd95   centos    "/bin/bash"              9 minutes ago    Up 9 minutes             elegant_feynman
[root@localhost testdocker]# docker logs -f -t --tail -n 20 a322207f709e
"docker logs" requires exactly 1 argument.
See 'docker logs --help'.

Usage:  docker logs [OPTIONS] CONTAINER

Fetch the logs of a container
[root@localhost testdocker]# docker logs -tf --tail 20 a322207f709e
2021-02-25T13:46:55.580039078Z fancy
2021-02-25T13:46:58.584268035Z fancy
2021-02-25T13:47:01.586785693Z fancy
2021-02-25T13:47:04.589277833Z fancy
2021-02-25T13:47:07.592404963Z fancy
2021-02-25T13:47:10.595028975Z fancy
2021-02-25T13:47:13.597351628Z fancy
2021-02-25T13:47:16.600211559Z fancy
2021-02-25T13:47:19.603259169Z fancy
2021-02-25T13:47:22.606129199Z fancy
2021-02-25T13:47:25.608407915Z fancy
2021-02-25T13:47:28.611234019Z fancy
2021-02-25T13:47:31.613651590Z fancy
2021-02-25T13:47:34.618484751Z fancy
2021-02-25T13:47:37.624562865Z fancy
2021-02-25T13:47:40.627609493Z fancy
2021-02-25T13:47:43.630150550Z fancy
2021-02-25T13:47:46.632715132Z fancy
2021-02-25T13:47:49.642774008Z fancy
2021-02-25T13:47:52.640246056Z fancy
2021-02-25T13:47:55.647093146Z fancy
2021-02-25T13:47:58.653737677Z fancy
2021-02-25T13:48:01.656047755Z fancy
2021-02-25T13:48:04.659349493Z fancy
^C
[root@localhost testdocker]#

2.4.3 查看容器中进程信息

docker top 容器id

# 先选定要查看容器的id
[root@localhost testdocker]# docker ps
CONTAINER ID   IMAGE     COMMAND                  CREATED          STATUS          PORTS     NAMES
a322207f709e   centos    "/bin/bash -c 'while…"   7 minutes ago    Up 7 minutes              objective_edison
9e1e8fd0fd95   centos    "/bin/bash"              16 minutes ago   Up 16 minutes             elegant_feynman
# 查看给定id容器的进程信息
[root@localhost testdocker]# docker top a322207f709e
UID                 PID                 PPID                C                   STIME               TTY                 TIME                CMD
root                184986              184964              0                   21:46               ?                   00:00:00            /bin/bash -c while true;do echo fancy;sleep 3;done
root                185581              184986              0                   21:54               ?                   00:00:00            /usr/bin/coreutils --coreutils-prog-shebang=sleep /usr/bin/sleep 3
[root@localhost testdocker]# 

2.4.4 查看容器内部信息

# 用法
[root@localhost testdocker]# docker inspect --help

Usage:  docker inspect [OPTIONS] NAME|ID [NAME|ID...]

Return low-level information on Docker objects

Options:
  -f, --format string   Format the output using the given Go template
  -s, --size            Display total file sizes if the type is container
      --type string     Return JSON for specified type
# 查看运行中的容器
[root@localhost testdocker]# docker ps -a
CONTAINER ID   IMAGE     COMMAND                  CREATED             STATUS                         PORTS     NAMES
a322207f709e   centos    "/bin/bash -c 'while…"   18 minutes ago      Up 18 minutes                            objective_edison
a4ec12f65c4f   centos    "/bin/bash -C 'while…"   19 minutes ago      Exited (127) 19 minutes ago              brave_mestorf
9e1e8fd0fd95   centos    "/bin/bash"              27 minutes ago      Up 27 minutes                            elegant_feynman
a72e5614e1b9   centos    "/bin/bash"              About an hour ago   Exited (0) About an hour ago             musing_cray
[root@localhost testdocker]# docker inspect a322207f709e
[
    {
        "Id": "a322207f709ece46ee0e0e677133c39117f8ca8684af8415f9f1d3b834cd8102",
        "Created": "2021-02-25T13:46:23.922794199Z",
        "Path": "/bin/bash",
        "Args": [
            "-c",
            "while true;do echo fancy;sleep 3;done"
        ],
        "State": {
            "Status": "running",
            "Running": true,
            "Paused": false,
            "Restarting": false,
            "OOMKilled": false,
            "Dead": false,
            "Pid": 184986,
            "ExitCode": 0,
            "Error": "",
            "StartedAt": "2021-02-25T13:46:25.540036291Z",
            "FinishedAt": "0001-01-01T00:00:00Z"
        },
        ...
    }
]
[root@localhost testdocker]# 

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

通常容器时使用后台方式运行的,需要进入容器修改一些配置:

2.4.5.1 docker exec -it 容器id bashshell(进入容器落后开启新的命令行,常用)
[root@localhost testdocker]# docker exec --help

Usage:  docker exec [OPTIONS] CONTAINER COMMAND [ARG...]

Run a command in a running container

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
[root@localhost testdocker]# docker ps
CONTAINER ID   IMAGE     COMMAND                  CREATED          STATUS          PORTS     NAMES
a322207f709e   centos    "/bin/bash -c 'while…"   34 minutes ago   Up 34 minutes             objective_edison
9e1e8fd0fd95   centos    "/bin/bash"              43 minutes ago   Up 42 minutes             elegant_feynman
[root@localhost testdocker]# docker exec -it 9e1e8fd0fd95 /bin/bash
[root@9e1e8fd0fd95 /]# ls
bin  dev  etc  home  lib  lib64  lost+found  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var
[root@9e1e8fd0fd95 /]# ps -ef
UID          PID    PPID  C STIME TTY          TIME CMD
root           1       0  0 13:37 pts/0    00:00:00 /bin/bash
root          15       0  1 14:22 pts/1    00:00:00 /bin/bash
root          30      15  0 14:22 pts/1    00:00:00 ps -ef
[root@9e1e8fd0fd95 /]#
2.4.5.2 docker attach 容器id(进入容器正在进行的终端)
[root@localhost testdocker]# docker attach --help

Usage:  docker attach [OPTIONS] CONTAINER

Attach local standard input, output, and error streams to a running container

Options:
      --detach-keys string   Override the key sequence for detaching a container
      --no-stdin             Do not attach STDIN
      --sig-proxy            Proxy all received signals to the process (default true)

[root@localhost testdocker]# docker attach 容器id
正在执行当前代码。。。

2.4.6 从容器中拷贝文件到主机

docker cp 容器id:容器内路径 目的主机路径

[root@localhost testdocker]# docker cp --help

Usage:  docker cp [OPTIONS] CONTAINER:SRC_PATH DEST_PATH|-
	docker cp [OPTIONS] SRC_PATH|- CONTAINER:DEST_PATH

Copy files/folders between a container and the local filesystem

Use '-' as the source to read a tar archive from stdin
and extract it to a directory destination in a container.
Use '-' as the destination to stream a tar archive of a
container source to stdout.

Options:
  -a, --archive       Archive mode (copy all uid/gid information)
  -L, --follow-link   Always follow symbol link in SRC_PATH
# 拷贝操作前当前目录
[root@localhost testdocker]# ls
flasky:latest
# 进入容器内部,准备演示数据
[root@localhost testdocker]# docker attach e7a7549e34f3
[root@e7a7549e34f3 /]# cd /home/
[root@e7a7549e34f3 home]# ls
[root@e7a7549e34f3 home]# touch demo.text
# 退出容器,回到主机
[root@e7a7549e34f3 home]# exit
exit
# 由于exit出容器,容器停止
[root@localhost testdocker]# docker ps
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES
[root@localhost testdocker]# docker ps -a
CONTAINER ID   IMAGE     COMMAND       CREATED          STATUS                      PORTS     NAMES
e7a7549e34f3   centos    "/bin/bash"   14 minutes ago   Exited (0) 15 seconds ago             priceless_golick
# 复制e7a7549e34f3:/home/demo.text文件到主机当前目录
[root@localhost testdocker]# docker cp e7a7549e34f3:/home/demo.text .
# 可以发现,当前目录多了一个复制过来的文件demo.text
[root@localhost testdocker]# ls
demo.text  flasky:latest
[root@localhost testdocker]# 

这个例子是一个手动拷贝的过程,使用-v 卷技术可以实现自动同步

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值