1:什么是Docker
Docker 是一个开源的应用容器引擎,基于 Go 语言 并遵从 Apache2.0 协议开源。 Docker
可以让开发者打包他们的应用以及依赖包到一个轻量级、可移植的容器中,然后发布到任何流行的 Linux 机器上,也可以实现虚拟化。
容器是完全使用沙箱机制,相互之间不会有任何接口,更重要的是容器性能开销极低。
Docker能干什么?
- Web 应用的自动化打包和发布。
- 自动化测试和持续集成、发布。
- 在服务型环境中部署和调整数据库或其他的后台应用。
- 从头编译或者扩展现有的 OpenShift 或 Cloud Foundry 平台来搭建自己的 PaaS 环境。
2:镜像,容器,仓库
镜像:
一个只读的模板,可以通过这个模板创建容器服务,一个镜像可以创建多个容器(最终服务运行或者项目运行就是在容器中)
容器:
- 容器是对进程进行封装隔离,属于操作系统层面的虚拟化技术。由于隔离的进程独立于宿主和其它的隔离的进程
- 一个容器包括了完整的运行时环境:除了应用程序本身之外,还有这个应用程序所需的全部依赖、类库、其他二进制文件、配置文件等
- 将应用程序本身和其依赖容器化,使其运行的环境和操作系统的基础环境造成的差异都被抽象掉
仓库:
集中存放镜像文件的场所。Docker用Registry来保存用户构建的镜像。Registry分为公有和私有两种
3:安装Docker
卸载之前的Dokcer:
yum remove docker \
docker-client \
docker-client-latest \
docker-common \
docker-latest \
docker-latest-logrotate \
docker-logrotate \
docker-engine
安装Docker软件包:
yum install -y yum-utils
使用阿里云镜像:
yum-config-manager \
--add-repo \
http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
安装DockerEngine和容器:
yum makecache fast
yum install docker-ce docker-ce-cli containerd.io
启动Docker:
systemctl start docker
docker version#查看版本
配置aliyun镜像(在/etc/docker下新建daemon.json文件):
vi daemon.json
#添加以下内容
{
"registry-mirrors": ["https://z117ufx7.mirror.aliyuncs.com"]
}
4:测试
查看镜像:
docker images
拉取hello-world镜像:
docker pull hello-world
再次查看镜像:
docker images
#输入命令后应该有如下内容
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest feb5d9fea6a5 9 months ago 13.3kB
运行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-world镜像运行后会停止,所以需要-a参数才能查看所有的镜像
docker ps -a
#执行命令后应该有如下内容
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
9df5d2e8d1fd hello-world "/hello" About a minute ago Exited (0) About a minute ago condescending_cray
删除容器和镜像:
#删除容器
docker rm 9df5d2e8d1fd(此次填写docker ps -a查询到的CONTAINER ID的值)
#删除镜像
docker rmi hello-world:latest
- 容器未停止需要-f参数强制删除
- 如果镜像有对应的容器,需要删除容器后再删除镜像