命令
docker exec -it 容器id bashshell
#测试
➜ ~ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
55321bcae33d centos “/bin/sh -c 'while t…” 10 minutes ago Up 10 minutes bold_bell
a7215824a4db centos “/bin/sh -c 'while t…” 13 minutes ago Up 13 minutes zen_kepler
55a31b3f8613 centos “/bin/bash” 15 minutes ago Up 15 minutes lucid_clarke
➜ ~ docker exec -it 55321bcae33d /bin/bash
[root@55321bcae33d /]#

# 方式二
docker attach 容器id
#测试
docker attach 55321bcae33d 
正在执行当前的代码...
区别
#docker exec #进入当前容器后开启一个新的终端,可以在里面操作。(常用)
#docker attach # 进入容器正在执行的终端

6. 从容器内拷贝到主机上
docker cp 容器id:容器内路径  主机目的路径
[root@iz2zeak7sgj6i7hrb2g862z ~]# docker ps
CONTAINER ID
IMAGE    COMMAND
CREATED
STATUS
PORTS
NAMES
56a5583b25b4
centos   "/bin/bash" 7seconds ago    Up 6 seconds
#1. 进入docker容器内部
[root@iz2zeak7sgj6i7hrb2g862z ~]# docker exec -it 56a5583b25b4 /bin/bash
[root@55321bcae33d /]# ls
bin  dev  etc  home  lib  lib64  lost+found  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var
#新建一个文件
[root@55321bcae33d /]# echo "hello" > java.java
[root@55321bcae33d /]# cat hello.java 
hello
[root@55321bcae33d /]# exit
exit
#hello.java拷贝到home文件加下
[root@iz2zeak7sgj6i7hrb2g862z /]# docker cp 56a5583b25b4:/hello.java /home 
[root@iz2zeak7sgj6i7hrb2g862z /]# cd /home
[root@iz2zeak7sgj6i7hrb2g862z home]# ls -l	#可以看见java.java存在
total 8
-rw-r--r-- 1 root root    0 May 19 22:09 haust.java
-rw-r--r-- 1 root root    6 May 22 11:12 java.java
drwx------ 3 www  www  4096 May  8 12:14 www

命令大全
  attach
Attach local standard input, output, and error streams to a running container
  #当前shell下 attach连接指定运行的镜像
  build
Build an image from a Dockerfile # 通过Dockerfile定制镜像
  commit
Create a new image from a container's changes #提交当前容器为新的镜像
  cp
Copy files/folders between a container and the local filesystem #拷贝文件
  create
Create a new container #创建一个新的容器
  diff
Inspect changes to files or directories on a container's filesystem #查看docker容器的变化
  events
Get real time events from the server # 从服务获取容器实时时间
  exec
Run a command in a running container # 在运行中的容器上运行命令
  export
Export a container's filesystem as a tar archive #导出容器文件系统作为一个tar归档文件[对应import]
  history
Show the history of an image # 展示一个镜像形成历史
  images
List images #列出系统当前的镜像
  import
Import the contents from a tarball to create a filesystem image #从tar包中导入内容创建一个文件系统镜像
  info
Display system-wide information # 显示全系统信息
  inspect
Return low-level information on Docker objects #查看容器详细信息
  kill
Kill one or more running containers # kill指定docker容器
  load
Load an image from a tar archive or STDIN #从一个tar包或标准输入中加载一个镜像[对应save]
  login
Log in to a Docker registry #
  logout
Log out from a Docker registry
  logs
Fetch the logs of a container
  pause
Pause all processes within one or more containers
  port
List port mappings or a specific mapping for the container
  ps
List containers
  pull
Pull an image or a repository from a registry
  push
Push an image or a repository to a registry
  rename
Rename a container
  restart
Restart one or more containers
  rm
Remove one or more containers
  rmi
Remove one or more images
  run
Run a command in a new container
  save
Save one or more images to a tar archive (streamed to STDOUT by default)
  search
Search the Docker Hub for images
  start
Start one or more stopped containers
  stats
Display a live stream of container(s) resource usage statistics
  stop
Stop one or more running containers
  tag
Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE
  top
Display the running processes of a container
  unpause
Unpause all processes within one or more containers
  update
Update configuration of one or more containers
  version
Show the Docker version information
  wait
Block until one or more containers stop, then print their exit codes
2.5 练习
1. 安装 Nginx
#1. 搜索镜像 search 建议大家去docker搜索,可以看到帮助文档
[root@iz2zeak7sgj6i7hrb2g862z ~]# docker search nginx
#2. 拉取下载镜像 pull
[root@iz2zeak7sgj6i7hrb2g862z ~]# docker pull nginx
#3. 查看是否下载成功镜像
[root@iz2zeak7sgj6i7hrb2g862z ~]# docker images
#3. 运行测试
# -d 后台运行
# --name 给容器命名
# -p 宿主机端口:容器内部端口
[root@iz2zeak7sgj6i7hrb2g862z ~]# docker run -d --name nginx01 -p 3344:80 nginx
aa664b0c8ed98f532453ce1c599be823bcc1f3c9209e5078615af416ccb454c2
#4. 查看正在启动的镜像
[root@iz2zeak7sgj6i7hrb2g862z ~]# docker ps
CONTAINER ID
IMAGE
COMMAND
CREATED
STATUS
PORTS
NAMES
75943663c116
nginx
"nginx -g 'daemon of…"   41 seconds ago
Up 40 seconds
0.0.0.0:82->80/tcp   nginx00
#5. 进入容器
[root@iz2zeak7sgj6i7hrb2g862z ~]# docker exec -it nginx01 /bin/bash #进入
root@aa664b0c8ed9:/# whereis nginx	#找到nginx位置
nginx: /usr/sbin/nginx /usr/lib/nginx /etc/nginx /usr/share/nginx
root@aa664b0c8ed9:/# cd /etc/nginx/
root@aa664b0c8ed9:/etc/nginx# ls
conf.d	fastcgi_params	koi-utf  koi-win  mime.types  modules  nginx.conf  scgi_params	uwsgi_params  win-utf
#6. 退出容器
root@aa664b0c8ed9:/etc/nginx# exit
exit
#7. 停止容器
[root@iz2zeak7sgj6i7hrb2g862z ~]# docker ps
CONTAINER ID
IMAGE
COMMAND
CREATED
STATUS
PORTS
NAMES
aa664b0c8ed9
nginx
"nginx -g 'daemon of…"   10 minutes ago
Up 10 minutes
0.0.0.0:3344->80/tcp   nginx01
[root@iz2zeak7sgj6i7hrb2g862z ~]# docker stop aa664b0c8ed9
宿主机端口 和 容器内部端口 以及端口暴露: