Change Cache-Control nginx container on docker - docker

I installed docker on my linux and I installed nginx container like this:
docker pull nginx
docker run -it -d -p 8081:80 --name web -v /mnt/Project/Flutter/Projects/app_web2/build/web:/usr/share/nginx/html nginx
Now I want to change Cache-Control on my nginx container. Because I write a pwa with flutter, Every time I changed my page, when I launch page I still see old version of page, Now I want to change nginx caching.
How can I change it's default?

I suspect that you can achieve this directly. There are two approach.
Run nginx container as you running as of now. Then perform following operation.
docker exec -it <<containername>>
go to /etc/nginx/conf.d/default.conf ( Edit this file)
Note : This approach is problem as you have to do this every time.
Another approach is create custom image based on nginx.
FROM nginx:latest
COPY ./default.conf /etc/nginx/conf.d/default.conf
This ./default.conf will reside in your directory and from where you execute following command.
docker build . mynginx:latest
In this default.conf at your directory you can add custom header.
You can find sample over here : https://github.com/jp1482/mynginxwithcustomerheader

Related

Hot reload not working with Bigcommerce/Stencil on docker

I would like to use Docker because bigcommerce/stencil-cli only supports node version 10x or 12x. I can't configure docker correctly for the reload (browsersync) to work.
When I edit any SASS or HTML theme files my changes are not showing at localhost:3000.
I don't really understand why, the configuration seems ok to me.
Here is the Dockerfile config
FROM node:10
WORKDIR /theme
RUN npm -g config set user root
RUN npm install -g #bigcommerce/stencil-cli
EXPOSE 3000 3001 3002
I am using the default Cornerstone Theme
To build my docker image.
docker build -t docker-stencil .
To run the docker container
docker run -it -v /$(pwd):/theme -p3000:3000 -p3001:3001 -p3002:3002 docker-stencil stencil start
Have you initialized stencil on the container with ‘stencil init’? From the commands you posted, it looks like this step may have been skipped. The CLI could be looking at your machine and not the docker vm instance if it was initialized on their machine first, if that’s the case.
Let me know!
Also, this is a great resource as well to keep around: Dockerizing BC Stencil!

403 Forbidden error when using nginx inside docker

I am trying to build a nginx webserver to share files among team members.
In 'ubuntu 16.04', I am running following command:
root#automation00-new:/home/test# docker run -d -p 8081:80 -v /var/www/apj/:/usr/share/nginx/html --name test-nginx nginx:latest
As shown below docker is able to mount the files successfully.
root#automation00-new:/home/test# docker exec ec795af0f1f2 ls /usr/share/nginx/html
Builds
Logs_for_perf_Testing
json.txt
ravi
root#automation00-new:/home/test#
But when I try to access webserver using browser "http://1.1.1.1/8081" I am seeing '403 forbidden' error.
But if I try 'http://1.1.1.1/8081/json.txt', I am able to view the 'json.txt' contents on browser.
I want to browse all the directories and files inside.
Any idea on how to fix this issue please?
Please give us more context in terms of your nginx configuration.
Are you using the default nginx.conf or do you have done modifications?
The solution should be to add all relevant files to the nginx index
Details also here, you will need to modify your nginx.conf:
autoindex needs to be turned on for the location /

Can't access webserver of airflow after run the container

I pulled the latest version of airflow image from docker hub.
apache/airflow.
And I tried to run a container base on this image.
docker run -d -p 127.0.0.1:5000:5000 apache/airflow webserver
The container is running and the status of port is fine. But I still can't access the airflow webserver from my browser.
This site can’t be reached.
127.0.0.1 refused to connect.
After few minutes, the container will stop automatically.
Is there anyone could advise?
I don't have experience with airflow, but this is how you fix this image to run:
First of all you have to overwrite the entrypoint because the existing one doesn't help a lot. From what I understand this image needs 2 steps in order to run: initdb and webserver. For this reason the existing entrypoint is not useful.
Run:
docker run -p 5000:8080 --entrypoint /bin/bash -ti apache/airflow
This will open a shell inside a running container. Also note that I mapped port 8080 inside the container.
Then inside the container run:
airflow db init
airflow webserver -p 8080
Note that in older versions of airflow, the command to initialize the database is airflow initdb, instead of airflow db init.
Open a browser and navigate to http://localhost:5000
When you close the container your work is gone thou ;)
Another thing you can do is put the 2 airflow commands in a bash script and map that script inside the container and use it as entrypoint. Something like this:
docker run -p 5000:8080 -v $(pwd)/startup.sh:/opt/airflow/startup.sh --entrypoint /opt/airflow/startup.sh -d --name airflow apache/airflow
You should make startup.sh executable before running this.
Let me know if you run into issues.

How to start nginx in Docker on Windows

I am using Windows 10 and I have installed Docker and pulled nginx:
docker pull nginx
I started nginx with this command:
docker run -dit --rm --name nginx -p 9001:80 nginx
And simple page is available on localhost:9001.
I would like to pass nginx.conf file to nginx. Also, I would like to give it a folder root, so that on localhost:9001 I see static page D:/nginx/website_files/index.html. In folder website_files there are also other static pages.
How to pass nginx.conf and folder path to nginx in Docker on Windows?
I started using Kitematic and pulled hello-world-nginx. With it I was able to browse files by clicking on Volumes -> /website_files. On path that opens, other static files can be added. After that nginx can be restarted and it increments port by 1. Port number can be seen with docker ps.
To change nginx config file, after starting nginx I run this command docker cp D:/nginx/multi.conf b3375f37a95c:/etc/nginx/nginx.conf where b3375f37a95c is container id obtained from docker ps command. After that nginx should be restarted from Kitematic.
If you only want to edit nginx.conf instead of completely changing it, you can first get current conf file with docker cp b3375f37a95c:/etc/nginx/nginx.conf D:/nginx/multi.conf, edit multi.conf and than copy it back as before.
You can use host volume mapping
-v <host-directory>:<container-path>
for example:
docker run -dit --rm -v d:/data:/data -p 9001:80 nginx /bin/sh
Try with this in PS :
PS C:\> docker run --name myNGinX -p 80:80 -p 443:443 -v C:\Docker\Volumes\myNGinX\www\:/etc/nginx/www/:ro -v C:\Docker\Volumes\myNGinX\nginx.conf:/etc/nginx/conf.d/:ro -d nginx
Late to the answer-party, and shameless self-promotion, but I created a repo using Docker-compose having an Nginx proxy server and 2 other websites all in Containers.
Check it out here

Deploying new versions of an image instantly

I would like to have 3 versions of my container running at any one time (on the same machine). Something like this:
version v7 (stage)
version v6 (live)
version v5 (old)
then I would like to map this to 3 urls:
v7.example.com
v6.example.com
v5.example.com
And also, a 4th url, which refers to the current (or default) version:
www.example.com (which maps to http://v6.mydomain.com)
Presumably, I could take some configuration step that would change the "default" version from v6 to v7. That step should hopefully be instant and atomic.
The idea is that deploying the next version of an app is a distinct step from activating that version (by activate, I mean making that version the default).
Therefore a rollout (or a rollback) would simply be a matter of changing the default version to the next (or previous) version.
Google App Engine supports this kind of pattern and I really like it.
Has anyone set something like this up using Docker? I would appreciate any advice on how to do it. Thanks.
I would do this with a reverse proxy in front of the containers running your webapp.
Example using the jwilder/nginx-proxy image
Let's say your docker host IP address is 11.22.33.44.
Let's say your docker images are:
mywebapp:5 for v5
mywebapp:6 for v6
mywebapp:7 for v7
First, make sure your DNS is set up so that v5.example.com, v6.example.com, v7.example.com and www.example.com all resolve to 11.22.33.44.
Start a jwilder/nginx-proxy on your docker host:
docker run -d --name reverseproxy -p 80:80 -v /var/run/docker.sock:/tmp/docker.sock:ro -e DEFAULT_HOST=www.example.com jwilder/nginx-proxy
Set v6 as the default one
Start the webapps containers:
docker run -d -name webapp5 -e VIRTUAL_HOST="v5.example.com" mywebapp:5
docker run -d -name webapp6 -e VIRTUAL_HOST="v6.example.com,www.example.com" mywebapp:6
docker run -d -name webapp7 -e VIRTUAL_HOST="v7.example.com" mywebapp:7
The jwilder/nginx-proxy will use the value of the VIRTUAL_HOST environment variable to update its configuration and route the requests to the correct container.
How to make v7 the new default one
First, remove container webapp7 and create a new one with www.example.com added to the VIRTUAL_HOST variable:
docker rm webapp7
docker run -d -name webapp7 -e VIRTUAL_HOST="v7.example.com,www.example.com" mywebapp:7
In this state, the reverse proxy will load balance queries for www.example.com to both webapp6 and webapp7 containers.
Finally, remove container webapp6 and eventually recreate it, but without www.example.com in the VIRTUAL_HOST value:
docker rm webapp6
docker run -d -name webapp6 -e VIRTUAL_HOST="v6.example.com" mywebapp:7
I thought I would share what I ended up doing. I took Thomasleveil's advice to use nginx. But rather than starting and stopping a whole docker container and nginx just to switch versions, I do this:
Change the port number in the nginx config file (see file below)
Call service nginx reload (which is instant).
server{
location / {
proxy_pass http://192.168.1.50:81/;
}
}

Resources