www.docker.com
docs.docke.com
[root@localhost yum.repos.d]# wget https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/centos/docker-ce.repo
[root@localhost yum.repos.d]# ls
CentOS-Base.repo CentOS-Debuginfo.repo CentOS-Media.repo CentOS-Vault.repo
CentOS-CR.repo CentOS-fasttrack.repo CentOS-Sources.repo docker-ce.repo
[root@localhost ~]# yum install -y docker-ce
[root@localhost ~]# systemctl enable docker
Created symlink from /etc/systemd/system/multi-user.target.wants/docker.service to /usr/lib/systemd/system/docker.service.
[root@localhost ~]# systemctl start docker
[root@localhost ~]# docker version
Client: Docker Engine - Community
Version: 19.03.7
API version: 1.40
Go version: go1.12.17
Git commit: 7141c199a2
Built: Wed Mar 4 01:24:10 2020
OS/Arch: linux/amd64
Experimental: false
Server: Docker Engine - Community
Engine:
Version: 19.03.7
API version: 1.40 (minimum version 1.12)
Go version: go1.12.17
Git commit: 7141c199a2
Built: Wed Mar 4 01:22:45 2020
OS/Arch: linux/amd64
Experimental: false
containerd:
Version: 1.2.13
GitCommit: 7ad184331fa3e55e52b890ea95e65ba581ae3429
runc:
Version: 1.0.0-rc10
GitCommit: dc9208a3303feef5b3839f4323d9beb36df0a9dd
docker-init:
Version: 0.18.0
GitCommit: fec3683
[root@localhost ~]# vim /etc/docker/daemon.json
{
"registry-mirrors": ["https://vmpaj1xg.mirror.aliyuncs.com"]
}
[root@localhost ~]# systemctl daemon-reload
[root@localhost ~]# systemctl restart docker
搜索dockerhub是否centos镜像
[root@localhost ~]# docker search centos
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
centos The official build of CentOS. 5855 [OK]
下载centos容器镜像
[root@localhost ~]# docker pull centos
Using default tag: latest
latest: Pulling from library/centos
8a29a15cefae: Pull complete
Digest: sha256:fe8d824220415eed5477b63addf40fb06c3b049404242b31982106ac204f6700
Status: Downloaded newer image for centos:latest
docker.io/library/centos:latest
验证centos容器镜像是否下载
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos latest 470671670cac 6 weeks ago 237MB
如果pull的时候提供超时,再重新pull一次。
[root@localhost ~]# docker run -itd --name=centos centos:latest /bin/bash
42a9bd3733cac70a34f84534cbb4e22e715c6d93c35496a87a811385cfd58baa
验证容器是否运行成功
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
42a9bd3733ca centos:latest "/bin/bash" 27 seconds ago Up 25 seconds centos
[root@localhost ~]# docker pull wordpress:latest
[root@localhost ~]# docker pull mysql:5.7
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mysql 5.7 895f791fab9f 34 hours ago 435MB
wordpress latest 126aa00ecc0c 6 days ago 540MB
centos latest 470671670cac 6 weeks ago 237MB
为接下来启动容器进行编排
LAMP先启动mysql,再启动apache
部署wordpress应用时,应该先启动mysql容器,再启动wordpress
准备epel YUM源
[root@localhost ~]# wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
准备pip工具,用于批量管理python语言开发的工具包
[root@localhost ~]# yum install -y python2-pip
由于有安装依赖,先安装依赖包,再安装docker-compose
[root@localhost ~]# yum update -y
[root@localhost ~]# yum install -y gcc libffi-devel python-devel openssl-devel openldap-devel
[root@localhost ~]# pip install python-ldap --user -U
[root@localhost ~]# pip install six --user -U
[root@localhost ~]# pip install pyudev --user -U
[root@localhost ~]# pip install dnspython --user -U
[root@localhost ~]# pip install cffi --user -U
[root@localhost ~]# pip install docker-compose --ignore-installed requests
验证是否安装成功
[root@localhost ~]# docker-compose version
docker-compose version 1.25.4, build unknown
docker-py version: 4.2.0
CPython version: 2.7.5
OpenSSL version: OpenSSL 1.0.2k-fips 26 Jan 2017
pip install的时候可能会速度过慢导致超时
报错信息:
raise ReadTimeoutError(self._pool, None, 'Read timed out.')
ReadTimeoutError: HTTPSConnectionPool(host='files.pythonhosted.org', port=443): Read timed out.
解决方法:
1. 临时方法:可以在使用pip的时候加参数-i
例如:pip install -i https://pypi.tuna.tsinghua.edu.cn/simple six
这样就会从清华那边的镜像去安装six
2. 永久修改:
首先创建一个隐藏目录
[root@localhost ~]# mkdir ~/.pip/
然后创建一个配置文件
[root@localhost ~]# vim ~/.pip/pip.conf
[global]
index-url = https://pypi.tuna.tsinghua.edu.cn/simple
[install]
trusted-host=mirrors.aliyun.com
将镜像源改为国内的阿里云提供,速度会提升很多。
准备容器镜像文件
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mysql 5.7 895f791fab9f 35 hours ago 435MB
wordpress latest 126aa00ecc0c 6 days ago 540MB
centos latest 470671670cac 6 weeks ago 237MB
[root@localhost ~]# mkdir wordpressproject
[root@localhost ~]# cd wordpressproject/
工程文件就是docker-compose.yaml
工程文件中包含service
本次工程文件中包含wordpress、db服务
服务使用容器运行
[root@localhost wordpressproject]# vim docker-compose.yaml
version: '3'
service:
db:
image: mysql:5.7
volumes:
- "./data:/var/lib/mysql"
restart: always
environment:
MYSQL_ROOT_PASSWORD: 000000
MYSQL_DATABASE: wordpress
MYSQL_USER: wordpress
MYSQL_PASSWORD: wordpress
expose:
- "3306"
wordpress:
depends_on:
- db
image: wordpress:latest
links:
- db
ports:
- "8080:80"
restart: always
environment:
WORDPRESS_DB_HOST=: db:3306
WORDPRESS_DB_PASSWORD: wordpress
这个应用定义了两个容器服务:db、wordpress
db容器通过mysql:5.7镜像启动
- mysql的数据库目录挂载到当前目录./data,此目录不存在会自动创建
- 容器重启策略为always
- 设置了连接mysql的4个变量
wordpress容器通过wordpress:latest启动
- 需要db容器先启动再启动wordpress容器
- wordpress容器要link连接db容器
- wordpress容器将80端口映射到宿主机的8080端口
- 容器重启策略为always
- 设置连接数据库的变量
首先开启IP转发功能(必须)
[root@localhost wordpressproject]# echo 'net.ipv4.ip_forward = 1' >> /etc/sysctl.conf
[root@localhost wordpressproject]# sysctl -p
net.ipv4.ip_forward = 1
[root@localhost wordpressproject]# docker-compose up
#只能在工程目录中使用,用于启动工程
[root@localhost wordpressproject]# docker-compose down
#停止并删除工程已经创建的容器