Docker安装 LAMP

                                            我用的centos7.5 .1804

一、通过 RPM 安装 docker 17.03.0 版本并且配置 docker 阿里加速

安装依赖包

[root@centos7 ~]# yum install -y yum-utils device-mapper-persistent-data lvm2

配置docker yum源

[root@centos7 ~]# cat <<END >/etc/yum.repos.d/docker.repo
[docker]
name=docker
baseurl = https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/centos/7/x86_64/stable/
enabled=1
gpgcheck=0
END
[root@centos7 ~]# yum clean all && yum makecache

查看docker 版本

[root@centos7 ~]# yum -y list docker-ce --showduplicates |sort -r

安装

[root@centos7 ~]# yum install docker-ce-17.03.0.ce-1.el7.centos

启用阿里镜像加速   【我的镜像加速  阿里云注册】

[root@centos7 ~]# mkdir -p /etc/docker
[root@centos7 ~]# tee /etc/docker/daemon.json <<-'EOF'
> {
>   "registry-mirrors": ["https://wjq8xgjg.mirror.aliyuncs.com"]
> }
> EOF
[root@centos7 ~]# systemctl daemon-reload
[root@centos7 ~]# systemctl restart docker

二、通过 docker 安装一个 LAPM 架构

查找lamp的镜像

[root@centos7 ~]# docker search -s 10 lamp 
Flag --stars has been deprecated, use --filter=stars=3 instead
NAME                DESCRIPTION                                     STARS               OFFICIAL            AUTOMATED
mattrayner/lamp     A simple LAMP docker image running the prere…   240                                     [OK]
linode/lamp         LAMP on Ubuntu 14.04.1 LTS Container            178                                     
tutum/lamp          Out-of-the-box LAMP image (PHP+MySQL)           141                                     
greyltc/lamp        a super secure, up-to-date and lightweight L…   100                                     [OK]
fauria/lamp         Modern, developer friendly LAMP stack. Inclu…   93                                      [OK]
lioshi/lamp         Docker image for LAMP under debian              15                                      [OK]
dgraziotin/lamp                                                     11                                      [OK]

把第一个拉下来

[root@centos7 ~]# docker pull mattrayner/lamp

等……

[root@centos7 ~]# docker images
mattrayner/lamp     latest              05750cfa54d5        3 months ago        915MB

创建目录以及容器    并挂载到宿主机的目录到容器内

[root@centos7 ~]# mkdir /mysql_data
[root@centos7 ~]# docker run -d --name=lamp -p 8080:80 -p 3306:3306  -v /mysql_data:/var/lib/mysql docker.io/mattrayner/lamp
a58e4fe81f7cda319bdac3df7c5c60a413e75356c26ec3dd9766e59b5e2f2335

进入容器    初始化数据库

[root@centos7 ~]# mkdir /mysql_data
[root@centos7 ~]# docker run -d --name=lamp -p 8080:80 -p 3306:3306  -v /mysql_data:/var/lib/mysql docker.io/mattrayner/lamp
a58e4fe81f7cda319bdac3df7c5c60a413e75356c26ec3dd9766e59b5e2f2335
[root@centos7 ~]# docker exec -it lamp bash
root@a58e4fe81f7c:/# mysql_secure_installation

Securing the MySQL server deployment.

Connecting to MySQL using a blank password.

VALIDATE PASSWORD PLUGIN can be used to test passwords
and improve security. It checks the strength of password
and allows the users to set only those passwords which are
secure enough. Would you like to setup VALIDATE PASSWORD plugin?

Press y|Y for Yes, any other key for No: y                           #是否要设置验证密码插件              

There are three levels of password validation policy:

LOW    Length >= 8                                                   #低长度>=8
MEDIUM Length >= 8, numeric, mixed case, and special characters      #中等长度>=8,数字、混合大小写和特殊字符
STRONG Length >= 8, numeric, mixed case, special characters and dictionary                   file   #强长度>=8,数字,混合大小写,特殊字符和字典文件

Please enter 0 = LOW, 1 = MEDIUM and 2 = STRONG: 0
New password:                                                                            #密码

Re-enter new password:

Estimated strength of the password: 50 
Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) : y             #继续
By default, a MySQL installation has an anonymous user,
allowing anyone to log into MySQL without having to have
a user account created for them. This is intended only for
testing, and to make the installation go a bit smoother.
You should remove them before moving into a production
environment.

Remove anonymous users? (Press y|Y for Yes, any other key for No) :y                      #删除匿名帐户

Normally, root should only be allowed to connect from
'localhost'. This ensures that someone cannot guess at
the root password from the network.

Disallow root login remotely? (Press y|Y for Yes, any other key for No) :                #不允许root远程登陆   

... skipping.
By default, MySQL comes with a database named 'test' that
anyone can access. This is also intended only for testing,
and should be removed before moving into a production
environment.


Remove test database and access to it? (Press y|Y for Yes, any other key for No) :y          #删除test数据库
- Dropping test database...
Success.
- Removing privileges on test database...
Success.
Reloading the privilege tables will ensure that all changes
made so far will take effect immediately.
Reload privilege tables now? (Press y|Y for Yes, any other key for No) :y                      #重新加载特权表
Success.

All done!

该镜像是基于ubuntu1804系统的  

查看php mysql apache都是哪个版本

root@a58e4fe81f7c:~# dpkg -l

得知

php7.4     MySQL5.7.30      apache2.4.29

下载个包查看端口信息

root@a58e4fe81f7c:/# apt-get install net-tools

查看端口

root@a58e4fe81f7c:/# netstat -aptn
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 0.0.0.0:3306            0.0.0.0:*               LISTEN      -                   
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      504/apache2         

docker里面的host不能配置127.0.0.1 或者192.168.0.1 或则宿主机器将无法访问端口

root@a58e4fe81f7c:~# cd /var/www/html/                            #测试页面
root@a58e4fe81f7c:/var/www/html# vim index.php

<?php
    echo phpinfo();
?>

访问

三、docker run 命令的延申指令,如怎么在停止一个 docker 容器的时候自动删除该容器

[root@centos7 ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
[root@centos7 ~]# docker run -it --rm centos bash
[root@6cc822095ce4 /]# exit
exit
[root@centos7 ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES

四、docker run 命令在自动启动 docker 服务时  容器也自动启动

[root@centos7 ~]# docker run -d --name centos1 --restart=always centos
原文地址:https://www.cnblogs.com/zhangty333/p/13973444.html