官术网_书友最值得收藏!

A container more practical

This is probably where we start perging from other Docker materials out there that practically assume that with just this basic knowledge, the rest of the work is a cakewalk when it is really nothing like that. It is not rocket science, but these simple examples really do not do enough to get us where we need to be, so we will use a practical example based a bit on our previous work with NGINX and create a container that uses this web server image to provide and serve up content that we will bake into the image.

This example and all the other ones in this book are also available on GitHub at https://github.com/sgnn7/deploying_with_docker. You can use either git or their web interface to follow along with the examples, but all examples of code that we will use will be directly included in the book too.

To begin creating our web server, we need to create a directory to put all of our files in:

$ mkdir ~/advanced_nginx
$ cd ~/advanced_nginx

The first file we need to create is our dummy text file that we will try to serve up in the image:

$ echo "Just a test file" > test.txt

The next file we will need is the required NGINX configuration. Put the following text into a file called nginx_main_site.conf:

    server {
listen 80;
server_name _;
root /srv/www/html;

# Deny access to any files prefixed with '.'
location ~/\. {
deny all;
}

# Serve up the root path at <host>/
location / {
index index.html;
autoindex on;
}
}

If you've never worked with NGINX, let's check out what this file does. In the first block, we are creating a server that listens on port 80 rooted in /srv/www/html on the image. The second block, while not strictly needed and would require changing for bigger websites, should be muscle memory for anyone working on NGINX since it prevents the downloading of hidden files like .htaccess, .htpasswd, and many others that should not be available publicly. The last block just makes sure that any path starting with / will be read from root and if the index file is not provided, it will use index.html. If no such file is available and we are in a directory, autoindex ensures that it can show you a human-readable listing of a directory.

While this NGINX configuration is functional, there are many things that it is not including (SSL configuration, logging, error files, file lookup matching, and so on), but that is mostly because this is a book is trying to focus on Docker itself and not NGINX. If you would like to learn more about how to fully and properly configure NGINX, you can visit https://nginx.org/en/docs/ for more information.

With the configuration written, we can now create our Dockerfile, which will take our test file, our configuration file, and the NGINX image and turn it all into a Docker image that runs a web server and serves up our test file:

FROM nginx:latest

# Make sure we are fully up to date
RUN apt-get update -q && \
apt-get dist-upgrade -y

# Remove the default configuration
RUN rm /etc/nginx/conf.d/default.conf

# Create our website's directory and make sure
# that the webserver process can read it
RUN mkdir -p /srv/www/html && \
chown nginx:nginx /srv/www/html

# Put our custom server configuration in
COPY nginx_main_site.conf /etc/nginx/conf.d/

# Copy our test file in the location that is
# being served up
COPY test.txt /srv/www/html/

This Dockerfile probably looks a lot different from the first one, so we will spend some time ping into what we are doing here. 

主站蜘蛛池模板: 梁平县| 台湾省| 芜湖市| 涿鹿县| 浦江县| 九寨沟县| 麦盖提县| 时尚| 蓬溪县| 福贡县| 合阳县| 东乌珠穆沁旗| 镇安县| 恭城| 星子县| 诸暨市| 遂平县| 监利县| 基隆市| 全州县| 邵武市| 惠安县| 吐鲁番市| 会宁县| 治县。| 洞口县| 宜兰县| 金坛市| 兴仁县| 吴江市| 龙川县| 区。| 黄浦区| 清苑县| 全椒县| 萨嘎县| 峨山| 五河县| 姜堰市| 长葛市| 松阳县|