容器使用
获取镜像
$ docker pull ubuntu
启动容器
$ docker run -it ubuntu /bin/bash
[root@web1 ~]# docker run -it ubuntu /bin/bash
root@fa57e7667d42:/# exit
exit
查看所有容器
$docker ps -a
[root@web1 ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
fa57e7667d42 ubuntu "/bin/bash" 53 seconds ago Exited (0) 38 seconds ago silly_driscoll
启动一个已停止的容器
[root@web1 ~]# docker start fa57e7667d42
fa57e7667d42
[root@web1 ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
fa57e7667d42 ubuntu "/bin/bash" 2 minutes ago Up 33 seconds silly_driscoll
后台运行
[root@web1 ~]# docker run -itd --name ubuntu-test ubuntu /bin/bash
f3519be187784ebf1aa33815ab2fc52c4be1ae21fe290ff1485c80013b93c300
停止一个容器
$ docker stop <容器 ID>
[root@web1 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f3519be18778 ubuntu "/bin/bash" About a minute ago Up About a minute ubuntu-test
[root@web1 ~]# docker stop f3519be18778
f3519be18778
重启一个容器
$ docker restart <容器 ID>
[root@web1 ~]# docker restart f3519be18778
f3519be18778
进入容器有2中方式
第一种
$docker attach
[root@web1 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f3519be18778 ubuntu "/bin/bash" 3 minutes ago Up 2 seconds ubuntu-test
[root@web1 ~]# docker attach f3519be18778
root@f3519be18778:/# exit
exit
[root@web1 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@web1 ~]#
注意:如果从这个容器退出,会导致容器的停止
第二种
$docker exec
[root@web1 ~]# docker restart f3519be18778
f3519be18778
[root@web1 ~]# docker exec -it f3519be18778 /bin/bash
root@f3519be18778:/# exit
exit
[root@web1 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f3519be18778 ubuntu "/bin/bash" 8 minutes ago Up 28 seconds ubuntu-test
[root@web1 ~]#
注意:如果以这种方式从容器退出,不会导致容器的停止,这就是为什么推荐大家使用docker exec的原因
导出和导入容器
导出容器
如果要导出本地某个容器,可以使用docker export命令
$ docker export <容器ID> > ubuntu.tar
[root@web1 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f3519be18778 ubuntu "/bin/bash" 8 minutes ago Up 28 seconds ubuntu-test
[root@web1 ~]# docker export f3519be18778 > ubuntu.tar
[root@web1 ~]# ls
anaconda-ks.cfg host.py ip.yaml mongodb-linux-x86_64-rhel70-3.6.9.tgz
file.csv inventory mongodb-linux-x86_64-rhel70-3.6.9 ubuntu.tar
[root@web1 ~]#
导入容器快照
可以使用docker import从容器快照文件中再导入为镜像
[root@web1 ~]# cat ./ubuntu.tar | docker import - test/ubuntu:v1
sha256:74bee83de2c94d6f7f70c71d6d9abc2a85257423b5b25ea02ba62bca094d913c
[root@web1 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
test/ubuntu v1 74bee83de2c9 28 seconds ago 72.9MB
ubuntu latest d70eaf7277ea 5 days ago 72.9MB
[root@web1 ~]#
此外,也可以通过指定 URL 或者某个目录来导入,例如:
$ docker import http://example.com/exampleimage.tgz example/imagerepo
删除容器
删除容器使用docker rm 命令:
$docker rm -f
[root@web1 ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f3519be18778 ubuntu "/bin/bash" 32 minutes ago Up 24 minutes ubuntu-test
fa57e7667d42 ubuntu "/bin/bash" 2 hours ago Exited (0) 2 hours ago silly_driscoll
[root@web1 ~]# docker rm -f fa57e7667d42
fa57e7667d42
[root@web1 ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f3519be18778 ubuntu "/bin/bash" 33 minutes ago Up 25 minutes ubuntu-test
[root@web1 ~]#
下面的命令可以清理掉所有处于终止状态的容器。
$ docker container prune
运行一个web应用
runoob@runoob:~# docker pull training/webapp # 载入镜像
runoob@runoob:~# docker run -d -P training/webapp python app.py
我们也可以通过 -p 参数来设置不一样的端口:
[root@web1 ~]# docker run -d -p 5000:5000 training/webapp python app.py
c71bdd75c80ff0744dcab47c077bdefc862c2996a62eec2d07524c30e9a25f6d
[root@web1 ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c71bdd75c80f training/webapp "python app.py" 3 seconds ago Up 2 seconds 0.0.0.0:5000->5000/tcp zen_bouman
ccc5f08a6edf training/webapp "python app.py" 19 hours ago Up 19 hours 0.0.0.0:32768->5000/tcp eager_joliot
f3519be18778 ubuntu "/bin/bash" 2 weeks ago Up 2 weeks ubuntu-test
网络端口的快捷方式
通过 docker ps 命令可以查看到容器的端口映射,docker 还提供了另一个快捷方式 docker port,使用 docker port 可以查看指定 (ID 或者名字)容器的某个确定端口映射到宿主机的端口号
[root@web1 ~]# docker port c71bdd75c80f
5000/tcp -> 0.0.0.0:5000
[root@web1 ~]# docker port eager_joliot
5000/tcp -> 0.0.0.0:32768
查看 WEB 应用程序日志
docker logs [ID或者名字]可以查看容器内部的标准输出
[root@web1 ~]# docker logs c71bdd75c80f
* Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)
192.168.72.1 - - [19/Nov/2020 02:46:37] "GET / HTTP/1.1" 200 -
192.168.72.1 - - [19/Nov/2020 02:46:37] "GET /favicon.ico HTTP/1.1" 404 -
192.168.72.1 - - [19/Nov/2020 02:46:42] "GET / HTTP/1.1" 200 -
[root@web1 ~]# docker logs zen_bouman
* Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)
192.168.72.1 - - [19/Nov/2020 02:46:37] "GET / HTTP/1.1" 200 -
192.168.72.1 - - [19/Nov/2020 02:46:37] "GET /favicon.ico HTTP/1.1" 404 -
192.168.72.1 - - [19/Nov/2020 02:46:42] "GET / HTTP/1.1" 200 -
查看WEB应用程序容器的进程
docker top
[root@web1 ~]# docker top zen_bouman
UID PID PPID C STIME TTY TIME CMD
root 59457 59440 0 10:45 ? 00:00:00 python app.py
[root@web1 ~]# docker top c71bdd75c80f
UID PID PPID C STIME TTY TIME CMD
root 59457 59440 0 10:45 ? 00:00:00 python app.py
检查web应用程序
docker inspect 来查看Docker的底层信息。
[root@web1 ~]# docker inspect zen_bouman
[
{
"Id": "c71bdd75c80ff0744dcab47c077bdefc862c2996a62eec2d07524c30e9a25f6d",
"Created": "2020-11-19T02:45:12.143072083Z",
"Path": "python",
"Args": [
"app.py"
],
"State": {
"Status": "running",
"Running": true,
"Paused": false,
"Restarting": false,
"OOMKilled": false,
"Dead": false,
"Pid": 59457,
"ExitCode": 0,
"Error": "",
"StartedAt": "2020-11-19T02:45:13.220514351Z",
"FinishedAt": "0001-01-01T00:00:00Z"
},
"Image": "sha256:6fae60ef344644649a39240b94d73b8ba9c67f898ede85cf8e947a887b3e6557",
...
停止WEB应用容器
[root@web1 ~]# docker stop zen_bouman
zen_bouman
重启WEB应用容器
[root@web1 ~]# docker start zen_bouman
zen_bouman
docker ps -l 查询最后一次创建的容器:
[root@web1 ~]# docker ps -l
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c71bdd75c80f training/webapp "python app.py" 42 minutes ago Up 13 seconds 0.0.0.0:5000->5000/tcp zen_bouman
移除WEB应用容器
删除容器时,容器必须是停止状态,否则会报如下错误
[root@web1 ~]# docker rm c71bdd75c80f
Error response from daemon: You cannot remove a running container c71bdd75c80ff0744dcab47c077bdefc862c2996a62eec2d07524c30e9a25f6d. Stop the container before attempting removal or force remove
[root@web1 ~]# docker stop c71bdd75c80f
c71bdd75c80f
[root@web1 ~]# docker rm c71bdd75c80f
c71bdd75c80f
[root@web1 ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
ccc5f08a6edf training/webapp "python app.py" 20 hours ago Up 20 hours 0.0.0.0:32768->5000/tcp eager_joliot
f3519be18778 ubuntu "/bin/bash" 2 weeks ago Up 2 weeks ubuntu-test
网友评论