docker 三大组建:
镜像 image
容器 container
仓库 repository
docker利用容器来运行应用,容器是从镜像创建的运行实例
仓库分为公开仓库 docker hub,私有仓库Private
1.安装docker
ubuntu
sudoapt−getupdate
sudo apt-get install -y docker.io
sudoln−sf/usr/bin/docker.io/usr/local/bin/docker
sudo sed -i '$acomplete -F _docker docker’ /etc/bash_completion.d/docker.io
centos7
$ sudo yum install docker
开启docker服务
service docker start
Docker 镜像:docker运行容器前需要在本地存在对应的镜像,如果不存在镜像,会从镜像仓库下载(默认的镜像仓库是docker hub 公共注册服务器中的仓库)
2.如何获取镜像
可以使用 docker pull命令从仓库获取所需要的镜像
例如:从docker hub仓库中获取ubuntu12.04的镜像:
docker pull ubuntu:12.04
Pulling repository ubuntu
ab8e2728644c: Pulling dependent layers
511136ea3c5a: Download complete
5f0ffaa9455e: Download complete
a300658979be: Download complete
904483ae0c30: Download complete
ffdaafd1ca50: Download complete
d047ae21eeaf: Download complete
该命令实际上相当于
sudodockerpullregistry.hub.docker.com/ubuntu:12.04命令,即从注册服务器registry.hub.docker.com中的ubuntu仓库来下载标记为12.04的镜像。有时候官方仓库注册服务器下载较慢,可以从其他仓库下载。从其它仓库下载时需要指定完整的仓库注册服务器地址。例如
sudo docker pull dl.dockerpool.com:5000/ubuntu:12.04
Pulling dl.dockerpool.com:5000/ubuntu
ab8e2728644c: Pulling dependent layers
511136ea3c5a: Download complete
5f0ffaa9455e: Download complete
a300658979be: Download complete
904483ae0c30: Download complete
ffdaafd1ca50: Download complete
d047ae21eeaf: Download complete
3.开始使用镜像,创建一个容器
查看已经存在的镜像:
docker images
root@VM_185_235_centos ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
docker.io/ubuntu latest f753707788c5 3 weeks ago 127.1 MB
TAG 信息用来标记来自同一个仓库的不同镜像。例如 ubuntu 仓库中有多个镜像,通过 TAG 信息来区分
发行版本,例如 10.04 、 12.04 、 12.10 、 13.04 、 14.04 等
我们可以使用下面这样的命令来指定使用的镜像开启一个容器:
docker run -t -i ubuntu:latest /bin/bash
如果不指定具体的标记,则默认使用 latest 标记信息。
使用 docker commit 来扩展一个镜像比较简单,但是不方便在一个团队中分享。我们可以使用 docker
build 来创建一个新的镜像。为此,首先需要创建一个 Dockerfile,包含一些如何创建镜像的指令