yum -y install docker
systemctl start docker
systemctl enable docker
https://docs.docker.com/compose/install/
git clone https://github.com/docker/compose.git
chmod u+x compose
./compose
or
pip install docker-compose
docker-compose -version
docker pull mysql
docker run -p 3306:3306 --name mysql_test -v $PWD/mysql/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=passwd -d --privileged=true mysql
-p 3306:3306:將容器的3306端口映射到主機的3306端口 -v PWD/mysql/data:/var/lib/mysql:將主機當前目錄下的mysql/data文件夾掛載到容器的/var/lib/mysql 下,在mysql容器中產生的數據就會保存在本機mysql/data目錄下 -e MYSQL_ROOT_PASSWORD=passwd:初始化root用戶的密碼 -d 後臺運行容器 --name 給容器指定別名 --privileged=true centos7 可能會碰到權限問題,須要加參數
docker exec -it mysql_test /bin/bash
root@39e0abed7609:/# mysql -u root -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 8 Server version: 8.0.12 MySQL Community Server - GPL Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | sys | +--------------------+ 4 rows in set (0.02 sec) mysql>
vim Dockerfile
FROM php:5.6-fpm RUN apt-get update && apt-get install -y \ libfreetype6-dev \ libjpeg62-turbo-dev \ libpng12*-dev \ vim \ && docker-php-ext-install pdo_mysql \ && docker-php-ext-configure gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/ \ && docker-php-ext-install gd \
構造鏡像
docker build -t="php-fpm5.6/v2" .
docker run -d -p 9000:9000 -v /var/www/html/:/var/www/html/ --name php-with-mysql --link mysql_test:mysql --volumes-from mysql_test --privileged=true php-fpm5.6/v2
參數解析
-v 將本地磁盤上的php代碼掛載到docker 環境中,對應docker的目錄是 /var/www/html/ --name 新建的容器名稱 php-with-mysql --link 連接的容器,連接的容器名稱:在該容器中的別名,運行這個容器是,docker中會自動添加一個host識別被連接的容器ip --privileged=true 權限問題
進入容器php
docker exec -it php-with-mysql /bin/bash
cd /var/www/html/ && ls
vim default.confhtml
server { listen 80; server_name localhost; location / { root /var/www/html; index index.html index.htm index.php; # 增長index.php } #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 /var/www/html; } location ~ \.php$ { root /var/www/html; # 代碼目錄 fastcgi_pass phpfpm:9000; # 修改成phpfpm容器 fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; # 修改成$document_root include fastcgi_params; } }
docker run -d --link php-with-mysql:phpfpm --volumes-from php-with-mysql -p 80:80 -v /var/www/nginx/conf/default.conf:/etc/nginx/conf.d/default.conf --name nginx-php --privileged=true nginx
參數解析mysql
--link php-with-mysql:phpfpm 將php容器連接到nginx容器裏來,phpfpm是nginx容器裏的別名。 --volumes-from php-with-mysql 將php-with-mysql 容器掛載的文件也一樣掛載到nginx容器中 -v /var/www/nginx/conf/default.conf:/etc/nginx/conf.d/default.conf 將nginx 的配置文件替換,掛載本地編寫的配置文件 docker exec -it nginx-php bash root@32de01dbee49:/# cd /var/www/html/&&ls index.php mysql.php testmysql.php webview
[root@cc home]# tree compose-lnmp/ compose-lnmp/ |-- docker-compose.yml |-- html
| |-- index.html
|-- mysql
| `-- Dockerfile |-- nginx | |-- conf | | `-- default.conf | `-- Dockerfile `-- phpfpm `-- Dockerfile
[root@cc compose-lnmp]# cat docker-compose.yml nginx: build: ./nginx ports: - "80:80" links: - "phpfpm" volumes: - /home/compose-lnmp/html/:/var/www/html/ - /home/compose-lnmp/nginx/conf/default.conf:/etc/nginx/conf.d/default.conf phpfpm: build: ./phpfpm ports: - "9000:9000" volumes: - ./html/:/var/www/html/ links: - "mysql" mysql: build: ./mysql ports: - "3306:3306" volumes: - /home/compose-lnmp/mysql/data/:/var/lib/mysql/ environment: MYSQL_ROOT_PASSWORD : 123456
[root@cc compose-lnmp]# cat mysql/Dockerfile
FROM mysql:5.6nginx
[root@cc compose-lnmp]# cat nginx/Dockerfile
FROM nginx:latest
RUN apt-get update && apt-get install -y vimgit
[root@cc compose-lnmp]# cat nginx/conf/default.conf
server {
listen 80;
server_name localhost;
location / {
root /var/www/html;
index index.html index.htm index.php; # 增長index.php
}
#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 /var/www/html;
}
location ~ \.php$ {
root /var/www/html; # 代碼目錄
fastcgi_pass phpfpm:9000; # 修改成phpfpm容器
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; # 修改成$document_root
include fastcgi_params;
}
}github
[root@cc compose-lnmp]# cat phpfpm/Dockerfile
FROM php:5.6-fpm
RUN apt-get update && apt-get install -y \
libfreetype6-dev \
libjpeg62-turbo-dev \
libpng12*-dev \
vim \
&& docker-php-ext-install pdo_mysql \
&& docker-php-ext-configure gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/ \
&& docker-php-ext-install gd \web
執行docker-composesql
[root@cc compose-lnmp]# docker-compose up -ddocker