Skip to content

GoogleCloudPlatform/nginx-docker

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

nginx-docker

Dockerfile source for nginx docker image.

Upstream

This source repo was originally copied from: https://github.com/nginxinc/docker-nginx

Disclaimer

This is not an official Google product.

About

This image contains an installation Nginx 1.x.

For more information, see the Official Image Marketplace Page.

Pull command (first install gcloud):

gcloud auth configure-docker && docker -- pull marketplace.gcr.io/google/nginx1

Dockerfile for this image can be found here.

Table of Contents

Using Kubernetes

Consult Marketplace container documentation for additional information about setting up your Kubernetes environment.

Running Nginx

Start a Nginx web server

Copy the following content to pod.yaml file, and run kubectl create -f pod.yaml.

apiVersion: v1
kind: Pod
metadata:
  name: some-nginx
  labels:
    name: some-nginx
spec:
  containers:
    - image: marketplace.gcr.io/google/nginx1
      name: nginx

Run the following to expose the ports. Depending on your cluster setup, this might expose your service to the Internet with an external IP address. For more information, consult Kubernetes documentation.

kubectl expose pod some-nginx --name some-nginx-80 \
  --type LoadBalancer --port 80 --protocol TCP
kubectl expose pod some-nginx --name some-nginx-443 \
  --type LoadBalancer --port 443 --protocol TCP

For information about how to retain your data across restarts, see Use a persistent data volume.

For information about how to configure your web server, see Web server configuration.

Use a persistent data volume

To preserve your web server data when the container restarts, put the web content directory on a persistent volume.

By default, /usr/share/nginx/html directory on the container houses all the web content files.

Copy the following content to pod.yaml file, and run kubectl create -f pod.yaml.

apiVersion: v1
kind: Pod
metadata:
  name: some-nginx
  labels:
    name: some-nginx
spec:
  containers:
    - image: marketplace.gcr.io/google/nginx1
      name: nginx
      volumeMounts:
        - name: webcontent
          mountPath: /usr/share/nginx/html
  volumes:
    - name: webcontent
      persistentVolumeClaim:
        claimName: webcontent
---
# Request a persistent volume from the cluster using a Persistent Volume Claim.
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
  name: webcontent
  annotations:
    volume.alpha.kubernetes.io/storage-class: default
spec:
  accessModes: [ReadWriteOnce]
  resources:
    requests:
      storage: 5Gi

Run the following to expose the ports. Depending on your cluster setup, this might expose your service to the Internet with an external IP address. For more information, consult Kubernetes documentation.

kubectl expose pod some-nginx --name some-nginx-80 \
  --type LoadBalancer --port 80 --protocol TCP
kubectl expose pod some-nginx --name some-nginx-443 \
  --type LoadBalancer --port 443 --protocol TCP

The web server configuration should also be on a persistent volume. For more information, see Web server configuration.

Web server configuration

Viewing existing configuration

Nginx configuration file is at /etc/nginx/nginx.conf.

kubectl exec some-nginx -- cat /etc/nginx/nginx.conf

Using configuration volume

The default nginx.conf includes all configuration files under /etc/nginx/conf.d directory. If you have a /path/to/your/site.conf file locally, you can start the server as followed to mount it under conf.d directory.

Create the following configmap:

kubectl create configmap site-conf \
  --from-file=/path/to/your/site.conf

Copy the following content to pod.yaml file, and run kubectl create -f pod.yaml.

apiVersion: v1
kind: Pod
metadata:
  name: some-nginx
  labels:
    name: some-nginx
spec:
  containers:
    - image: marketplace.gcr.io/google/nginx1
      name: nginx
      volumeMounts:
        - name: site-conf
          mountPath: /etc/nginx/conf.d
  volumes:
    - name: site-conf
      configMap:
        name: site-conf

Run the following to expose the ports. Depending on your cluster setup, this might expose your service to the Internet with an external IP address. For more information, consult Kubernetes documentation.

kubectl expose pod some-nginx --name some-nginx-80 \
  --type LoadBalancer --port 80 --protocol TCP
kubectl expose pod some-nginx --name some-nginx-443 \
  --type LoadBalancer --port 443 --protocol TCP

Moving the web content to Nginx

We can move the web content to the container with the commands below, assuming /usr/share/nginx/html is where nginx has been configured to read from.

Create the directory if it does not exist yet.

kubectl exec some-nginx -- mkdir -p /usr/share/nginx/html

Copy the index.html file.

kubectl cp /path/to/your/index.html some-nginx:/usr/share/nginx/html/index.html

Follow instructions in Testing the web server, you should get back the content of your index.html.

Testing the web server

Accessing the web server from within the container

Attach to the webserver.

kubectl exec -it some-nginx -- bash

Install curl.

apt-get update && apt-get install -y curl

We can now use curl to see if the webserver returns content.

curl http://localhost

Using Docker

Consult Marketplace container documentation for additional information about setting up your Docker environment.

Running Nginx

Start a Nginx web server

Use the following content for the docker-compose.yml file, then run docker-compose up.

version: '2'
services:
  nginx:
    container_name: some-nginx
    image: marketplace.gcr.io/google/nginx1
    ports:
      - '80:80'
      - '443:443'

Or you can use docker run directly:

docker run \
  --name some-nginx \
  -p 80:80 \
  -p 443:443 \
  -d \
  marketplace.gcr.io/google/nginx1

For information about how to retain your data across restarts, see Use a persistent data volume.

For information about how to configure your web server, see Web server configuration.

Use a persistent data volume

To preserve your web server data when the container restarts, put the web content directory on a persistent volume.

By default, /usr/share/nginx/html directory on the container houses all the web content files.

Also assume that /my/persistent/dir/www is the persistent directory on the host.

Use the following content for the docker-compose.yml file, then run docker-compose up.

version: '2'
services:
  nginx:
    container_name: some-nginx
    image: marketplace.gcr.io/google/nginx1
    ports:
      - '80:80'
      - '443:443'
    volumes:
      - /my/persistent/dir/www:/usr/share/nginx/html

Or you can use docker run directly:

docker run \
  --name some-nginx \
  -p 80:80 \
  -p 443:443 \
  -v /my/persistent/dir/www:/usr/share/nginx/html \
  -d \
  marketplace.gcr.io/google/nginx1

The web server configuration should also be on a persistent volume. For more information, see Web server configuration.

Web server configuration

Viewing existing configuration

Nginx configuration file is at /etc/nginx/nginx.conf.

docker exec some-nginx cat /etc/nginx/nginx.conf

Using configuration volume

The default nginx.conf includes all configuration files under /etc/nginx/conf.d directory. If you have a /path/to/your/site.conf file locally, you can start the server as followed to mount it under conf.d directory.

Use the following content for the docker-compose.yml file, then run docker-compose up.

version: '2'
services:
  nginx:
    container_name: some-nginx
    image: marketplace.gcr.io/google/nginx1
    ports:
      - '80:80'
      - '443:443'
    volumes:
      - /path/to/your/site.conf:/etc/nginx/conf.d/site.conf

Or you can use docker run directly:

docker run \
  --name some-nginx \
  -p 80:80 \
  -p 443:443 \
  -v /path/to/your/site.conf:/etc/nginx/conf.d/site.conf \
  -d \
  marketplace.gcr.io/google/nginx1

Moving the web content to Nginx

We can move the web content to the container with the commands below, assuming /usr/share/nginx/html is where nginx has been configured to read from.

Create the directory if it does not exist yet.

docker exec some-nginx mkdir -p /usr/share/nginx/html

Copy the index.html file.

docker cp /path/to/your/index.html some-nginx:/usr/share/nginx/html/index.html

Follow instructions in Testing the web server, you should get back the content of your index.html.

Testing the web server

Accessing the web server from within the container

Attach to the webserver.

docker exec -it some-nginx bash

Install curl.

apt-get update && apt-get install -y curl

We can now use curl to see if the webserver returns content.

curl http://localhost

References

Ports

These are the ports exposed by the container image.

Port Description
TCP 80 Nginx http default port
TCP 443 Nginx https secure connection over SSL
TCP 9113 Prometheus metrics exporter

Volumes

These are the filesystem paths used by the container image.

Path Description
/etc/nginx Contains nginx configuration files, including nginx.conf.

The default nginx.conf include all .conf files under the subdirectory conf.d.