参照,进行了简单理解并照其所说进行了centos上的部署。 #1 前提# ##1.1 系统要求## 64位的CentOS 7 ##1.2 移除非官方的docker包##
sudo yum -y remove docker docker-common container-selinux
避免与官方的docker-engine冲突,所以同样的移除docker-selinux
sudo yum -y remove docker-selinux
#2 安装Docker# ##2.1 用repo安装## ###2.2.1 安装yum-utils### yum-utils提供了yum-config-manager组件。
sudo yum install -y yum-utils
###2.2.2 获取稳定版repo###
sudo yum-config-manager \ --add-repo \ https://download.docker.com/linux/centos/docker-ce.repo
###2.2.3 配置testing仓库(可选)###
sudo yum-config-manager --enable docker-testing
也可以用--disable来关闭
sudo yum-config-manager --disable docker-testing
##2.2 安装Docker## ###2.2.1 更新yum资源索引###
sudo yum makecache fast
###2.2.2 安装Docker### 安装最新版Docker
sudo yum -y install docker-ce
或者用下列命令安装之前版本的docker
yum list docker-engine.x86_64 --showduplicates |sort -r
根据常看的版本信息,用下面命令来指定版本
sudo yum -y install docker-engine-
###2.2.3 启动Docker###
sudo systemctl start docker
###2.2.4 确认完成###
sudo docker run hello-world
这里用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. 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 bashShare images, automate workflows, and more with a free Docker ID: https://cloud.docker.com/For more examples and ideas, visit: https://docs.docker.com/engine/userguide/
#3 安装Docker Compose# Docker工具箱同时提供了Engine和Compose,不过本人所用CentOS 7,所以就继续下面流程: ##3.1 下载Docker Compose## 使用curl下载compose,并赋予可执行权限:
curl -L https://github.com/docker/compose/releases/download/1.11.1/docker-compose-`uname -s`-`uname -m` > /usr/local/bin/docker-composechmod +x /usr/local/bin/docker-compose
也可以用python pip来安装:
pip install -U docker-compose
##3.2 测试##
docker-compose --version
#3 Docker Compose使用# ##3.1 编写项目代码## 1.先创建一个目录,作为项目目录。
mkdir composetestcd composetest
2.创建app.py:
from flask import Flaskfrom redis import Redisapp = Flask(__name__)redis = Redis(host='redis', port=6379)@app.route('/')def hello(): count = redis.incr('hits') return 'Hello World! I have been seen {} times.\n'.format(count)if __name__ == "__main__": app.run(host="0.0.0.0", debug=True)
3.再创建一个叫做requirements.txt的文件,内容如下:
flaskredis
##3.2 编写Dockerfile##
FROM python:3.4-alpineADD . /codeWORKDIR /codeRUN pip install -r requirements.txtCMD ["python", "app.py"]
##3.3 编写Compose配置文件## 在当前目录下创建docker-compose.yml,内容如下:
version: '2'services: web: build: . ports: - "5000:5000" volumes: - .:/code redis: image: "redis:alpine"
##3.4 启动Docker Compose## 在项目目录下,用下面命令启动Docker Compose:
docker-compose up
然后就可以用浏览器访问 来查看了,出现下面信息即为成功。
Hello World! I have been seen 1 times.
##3.5 更新应用## 修改app.py,将返回修改为:
return 'Hello from Docker! I have been seen {} times.\n'.format(count)
刷新浏览器,即可看到
Hello from Docker! I have been seen 2 times.
##3.6 Docker Compose其他命令## 1.后台运行
docker-compose up -d
2.列举后台已启动容器
docker-compose ps
3.启动已关闭的服务
docker-compose run web env
4.关闭启动的服务
docker-compose stop
5.彻底关闭服务并移除容器,--volumes 清理Redis容器产生的数据量
docker-compose down --volumes