Docker学习笔记

 

一、制作 nginx 镜像

1、下载配置文件

  • mkdir /opt/nginx_docker && cd /opt/nginx_docker
  • mkdir nginx && cd nginx
  • wget https://raw.githubusercontent.com/jamtur01/dockerbook-code/master/code/5/sample/nginx/global.conf
  • wget https://raw.githubusercontent.com/jamtur01/dockerbook-code/master/code/5/sample/nginx/nginx.conf
  • cd ..

2、编写Dockerfile&&制作镜像

  • cd /opt/nginx_docker

  • vi Dockerfile

  • docker build -t liangshengqi/nginx .
  • docker history xxxxxxx

FROM ubuntu: 14.04

MAINTAINER: LIANGSHENGQI "liangshengqi@inspur.com"

ENV REFRESHED_AT 2017-11-14

RUN apt-get update

RUN apt-get -y -q install nginx

RUN mkdir -p /var/www/html

ADD nginx/global.conf /etc/nginx/conf.d/

ADD nginx/nginx.conf /etc/nginx/nginx.conf

EXPOSE 80

3、运行nginx镜像/创建容器

cd nginx_docker

mkdir website && cd website

wget https://raw.githubusercontent.com/jamtur01/dockerbook-code/master/code/5/sample/website/index.html

docker run -d -p 80 --name myweb -v /opt/nginx_docker/website:/var/www/html/website lexiaofei/nginx nginx

查看日志 docker logs -f myweb 

二、在容器内安装运行 nginx

1、启动容器

设置端口映射,允许访问容器的80端口

  • -P 为所有端口映射 
  • -p 指定映射端口
  • docker run -p 80 -i -t ubuntu /bin/bash
  • docker run -p 8080:80 -i -t ubuntu /bin/bash
  • docker run -p 0.0.0.0:80 -i -t ubuntu /bin/bash
  • docker run -p 0.0.0.0:8080:80 -i -t ubuntu /bin/bash 

#创建映射80端口的交互式容器

  • docker run -p 80 --name web ubuntu /bin/bash 

2、安装nginx 

#安装nginx, 安装vim,创建静态页面

容器内#apt-get install -y nginx

容器内#apt-get install -y vim

容器内#mkdir -p /var/www/html

容器内#cd /var/www/html

容器内#vi index.html

<html>

<head><title>Hello</title></head>

<body>

<h1>Hahaha!</h1>

</body> 

</html>

#修改nginx配置文件

whereis nginx

ls /etc/nginx 

vim /etc/nginx/sites-enabled/default

root /var/www/html/index.html 

3、#运行nginx

启动nginx(此时在容器内,所以在容器内运行)::nginx

查看正在运行的进程(此时在容器内,所以列出的是容器内的正在运行的进程)::ps -ef

退出容器::ctrl+p/ctrl+q 

列出正在运行的容器:docker ps -a

列出指定的容器的端口映射::docker port web 

4、#验证网站访问

curl http://127.0.0.1:49167

docker inspect web

curl http://172.17.0.32/ 

docker stop web

docker start -i web

ctrl+p/q

在容器内启动进程::docker exec web nginx

查看容器内运行的进程::docker top web

curl http://172.17.0.32 # 这次不行了,重新启动后ip变了!

原文地址:https://www.cnblogs.com/lexiaofei/p/6361630.html