-env_file
#冲文件中添加环境变量,可以是单个值或者列表 单个值: env_file: .env 列表: env_file: - ./common.env - ./apps/web.env - /opt/secrets.env
-environment
#添加环境变量,可以是数组或者字典,布尔值需要使用括号括起来,可以指定默认值也可以不指定默认值 下面两种方法都可以 environment: RACK_ENV: development SHOW: 'true' SESSION_SECRET: environment: - RACK_ENV=development - SHOW=true - SESSION_SECRET
-expose
#声明容器的服务端口,可以声明多个 expose: - "3000" - "8000"
-links
#连接到另一个容器,解决容器连接问题 web: links: #表示web会连接到db、db:database、redis - db - db:database - redis #db:database,其中database是别名,使用的别名将会自动在服务容器中的/etc/hosts里创建相应的环境变量也将被创建
-external_links
#连接Compose之外的容器 external_links: - redis_1 - project_db_1:mysql - project_db_1:postgresql
-extra_hosts
#添加主机名映射,与—addhost相同,也就是添加主机名的标签,往hosts文件中添加这些记录 extra_hosts: - "somehost:162.242.195.82" - "otherhost:50.31.209.229"
-logging
#记录该服务的日志。与—logdriver相同 logging: driver: syslog options: syslog-address: "tcp://192.168.0.42:123"
-network_mode
#指定网络模式,与—net相同 network_mode: "bridge" network_mode: "host" network_mode: "none" network_mode: "service:[service name]" network_mode: "container:[container name/id]"
-networks
#要加入的网络,即表示创建的镜像加入的网络模式 services: some-service: networks: - some-network - other-network #关于这个标签还有一个特别的子标签aliases,这是一个用来设置服务别名的标签,例如: services: some-service: networks: some-network: aliases: - alias1 - alias3 other-network: aliases: - alias2 #相同的服务可以在不同的网络有不同的别名。
-ipv4_address,ipv6_address
#在加入网络时为该服务指定容器的静态IP地址 version: '2.1' services: app: image: busybox command: ifconfig networks: app_net: ipv4_address: 172.16.238.10 #设置ipv4地址 networks: app_net: driver: bridge enable_ipv6: true ipam: driver: default config: - subnet: 172.16.238.0/24 #设置网段
-pid
#将PID模式设置为主机PID模式,与宿主机共享PID地址空间。 pid: "host"
-ports
#指定容器与主机映射的端口,端口不低于60 ports: - "3000" - "3000-3005" - "8000:8000" - "9090-9091:8080-8081" - "49100:22" - "127.0.0.1:8001:8001" - "127.0.0.1:5000-5010:5000-5010" - "6060:6060/udp"
-sysctls
#在容器内设置内核参数,值可以是数组或字典 两种方法都可以 sysctls: net.core.somaxconn: 1024 net.ipv4.tcp_syncookies: 0 sysctls: - net.core.somaxconn=1024 - net.ipv4.tcp_syncookies=0
-ulimits
#使用此选项会覆盖容器的默认ulimits ulimits: nproc: 65535 nofile: soft: 20000 hard: 40000
-volumes
#挂载一个目录或一个已经存在的数据卷容器到容器 #source:是宿主机的目录 #target:容器的目录 version: "3.2" services: web: image: nginx:alpine volumes: #挂载 - type: volume #类型选择挂载 source: mydata #和compose文件同一目录的目录挂载到容器的/data目录 target: /data volume: nocopy: true - type: bind source: ./static target: /opt/app/static NFS持久化 version: '3.3' services: redis: image: 192.168.1.81:5000/redis:4.0.6 volumes: - type: volume source: redis-nfs target: /usr/local/redis-4.0.9/data volume: nocopy: true volumes: redis-nfs: driver: local driver_opts: type: "nfs" #类型选择nfs o: "addr=192.168.1.81,vers=4,soft,timeo=180,bg,tcp,rw" #nfs的命令 device: "192.168.1.81:/data"
-restart
#重新启动容器,此选项默认为no,值有always|onfailure|unless-stopped restart: "no" restart: always restart: on-failure restart: unless-stopped no:是默认的重启策略,在任何情况下都不会重启容器。 always:容器总是重新启动。 on-failure:在容器非正常退出时(退出状态非0),才会重启容器。 unless-stopped:在容器退出时总是重启容器,但是不考虑在Docker守护进程启动时就已经停止了的容器
-hostname
#指定主机名 domainname: foo.com hostname: foo ipc: host mac_address: 02:42:ac:11:65:43
-working_dir
#指定工作目录 user: postgresql #指定用户 working_dir: /code
四、使用Docker-Compose示例
Docker Compose文件使用格式版本需要与Docker版本对应可在官网内查找
(1)步骤
提前安装并启动docker(略) ******(1)创建compose目录 [root@Centos7 ~]# mkdir -p /compose-test/compose #创建compose目录 [root@Centos7 ~]# cd /compose-test/compose/ ******(2)编写compose文件 [root@Centos7 compose]# vim docker-compose.yml #编写compose文件 version: '3' #docker对应的compose版本 services: #容器组 web: #镜像容器服务的标识 build: . #指定构建镜像上下文的目录,. 表示在当前目录 ports: #指定容器映射的端口 - "8080:80" #保存退出 ******(3)创建Dockerfile文件 [root@Centos7 compose]# vim Dockerfile FROM centos:7.4 #指定基础镜像 MAINTAINER rzy #指定作者 RUN rm -rf /etc/yum.repos.d/* #RUN指定命令 COPY centos.repo /etc/yum.repos.d/ #COPY指定复制文件 RUN yum -y install httpd RUN echo "aaaaaaaa" > /var/www/html/index.html CMD ["/usr/sbin/httpd","-D","FOREGROUND"] #CMD容器开启后执行的命令,这个命令是把httpd放到前台运行使容器不会退出 EXPOSE 80 #指定端口 #保存退出 ******(4)配置yum源,因为centos镜像的yum源是网络yum源,所以可以修改一下为本地yum源 [root@Centos7 compose]# cp /etc/yum.repos.d/centos.repo . #复制一下yum源文件 [root@Centos7 compose]# ll 总用量 12 -rw-r--r-- 1 root root 56 7月 30 13:57 centos.repo -rw-r--r-- 1 root root 72 7月 30 13:50 docker-compose.yml -rw-r--r-- 1 root root 220 7月 30 13:55 Dockerfile [root@Centos7 compose]# vim centos.repo #编写yum源文件 [aaa] name=aaa baseurl=ftp://192.168.100.202/aaa #修改为ftp enabled=1 gpgcheck=0 #保存退出 [root@Centos7 compose]# yum -y install vsftpd #安装ftp服务 。。。。。。 完毕! [root@Centos7 compose]# systemctl start vsftpd [root@Centos7 compose]# nkdir /var/ftp/aaa #创建目录 [root@Centos7 compose]# mount /dev/cdrom /var/ftp/aaa #挂载光盘 ******(5)执行 docker-compose.yml 执行命令 # compose命令指定yml文件,后面可以跟SERVICE项中的服务,如web docker-compose up # 查看构建容器启用情况 compose命令:docker-compose ps # 查看构建容器启用情况 docker命令:docker ps [root@Centos7 compose]# docker-compose up -d #创建并启动,-d是放到后台 。。。。。。 WARNING: Image for service web was built because it did not already exist. To rebuild this image you must use `docker-compose build` or `docker-compose up --build`. Creating compose_web_1 ... done [root@Centos7 compose]# docker ps #查看所有容器 CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES f7ac5df20bd4 compose_web "/usr/sbin/httpd -D …" About a minute ago Up About a minute 0.0.0.0:8080->80/tcp compose_web_1 #可以看到名称是意compose开头的 [root@Centos7 compose]# docker images #查看镜像,也是以compose开头的 REPOSITORY TAG IMAGE ID CREATED SIZE compose_web latest b6757aa739f9 2 minutes ago 268MB httpd latest bd29370f84ea 3 weeks ago 138MB
使用浏览器访问测试,成功访问!!
五、Docker Compose 一键部署LNMP
(1)目录结构
(2)实验步骤(手动装LNMP)
- 提前在本地搭建好vsftpd服务:
[root@Centos7 ~]# yum -y install vsftpd #安装ftp服务 。。。。。。 完毕! [root@Centos7 ~]# systemctl start vsftpd [root@Centos7 ~]# nkdir /var/ftp/aaa #创建目录 [root@Centos7 ~]# mount /dev/cdrom /var/ftp/aaa #挂载光盘 [root@Centos7 ~]# vim centos.repo #编写yum源文件,下面的每个服务都需要穿这个文件 [aaa] name=aaa baseurl=ftp://192.168.100.202/aaa #修改为ftp enabled=1 gpgcheck=0 [root@Centos7 compose-lnmp]# tree #最终目录所需要的软件和文件 . ├── docker-compose.yml ├── mysql │ ├── centos.repo │ ├── cmake-2.8.6.tar.gz │ ├── conf │ │ └── my.cnf │ ├── Dockerfile │ └── mysql-5.6.36.tar.gz ├── nginx │ ├── centos.repo │ ├── Dockerfile │ ├── nginx-1.12.0.tar.gz │ └── nginx.conf ├── php │ ├── centos.repo │ ├── Dockerfile │ ├── Dockerfile[root@Centos7 │ ├── php-5.5.38.tar.gz │ └── php.ini └── wwwroot └── test.php
******(1)创建compose目录,以及各服务的子目录 [root@Centos7 ~]# mkdir /compose-lnmp [root@Centos7 ~]# cd /compose-lnmp/ [root@Centos7 compose-lnmp]# mkdir nginx mysql php wwwroot [root@Centos7 compose-lnmp]# tree . ├── mysql ├── nginx ├── php └── wwwroot 4 directories, 0 files ******(2)在各服务目录上传源码包 [root@Centos7 compose-lnmp]# tree . ├── mysql │ ├── cmake-2.8.6.tar.gz │ └── mysql-5.6.36.tar.gz ├── nginx │ ├── nginx-1.12.0.tar.gz ├── php │ ├── php-5.5.38.tar.gz └── wwwroot 5 directories, 8 files ******(3)编写Nginx的Dockerfile和nginx.conf文件 [root@Centos7 compose-lnmp]# cd nginx/ [root@Centos7 nginx]# vim Dockerfile FROM centos:7.4 MAINTAINER rzy RUN rm -rf /etc/yum.repos.d/* COPY centos.repo /etc/yum.repos.d/ RUN yum install -y gcc gcc-c++ make openssl-devel pcre-devel ADD nginx-1.12.0.tar.gz /tmp RUN cd /tmp/nginx-1.12.0 && ./configure --prefix=/usr/local/nginx && make -j 2 && make install RUN rm -f /usr/local/nginx/conf/nginx.conf COPY nginx.conf /usr/local/nginx/conf EXPOSE 80 CMD ["/usr/local/nginx/sbin/nginx", "-g", "daemon off;"] #保存退出 [root@Centos7 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; location / { root html; index index.html index.htm; } location ~\.php { root html; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; include fastcgi.conf; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } } #保存退出 ******(4)编写Mysql的Dockerfile和my.cnf文件 [root@Centos7 nginx]# cd .. [root@Centos7 compose-lnmp]# cd mysql/ [root@Centos7 mysql]# vim Dockerfile #如果在执行compose的时候显示安装yum源文件时报错,就把本地yum源那段删除,使用自带的网络yum源安装 FROM centos:7.4 MAINTAINER rzy RUN rm -rf /etc/RUN rm -rf /etc/yum.repos.d/* COPY centos.repo /etc/yum.repos.d/ ADD cmake-2.8.6.tar.gz /usr/src/ ADD mysql-5.6.36.tar.gz /usr/src/ WORKDIR /usr/src/cmake-2.8.6 RUN yum -y install ncurses-devel gcc gcc-c++ make perl perl-devel autoconf && ./configure && gmake && gmake install && groupadd mysql && useradd -M -s /sbin/nologin mysql -g mysql && cd /usr/src/mysql-5.6.36/ && cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DSYSCONFDIR=/etc -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DWITH_EXTRA_CHARSETS=all && make && make install && chown -R mysql:mysql /usr/local/mysql && rm -rf /etc/my.cnf && cp /usr/src/mysql-5.6.36/support-files/my-default.cnf /etc/my.cnf && /usr/local/mysql/scripts/mysql_install_db --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data/ && ln -s /usr/local/mysql/bin/* /usr/local/bin/ CMD ["sh","-c","mysqld_safe"] #保存退出 [root@Centos7 mysql]# mkdir conf [root@Centos7 mysql]# cd conf/ [root@Centos7 conf]# vim my.cnf [mysqld] user=mysql port=3306 datadir=/var/lib/mysql socket=/var/run/mysqld/mysqld.sock pid-file=/var/run/mysqld/mysqld.pid log_error=/var/log/mysql/error.log character_set_server = utf8 max_connections=3600 #保存退出 ******(4)编写php的Dockerfile和php.ini文件 [root@Centos7 mysql]# cd ../php/ [root@Centos7 php]# vim Dockerfile FROM centos:7.4 MAINTAINER rzy RUN rm -rf /etc/RUN rm -rf /etc/yum.repos.d/* COPY centos.repo /etc/yum.repos.d/ RUN yum install -y gcc gcc-c++ gd-devel libxml2-devel libcurl-devel libjpeg-devel libpng-devel openssl-devel make ADD php-5.5.38.tar.gz /tmp/ RUN cd /tmp/php-5.5.38 && \ ./configure --prefix=/usr/local/php \ --with-config-file-path=/usr/local/php/etc \ --with-mysql --with-mysqli \ --with-openssl --with-zlib --with-curl --with-gd \ --with-jpeg-dir --with-png-dir --with-iconv \ --enable-fpm --enable-zip --enable-mbstring && \ make -j 4 && make install && \ cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf && \ sed -i "s/127.0.0.1/0.0.0.0/" /usr/local/php/etc/php-fpm.conf && \ cp ./sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm && \ chmod +x /etc/init.d/php-fpm COPY php.ini /usr/local/php/etc EXPOSE 9000 CMD /etc/init.d/php-fpm start && tail -F /var/log/messages #保存退出 [root@Centos7 php]# vim php.ini 。。。。。。太长了,复制即可,路径在F:\源代码包\roles安装lnmp\php.ini ******(5)创建DockerCompose文件 [root@Centos7 php]# cd .. [root@Centos7 compose-lnmp]# vim docker-compose.yml version: '3' services: nginx: hostname: nginx build: context: ./nginx dockerfile: Dockerfile ports: - "80:80" links: - "php:php-cgi" volumes: - ./wwwroot:/usr/local/nginx/html php: hostname: php build: ./php links: - mysql:mysql-db #用来链接容器的,:前面指定服务名称,后面是别名 volumes: - ./wwwroot:/usr/local/nginx/html mysql: hostname: mysql build: ./mysql command: --default-authentication-plugin=mysql_native_password #这行代码解决无法访问的问题,如果这行报错,就先把这行注释 ports: - "3306:3306" #volumes: # #- ./mysql/conf:/etc/mysql/conf.d # #- ./mysql/data:/var/lib/mysql environment: MYSQL_ROOT_PASSWORD: 123456 MYSQL_USER: user MYSQL_PASSWORD: user123 #保存退出 ******(6)创建项目 [root@Centos7 compose-lnmp]# cd wwwroot/ [root@Centos7 wwwroot]# vim test.php <?php phpinfo()?> #保存退出 ******(7)执行docker-compose一键部署lnmp [root@Centos7 compose-lnmp]# docker-compose up -d 。。。。。。 #等待部署完成 [root@Centos7 compose-lnmp]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 4f07585e7670 compose-lnmp_nginx "/usr/local/nginx/sb…" 9 seconds ago Up 5 seconds 0.0.0.0:80->80/tcp compose-lnmp_nginx_1 051d965f5c74 compose-lnmp_php "/bin/sh -c '/etc/in…" 10 seconds ago Up 8 seconds 9000/tcp compose-lnmp_php_1 7fe2780aaf48 compose-lnmp_mysql "sh -c mysqld_safe" 10 seconds ago Up 9 seconds 0.0.0.0:3306->3306/tcp compose-lnmp_mysql_1 [root@Centos7 compose-lnmp]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE compose-lnmp_nginx latest 746296c1205d 4 minutes ago 331MB compose-lnmp_php latest 701308588742 5 minutes ago 825MB compose-lnmp_mysql latest ff67075ea79e 11 minutes ago 4.16GB httpd latest bd29370f84ea 3 weeks ago 138MB centos 7.4 196e0ce0c9fb 3 years ago 197MB
可以访问静态页面
(3)直接下载镜像部署LNMP
拉取镜像直接部署,这个比上面的方便的多
https://blog.csdn.net/weixin_44848382/article/details/106995188?
******(1)下载镜像 [root@Centos7 ~]# docker pull nginx [root@Centos7 ~]# docker pull mysql:5.7 [root@Centos7 ~]# docker pull cytopia/php-fpm-5.6 [root@Centos7 lnmp]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE mysql 5.7 8cf625070931 8 days ago 448MB nginx latest 4cdc5dd7eaad 3 weeks ago 133MB cytopia/php-fpm-5.6 latest 2943ebf1ef81 3 years ago 1.1GB
******(2)建立Compose的目录 [root@Centos7 ~]# mkdir lnmp [root@Centos7 ~]# cd lnmp/ [root@Centos7 lnmp]# mkdir conf [root@Centos7 lnmp]# mkdir html [root@Centos7 lnmp]# tree . ├── conf └── html 2 directories, 0 files
******(3)编写nginx配置文件 [root@Centos7 lnmp]# cd conf/ [root@Centos7 conf]# vim default.conf server { listen 80; listen [::]:80; server_name localhost; #charset koi8-r; #access_log /var/log/nginx/host.access.log main; location / { root /usr/share/nginx/html; index index.html index.php index.htm; } #error_page 404 /404.html; # redirect server error pages to the static page /50x.html # error_page 500 502 503 504 /50x.html; location = /50x.html { root /usr/share/nginx/html; } # proxy the PHP scripts to Apache listening on 127.0.0.1:80 # #location ~ \.php$ { # proxy_pass http://127.0.0.1; #} # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # location ~ \.php$ { root html; fastcgi_pass php:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME /var/www/html/$fastcgi_script_name; include fastcgi_params; } # deny access to .htaccess files, if Apache's document root # concurs with nginx's one # #location ~ /\.ht { # deny all; #} } #保存退出
******(4)编写页面 [root@Centos7 lnmp]# cd html/ [root@Centos7 html]# vim index.html aaaaaaaaaaaaaaaa #保存退出 [root@Centos7 html]# vim dbtest.php <?php $conn=mysql_connect("mysql","root","123456"); if ($conn) echo "it's ok!!!!!"; mysql_close(); ?> #保存退出 [root@Centos7 html]# vim test.php <?php phpinfo()?> #保存退出
******(5)编写docker-compose文件 [root@Centos7 html]# cd .. [root@Centos7 lnmp]# vim docker-compose.yml version: "3" services: nginx: image: nginx ports: - 80 volumes: - /root/lnmp/conf/default.conf:/etc/nginx/conf.d/default.conf ##配置文件位置映射 - /root/lnmp/html/:/usr/share/nginx/html ##网页文件位置映射 depends_on: - php php: image: cytopia/php-fpm-5.6 volumes: - /root/lnmp/html/:/var/www/html/ ##网页文件位置映射 depends_on: - mysql mysql: image: mysql:5.7 environment: - MYSQL_ROOT_PASSWORD=123456 ##数据库用户密码,直接拉取mysql镜像的密码默认为123456 #保存退出
******(6)构建compose [root@Centos7 lnmp]# tree . ├── conf │ └── default.conf ├── docker-compose.yml └── html ├── dbtest.php ├── index.html └── test.php 2 directories, 5 files [root@Centos7 lnmp]# docker-compose up -d Creating network "lnmp_default" with the default driver Creating lnmp_mysql_1 ... done Creating lnmp_php_1 ... done Creating lnmp_nginx_1 ... done [root@Centos7 lnmp]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 1b01794728a1 nginx "/docker-entrypoint.…" 10 seconds ago Up 9 seconds 0.0.0.0:32768->80/tcp lnmp_nginx_1 8be48bd6a700 cytopia/php-fpm-5.6 "/docker-entrypoint.…" 10 seconds ago Up 9 seconds 9000/tcp lnmp_php_1 98ee98e22865 mysql:5.7 "docker-entrypoint.s…" 11 seconds ago Up 10 seconds 3306/tcp, 33060/tcp lnmp_mysql_1
访问Nginx的静态页面
访问php页面
访问mysql页面