1、可以通过 docker pull 的方式从镜像源上载入nginx镜像
2、通过 Dockerfile 创建镜像
2.1、新建 Dockerfile 文件
[dkuser@c741 ~]$ mkdir centos_nginx
[dkuser@c741 ~]$ cd centos_nginx/
[dkuser@c741 centos_nginx]$ vi Dockerfile
FROM centos
MAINTAINER sxzhou sxzhou@xxx.com
LABEL Discription="基于centos的nginx镜像" version="1.0"
WORKDIR /usr/local/src
RUN yum install -y wget
RUN wget http://nginx.org/download/nginx-1.8.0.tar.gz
RUN tar -zxvf nginx-1.8.0.tar.gz
WORKDIR nginx-1.8.0
#安装nginx所依赖的包
RUN yum -y install gcc-c++
RUN yum -y install pcre pcre-devel
RUN yum -y install zlib zlib-devel
RUN yum -y install openssl openssl-devel libssl-dev
RUN ./configure
RUN make
RUN make install
EXPOSE 80
2.2、执行创建镜像命令
[dkuser@c741 centos_nginx]$ docker build -t centos_nginx .
.......
make[1]: Leaving directory `/usr/local/src/nginx-1.8.0'
Removing intermediate container 2471c1cc8e93
---> dcd18de4a1c9
Step 16/16 : EXPOSE 80
---> Running in e80691a08812
Removing intermediate container e80691a08812
---> 3a69ebee9a56
Successfully built 3a69ebee9a56
Successfully tagged centos_nginx:latest
查看镜像
[dkuser@c741 centos_nginx]$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos_nginx latest 3a69ebee9a56 52 seconds ago 745MB
3、运行容器
[dkuser@c741 centos_nginx]$ docker run -dit -p 8085:80 centos_nginx
参数说明:
-d 后台运行
-i 交互模式
-t 分配tty设备,支持终端登录
4、登陆容器
[dkuser@c741 centos_nginx]$ docker attach objective_banzai
查看文件
[root@8b534d79f7c5 nginx-1.8.0]# ll
total 632
-rw-r--r-- 1 1001 1001 249124 Apr 21 2015 CHANGES
-rw-r--r-- 1 1001 1001 379021 Apr 21 2015 CHANGES.ru
-rw-r--r-- 1 1001 1001 1397 Apr 21 2015 LICENSE
-rw-r--r-- 1 root root 366 Apr 25 08:52 Makefile
-rw-r--r-- 1 1001 1001 49 Apr 21 2015 README
drwxr-xr-x 6 1001 1001 312 Apr 25 08:48 auto
.....
拷贝启动脚本,启动nginx
[root@8b534d79f7c5 nginx-1.8.0]# mkdir sbin
[root@8b534d79f7c5 nginx-1.8.0]# cp objs/nginx sbin/
[root@8b534d79f7c5 nginx-1.8.0]# cd sbin
[root@8b534d79f7c5 sbin]# ./nginx
访问nginx
[root@8b534d79f7c5 sbin]# curl http://localhost:80
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
body {
width: 35em;
margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif;
}
.....
注意:
不要使用 exit 和 ctrl + D 退出容器,因为这样会停止容器的运行,可以使用 ctrl + P + Q 退出容器
5、宿主机访问nginx
[dkuser@c741 centos_nginx]$ curl http://localhost:8085
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
body {
width: 35em;
margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif;
}
</style>
</head>
.....
6、浏览器访问nginx
浏览器访问:
http://192.168.121.129:8085
参考:
https://www.runoob.com/docker/docker-tutorial.html