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

发布于:2025-02-24 ⋅ 阅读:(9) ⋅ 点赞:(0)

docker run 部署

 创建blog网络

[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容器

[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容器

[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

查看运行的容器

[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安装

sudo yum install -y python3 python3-pip
sudo pip3 install virtualenv
virtualenv venv
source venv/bin/activate
pip install docker-compose
[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