here is my Dockerfile:
FROM nginx
COPY /root/fe/foodsafe /app
COPY nginx.conf /etc/nginx/nginx.conf
I found in the nginx image doc:
If you add a custom CMD in the Dockerfile, be sure to include -g daemon off; in the CMD in order for
nginx to stay in the foreground, so that Docker can track the process properly (otherwise your
container will stop immediately after starting)!
but I didn't write any CMD in the Dockerfile, what's wrong?
Related
I have a node project and it has Dockerfile and docker-compose.yml as well.
Dockerfile
FROM node:lts-alpine as build-stage
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
RUN npm run build
FROM nginx:stable-alpine as production-stage
COPY --from=build-stage /app/dist /usr/share/nginx/html
EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]
docker-compose.yml
version: '3'
services:
my-service-name:
build:
context: .
dockerfile: Dockerfile
ports:
- "8080:80"
restart: unless-stopped
I uploaded the image to Docker Hub. When I tried to pull the image and run it, I needed to specify the port like this docker run -p 8080:80 my-username/my-image-name so I can open the project in localhost:8080 from NGINX expose 80.
What I want to do is run the image without specifying the port since I already specified the port in Dockerfile and docker-compose. I've been confused with how to achieve this. Does this mean my docker-compose is not uploaded to the Docker Hub and I should do so? Or is my current way is already correct?
When you use a docker-compose file, you have to run it with the docker-compose executable. What you are doing is bypassing the compose file altogether.
You are misinterpreting the meaning of EXPOSE in the Dockerfile. From the documentation:
The EXPOSE instruction does not actually publish the port. It functions as a type of documentation between the person who builds the image and the person who runs the container, about which ports are intended to be published. To actually publish the port when running the container, use the -p flag on docker run to publish and map one or more ports, or the -P flag to publish all exposed ports and map them to high-order ports.
So feel free to run containers without specifying exposed ports on the docker command line or in Docker-Compose or anywhere else The containers will run but it's like they are behind a firewall.
I'm trying to create a reverse proxy using nginx running in docker. I have set up the nginx.conf file and it's running fine locally. However, I don't know how to set up the nginx docker with this changed nginx.conf file. Is there any way to do this?
Update: I have been able to change the nginx.conf file inside Docker. However, going to localhost:80/go returns a 502 Bad Gateway. I have a go app running on port 8081 using go run main.go and a python app running on port 8080 using flask run. I'm on a Manjaro VM.
This is the server part of the nginx.conf file
server {
listen 80;
location / {
return 200 'hey there, welcome to our amazing app :)';
}
location /cbl {
proxy_pass http://127.0.0.1:8080;
}
location /go {
proxy_pass http://127.0.0.1:8081;
}
}
And this is the Dockerfile
FROM nginx
RUN mv /etc/nginx/nginx.conf /etc/nginx/nginxorig.conf
#RUN pwd
#RUN cp /home/shark/hwr-nginx/nginx.conf /etc/nginx/conf.d/default.conf
VOLUME /usr/share/nginx/html
VOLUME /etc/nginx
How do I fix this?
[shark#shark-virtualbox hwr-nginx]$ sudo docker build . -t nginx
Sending build context to Docker daemon 7.168kB
Step 1/3 : FROM nginx
---> 7084cd82dcbb
Step 2/3 : RUN mv /etc/nginx/nginx.conf /etc/nginx/nginx.conf_orig
---> Running in 80e011c5b125
mv: cannot stat '/etc/nginx/nginx.conf': No such file or directory
The command '/bin/sh -c mv /etc/nginx/nginx.conf /etc/nginx/nginx.conf_orig' returned a non-zero code: 1
[shark#shark-virtualbox hwr-nginx]$ cat Dockerfile
FROM nginx
RUN mv /etc/nginx/nginx.conf /etc/nginx/nginx.conf_orig
RUN cp hwr-nginx/nginx.conf /etc/nginx/nginx.conf
```` RUN mv and RUN cp are also not working for me :(.
You need to copy your nginx.conf to your container:
For example: COPY conf /etc/nginx
Docs here: https://docs.nginx.com/nginx/admin-guide/installing-nginx/installing-nginx-docker/#managing-content-and-configuration-files
In your Dockerfile, add an instruction to copy your project's reverse proxy configured ngxin.conf into the docker image you intend to build, in the location where nginx will look for its configurations. For example, assuming your base image is Debian, you could do the following in your Dockerfile:
# Move the default conf out of the way
RUN mv /etc/nginx/nginx.conf /etc/nginx/nginx.conf_orig
# Copy in your project's new nginx conf
RUN cp my_project/nginx.conf /etc/nginx/nginx.conf
I have problems with copying files into docker image. I'm a newbie in doing this and I have browsed several questions related, but none of them resolved my issue.
## base image
FROM node:13.6.0-stretch as build
WORKDIR /app
COPY package.json /app/package.json
COPY package-lock.json /app/package-lock.json
RUN npm ci
COPY . /app
RUN npm run build
FROM nginx:1.17.7
COPY --from=build /app/dist /usr/share/nginx/html #this works
COPY --from=build /app/nginx/default.conf /etc/nginx/conf.d/default.conf ## this works but.. not
RUN cat /etc/nginx/conf.d/default.conf; return 0 ## cat shows that the file is modified
EXPOSE 80 443
CMD ["nginx", "-g", "daemon off;"]
RUN cat /etc/nginx/conf.d/default.conf; return 0 ## once again verified
So in the log, it looks to be modified to desired file.
But when I check in deployed container:
sudo docker exec -it 9e2f628dc8a9 cat /etc/nginx/conf.d/default.conf
it has the default config provided by nginx.
I tried copying it with different name, still with no results.
Any advises highly appreciated.
I'm setting up a docker container that will serve my Angular 5 application on a nginx server, following this article.
The article proposes this Dockerfile:
# Stage 0, based on Node.js (for npm) to build and compile the Angular application.
FROM node:8.11.2 as node
WORKDIR /app
COPY package.json /app/
RUN npm install
COPY ./ /app/
ARG env=prod
# For Angular 6: ARG conf=production
RUN npm run build -- --prod --environment $env
# For Angular 6: RUN npm run build --configuration $conf
# Stage 1, based on Nginx, to have only the compiled app, ready for production with Nginx
FROM nginx:1.15
COPY --from=node /app/dist/ /usr/share/nginx/html
COPY ./nginx-custom.conf /etc/nginx/conf.d/default.conf
I was wondering: why are the following not included?
a port exposing instruction (e.g. EXPOSE 80), and
a nginx run command (e.g. RUN /usr/bin/nginx)
The article doesn't talk about starting the nginx server in any other way.
a port exposing instruction (e.g. EXPOSE 80)
The EXPOSE instruction is more documentation than anything else. The documentation for the nginx image tells you to publish the port when you run the container with -p 80:80, which doesn't require an EXPOSE instruction. EXPOSE 80 would allow you to use -P for the same purpose.
a nginx run command (e.g. RUN /usr/bin/nginx)
The nginx image does this for you :)
Since your are using nginx:1.15, check its Dockerfile:
...
EXPOSE 80
STOPSIGNAL SIGTERM
CMD ["nginx", "-g", "daemon off;"]
I want to redirect a user when they go to https to the http version of my website which is hosted in a Docker swarm.
I'm trying to do this using ngnix, however the setup that I'm using isn't working. I've created a new Core 2.0 Web App to try and get it working in the simplest context possible. In addition to the Web App I also have my Dockerfile:
FROM microsoft/aspnetcore-build:2.0 AS build-env
WORKDIR /app
# Copy csproj and restore as distinct layers
COPY *.csproj ./
RUN dotnet restore
# Copy everything else and build
COPY . ./
RUN dotnet publish -c Release -o out
# Build nginx image to redirect http to https
FROM nginx:alpine
EXPOSE 80
COPY nginx.conf /etc/nginx/nginx.conf
# Build runtime image
FROM microsoft/aspnetcore:2.0
WORKDIR /app
COPY --from=build-env /app/out .
ENTRYPOINT ["dotnet", "RedirectService.dll"]
and my nginx file:
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name _;
return 301 https://www.google.co.uk;
}
After building my image, I run it with docker run -p 8006:80 redirectservice. What I'm expecting to happen is that it will redirect me to Google when I navigate to http://localhost:8006, however no redirect happens.
Can anyone see anything that I'm doing wrong? Any help would be massively appreciated.
It's not redirecting you, because nginx process is not running.
Take a look at the nginx image Dockerfile (https://github.com/nginxinc/docker-nginx/blob/590f9ba27d6d11da346440682891bee6694245f5/mainline/alpine/Dockerfile) - last line is:
CMD ["nginx", "-g", "daemon off;"]
In your Dockerfile you replaced it with:
ENTRYPOINT ["dotnet", "RedirectService.dll"]
And thus the nginx is never started.
You need to create a sh script where you will run nginx and dotnet and wait until both of them ends (i.e. crashes).