Docker下创建基本Nginx镜像

1 创建centos下基本Nginx镜像

1.1 搜索centos镜像

[root@localhost ~] docker search centos
NAME                               DESCRIPTION                                     STARS               OFFICIAL            AUTOMATED
centos                             The official build of CentOS.                   5575                [OK]
ansible/centos7-ansible            Ansible on Centos7                              123                                     [OK]
jdeathe/centos-ssh                 OpenSSH / Supervisor / EPEL/IUS/SCL Repos - …   112                                     [OK]
consol/centos-xfce-vnc             Centos container with "headless" VNC session…   99                                      [OK]
centos/mysql-57-centos7            MySQL 5.7 SQL database server                   63
imagine10255/centos6-lnmp-php56    centos6-lnmp-php56                              57                                      [OK]
tutum/centos                       Simple CentOS docker image with SSH access      45
centos/postgresql-96-centos7       PostgreSQL is an advanced Object-Relational …   39
kinogmt/centos-ssh                 CentOS with SSH                                 29                                      [OK]

1.2 下载官方系统centos镜像并查看

[root@localhost ~] docker pull centos
Using default tag: latest
latest: Pulling from library/centos
d8d02d457314: Pull complete
Digest: sha256:307835c385f656ec2e2fec602cf093224173c51119bbebd602c53c3653a3d6eb
Status: Downloaded newer image for centos:latest
docker.io/library/centos:latest
[root@localhost ~]
[root@localhost ~] docker images
REPOSITORY                    TAG                 IMAGE ID            CREATED             SIZE
centos                        latest              67fa590cfc1c        5 weeks ago         202MB
centos                        7.6.1810            f1cb7c7d58b7        6 months ago        202MB

1.3 启动并进入到镜像中

[root@localhost ~] docker run -it --name mynginx centos /bin/bash
[root@637e2766790f /]

1.4 镜像定制化(进入之后的操作就跟正常在centos中操作是一样的)

wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
yum clean all
yum makecache

1.5 开始安装nginx

[root@637e2766790f ~] yum install -y nginx #默认的网络连接方式是可以连接外网的
[root@637e2766790f ~] vim /etc/nginx/nginx.conf #修改参数
加上一行 daemon off;(禁止后台运行,docker不允许nginx后台运行)

[root@a8882e2ef0d9 ~] nginx -t    #检查nginx配置文件语法
[root@a8882e2ef0d9 ~] nginx    #启动ngin

1.6 检查结果

正常在nginx命令运行后,界面是卡住的,所以另外打开一个ssh渠道执行

[root@localhost ~] docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                NAMES
637e2766790f        centos              "/bin/bash"              12 minutes ago      Up 12 minutes                            mynginx

1.7 提交镜像并查看

[root@localhost ~] docker commit -m "add new nginx docker images" mynginx murry/nginx:v2
sha256:6a3a0afe8f25bbe22cde37a9f3db4e7c8572504a6e06ef10648d179c6f318234
[root@localhost ~]
[root@localhost ~] docker images
REPOSITORY                    TAG                 IMAGE ID            CREATED             SIZE
murry/nginx                   v2                  6a3a0afe8f25        3 seconds ago       593MB

1.8 测试nginx

运行自己的镜像

docker run -it -d --name webserver_v2 -p 9000:80 murry/nginx:v2 nginx

浏览器访问服务器IP地址:http://192.168.50.70:9000/

在这里插入图片描述

1.9 进入到容器内部

docker exec -it webserver_v2 bash

1.10 配置文件、目录的映射

1.10.1 创建日志目录
/docker/nginxv03.1/logs

1.10.2 创建主配置文件
/docker/nginxv03.1/conf/nginx.conf

1.10.3 修改容器中主配置文件

增加子配置文件夹
include /usr/local/nginx/conf.d/*.conf;

1.10.4 创建配置文件扩展目录
/docker/nginxv03.1/conf.d

1.10.5 运行镜像创建container

docker run -it -d --name u_nginx_v7.01 \
-v /docker/nginxv03.1/logs:/usr/local/nginx/logs \
-v /docker/nginxv03.1/conf/nginx.conf:/usr/local/nginx/conf/nginx.conf \
-v /docker/nginxv03.1/conf.d/:/usr/local/nginx/conf.d/ \
-p 8000:80 \
-p 8001:81 \
-p 8002:82 \
u_nginx:v7 "/usr/local/nginx/sbin/nginx"

1.10.6 停止container,修改配置文件
修改主配置文件

开启访问日志,指定文件路径
log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                  '$status $body_bytes_sent "$http_referer" '
                  '"$http_user_agent" "$http_x_forwarded_for"';
access_log  logs/80.access.log  main;

创建子配置文件,新增2个站点

$ vim /docker/nginxv03.1/conf.d/more.conf
server {
	listen       81;
	server_name  localhost;

	#charset koi8-r;

	access_log  logs/81.access.log  main;
	add_header lwx "0601d";

	location / {
		default_type text/html;
		return 200 'This is docker 81!!!';
	}
}

server {
	listen       82;
	server_name  localhost;

	#charset koi8-r;

	access_log  logs/82.access.log  main;
	add_header lwx "0601d";

	location / {
		default_type text/html;
		return 200 'This is docker 82!!!';
	}
}

1.10.7 重新启动container

docker start 1b7e

检查

root@dc01:/docker/nginxv03.1# curl http://192.168.50.12:8001/
This is docker 81!!!
root@dc01:/docker/nginxv03.1# curl http://192.168.50.12:8002/
This is docker 82!!!


root@dc01:/docker/nginxv03.1# ll logs/
total 36
drwxr-xr-x 2 root root 4096 Oct 14 10:02 ./
drwxr-xr-x 6 root root 4096 Oct 14 10:00 ../
-rw-r--r-- 1 root root  253 Oct 14 10:04 80.access.log
-rw-r--r-- 1 root root  424 Oct 14 10:04 81.access.log
-rw-r--r-- 1 root root  424 Oct 14 10:04 82.access.log
-rw-r--r-- 1 root root  643 Oct 14 09:45 error.log
-rw-r--r-- 1 root root    2 Oct 14 10:02 nginx.pid

2 创建ubuntu下基本Nginx镜像

2.1 查找和拉取ubuntu镜像

$ docker search ubuntu
NAME                                                      DESCRIPTION                                     STARS               OFFICIAL            AUTOMATED
ubuntu                                                    Ubuntu is a Debian-based Linux operating sys…   10042               [OK]

$ docker pull ubuntu
Using default tag: latest
latest: Pulling from library/ubuntu
5667fdb72017: Pull complete
d83811f270d5: Pull complete
ee671aafb583: Pull complete
7fc152dfb3a6: Pull complete
Digest: sha256:b88f8848e9a1a4e4558ba7cfc4acc5879e1d0e7ac06401409062ad2627e6fb58
Status: Downloaded newer image for ubuntu:latest
docker.io/library/ubuntu:latest

2.2 启动container进入bash

$ docker run --name u_nginx_v01 -ti ubuntu /bin/bash

更新apt-get
$ apt-get update

安装必要的工具
$ apt-get install vim

2.3 安装nginx依赖

$ apt-get install build-essential
$ apt-get install libtool
$ apt-get install libpcre3 libpcre3-dev
$ apt-get install zlib1g-dev
$ apt-get install openssl

2.4 下载和安装nginx

$ wget http://nginx.org/download/nginx-1.17.4.tar.gz
$ tar -zxvf nginx-1.17.4.tar.gz
$ cd nginx-1.17.4
$ ./configure --prefix=/usr/local/nginx
$ make
$ make install

其他nginx和docker配置方式和上面Centos一样。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值