初见Docker使用记录

用到的一些docker命令

`docker ps -a` 列出docker容器
`docker images` 列出docker镜像
`docker build -t 镜像名字 目录` 用Dockerfile创建一个镜像
`docker exec -i -t 665b4a1e17b6 bash` 进入docker的shell

web内容储存

用来连接本地文件和docker容器,这样修改代码就不用进入容器里。

docker run --name=wwwroot -v /var/www/html:/var/www/html -d busybox echo wwwroot

`/var/www/html:/var/www/html` 为本地位置和容器内位置

php-fpm

先写个Dockerfile

FROM php:5.6-fpm-alpine

# Install other needed extensions
RUN	set -x \
	&& apk add --no-cache icu-libs \
	&& apk add --no-cache --virtual .php-ext-deps icu-dev \
	&& docker-php-ext-install intl mysqli pdo_mysql \
	&& apk del .php-ext-deps

WORKDIR /var/www

Dockerfile所在目录执行docker build -t local/php .,等镜像创建完后再

docker run --name=php-fpm --volumes-from wwwroot -d local/php

让镜像跑起来。

nginx

还是先建Dockerfile文件,写入

FROM nginx
RUN sed -i '/SCRIPT_NAME/ifastcgi_param  SCRIPT_FILENAME    $request_filename;' /etc/nginx/fastcgi_params
WORKDIR /var/www

以及用到的nginx配置文件`default.conf`

server {
	listen 80;
	root /var/www/html;
	index index.html index.htm index.php;
	server_name localhost;
	location / {
		# First attempt to serve request as file, then
		# as directory, then fall back to displaying a 404.
		try_files $uri $uri/ =404;
		# Uncomment to enable naxsi on this location
		# include /etc/nginx/naxsi.rules
	}
	location ~ \.php$ {
		fastcgi_split_path_info ^(.+\.php)(/.+)$;
		fastcgi_pass php:9000;
		fastcgi_index index.php;
		include fastcgi_params;
	}
}

创建镜像docker build -t local/nginx .并运行

docker run --name=nginx --volumes-from wwwroot --link php-fpm:php -v /var/docker/nginx/default.conf:/etc/nginx/conf.d/default.conf -p 80:80 -d local/nginx

/var/docker/nginx/default.conf是本地的文件位置

Leave a comment

* - Required fields