Docker学习整理-安装mysql nginx php

本文详细介绍了在CentOS系统中如何安装和使用Docker,包括启动、停止、重启和删除容器的操作。以MySQL和Nginx为例,演示了如何拉取镜像、创建并运行容器,以及配置容器端口映射和数据卷。此外,还提供了Nginx的default.conf配置文件参考,帮助读者理解容器的网络和目录映射设置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


安装docker
https://www.runoob.com/docker/centos-docker-install.html

容器的使用
https://www.runoob.com/docker/docker-container-usage.html

启动docker
sudo systemctl start docker


Docker安装一般程序的过程记录,以mysql为例

1.拉取 MySQL 镜像,最新版

docker pull mysql:latest

#也可以指定版本docker pull mysql:5.6


2.查看本地镜像
docker images


3.运行容器
docker run -itd --name mysql-test -p 3306:3306 -e MYSQL_ROOT_PASSWORD=123456 mysql


参数说明:

--name mysql-test  容器的名称
-p 3306:3306  映射容器服务的 3306 端口到宿主机的 3306 端口,外部主机可以直接通过 宿主机ip:3306 访问到 MySQL 的服务。
-e MYSQL_ROOT_PASSWORD=123456 配置参数 设置 MySQL 服务 root 用户的密码。

docker run 的 -i -t -d参数说明

OptionsMean
-i以交互模式运行容器,通常与 -t 同时使用;
-t为容器重新分配一个伪输入终端,通常与 -i 同时使用;
-d后台运行容器,并返回容器ID;

 

4.使用 docker start 启动一个已停止的容器 
docker start b750bbbcfd88 


5.重启一个容器(最后一段是容器的name,也可以用容器id)
docker restart nginx-test9


6.删除容器
docker rm -f 1e560fca3906


7.创建一个nginx的容器
docker run --name nginx-test9 -p 8089:80 -v /home/lnmp/nginx/conf:/etc/nginx/conf.d -v /home/lnmp/nginx/www:/usr/share/nginx/html -d nginx
参数说明:
    --name nginx-test9 起别名
    -p 8089:80 端口映射,即访问电脑的8089端口会转到当前容器的80端口
-v /home/lnmp/nginx/conf:/etc/nginx/conf.d 文件夹映射 相当于把前面的文件夹“/home/lnmp/nginx/conf”替换容器内的这个文件夹“/etc/nginx/conf.d”,这个文件夹是nginx镜像创建容器之后的配置文件所在目录; /home/lnmp/nginx/www:/usr/share/nginx/html,这段是替换web目录,nginx的web目录可以通过conf目录的default.conf文件配置
-d 在后台运行
nginx 指的镜像名称

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; #这里定义默认的web目录
        index  index.html 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   172.17.0.4:9000; #这里指的是PHP的调用地址,前面是PHP容器的id,后面是PHP监听的端口
        fastcgi_index  index.php;
        #fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        fastcgi_param  SCRIPT_FILENAME  /www/$fastcgi_script_name; #这里配置的是PHP的www目录,$后面的是变量,应该指的是原始的访问路径,即通过nginx的端口访问的后缀为php的文件会执行php容器的/www下面的文件
        include        fastcgi_params;
    }

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #    deny  all;
    #}
}

 

参考地址https://www.kancloud.cn/wyj0309/docker/533561
https://blog.csdn.net/a516972602/article/details/86376990/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值