- Deployment with Docker
- Srdjan Grubor
- 615字
- 2021-07-02 23:22:12
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.
- 工業(yè)機器人產(chǎn)品應(yīng)用實戰(zhàn)
- ETL with Azure Cookbook
- 返璞歸真:UNIX技術(shù)內(nèi)幕
- 自動檢測與傳感技術(shù)
- Expert AWS Development
- 21天學通ASP.NET
- STM32嵌入式微控制器快速上手
- 變頻器、軟啟動器及PLC實用技術(shù)260問
- 愛犯錯的智能體
- Working with Linux:Quick Hacks for the Command Line
- Ansible 2 Cloud Automation Cookbook
- Drupal高手建站技術(shù)手冊
- Effective Business Intelligence with QuickSight
- Deep Learning Essentials
- 網(wǎng)絡(luò)設(shè)備規(guī)劃、配置與管理大全(Cisco版)