I got a very basic Dockerfile:
FROM php:8.1-fpm-alpine
WORKDIR /app
COPY . .
EXPOSE 80
RUN nginx
I even tried
ENTRYPOINT [ "nginx" ]
but then the container is continuously restarting instead of starting.
But when I start the container (having RUN nginx in place) and I attach a console to the container and run nginx, the process starts.
You could try the CMD command.
CMD ["nginx", "-g", "daemon off;"]
Your Dockerfile would then look like this:
FROM php:8.1-fpm-alpine
WORKDIR /app
COPY . .
EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]
Source:
https://docs.nginx.com/nginx/admin-guide/installing-nginx/installing-nginx-docker/
https://www.baeldung.com/linux/nginx-docker-container
Related
I am kind of stuck trying to solve a problem. I have a Dockerfile as per the guide, everything works fine.
Now I want to add an automatic SSL from let's encrypt so it always runs with https.
How would you go with solving this question? Here is my Dockerfile.
FROM node:14.17.0-alpine as build-stage
WORKDIR /app
COPY package*.json ./
RUN npm cache verify
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
COPY nginx.conf /etc/nginx/conf.d/default.conf
EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]
After the image builds, I launch it with:
docker run -it -p 80:80 -d --rm --name app-container vuejs
Thanks for the help :)
I dockerise a React application for production. I added Nginx has the server inside Docker. my Dockerfile below:
FROM node:14-alpine AS builder
WORKDIR /app
COPY package.json ./
COPY package-lock.json ./
RUN npm ci
COPY . .
RUN npm run build
FROM nginx:alpine AS server
WORKDIR /usr/share/nginx/html
COPY --from=builder ./app/build .
CMD ["nginx", "-g", "daemon off;"]
How do I create TLS certificate for this application using Let's Encrypt? Thank you.
I have Dockerfile to deploy my frontend, which is in react.
This is what I have in my dockerfile.
# Stage 1
FROM node:8 as react-build
WORKDIR /app
COPY . ./
RUN yarn
RUN yarn build
# Stage 2 - the production environment
FROM nginx:alpine
COPY nginx.conf /etc/nginx/conf.d/default.conf
COPY --from=react-build /app/build /usr/share/nginx/html
EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]
When I run $ docker build . -t frontend
I am getting an error on step 8, which fails to copy the nginx.conf
Step 8/10 : COPY --from=react-build /app/build /usr/share/nginx/html
COPY failed: stat /var/lib/docker/overlay2/fef4deafd532bb0aa7eaea50ae25412e93f7972eab3a0579fe494de444d86a0b/merged/app/build: no such file or directory
But the nginx conf file exists, and I don't understand what's going on there, can somebody enlighten me what's going on?
http://prntscr.com/l2to47 This is my project structure, which clearly says there are nginx.conf but Docker can't find it.
It doesn't fail to copy the nginx.conf file, it fails to find the data generated during the react-build stage.
I had the same problem once, that I solved changing the WORKDIR directory, since the original one was exposed as VOLUME (and it seems like it doesn't work using multi-stage builds). Try with a different one, e.g.:
# Stage 1
FROM node:8 as react-build
WORKDIR /builddir
COPY . ./
RUN yarn
RUN yarn build
# Stage 2 - the production environment
FROM nginx:alpine
COPY nginx.conf /etc/nginx/conf.d/default.conf
COPY --from=react-build /builddir/build /usr/share/nginx/html
EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]
And see if it solves the problem.
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 have several base docker images which are not owned by me (so I cannot modify them). However, I'm creating new images from them with additional things installed.
What I can't figure out is how to tell dockerfile to copy the CMD (or ENTRYPOINT) of the base image. Something like this:
FROM other:latest
RUN my-extra-install
CMD <use-the-CMD-from-base-image>
I don't think there's any direct syntax for the CMD command to do what I want. I'm wondering if there's a workaround.
If you left it blank in your new Dockerfile, it will inherit the one from the base image.
For example:
base
FROM ubuntu
CMD ["echo", "AAA"]
layer1
FROM base
If you build above images and run layer1 you will get the following:
$ sudo docker run -it layer1
AAA
#Vor is right. But in case
# Dockerfile
FROM nginx:stable-alpine
ENTRYPOINT ["/docker-entrypoint.sh"]
COPY ./docker-entrypoint.sh /
and
# docker-entrypoint.sh
#!/usr/bin/env sh
set -e
exec "$#"
the default CMD from nginx:stable-alpine won't be executed in exec "$#".
You must to write default nginx-alpine's CMD by yourself(!) in Dockerfile
# Dockerfile
FROM nginx:stable-alpine
ENTRYPOINT ["/docker-entrypoint.sh"]
CMD ["nginx", "-g", "daemon off;"]
COPY ./docker-entrypoint.sh /
OR change your docker-entrypoint.sh
# docker-entrypoint.sh
#!/usr/bin/env sh
set -e
exec nginx -g "daemon off;"
Hope it helps