Docker简介
一、简介
1、官方网站:https://www.docker.com/
2、简单介绍:
Docker 是一个开源的应用容器引擎(基于go语言开发),让开发者可以打包他们的应用以及依赖包到一个可移植的容器中,然后发布到任何流行的Linux机器上,也可以实现虚拟化,容器是完全使用沙箱机制,相互之间不会有任何接口。
3、Docker安装
- 3.1、Centos安装:
(1)Docker支持的CentOS版本:CentOS 6.5 (64-bit),要求系统内核版本为 2.6.32-431 或者以上
CentOS 7 (64-bit),要求系统的内核版本高于 3.10
(2)查看系统版本
(3) 查看内核版本
(4)yum安装Docker
yum install -y yum-utils device-mapper-persistent-data lvm2
yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
yum makecache
yum -y install docker-ce
- 3.2docker-compose安装
(1) curl -L https://github.com/docker/compose/releases/download/1.17.1/docker-compose-uname -s
-uname -m
> /usr/local/bin/docker-compose
(2)chmod +x /usr/local/bin/docker-compose
4、Docker三要素
- 4.1镜像(images):其本质一个只读的模板,镜像可以用来创建Docker容器,一个镜像可以创建诸多容器。
- 4.2容器Container:基于镜像运行的实例
- 4.3仓库(Repository):仓库是集中存放镜像文件的场所,分为公开仓库(Public)和私有仓库(Private),最大的公开仓库是官方提供的https://hub.docker.com,存放着官方构建的镜像,用户也可自己构建私有仓库。
二、Docker之Hello-World
1、Hello-World例子
(1)启动Docker
systemctl start docker
(2)从官方仓库拉取HelloWorld镜像
docker pull hello-world
Using default tag: latest
latest: Pulling from library/hello-world
1b930d010525: Pull complete
Digest: sha256:6540fc08ee6e6b7b63468dc3317e3303aae178cb8a45ed3123180328bcc1d20f
Status: Downloaded newer image for hello-world:latest
[root@www ~]# docker run hello-world
(3)运行hello-world
docker run hello-world
Hello from Docker!
This message shows that your installation appears to be working correctly.
To generate this message, Docker took the following steps:
1. The Docker client contacted the Docker daemon.
2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
(amd64)
3. The Docker daemon created a new container from that image which runs the
executable that produces the output you are currently reading.
4. The Docker daemon streamed that output to the Docker client, which sent it
to your terminal.
To try something more ambitious, you can run an Ubuntu container with:
$ docker run -it ubuntu bash
Share images, automate workflows, and more with a free Docker ID:
https://hub.docker.com/
For more examples and ideas, visit:
https://docs.docker.com/get-started/
输出Hello from Docker! 这是Docker官方制作的一个Demo例子
三、镜像使用以及基本操作
1、从仓库拉取镜像,name代表镜像名字,tag代表镜像版本,不写默认拉取最新
docker pull name:tag
2、列出本地所有镜像
[root@openstack ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
192.168.220.3:5000/member2 latest e63d1e552172 3 weeks ago 506MB
centos-nginx latest d6095daea42e 3 weeks ago 626MB
luoyuan323/cent-nginx 1.0 d6095daea42e 3 weeks ago 626MB
cent-nginx latest de0c63ea1f5d 3 weeks ago 626MB
cmd latest 5e21f03ced39 3 weeks ago 202MB
cent-ng v1 72e8f1d0353b 3 weeks ago 314MB
centos latest 9f38484d220f 4 months ago 202MB
192.168.220.3:5000/reg 1.0 f32a97de94e1 5 months ago 25.8MB
registry latest f32a97de94e1 5 months ago 25.8MB
luoyuan323/luoyuan 2.0 f32a97de94e1 5 months ago 25.8MB
luoyuan323/reg 3.0 f32a97de94e1 5 months ago 25.8MB
nginx 1.13 ae513a47849c 15 months ago 109MB
-
REPOSITORY:表示镜像的仓库源
-
TAG:镜像的版本
-
IMAGE ID:镜像ID
-
CREATED:镜像创建时间
-
SIZE:镜像大小
3.运行一个镜像,name为镜像名字
docker run name
其中run后面可以跟很多参数
4、删除镜像
docker rmi name/id
四、容器
1、容器是基于镜像运行的实例
2、查看所有容器
[root@openstack ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
543084dcd379 192.168.220.3:5000/member2 "java -jar /web.jar" 2 weeks ago Exited (143) 2 weeks ago mem
45b9332a4217 registry "/entrypoint.sh /etc…" 2 weeks ago Up 2 weeks 0.0.0.0:5000->5000/tcp reg
11690df43cf4 192.168.220.3:5000/member2 "java -jar /web.jar" 3 weeks ago Up 2 weeks 8090/tcp compose_member-2_1
5b68e7525537 192.168.220.3:5000/member2 "java -jar /web.jar" 3 weeks ago Up 2 weeks 8090/tcp compose_member-1_1
ce3497370f61 nginx:1.13 "nginx -g 'daemon of…" 3 weeks ago Up 2 weeks 0.0.0.0:80->80/tcp member-nginx
43ab7cebdeaf cmd "/bin/bash -c 'echo …" 3 weeks ago Exited (0) 3 weeks ago inspiring_robinson
5009f3ced8f4 cmd "/bin/bash -c 'echo …" 3 weeks ago Exited (0) 3 weeks ago dazzling_mcclintock
f5ad8d19741d cmd "/bin/bash -c 'echo …" 3 weeks ago Exited (0) 3 weeks ago blissful_ganguly
38e78ea2a1fb cent-ng:v1 "/bin/bash" 3 weeks ago Exited (255) 2 weeks ago ng
af023cf90997 centos "/bin/bash" 3 weeks ago Exited (255) 2 weeks ago cent3
a91696b17abf centos "/bin/bash" 3 weeks ago Exited (137) 3 weeks ago ce
a2ad495b9e58 centos "/bin/bash" 3 weeks ago Exited (137) 3 weeks ago cent
3、查看运行中的容器
[root@openstack ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
45b9332a4217 registry "/entrypoint.sh /etc…" 2 weeks ago Up 2 weeks 0.0.0.0:5000->5000/tcp reg
11690df43cf4 192.168.220.3:5000/member2 "java -jar /web.jar" 3 weeks ago Up 2 weeks 8090/tcp compose_member-2_1
5b68e7525537 192.168.220.3:5000/member2 "java -jar /web.jar" 3 weeks ago Up 2 weeks 8090/tcp compose_member-1_1
ce3497370f61 nginx:1.13 "nginx -g 'daemon of…" 3 weeks ago Up 2 weeks 0.0.0.0:80->80/tcp member-nginx
4、删除容器(运行中的容器不能删除,删除会报错,到时可以强制删除docker rm -f name/id)
docker rm name/id
5、进入已经运行的容器
docker exec -it name/id bash