I have a simple static website, which I can host it from docker by writing a dockerfile.txt with the following commands
FROM nginx
RUN mkdir /usr/share/nginx/html/blog
COPY . /usr/share/nginx/html/blog
This works pretty well for me.
Now I'm trying to dockerize a static that was build using docker, what should I exactly write in the docker file
FROM klakegg/hugo
COPY ?????????????????????
Does hugo have a dir where I can place the website files in it? or does Hugo works completely diff?
Thanks in advance!
Your files need to be placed in /src
The klakegg/hugo container only acts as the "compiler". In order to host the generated files you also need nginx.
This can the achieved with multistage-builds
FROM klakegg/hugo AS build
COPY . /src
FROM nginx
COPY --from=build /src/public /usr/share/nginx/html
Related
I currently use NGINX as reverse proxy for multiple web applications (virtual hosts) hosted on a Linux server. It also serves static files for the applications (eg big Javascripts, bitmaps, software downloads) directly.
Now I want to package each application into a docker image. But what do I do with the static files?
I can either serve these files from the applications (which is slower) or have another images per application with it's own NGINX just for the static files.
Any other ideas?
Good news - you do pretty much exactly what you're already doing. Basically you'll use the Nginx official image and just copy the files into the appropriate directory.
The Dockerfile would look something like this for create-react-app (as an example):
FROM node:16-alpine as builder
RUN mkdir -p /app
WORKDIR /app
COPY package.json ./
RUN npm install
COPY ./ ./
RUN npm run build
from nginx:alpine
COPY --from=builder /app/build /usr/share/nginx/html
Hosting containers on cloud infrastructure can come with its challenges though. The company I work for, cycle.io, simplifies the process and would be a great place for you to deploy your containerized server.
Am trying to create a docker image where you can build/publish but also run a .net core website but having a real issue with
image mcr.microsoft.com/dotnet/sdk:5.0
If you build inside a container with that it publishes no problem but when you run you get issues.
By contrast if you run with
mcr.microsoft.com/dotnet/aspnet:5.0
then you get no issues, that runs the app fine, but of course you cant use that package to publish or build as its runtime only.
I have done an experiement to run a .net core web app using identical source code and dockerfiles except one is using the sdk and one is using aspnet.
Both dockerfiles build an image using pre-built dlls from a publish folder that was generated before. They look like this..
docker file 1
FROM mcr.microsoft.com/dotnet/aspnet:5.0
WORKDIR /app
#copy dlls from publish into working directory
COPY /publish .
ENTRYPOINT ["dotnet", "/app/Whipover.dll"]
docker file 2
FROM mcr.microsoft.com/dotnet/sdk:5.0
WORKDIR /app
#copy dlls from publish into working directory
COPY /publish .
ENTRYPOINT ["dotnet", "/app/Whipover.dll"]
Docker file 1 image runs the website no problem,
Docker file 2 image gives the error..
" Unable to bind to http://localhost:5000 on the IPv6 loopback interface: 'Cannot assign requested address'."
Do we know why that is? I thought the sdk image is supposed to be able to do everything the aspnet image can do.
As for the error I could modify docker file 2 to include
#ENV ASPNETCORE_URLS=http://+:80
#EXPOSE 80 5000
.. which runs the website but then it cant retrieve the contents of wwwroot.. so that would be another issue. Also I dont see why I need to manually expose ports when dockerfile 1 had no problem at all?
Thanks
The solution is to have a multipart dockerfile where you build with the sdk and then run with a runtime different image.
I was under the impression the sdk could do both.
I was looking at an out of date course on pluralsight. This is the best reference to use is
https://learn.microsoft.com/en-us/aspnet/core/host-and-deploy/docker/building-net-docker-images?view=aspnetcore-5.0
I'm following this tutorial on using Docker Compose to deploy a Django application. It suggests that in my Dockerfile I should have (abbreviated for clarity):
WORKDIR /usr/src/app
COPY . .
Where the local directory is the Django project root. But also that in my docker-compose.yml (in the parent directory to the project root) I should have the line
volumes:
- ./app/:/usr/src/app/
Where ./app/ is the project root, relative to docker-compose.yml. From reading, the volumes line in docker-compose should make my host directory ./app/ available in the container at /usr/src/app/. Testing also seems to confirm this.
In that case, what's the point of the COPY . . line? In fact beyond the point, why does it not throw an error? It seems to be copying a directory over itself.
Well at beginning in the tutorial, the write just pretends run Dockerfile and docker-composer without entrypoint.sh then is not necessary use: COPY . .
But while you continues reading there is a point in Dockerfile that is required have the code of Django inside de Container Docker.
The line that I'm talking about ends in ENTRYPOINT ["/usr/src/app/entrypoint.sh"]
So if COPY . . is not present, the image building will throw error.
I hope you got me, what I'm say.
I have a Blazor webassembly project which I have dockerized and I'm trying to read environment variables set in the container on startup.
If I do the following I am able to run a shell in the container and echo the variable.
docker run -e DEBUG=false --name blazortest dockerizedclientsideblazor
However if I try to read the environment variable in the application using this command.
Environment.GetEnvironmentVariable("DEBUG")
It returns as null and it's the same for trying to read mounted files.
Can explain to me why I'm experiencing this?
My Dockerfile looks like this.
FROM mcr.microsoft.com/dotnet/core/sdk:3.1 AS build-env
WORKDIR /app
COPY . ./
RUN ls -la ./
RUN dotnet publish -c Release -o output
FROM nginx:alpine
WORKDIR /var/www/web
COPY --from=build-env /app/output/wwwroot .
COPY nginx.conf /etc/nginx/nginx.conf
EXPOSE 80 443
Coming back to this question with my own answer.
Trying to retrieve the value of environment variables on the server(in this case a container) won't work since the code is being being downloaded and run on the client.
So the environment variables set on the server will not be present on the client side.
The solution would be to use appsettings instead.
In fact you can specify and environment variable even if executed on client side.
Multiple options :
Using Nginx? Add the header ‘blazer-environment’ in nginx.conf
Using web.config? See msdn: https://learn.microsoft.com/en-us/aspnet/core/blazor/fundamentals/environments
Want to determine it in code (startup), see same link as above
It returns as null and it's the same for trying to read mounted files.
Yes. Your WebAssembly app does not run in that container, it runs in the Browser of the end users.
You could read the variable and those files from a Service running on nginx.
Hello i am trying to recreate inside a docker image my host folder that contains:
Publish (folder containing a .NET app )
dockerfile
conf.json
dockerfile
FROM microsoft/aspnetcore
WORKDIR /app
ADD . /app
ENTRYPOINT ["dotnet","/publish/Bench.dll"]
EXPOSE 8300
When i am trying to see what it created using docker exec -it <id> bash it just takes all the content of publish and throws it inside app without copying conf.json.
I have also tried with
COPY . /app,
COPY /publish /app+COPY conf.json /app to no avail.
Whatever i am trying it won't copy the folder as-is and it wont put the json file beside it.
What am i doing wrong?
So I tested this out. I had the publish folder dockerfile and conf.json in the same directory where I build the image from. That is the key. Here I am using nginx as my base image. The following is the command I used to build the nginx image
docker build -t test/nginx .
and the dockerfile is as below. So I create the app directory by using the RUN command. You will have to use the similar command in .net to create that directory if it doesn't exist. Also pay attention to the docker build logs. It will tell you things that are important. Also you could add an ls command in the dockerfile to list the files in the folder /app if you want to.
FROM nginx
RUN mkdir /app
WORKDIR /app
ADD . /app
After I create a container from the image I built, i can navigate to /app folder and view my
Dockerfile, config.json and publish folder in there. Hope this helps
Let me know if you have any questions