docker&&小實戰
使用docker搭建一套簡單的LNMP環境
要求:
- 最終實現能夠訪問:index.php
- 新的網橋IP爲:192.168.0.100/24
- PHP版本爲PHP-7 nginx版本爲NGINX-1.14
- nginx配置文件物理機路徑:/data/nginxconf/nginx.conf,容器配置文件路徑:/etc/nginx/
- 使用nginx新的容器創建一個新的鏡像:鏡像名爲:local-nginx 版本爲:tag-V1
實現
主機操作:
sudo apt-get update
sudo apt-get install apt-transport-https ca-certificates curl software-properties-common
curl -fsSL https://mirrors.ustc.edu.cn/docker-ce/linux/ubuntu/gpg | sudo apt-key add -
sudo add-apt-repository "deb [arch=amd64] https://mirrors.ustc.edu.cn/docker-ce/linux/ubuntu \
$(lsb_release -cs) \
stable"
sudo apt-get update
sudo apt-get install docker-ce
curl -fsSL get.docker.com -o get-docker.sh
sudo sh get-docker.sh --mirror Aliyun
groupadd docker
usermod -aG docker $USER
docker network ls
brctl show
apt-get install bridge-utils
service docker stop
ip link set dev luckydog down
brctl delbr luckydog
brctl addbr luckydog
ip addr add 192.168.0.100/24 dev luckydog
ip link set dev luckydog up
ip addr show luckydog
echo 'DOCKER_OPTS="-b=luckydog"' >> /etc/default/docker
service docker start
service apache2 stop
docker pull ubuntu:18.04
root@ubuntu:~# docker run --name 200312a -p 80:80 -v /data/nginxconf:/etc/nginx -it ubuntu:18.04
容器內操作:
apt-get update
apt-get install nginx net_tools php7.2 php7.2-fpm vim lynx
mkdir -p /run/php
/etc/init.d/php7.2-fpm restart
cd /var/www/html/
root@f43f67699f19:/var/www/html# cat /var/www/html/index.php
<?php
echo "docker_nginx"
?>
vim /etc/nginx/sites-enabled/default
--snip--
location ~ \.php$ {
include snippets/fastcgi-php.conf;
#
# # With php-fpm (or other unix sockets):
fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
# # With php-cgi (or other tcp sockets):
# fastcgi_pass 127.0.0.1:9000;
}
nginx -t
service nginx restart
lynx 127.0.0.1/index.php
root@f43f67699f19:/var/www/html# php -v
PHP 7.2.24-0ubuntu0.18.04.3 (cli) (built: Feb 11 2020 15:55:52) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies
with Zend OPcache v7.2.24-0ubuntu0.18.04.3, Copyright (c) 1999-2018, by Zend Technologies
root@f43f67699f19:/var/www/html# nginx -v
nginx version: nginx/1.14.0 (Ubuntu)
主機操作:
root@ubuntu:~# curl 127.0.0.1/index.php
docker_nginx
root@ubuntu:~#
root@ubuntu:~# ls /data/nginxconf/
conf.d fastcgi_params koi-win modules-available nginx.conf scgi_params sites-enabled uwsgi_params
fastcgi.conf koi-utf mime.types modules-enabled proxy_params sites-available snippets win-utf
root@ubuntu:~# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f43f67699f19 ubuntu:18.04 "/bin/bash" 34 minutes ago Up 34 minutes 0.0.0.0:80->80/tcp 200312a
root@ubuntu:~# docker commit f43f67699f19
sha256:bfe293bd2d666d07a3bde40557e133c7c3f721514a44d28ede38f78046618d54
root@ubuntu:~# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
<none> <none> bfe293bd2d66 10 seconds ago 230 MB
ubuntu 18.04 72300a873c2c 2 weeks ago 64.2 MB
root@ubuntu:~# docker tag bfe293bd2d66 ubuntu/local_nginx:tag-v1
root@ubuntu:~# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
ubuntu/local_nginx tag-v1 bfe293bd2d66 4 minutes ago 230 MB