文章目录
- 一、前置条件
- 二、拉取镜像
- 三、安装工具及依赖
- 1.安装
- 2.升级pip
- 3.获取docker-compose
- 方法一
- 另一种方法(推荐)
- 四、创建容器,启用镜像,映射端口
- 五、访问并设置
一、前置条件
主机:20.0.0.142
安装docker
docker的安装见此篇文章:Linux企业应用——Docker(一)之初步了解Docker以及Docker的安装
二、拉取镜像
docker pull wordpress:latest
docker pull mysql:5.7
三、安装工具及依赖
1.安装
wget准备yum源,此处使用aliyun提供的源
yum -y install python2-pip
准备pip工具,用于批量管理python语言开发的工具包
yum -y install gcc libffi-devel python-devel openssl-devel openldap-devel
安装依赖
wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
yum -y install python2-pip
yum -y install gcc libffi-devel python-devel openssl-devel openldap-devel
2.升级pip
# 升级pip:
sudo wget https://bootstrap.pypa.io/pip/2.7/get-pip.py
sudo python get-pip.py
pip -V
3.获取docker-compose
#安装
sudo curl -L https://get.daocloud.io/docker/compose/releases/download/1.26.2/docker-compose-`uname -s`-`uname -m` > /usr/local/bin/docker-compose
#授权
sudo chmod +x /usr/local/bin/docker-compose
#查看
docker-compose --version
方法一
使用yaml文件直接进行配置
mkdir /root/wordpressproject
cd /root/wordpressproject/
[root@server1 wordpressproject]# vi docker-compose.yaml
[root@server1 wordpressproject]# cat docker-compose.yaml
version: '3.3'
service:
db:
image: mysql:5.7
restart: always
enviroment:
MYSQL_ROOT_PASSWORD: somewordpress
MYSQL_DATABASE: wordpress
MYSQL_USER: wordpress
MYSQL_PASSWORD: wordpress
wordpress:
depends_on:
- db
image: wordpress:latest
ports:
- "8000:80"
restart: always
enviroment:
WORDPRESS_DB_HOST: db:3306
WORDPRESS_DB_USER: wordpress
WORDPRESS_DB_PASSWORD: wordpress
WORDPRESS_DB_NAME: wordpress
另一种方法(推荐)
创建wp-mysql容器启用mysql镜像
[root@server1 wordpressproject]# docker run --name wp-mysql -e MYSQL_ROOT_PASSWORD=123456 -p 3306:3306 -d mysql:5.7
7165fc9f14469e651a28dfe93fa5442d087132f780cf7b986a3b029e94c5c3aa
创建数据库
docker exec -it wp-mysql /bin/bash
mysql -uroot -p123456
create database wordpress;
如下:
[root@server1 wordpressproject]# docker exec -it wp-mysql /bin/bash
bash-4.2# mysql -uroot -p123456
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.41 MySQL Community Server (GPL)
Copyright (c) 2000, 2023, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> create database wordpress;
Query OK, 1 row affected (0.00 sec)
mysql> exit
四、创建容器,启用镜像,映射端口
docker run --name myWordpress --link wp-mysql:mysql -p 8080:80 -d wordpress
创建一个基于wordpres:latest镜像,名为myWordpress的容器,并立即启动,映射到8080端口,连接到mysql
如果的端口被占用,使用netstat -anp | grep 端口号
查看,最后kill 进程号
,删除原来容器后再次创建
[root@server1 wordpressproject]# netstat -anp | grep 8080
[root@server1 wordpressproject]# docker remove myWordpress
myWordpress
[root@server1 wordpressproject]# docker run --name myWordpress --link wp-mysql:mysql -p 8080:80 -d wordpress
23c5656f70ebf49995f04f5fbb20e77641da9e18471e12d191d661c8cee48cc4
五、访问并设置
浏览器访问20.0.0.142:8080
选择简体中文
输入命令设置的信息
设置用户名和密码
登录
成功