docker-compose部署及使用 原创

Docker Compose是一个用于定义和运行多个Docker容器的工具。它使用YAML文件来配置应用程序的服务、网络和卷等方面,使得在单个主机上进行部署更加简单。通过定义一个Compose文件,你可以一次性启动、停止和管理整个应用程序的多个容器。

Compose文件包含了应用程序的各种服务的配置选项,如镜像、端口映射、环境变量、卷挂载等。你只需在Compose文件中定义所需的服务和其配置,然后使用docker-compose up命令即可启动整个应用程序。此外,你还可以使用docker-compose down命令来停止和删除所有相关的容器。

1.  docker-compose部署

[root@localhost bin]#  pwd
/usr/local/bin
[root@localhost bin]# curl -L "https://github.com/docker/compose/releases/download/1.29.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:--  0:00:01 --:--:--     0
100 12.1M  100 12.1M    0     0   5094      0  0:41:40  0:41:40 --:--:--  5509

[root@localhost bin]# ll
total 12440
-rw-r--r--. 1 root root 12737304 Mar 10 02:25 docker-compose
[root@localhost bin]# chmod 777 docker-compose

[root@localhost bin]# docker-compose --version
docker-compose version 1.29.2, build 5becea4c

查看帮助

[root@localhost ~]# docker-compose --help
Define and run multi-container applications with Docker.

Usage:
  docker-compose [-f <arg>...] [options] [COMMAND] [ARGS...]
  docker-compose -h|--help

Options:
  -f, --file FILE             Specify an alternate compose file (default: docker-compose.yml)
  -p, --project-name NAME     Specify an alternate project name (default: directory name)
  --verbose                   Show more output
  --no-ansi                   Do not print ANSI control characters
  -v, --version               Print version and exit
  -H, --host HOST             Daemon socket to connect to

  --tls                       Use TLS; implied by --tlsverify
  --tlscacert CA_PATH         Trust certs signed only by this CA
  --tlscert CLIENT_CERT_PATH  Path to TLS certificate file
  --tlskey TLS_KEY_PATH       Path to TLS key file
  --tlsverify                 Use TLS and verify the remote
  --skip-hostname-check       Don't check the daemon's hostname against the name specified
                              in the client certificate (for example if your docker host
                              is an IP address)
  --project-directory PATH    Specify an alternate working directory
                              (default: the path of the Compose file)

Commands:
  build              Build or rebuild services
  bundle             Generate a Docker bundle from the Compose file
  config             Validate and view the Compose file
  create             Create services
  down               Stop and remove containers, networks, images, and volumes
  events             Receive real time events from containers
  exec               Execute a command in a running container
  help               Get help on a command
  images             List images
  kill               Kill containers
  logs               View output from containers
  pause              Pause services
  port               Print the public port for a port binding
  ps                 List containers
  pull               Pull service images
  push               Push service images
  restart            Restart services
  rm                 Remove stopped containers
  run                Run a one-off command
  scale              Set number of containers for a service
  start              Start services
  stop               Stop services
  top                Display the running processes
  unpause            Unpause services
  up                 Create and start containers
  version            Show the Docker-Compose version information

2.  docker-compose.yml模板

version: '3.8'
services:
  web:
    image: nginx:latest
    ports:
      - "8080:80"
  db:
    image: mysql:latest
    environment:
      MYSQL_ROOT_PASSWORD: example
      MYSQL_DATABASE: mydatabase
      MYSQL_USER: user
      MYSQL_PASSWORD: password
包含了两个服务:web 和 db。web 服务使用 Nginx 镜像并将容器的 80 端口映射到宿主机的 8080 端口;db 服务使用 MySQL 镜像,并设置了一些环境变量用于配置 MySQL 实例。

解析:

version: 指定了 Docker Compose 文件的版本。
services: 定义了各个服务。
web 和 db 是服务的名称,可以根据实际情况自行命名。
image: 指定了服务所使用的镜像。
ports: 定义了端口映射关系,格式为 "宿主机端口:容器端口"。
environment: 设置了该服务运行时需要的环境变量,这里设置了 MySQL 的 root 密码、数据库名、用户名和密码。

3.  使用compose搭建WordPress

[root@localhost ~]# cd /home/
[root@localhost home]# ll
total 0

# 创建项目目录
[root@localhost home]# mkdir wordpress
[root@localhost home]# vi docker-compose.yml
[root@localhost home]# cat docker-compose.yml
version: "3"
services:

   db:
     image: mysql:8.0
     command:
      - --default_authentication_plugin=mysql_native_password
      - --character-set-server=utf8mb4
      - --collation-server=utf8mb4_unicode_ci
     volumes:
       - db_data:/var/lib/mysql
     restart: always
     environment:
       MYSQL_ROOT_PASSWORD: 123456
       MYSQL_DATABASE: wordpress
       MYSQL_USER: wordpress
       MYSQL_PASSWORD: wordpress

   wordpress:
     depends_on:
       - db
     image: wordpress:latest
     ports:
       - "8000:80"
     restart: always
     environment:
       WORDPRESS_DB_HOST: db:3306
       WORDPRESS_DB_USER: wordpress
       WORDPRESS_DB_PASSWORD: wordpress
volumes:
  db_data:


启动项目

[root@localhost home]# docker-compose up

[root@localhost home]# docker ps
CONTAINER ID   IMAGE              COMMAND                  CREATED         STATUS         PORTS                                   NAMES
edeae04f9543   wordpress:latest   "docker-entrypoint.s…"   7 minutes ago   Up 7 minutes   0.0.0.0:8000->80/tcp, :::8000->80/tcp   home_wordpress_1
ebaa335a1d47   mysql:8.0          "docker-entrypoint.s…"   7 minutes ago   Up 7 minutes   3306/tcp, 33060/tcp                     home_db_1

#必须在项目目录中才能使用这个命令
[root@localhost home]# docker-compose ps
      Name                    Command               State                  Ports
------------------------------------------------------------------------------------------------
home_db_1          docker-entrypoint.sh --def ...   Up      3306/tcp, 33060/tcp
home_wordpress_1   docker-entrypoint.sh apach ...   Up      0.0.0.0:8000->80/tcp,:::8000->80/tcp

关闭防火墙,通过浏览器进行访问

[root@localhost home]# systemctl stop firewalld
[root@localhost home]# setenforce 0

posted @   SKY慕雪  阅读(0)  评论(0编辑  收藏  举报  
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
点击右上角即可分享
微信分享提示