Docker容器

一、创建容器

1.新建容器

D:docker_test>docker create -it ubuntu:18.04
27a54582fc805198ef33509a0be1afc75518dc20b2adc8ece019eef94c9391e1

2.启动容器

D:docker_test>docker start 27a54582fc80
27a54582fc80

3.新建并启动容器

D:docker_test>docker run ubuntu:18.04 /bin/echo "hello world"
hello world

启动一个bash终端,允许用户进行交互:

D:docker_test>docker run -it ubuntu:18.04 /bin/bash
root@4bce07ec5055:/#

-t:分配一个伪终端并绑定在容器的标准输入上

-i:让容器的标准输入打开

退出容器

root@4bce07ec5055:/# exit
exit

4.守护进程运行

D:docker_test>docker run -d ubuntu:18.04  /bin/sh -c "while true; do echo hello world; sleep 1; done"
5de1d8da353b84babeeea7626b82ab541956ed0abb1e6ac5cedc6c043f76d453

5.查看容器输出

D:docker_test>docker logs 5de1d8da353b
hello world
hello world

二、停止容器

1.暂停容器

D:docker_test>docker pause 27a54582fc80
27a54582fc80

2.停止容器

D:docker_test>docker stop 27a54582fc80

3.重启容器

D:docker_test>docker restart 27a54582fc80
27a54582fc80

三、进入容器

在使用-d参数时,容器启动后会进入后台,用户无法看到容器中的信息,也无法进行操作。这个时候如果需要进入容器进行操作,推荐使用官方的attach或exec命令。

1.attach命令

D:docker_test>docker attach 27a54582fc80
root@27a54582fc80:/# exit
exit

当退出容器后,容器状态也停止

D:docker_test>docker ps
CONTAINER ID   IMAGE          COMMAND                  CREATED          STATUS                   PORTS     NAMES
5de1d8da353b   ubuntu:18.04   "/bin/sh -c 'while t…"   13 minutes ago   Up 13 minutes (Paused)             xenodochial_blackburn

2.exec命令

D:docker_test>docker exec -it 5de1d8da353b /bin/bash
root@5de1d8da353b:/# exit
exit

D:docker_test>docker ps
CONTAINER ID   IMAGE          COMMAND                  CREATED          STATUS              PORTS     NAMES
5de1d8da353b   ubuntu:18.04   "/bin/sh -c 'while t…"   19 minutes ago   Up About a minute             xenodochial_blackburn

退出容器,容器状态不停止

四、删除容器

D:docker_test>docker rm 27a54582fc80
27a54582fc80

默认情况下,docker rm命令只删除已经停止或退出状态的容器,并不能删除正在运行的容器

-f:强制停止并删除正在运行的容器

-l:删除容器的连接,但保留容器

-v:删除容器挂载的数据卷

五、导入和导出容器

1.导出容器

D:docker_test>docker export -o test_for_a41e52d3fb88.tar a41e52d3fb88

-o:指定导出tar的文件名

2.导入容器

D:docker_test>docker import test_for_a41e52d3fb88.tar test/ubuntu:18.041

注意:导入的容器会变成镜像,而不是容器

D:docker_test>docker images
REPOSITORY    TAG       IMAGE ID       CREATED              SIZE
test/ubuntu   18.041    abd89c228b21   About a minute ago   63.1MB

六、查看容器

1.查看容器详情

D:docker_test>docker inspect 5de1d8da353b

2.查看容器内进程

D:docker_test>docker top 5de1d8da353b
UID                 PID                 PPID                C                   STIME               TTY                 TIME                CMD
root                2291                2271                0                   13:09               ?                   00:00:01            /bin/sh -c while true; do echo hello world; sleep 1; done
root                3801                2291                0                   13:32               ?                   00:00:00            sleep 1

3.查看统计信息

D:docker_test>D:docker_test>docker stats

4.其他命令

 1)复制文件

D:docker_test>docker cp data 5de1d8da353b:/tmp/

 2)查看变更

D:docker_test>docker diff 5de1d8da353b
C /root
A /root/.bash_history

 3)查看端口映射

D:docker_test>docker port 4bce07ec5055

 4)更新配置

D:docker_test>docker update 4bce07ec5055
原文地址:https://www.cnblogs.com/shier-dong/p/15487256.html