I have a docker-compose file that works just fine, and a test-containers.ps1 script that run docker-compose. Everything works great locally.
version: '3.4'
services:
horizon.api.v1:
image: ${DOCKER_REGISTRY-}horizonapiv1
environment:
ASPNETCORE_ENVIRONMENT: Development
build:
context: .
dockerfile: api/horizon.api.v1/Dockerfile
Here's the dockerfile to which the docker-compose and heroku.yml files refer:
# horizon.api
FROM microsoft/dotnet:2.2-aspnetcore-runtime AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443
FROM microsoft/dotnet:2.2-sdk AS build
WORKDIR /src
COPY api/horizon.api.v1/horizon.api.v1.csproj api/horizon.api.v1/
RUN dotnet restore api/horizon.api.v1/horizon.api.v1.csproj
COPY . .
WORKDIR /src/api/horizon.api.v1
RUN dotnet build horizon.api.v1.csproj -c Release -o /app
FROM build AS publish
RUN dotnet publish horizon.api.v1.csproj -c Release -o /app
FROM base AS final
WORKDIR /app
COPY --from=publish /app .
ENTRYPOINT ["dotnet", "horizon.api.v1.dll"]
I wanted to get this up and running in Heroku so my students could see it running, and so students that don't have access to docker could play with the application. I made a heroku.yml file that looks like it should work...but this is the first time I've ever used heroku and it's probable I'm doing something stupid.
build:
config:
ASPNETCORE_ENVIRONMENT: Development
docker:
api: ./api/horizon.api.v1/Dockerfile
When I try to push my repo to heroku I get the following error:
remote: Step 7/17 : COPY ["routing/horizon.routing/horizon.routing.csproj", "routing/horizon.routing/"]
remote: COPY failed: stat /var/lib/docker/tmp/docker-builder722311220/routing/horizon.routing/horizon.routing.csproj: no such file or directory
I've seen a few posts online about a .dockerignore file causing problems, so I renamed my .dockerignore file to take it out of the picture until I figure this out.
The paths should all be correct, why does docker-compose not have any problem finding the necessary paths on my local machine but heroku says they don't exist? What am I doing wrong?
I believe it's probably related to the build context. I'm guessing Heroku doesn't use the context from the root of the project, and uses the directory where the Dockerfile is located.
Related
Goal
Dockerize NextJS application
Problem
Docker compose up yields in the following error: Couldn't find a pages directory. Please create one under the project root.
Application
Files & folders
docker-compose.yml
web
.next
pages
public
.dockerignore
dockerfile
[more nextjs files & folders here]
docker-compose
version: '3'
services:
web:
build:
context: web
dockerfile: dockerfile
ports:
- "3000:3000"
container_name: rughood_web
dockerfile
FROM node:16
WORKDIR /web
COPY package*.json .
RUN npm install
COPY . .
EXPOSE 3000
CMD ["npm", "run", "dev"]
.dockerignore
Dockerfile
.dockerignore
node_modules
npm-debug.log
README.md
.git
Note!
The NextJS application itself is working fine when I run npm run dev within the web directory (which invokes the script "dev": "next dev" in package.json). I only have the error when trying to dockerize it. Moreover, in the docker-compose I also initiate a Redis cache, which is working fine too. Therefore I conclude the error must be how I try to combine Docker and NextJS. Thank you very much in advance :)
Update 1
How I got there
Using the tips from #HansKilian and Exploring Docker container's file system I did the following:
Cd to the web directory
Built an image from the dockerfile docker build .
Explored the image with the following command docker run --rm -it --entrypoint=/bin/bash name-of-image
Once inside, execute ls or ls -lsa
This gave me the following results:
What's in the derived image
dockerfile
next-env.d.ts
next.config.js
node_modules
package-lock.json
package.json
pages
public
tsconfig.json
[Among other files/folders]
So the pages folder actually seems to be in the root of the container, yet still I get the error (pages is a directory in the container in which I can cd and -ls)
P.s. don't forget to delete your image if you're not going to using it anymore
Update 2
Building the image and running it from within the web directory actually works, so it might actually have something to do with the docker-compose?
Here is my working Dockerfile with nextjs:
FROM node:16.14.0
RUN npm install -g npm#8.5.5
RUN mkdir -p /app
WORKDIR /app
COPY package*.json /app
RUN npm i
COPY . /app
EXPOSE 3000
RUN npm run build
CMD ["npm", "run", "dev"]
And docker-compose.yml :
version: "3.7"
services:
project-name:
image: project-name
build:
context: .
dockerfile: Dockerfile
container_name: project-name
restart: always
volumes:
- ./:/app
- /app/node_modules
- /app/.next
ports:
- "3000:3000"
While I was trying every single line of code ever uploaded to the internet, I came back to my initial set-up (from the question) and suddenly it now does work. Source control confirming I didn't change a thing.
To be sure, I deleted all containers, images and volumes from Docker and ran docker compose up. Yet still it worked. Tried many things to recreated the error, but I couldn't. Thank you all for helping and hopefully this may be come to use for someone else!
I have NestJS monorepo project with structure as below:
...
apps
app1
app2
app3
...
If I got an idea correctly, I have possibility to run all the applications in same time, i.e. I run command and have access to apps by paths like http://my.domain/app1/, http://my.domain/app2/, http://my.domain/app3/ or in some similar way. And I need to put all apps in a docker container(s) and run them from there.
I haven't found something about this proceess. Did I undestand the idea correctly and where could I know more about deployment NestJS monorepo project?
This is how I solved it:
apps
app1
Dockerfile
...
app2
Dockerfile
...
app3
Dockerfile
...
docker-compose.yml
Each Dockerfile does the same:
FROM node:16.15.0-alpine3.15 AS development
WORKDIR /usr/src/app
COPY package*.json ./
RUN npm install
COPY . .
RUN npm run build
FROM node:16.15.0-alpine3.15 AS production
ARG NODE_ENV=production
ENV NODE_ENV=${NODE_ENV}
WORKDIR /usr/src/app
COPY package*.json ./
RUN npm install --only=production --omit=dev
COPY --from=development /usr/src/app/dist ./dist
CMD ["npm", "run", "start-app1:prod"]
Where the last line should start the application so adjust that to your project naming.
Later you should build each of the images in your CI/CD pipeline and deploy them separately. To run the docker build from the root folder of the project you just need to provide a Dockerfile path for -f parameter, for example:
docker build -f apps/app1/Dockerfile -t app1:version1 .
docker build -f apps/app2/Dockerfile -t app2:version1 .
docker build -f apps/app3/Dockerfile -t app3:version1 .
To run it locally for tests, utilize docker-compose.yml
version: '3.8'
services:
app1:
image: app1:version1
ports:
- 3000:3000 # set according to your project setup
app2:
...
app3:
...
And start it by calling docker compose up
After refactoring from docker-compose into separate Docker file for each project, I'm Unable to run the container, it simply exits. I'm using both docker and docker-compose since I'll have more projects down the road.
My docker files are as follows.
docker-compose.yml
version: '3'
services:
customer:
image: customer
container_name: customer
build:
context: ./Customer
dockerfile: Dockerfile
Customer/Dockerfile
FROM mcr.microsoft.com/dotnet/core/runtime:2.2
WORKDIR /Customer
EXPOSE 80
COPY ./bin/Release/netcoreapp2.2/ service/
ENTRYPOINT ["dotnet", "service/Customer.dll"]
Also I had this within docker-compose file before. How do I map 6001 to 80 within Dockerfile?
ports:
- 6001:80
Attempt 2
FROM mcr.microsoft.com/dotnet/core/sdk:2.2
WORKDIR /Customer
ENV DATABASE_HOST=database
ENV DATABASE_NAME=db
ENV DATABASE_USER=sa
ENV DATABASE_PASSWORD=Password
EXPOSE 80
COPY . .
CMD dotnet build
ENTRYPOINT ["dotnet", "Customer/bin/Release/netcoreapp2.2/Department.dll"]
Attempt 3
copied from main site.
Copied from here https://docs.docker.com/engine/examples/dotnetcore/
FROM mcr.microsoft.com/dotnet/core/sdk:2.2 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 runtime image
FROM mcr.microsoft.com/dotnet/core/aspnet:2.2
WORKDIR /app
COPY --from=build-env /app/out .
ENTRYPOINT ["dotnet", "aspnetapp.dll"]
Did you mean to run dotnet SDK commands? Please install dotnet SDK
from: https://go.microsoft.com/fwlink/?LinkID=798306&clcid=0x409
Here is a lazy and basic docker file. I added some comments and some helpful build/debug options, e.g: "RUN ls -al" to list the current directory. Just like a linux VM.
# step 1 - building you app with SDK as base image
FROM microsoft/dotnet:2.2-sdk AS build-env # call the environment build
WORKDIR /build # create a work dir
COPY . . # you don't need copy everything to build your app, but this is for simplisity
RUN ls -al # linux command to list dir content
RUN cd /Customer && dotnet publish -o out # actually building the app and publishing to /out dir
RUN cd /Customer && ls -al # navigate to the folder you copied and list dir
FROM microsoft/dotnet:2.2-aspnetcore-runtime AS runtime # step 2, runtime env (slimmed down container)
WORKDIR /app # create workdir
COPY --from=build-env /<YOUR_BULD_PATH>/out ./ # copy from prev container build output
RUN ls -al # list again
ENTRYPOINT ["dotnet", "Department.dll", "--urls", "http://*:6001"] # example from .NET Core 2.2 webapi with port 6005, this might not be your case
Now to run the docker-compose, just point out the docker file like you have already done. But both docker/docker-compose should work just fine now. Ofcourse you need to tweak the docker file a bit, i dont know your app or folder structure.
And just a tip, if you want to run your docker file as stand alone, dont forget the args when you start it to map ports --> -p 6001:80
I've read here that I can get possibility not to run docker-compose build every time code changes by adding volumes sections to docker-compose.yml. But I can't achieve this feature in my ASP.NET Core application.
Dockerfile:
FROM microsoft/dotnet:2.1-aspnetcore-runtime AS base
WORKDIR /app
EXPOSE 80
FROM microsoft/dotnet:2.1-sdk AS build
WORKDIR /src
COPY ["dockercompose/dockercompose.csproj", "dockercompose/"]
RUN dotnet restore "dockercompose/dockercompose.csproj"
COPY . .
WORKDIR "/src/dockercompose"
RUN dotnet build "dockercompose.csproj" -c Release -o /app
FROM build AS publish
RUN dotnet publish "dockercompose.csproj" -c Release -o /app
FROM base AS final
WORKDIR /app
COPY --from=publish /app .
ENTRYPOINT ["dotnet", "dockercompose.dll"]
docker-compose.yml:
version: '3.4'
services:
dockercompose:
image: ${DOCKER_REGISTRY}dockercompose
build:
context: .
dockerfile: dockercompose/Dockerfile
ports:
- "9000:80"
volumes:
- .:/src/dockercompose
What am I missing?
You should use dotnet watch run for that. However that means you need to use the SDK image (microsoft/dotnet:2.1-sdk) not the runtime only image (microsoft/dotnet:2.1-aspnetcore-runtime).
I just use two Docker files and have two services defined in the docker-compose.yml. One uses the dockerfile with the runtime image (for deployment), the other one with the SDK image (for development).
The development dockerfile could look something like this:
FROM microsoft/dotnet:2.1-sdk AS build-env
# Use native linux file polling for better performance
ENV DOTNET_USE_POLLING_FILE_WATCHER 1
WORKDIR /app
ENTRYPOINT dotnet watch run --urls=http://+:5000
You might have to chnage the path according to your project and how you map it in your docker-compose.yml, but this is more or less it.
I just got a MacBook Pro and am trying to run a dotnet core web api project I created on my Windows PC. I also am learning how to use Docker and am trying to integrate it into my project.
When I right click on my API project and hit Add > Docker Support, a docker-compose project and corresponding .yml file are generated and a Dockerfile is added to my Api project.
This is what the .yml file looks like:
version: '3.4'
services:
api:
image: api
build:
context: .
dockerfile: Api/Dockerfile
And here is the Dockerfile that is generated:
FROM microsoft/aspnetcore:2.0 AS base
WORKDIR /app
EXPOSE 80
FROM microsoft/aspnetcore-build:2.0 AS build
WORKDIR /src
COPY AdamWebsiteApi.sln ./
COPY Api/Api.csproj Api/
RUN dotnet restore -nowarn:msb3202,nu1503
COPY . .
WORKDIR /src/Api
RUN dotnet build -c Release -o /app
FROM build AS publish
RUN dotnet publish -c Release -o /app
FROM base AS final
WORKDIR /app
COPY --from=publish /app .
ENTRYPOINT ["dotnet", "Api.dll"]
When I run the solution, using the docker-compose project as the startup, I get the following error:
Cannot start service api: Mounts denied:
The path /usr/local/share/dotnet/sdk/NuGetFallbackFolder
is not shared from OS X and is not known to Docker.
You can configure shared paths from Docker -> Preferences... -> File Sharing.
See https://docs.docker.com/docker-for-mac/osxfs/#namespaces for more info.
Any idea what I need to do to get this working? It might be really simple and obvious but I am very new to Docker and Macs so I'm not sure what I'm doing.
Add the folder /usr/local/share/dotnet/sdk/NuGetFallbackFolder to the File Sharing list in Docker Preferences. It will work.