Explanation
This article describes how to build a docker ubuntu mirror contains commonly used commands, start nginx mirror runtime (nginx uses its own compiled version) to provide web services.
Create a build directory
Create a mirror image of the directory compiled build, a local copy of nginx folder to the new directory.
mkdir build
cd build
cp -r /usr/local/nginx .
Modify the nginx/conf/nginx.conf
file to configure what you want. In this case nginx listening port 8000, provide web services.
Create a file start.sh
This file is used to start nginx. It reads as follows:
#!/bin/bash
# 启动 nginx
nginx
# 循环,避免执行完命令后 docker 容器自动退出
while true; do sleep 1000; done
Use chmod +x start.sh
the command to the file with executable permissions.
Create a file Dockerfile
It reads as follows:
# 基础镜像使用ubuntu16.04
FROM ubuntu:16.04
# 设置apt源
RUN echo "deb http://mirrors.163.com/ubuntu precise main universe" > /etc/apt/sources.list
# 安装 vim ping ifconfig ip tcpdump nc curl iptables python 常用命令
RUN apt-get -y update && apt-get -qq -y install vim iputils-ping net-tools iproute tcpdump netcat curl iptables
# 指定工作目录
WORKDIR /root
# 复制 nginx 目录
COPY nginx /usr/local/nginx
# 复制进程启动脚本
COPY start.sh .
# 设置 PATH 环境变量包含 nginx 可执行文件
ENV PATH "$PATH:/usr/local/nginx/sbin"
# 对外使用端口8000
EXPOSE 8000
# 执行启动脚本
CMD ["/root/start.sh"]
Compile Mirror
Execute docker build --tag=myubuntu .
command to compile became known for myubuntu the mirror.
Run-time image
Execute docker run -d --rm --name nginx -p 127.0.0.1:8000:8000 --privileged myubuntu
command run-time image. This command will map the internal port 8000 to local port 8000 and listens address 127.0.0.1. Add --privileged
option for normal use iptables
commands.
Verify web services
Container nginx whether to start with the curl command check is successful:
root@ubuntu:~# curl 127.0.0.1:8000 -I
HTTP/1.1 200 OK
Server: nginx/1.12.2
Date: Sun, 16 Jun 2019 12:25:37 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Sun, 16 Jun 2019 02:41:51 GMT
Connection: keep-alive
ETag: "5d05ac6f-264"
Accept-Ranges: bytes
The above description of the service is OK.
Modify the configuration nginx
To change the nginx configuration, the following:
- Into the container execution shell.
root@ubuntu:~# docker exec -it nginx /bin/bash
root@b67da4091091:~#
- Use vi modify
/usr/local/nginx/conf/nginx.conf
configuration files. - Execute
nginx -s reload
command causes nginx to work with the new configuration. - Execution
exit
exits shell.