Docker 学习笔记

来源:互联网 发布:excel怎样数据保护 编辑:程序博客网 时间:2024/06/06 03:57
1. 使用Docker测试静态网站

使用nginx提供HTTP服务.

Dockerfile 创建镜像:
dongli@ubuntu:~/Docker/sample$ cat Dockerfile
FROM ubuntu:14.04
MAINTAINER Edgar Li "dongli@test.com"
RUN apt-get update
RUN apt-get -y -q install nginx
RUN mkdir -p /var/www/html
ADD global.conf /etc/nginx/conf.d/
ADD nginx.conf /etc/nginx/nginx.conf
EXPOSE 80

Nginx 配置文件:
dongli@ubuntu:~/Docker/sample$ cat global.conf
server {
        listen          0.0.0.0:80;
        server_name     _;

        root            /var/www/html/website;
        index           index.html index.htm;

        access_log      /var/log/nginx/default_access.log;
        error_log       /var/log/nginx/default_error.log;
}

dongli@ubuntu:~/Docker/sample$ cat nginx.conf
user www-data;
worker_processes 4;
pid /run/nginx.pid;
daemon off; \\ 非守护进程模式, 否则容器无法持续运行

events {  }

http {
  sendfile on;
  tcp_nopush on;
  tcp_nodelay on;
  keepalive_timeout 65;
  types_hash_max_size 2048;
  include /etc/nginx/mime.types;
  default_type application/octet-stream;
  access_log /var/log/nginx/access.log;
  error_log /var/log/nginx/error.log;
  gzip on;
  gzip_disable "msie6";
  include /etc/nginx/conf.d/*.conf;
}
dongli@ubuntu:~/Docker/sample$

创建镜像:
dongli@ubuntu:~/Docker/sample$ sudo docker build -t nginx .
Sending build context to Docker daemon 4.096 kB
Step 1 : FROM ubuntu:14.04
 ---> e5ada6f7fa49
Step 2 : MAINTAINER Edgar Li "dongli@test.com"
 ---> Running in 48090fe36d0d
 ---> 71c932e5c81a
Removing intermediate container 48090fe36d0d
Step 3 : RUN apt-get update
 ---> Running in 55d5802aec9b
Get:1 http://security.ubuntu.com trusty-security InRelease [65.9 kB]
Ign http://archive.ubuntu.com trusty InRelease
Get:2 http://archive.ubuntu.com trusty-updates InRelease [65.9 kB]
Get:3 http://security.ubuntu.com trusty-security/universe Sources [77.9 kB]
。。。。。。
Fetched 21.1 MB in 2min 11s (160 kB/s)
Reading package lists...
 ---> 59cc2c500409
Removing intermediate container 55d5802aec9b
Step 4 : RUN apt-get -y -q install nginx
 ---> Running in 3a1cd91d54e3
Reading package lists...
Building dependency tree...
Reading state information...
The following extra packages will be installed:
  fontconfig-config fonts-dejavu-core geoip-database libfontconfig1
  libfreetype6 libgd3 libgeoip1 libjbig0 libjpeg-turbo8 libjpeg8 libtiff5
  libvpx1 libx11-6 libx11-data libxau6 libxcb1 libxdmcp6 libxml2 libxpm4
  libxslt1.1 nginx-common nginx-core sgml-base xml-core
Suggested packages:
  libgd-tools geoip-bin fcgiwrap nginx-doc sgml-base-doc debhelper
The following NEW packages will be installed:
  fontconfig-config fonts-dejavu-core geoip-database libfontconfig1
  libfreetype6 libgd3 libgeoip1 libjbig0 libjpeg-turbo8 libjpeg8 libtiff5
  libvpx1 libx11-6 libx11-data libxau6 libxcb1 libxdmcp6 libxml2 libxpm4
  libxslt1.1 nginx nginx-common nginx-core sgml-base xml-core
0 upgraded, 25 newly installed, 0 to remove and 5 not upgraded.
Need to get 5589 kB of archives.
After this operation, 19.8 MB of additional disk space will be used.
Get:1 http://archive.ubuntu.com/ubuntu/ trusty/main libgeoip1 amd64 1.6.0-1 [71.0 kB]
。。。。。。。。。。。。。
debconf: unable to initialize frontend: Dialog
debconf: (TERM is not set, so the dialog frontend is not usable.)
 ...
 ---> a60cea46a10b
Removing intermediate container 3a1cd91d54e3
Step 5 : RUN mkdir -p /var/www/html
 ---> Running in 667f690673ed
 ---> 2e209bf02b9a
Removing intermediate container 667f690673ed
Step 6 : ADD global.conf /etc/nginx/conf.d/
 ---> bf6ceb7d87dd
Removing intermediate container 1cd2104c0656
Step 7 : ADD nginx.conf /etc/nginx/nginx.conf
 ---> 97abaaf1fcac
Removing intermediate container eb637ad33282
Step 8 : EXPOSE 80
 ---> Running in 016bfe4fe0e7
 ---> f31c1b5bfdc3
Removing intermediate container 016bfe4fe0e7
Successfully built f31c1b5bfdc3
dongli@ubuntu:~/Docker/sample$


dongli@ubuntu:~/Docker/sample$ sudo docker history nginx  // 查看nginx 镜像构建过程
IMAGE               CREATED             CREATED BY                                      SIZE                COMMENT
f31c1b5bfdc3        8 minutes ago       /bin/sh -c #(nop) EXPOSE 80/tcp                 0 B
97abaaf1fcac        8 minutes ago       /bin/sh -c #(nop) ADD file:30250a5447ff32cdb6   415 B
bf6ceb7d87dd        8 minutes ago       /bin/sh -c #(nop) ADD file:6fd99da3abf2d9dd4d   286 B
2e209bf02b9a        8 minutes ago       /bin/sh -c mkdir -p /var/www/html               0 B
a60cea46a10b        8 minutes ago       /bin/sh -c apt-get -y -q install nginx          20.83 MB
59cc2c500409        9 minutes ago       /bin/sh -c apt-get update                       21.08 MB
71c932e5c81a        11 minutes ago      /bin/sh -c #(nop) MAINTAINER Edgar Li "dongli   0 B

启动容器:
建立html 文件:
dongli@ubuntu:~/Docker/sample$ mkdir website
dongli@ubuntu:~/Docker/sample$ cd website/
wget https://raw.githubusercontent.com/jamtur01/dockerbook-code/master/code/5/sample/website/index.html

sudo docker run -d -p 127.0.0.1::80 --name websize -v $PWD/website:/var/www/html/website nginx nginx
-v : 允许我们将宿主主机目录作为卷,挂载到容器。 指定了 卷的源目录(宿主主机目录)和容器里面的目的目录。 后可加rw,ro 读写状态 。 可以实现docker之间的文件的共享.
直接修改宿主主机上的index.html
sudo docker run -d -p 127.0.0.1::80 --name websize -v $PWD/website:/var/www/html/website nginx nginx -g "daemon off;"

dongli@ubuntu:~$ sudo docker port fb2b21ec4f70
80/tcp -> 127.0.0.1:32768

dongli@ubuntu:~/Docker/sample/website$ curl 127.0.0.1:32771
<head>

<title>Test website</title>

</head>

<body>

<h1>This is a test website</h1>
<br>
<h1>Edgar Li Add</h1>

</body>
原创粉丝点击