docker-compose单机容器编排

发布时间:2023年12月17日

Dockerfile:先配置好文件,然后build,镜像-------->容器。
docker-conpose 既可以基于dockerfile,也可以基于镜像,一键式拉起镜像和容器。
docker-compose核心就是yml文件,可以定义容器的一切。通过yml配置,直接运行容器。docker:单节点。docker没有统一对外提供服务的端口和配置。docker一般都是内部访问。无需使用https。
如果要对外以集群和分布式的方式提供统一访问的端口,k8s+ingress.

Docker Compose的解决方案

提供工具用于定义和运行多个docker容器应用;

使用yaml文件来配置应用服务(docker-compse.yml);

可以通过一个简单的命令docker-compse up可以按照依赖关系启动所有服务;

可以通过一个简单的命令docker-compose down停止所有服务;

当一个服务需要的时候,可以很简单地通过--scale进行扩容;

Compose的特征?

更高的可移植性,Docker Compose仅需一个docker-compse up可以完成按照依赖关系启动所有服务,然后使用docker-compose down轻松将其拆解。帮助我们更轻松地部署复杂的应用程序;

单个主机上的多个隔离环境,Compose可以使用项目名称将环境彼此隔离,这带可以在一台计算机上运行同一环境的多个副本,它可以防止不同的项目和服务相互干扰;

Docker Compose配置常用字段:

docker-compose的常用命令:

build:构建服务
ps:列出由docker-compose编排出来的容器
up:创建和启动容器
top:显示容器的进程
logs:查看容器的输出日志
down:删除容器,网络,数据卷。
exec:在容器中执行命令。
stop/start/retart:停止,启动,重启容器。
scale: 指定启动的容器数量

docker-compose编译安装lnmp

安装docker-compose
#下载
curl -L https://github.com/docker/compose/releases/download/1.21.1/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose
#安装
chmod +x /usr/local/bin/docker-compose
#查看版本
docker-compose --version

或者

cd /opt? 拖入安装包

mv docker-compose-linux-x86_64 docker-compose
mv docker-compose ?/usr/local/bin/
chmod 777 /usr/local/bin/docker-compose

[root@localhost opt]# mkdir lnmp
[root@localhost opt]# cd lnmp
[root@localhost lnmp]# mkdir nginx mysql php

nginx

[root@localhost lnmp]# cd nginx

安装包

[root@localhost nginx]# ls
nginx-1.22.0.tar.gz ?wordpress-6.4.2-zh_CN.tar.gz

[root@localhost nginx]# vim nginx.conf

worker_processes 1;
events {
? ? ? worker_connections 1024;
}
http {
? ? ? ?include ? ? ? ? ? mime.types;
? ? ? ?default_type ? ?application/octet-stream;
? ? ? ?sendfile ? ? ? ? ? ? ? ?on;
? ? ? ?keepalive_timeout ? ?65;
? ? ? ?server {
? ? ? ? ? ? ? ? ? ?listen ? ? ?80;
? ? ? ? ? ? ? ? ? ?server_name ? localhost;
? ? ? ? ? ? ? ? ? ?charset utf-8;
? ? ? ? ? ? ? ? ? ? location / {
? ? ? ? ? ? ? ? ? ? ? ? ? ? root ? ?html;
? ? ? ? ? ? ? ? ? ? ? ? ? ? index ?index.html index.php;
?}
?error_page ? 500 502 503 504 ? /50x.html;
?location = /50x.html ?{
? ? ? ? root ? ?html;
}

location ~ \.php$ {
? ? ? ? ? ? ? ? ?root ? ? ? ? ? html;
? ? ? ? ? ? ? ? ?fastcgi_pass ? 172.111.0.30:9000;
? ? ? ? ? ? ? ? ?fastcgi_index ?index.php;
? ? ? ? ? ? ? ? ?fastcgi_param ?SCRIPT_FILENAME ?/usr/local/nginx/html$fastcgi_script_name;
? ? ? ? ? ? ? ? ?include ? ? ? ?fastcgi_params; ?
? ? ? ? ? ? ? } ?
} ? ? ? ? ? ??
}

[root@localhost nginx]# vim Dockerfile
FROM centos:7
RUN yum -y install pcre-devel zlib-devel gcc gcc-c++ make && useradd -M -s /sbin/nologin nginx
ADD nginx-1.22.0.tar.gz /usr/local/src/
WORKDIR /usr/local/src/nginx-1.22.0
RUN ./configure \
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-http_stub_status_module && make -j 4 && make install
ENV PATH /usr/local/nginx/sbin:$PATH
COPY nginx.conf /usr/local/nginx/conf/
ADD wordpress-6.4.2-zh_CN.tar.gz /usr/local/nginx/html
RUN chmod 777 -R /usr/local/nginx/html/
EXPOSE 80
VOLUME ["/usr/local/nginx/html/"]
CMD ["/usr/local/nginx/sbin/nginx","-g","daemon off;"]

MySQL

[root@localhost nginx]# cd ..
[root@localhost lnmp]# cd mysql/
[root@localhost mysql]# rz -E
rz waiting to receive.
[root@localhost mysql]# ls
mysql-boost-5.7.20.tar.gz

[root@localhost mysql]# vim my.cnf

[client]
port = 3306
socket=/usr/local/mysql/mysql.sock

[mysqld]
user = mysql
basedir=/usr/local/mysql
datadir=/usr/local/mysql/data
port = 3306
character-set-server=utf8
pid-file = /usr/local/mysql/mysqld.pid
socket=/usr/local/mysql/mysql.sock
bind-address = 0.0.0.0
skip-name-resolve
max_connections=2048
default-storage-engine=INNODB
max_allowed_packet=16M
server-id = 1
general_log=ON
general_log_file=/usr/local/mysql/data/mysql_general.log

sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_AUTO_VALUE_ON_ZERO,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,PIPES_AS_CONCAT,ANSI_QUOTES
?

[root@localhost mysql]# vim Dockerfile

FROM centos:7
MAINTAINER this is mysql image <lnmp>
RUN yum -y install ncurses ncurses-devel bison cmake pcre-devel zlib-devel gcc gcc-c++ make && useradd -M -s /sbin/nologin mysql
ADD mysql-boost-5.7.20.tar.gz /usr/local/src/
WORKDIR /usr/local/src/mysql-5.7.20/
RUN cmake \
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
-DMYSQL_UNIX_ADDR=/usr/local/mysql/mysql.sock \
-DSYSCONFDIR=/etc \
-DSYSTEMD_PID_DIR=/usr/local/mysql \
-DDEFAULT_CHARSET=utf8 \
-DDEFAULT_COLLATION=utf8_general_ci \
-DWITH_EXTRA_CHARSETS=all \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_ARCHIVE_STORAGE_ENGINE=1 \
-DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
-DWITH_PERFSCHEMA_STORAGE_ENGINE=1 \
-DMYSQL_DATADIR=/usr/local/mysql/data \
-DWITH_BOOST=boost \
-DWITH_SYSTEMD=1 && make -j 4 && make install
COPY my.cnf /etc/my.cnf
EXPOSE 3306
RUN chown -R mysql:mysql /usr/local/mysql/ && chown mysql:mysql /etc/my.cnf
WORKDIR /usr/local/mysql/bin/
RUN ./mysqld \
--initialize-insecure \
--user=mysql \
--basedir=/usr/local/mysql \
--datadir=/usr/local/mysql/data && cp /usr/local/mysql/usr/lib/systemd/system/mysqld.service /usr/lib/systemd/system/;systemctl enable mysqld
ENV PATH=/usr/local/mysql/bin:/usr/local/mysql/lib:$PATH
VOLUME ["/usr/local/mysql"]
ENTRYPOINT ["/usr/sbin/init"]

PHP

[root@localhost mysql]# cd ..
[root@localhost lnmp]# ls
mysql ?nginx ?php
[root@localhost lnmp]# cd php
[root@localhost php]# rz -E
rz waiting to receive.
[root@localhost php]# rz -E
rz waiting to receive.
[root@localhost php]# rz -E
rz waiting to receive.
[root@localhost php]# rz -E
rz waiting to receive.
[root@localhost php]# ls
php-7.1.10.tar.bz2 ?php-fpm.conf ?php.ini ?www.conf
[root@localhost php]# vim Dockerfile

FROM centos:7
MAINTAINER this is php image <lnmp>
RUN yum -y install gd \
libjpeg libjpeg-devel \
libpng libpng-devel \
freetype freetype-devel \
libxml2 libxml2-devel \
zlib zlib-devel \
curl curl-devel \
openssl openssl-devel \
gcc gcc-c++ make pcre-devel && useradd -M -s /sbin/nologin nginx
ADD php-7.1.10.tar.bz2 /usr/local/src/
WORKDIR /usr/local/src/php-7.1.10
RUN ./configure \
--prefix=/usr/local/php \
--with-mysql-sock=/usr/local/mysql/mysql.sock \
--with-mysqli \
--with-zlib \
--with-curl \
--with-gd \
--with-jpeg-dir \
--with-png-dir \
--with-freetype-dir \
--with-openssl \
--enable-fpm \
--enable-mbstring \
--enable-xml \
--enable-session \
--enable-ftp \
--enable-pdo \
--enable-tokenizer \
--enable-zip && make -j 4 && make install
ENV PATH /usr/local/php/bin:/usr/local/php/sbin:$PATH
COPY php.ini /usr/local/php/lib/
COPY php-fpm.conf /usr/local/php/etc/
COPY www.conf /usr/local/php/etc/php-fpm.d/
EXPOSE 9000
ENTRYPOINT ["/usr/local/php/sbin/php-fpm","-F"]

docker.compose
?

cd ..

[root@localhost lnmp]# vim docker-compose.yml

version: '3'
services:
? nginx:
? ? hostname: nginx
? ? bulid:
? ? ? context: /opt/lnmp/nginx/
? ? ? dockerfile: Dockerfile
? ? ports:
? ? ? - 1314:80
? ? container_name: nginx_server
? ? networks:
? ? ? lnmp:
? ? ? ? ipv4_address: 172.111.0.10
? ? volumes:
? ? ? - "nginx-data:/usr/local/nginx/html"

? mysql:
? ? hostname: mysql
? ? build:
? ? ? context: /opt/lnmp/mysql/
? ? ? dockerfile: Dockerfile
? ? ports:
? ? ? - 3306:3306
? ? container_name: mysql_server
? ? networks:
? ? ? lnmp:
? ? ? ? ipv4_address: 172.111.0.20
? ? privileged: true
? ? volumes:
? ? ? - "mysql-data:/usr/local/mysql/"

?php:
? ? hostname: php
? ? build:
? ? ? context: /opt/lnmp/php/
? ? ? dockerfile: Dockerfile
? ? ports:
? ? ? - 9000:9000
? ? container_name: php_server
? ? networks:
? ? ? lnmp:
? ? ? ? ipv4_address: 172.111.0.30
? ? volumes:
? ? ? - "nginx-data:/usr/local/nginx/html/"
? ? ? - "mysql-data:/usr/local/mysql/"
?

networks:
? lnmp:
? ? driver: bridge
? ? ipam:
? ? ? config:
? ? ? ? - subnet: 172.111.0.0/16

volumes:
? nginx-data:
? mysql-data:

[root@localhost lnmp]# docker-compose -f docker-compose.yml up -d
yum安装出现错误 systemctl restart docker

出现 ? Network lnmp_lnmp ?Error ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 0.0s?
failed to create network lnmp_lnmp: Error response from daemon: Pool overlaps with other one on this address space

网卡冲突,更改docker-compose.yml的IPv4地址

[root@localhost lnmp]# docker exec -it mysql_server bash
[root@mysql bin]# mysql -u root -p
默认无密码

mysql> create database wordpress;
Query OK, 1 row affected (0.00 sec)
?
mysql> grant all privileges on wordpress.* to 'wordpress'@'%' identified by '123456';
Query OK, 0 rows affected, 1 warning (0.00 sec)
?
mysql> grant all privileges on *.* to 'root'@'%' identified by '123456';
Query OK, 0 rows affected, 1 warning (0.00 sec)
?
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
?


?

文章来源:https://blog.csdn.net/abc13245821907/article/details/134963189
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。