Docker: Building a Server with Dockerfile
Jump to navigation
Jump to search
Setting up a Nginx Webserver with Dockerfile
Let's build a static web server with a Dockerfile. The Dockerfile provides a set of instructions for Docker to run on a container. This lets us automate installing items as if it were a bash script.
Create a new directory and cd into it. Because we're installing Nginx, let's create a default configuration file that we'll use for it.
Create a file called default (the most basic nginx configuration file)
[root@blade02 ~]# mkdir nginx-dockerfile
[root@blade02 ~]# cd nginx-dockerfile/
[root@blade02 nginx-dockerfile]# vi default
[root@blade02 nginx-dockerfile]# cat default
server {
root /var/www;
index index.html index.htm;
# Make site accessible from http://localhost/
server_name localhost;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to index.html
try_files $uri $uri/ /index.html;
}
}Now lets create the Docker file;
[root@blade02 nginx-dockerfile]# cat Dockerfile
FROM ubuntu
RUN echo "deb http://archive.ubuntu.com/ubuntu precise main universe" > /etc/apt/sources.list
RUN apt-get update
RUN apt-get -y install nginx
RUN echo "daemon off;" >> /etc/nginx/nginx.conf
RUN mkdir /etc/nginx/ssl
ADD default /etc/nginx/sites-available/default
EXPOSE 80
CMD ["nginx"]What's this doing?
- FROM will tell Docker what image (and tag in this case) to base this off of
- RUN will run the given command (as user "root") using sh -c "your-given-command"
- ADD will copy a file from the host machine into the container. This is handy for configuration files or scripts to run, such as a process watcher like supervisord, systemd, upstart, forever (etc)
- EXPOSE will expose a port to the host machine. You can expose multiple ports like so: EXPOSE 80 443 8888
- CMD will run a command (not using sh -c). This is usually your long-running process. In this case, we're simply starting Nginx.
In production, we'd want something watching the nginx process in case it fails
We can now build a new docker image from this Dockerfile
docker build -t nginx-example .