Docker实战-使用docker compose搭建博客

docker run 部署

创建blog网络

bash 复制代码
[root@k8s-master ~]# docker network create blog
8f533a5a1ec65eae3f98c0ae5a76014a3ab1bf3c087ad952cdc100cc7a658948
[root@k8s-master ~]#  docker network ls
NETWORK ID     NAME      DRIVER    SCOPE
8f533a5a1ec6   blog      bridge    local
6c444ff0e0fc   bridge    bridge    local
7172dc5dc9d3   host      host      local
5451367be09b   none      null      local

创建MySQL容器

bash 复制代码
[root@k8s-master ~]# docker run -d -p 3306:3306 \
> -e MYSQL_ROOT_PASSWORD=123456 \
> -e MYSQL_DATABASE=wordpress \
> -v mysql-data:/var/lib/mysql \
> --restart always --name mysql \
> --network blog \
> mysql:8.0
Unable to find image 'mysql:8.0' locally
8.0: Pulling from library/mysql
43759093d4f6: Pull complete 
126d8d3809b3: Pull complete 
157a7408a55d: Pull complete 
c405af89949f: Pull complete 
c92147eb3382: Pull complete 
b3ec9882b976: Pull complete 
ad072341f26b: Pull complete 
bda7c2eca56f: Pull complete 
b6b5c46ac97d: Pull complete 
8cd34fa224e6: Pull complete 
b4144aa75def: Pull complete 
Digest: sha256:50b5ee0656a2caea76506fe702b1baddabe204cf3ab34c03752d2d7cd8ad83fc
Status: Downloaded newer image for mysql:8.0
1f0d309d07db2e0bed62868a43bcff0fcb811d0507317efbffa3d6d77bf9f53b

创建wordpress容器

bash 复制代码
[root@k8s-master ~]# docker run -d -p 8080:80 \
>     -e WORDPRESS_DB_HOST=mysql \
>     -e WORDPRESS_DB_USER=root \
>     -e WORDPRESS_DB_PASSWORD=123456 \
>     -e WORDPRESS_DB_NAME=wordpress \
>     -v wordpress:/var/www/html \
>     --restart always --name wordpress-app \
>     --network blog \
>     wordpress:latest
Unable to find image 'wordpress:latest' locally
latest: Pulling from library/wordpress
c29f5b76f736: Pull complete 
814b6ecb84b0: Pull complete 
a4e58aa84c36: Pull complete 
b545bb7ff18e: Pull complete 
8ca47539e139: Pull complete 
ea823f46cc3c: Pull complete 
bcbecb454049: Pull complete 
68d70c2b9fc9: Pull complete 
b9903ecbcf0b: Pull complete 
f473bcbd0e44: Pull complete 
d8b79b64a9d5: Pull complete 
5c36aa47b3f5: Pull complete 
c6834909cd19: Pull complete 
4f4fb700ef54: Pull complete 
beeeb2e72514: Pull complete 
b7fd27be5b20: Pull complete 
1475f50d3e62: Pull complete 
5ac3fe5e5e7e: Pull complete 
e2a530cb2e77: Pull complete 
ed50d9a76bab: Pull complete 
08dc3bd6914b: Pull complete 
63eaf96fd2ee: Pull complete 
Digest: sha256:0211348df9f5e29a32cb6a65ec5aff2c2e775670fdb054ab98f7833eb539234a
Status: Downloaded newer image for wordpress:latest
3435c46b6bb674a92881e7644ce5ebb94b1b11ad36d815467628bca594e2b28e

查看运行的容器

bash 复制代码
[root@k8s-master ~]# docker ps
CONTAINER ID   IMAGE                                                             COMMAND                  CREATED          STATUS          PORTS                               NAMES
3435c46b6bb6   wordpress:latest                                                  "docker-entrypoint.s..."   26 minutes ago   Up 26 minutes   0.0.0.0:8080->80/tcp                wordpress-app
1f0d309d07db   mysql:8.0                                                         "docker-entrypoint.s..."   34 minutes ago   Up 33 minutes   0.0.0.0:3306->3306/tcp, 33060/tcp   mysql

docker-compose部署

docker-compose安装

bash 复制代码
sudo yum install -y python3 python3-pip
sudo pip3 install virtualenv
virtualenv venv
source venv/bin/activate
pip install docker-compose
bash 复制代码
[root@k8s-master ~]# cat docker_compose.yaml 
version: '3.8'  
services:
  mysql:
    image: mysql:8.0
    ports:
      - "3306:3306"
    environment:
      - MYSQL_ROOT_PASSWORD=123456
      - MYSQL_DATABASE=wordpress
    volumes:
      - mysql-data:/var/data/mysql  
      - /app/myconf:/etc/mysql/conf.d
    restart: always
    networks:
      - blog

  wordpress:
    image: wordpress
    ports:
      - "8080:80"
    environment:
      WORDPRESS_DB_HOST: mysql
      WORDPRESS_DB_USER: root
      WORDPRESS_DB_PASSWORD: 123456
      WORDPRESS_DB_NAME: wordpress
    volumes:
      - wordpress:/var/www/html
    restart: always
    networks:  
      - blog
    depends_on:
      - mysql

volumes:
  mysql-data:
  wordpress:

networks:
  blog:
           
[root@k8s-master ~]# docker compose -f docker_compose.yaml up -d 

相关推荐
用户03284722207014 小时前
如何搭建本地yum源(上)
运维
武子康15 小时前
调查研究-183 Apple container:Mac 上用轻量 VM 跑 Linux 容器,Swift 会改写本地容器体验吗?
docker·容器·apple
大树884 天前
金刚石散热越强,管路越先见顶
大数据·运维·服务器·人工智能·ai
摇滚侠4 天前
Linux CentOS7 rpm 安装 MySQL 5.7
linux·运维·mysql
霸道流氓气质4 天前
领域驱动设计(DDD)在 Spring Boot 微服务中的实践指南
运维·spring boot·微服务
Inhand陈工4 天前
基于台达PLC与映翰通IG502的智慧水产养殖精准投喂与远程运维解决方案
运维·人工智能·物联网·阿里云·信息与通信
Alsn864 天前
等待学习-学习目录:Docker 容器安全攻防
学习·安全·docker
酣大智4 天前
ARP代理--工作原理
运维·网络·arp·arp代理
shushangyun_4 天前
2026年快消品B2B系统推荐:支持终端门店订货、促销政策自动化的工具?
java·运维·网络·数据库·人工智能·spring·自动化
施努卡机器视觉4 天前
SNK施努卡侧滑门锁上滑轮总成自动化装配线,从零件到组件,全流程精密制造方案
运维·自动化·制造