使用docker管理容器:
理解下容器、镜像、仓库、docker daemon和docker client三者之间的关系:
总结:
1)docker客户端下达命令docker daemon。
2)docker daemon 先到本地镜像目录查找容器,如果没有,则到镜像仓库下载镜像到本地,然后在通过这个镜像启动容器。
3)docker daemon借助于镜像启动容器。
启动容器:
验证是否有镜像在本地:
docker images
本地没有镜像,需要到默认仓库中search:
[root@vm1 ~]# docker search centos
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
centos DEPRECATED; The official build of CentOS. 7601 [OK]
kasmweb/centos-7-desktop CentOS 7 desktop for Kasm Workspaces 38
bitnami/centos-base-buildpack Centos base compilation image 0 [OK]
couchbase/centos7-systemd centos7-systemd images with additional debug… 8 [OK]
continuumio/centos5_gcc5_base 3
datadog/centos-i386 0
dokken/centos-7 CentOS 7 image for kitchen-dokken 5
默认仓库:dockerhub
本地没有镜像,需要下载镜像到本地:
[root@vm1 ~]# docker pull centos:latest
latest: Pulling from library/centos
a1d0c7532777: Pull complete
Digest: sha256:a27fd8080b517143cbbbab9dfb7c8571c40d67d534bbdee55bd6c473f432b177
Status: Downloaded newer image for centos:latest
docker.io/library/centos:latest
通过docker images进行检查镜像:
docker images
运行容器:
docker run -it --name=c1 centos:latest /bin/bash
[root@vm1 ~]# docker run -it --name=c1 centos:latest /bin/bash
[root@d6bfe9c8a0d8 /]# ls
bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var
[root@d6bfe9c8a0d8 /]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
4: eth0@if5: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default
link/ether 02:42:ac:11:00:02 brd ff:ff:ff:ff:ff:ff link-netnsid 0
inet 172.17.0.2/16 brd 172.17.255.255 scope global eth0
valid_lft forever preferred_lft forever
[root@d6bfe9c8a0d8 /]#
-it: 终端交互。
容器:是隔离出来的空间。
镜像:是不可写。
查看正在运行的容器:
# 查看正在运行的容器
docker ps
docker ps -a
docker ps --all
# 停止一个正在运行的容器,d是容器ID的简写,也可以写容器的名称,但是ID要能够唯一被识别
docker stop d
# 关闭多个正在运行的容器:
docker stop c1 c2
# 启动容器
docker start c1
# 开启已停止的容器
docker start c1
# docker attach --help
[root@vm1 ~]# 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)
# 进入到容器,但是不能进入到一个停止的容器
docker attach c1
# 删除已停止的容器
[root@vm1 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
13ebf1339635 centos:latest "/bin/bash" About an hour ago Up 5 minutes c1
273f4d145387 centos:latest "/bin/bash" 2 hours ago Up 2 hours c2
[root@vm1 ~]# docker rm c1
Error response from daemon: You cannot remove a running container 13ebf1339635ea9c78440b9781aae1dd5a6c5b581baf014cfb49be4a89b8a769. Stop the container before attempting removal or force remove
# 停止容器
docker stop c1
# 删除容器
docker rm c1
我们理解下这句话:
Attach local standard input, output, and error streams to a running container
附加本地标准输入输出和错误输出到一个正在运行的容器。