The docker image build is stuck - windows

I need to build a windows docker image for a window app on windows 10 with docker desktop installed on vm, but when the build process is stuck when running the installation of this app. the log looks like something bellow,
PS C:\docker> docker image build . -t app
Sending build context to Docker daemon 144.1MB
Step 1/6 : FROM mcr.microsoft.com/dotnet/framework/aspnet:4.8
---> 9b87edf03093
Step 2/6 : COPY . /app
---> ac4b1124d856
Step 3/6 : WORKDIR /app
---> Running in cf4bd2345d26
Removing intermediate container cf4bd2345d26
---> d4f28097afd9
Step 4/6 : RUN .\ELSA1.0_2.6.6.243.exe
---> Running in b9356f975aa6
(And the process is stuck for several hours here and is terminated by me)
the docker file is
FROM mcr.microsoft.com/dotnet/framework/aspnet:4.8 # this is a base image because asp.net 3.5 is a prerequisite for the app
COPY . /app
WORKDIR /app
RUN .\ELSA1.0_2.6.6.243.exe # I am stuck here!
I have tried to do something in the base image like so, but it seems that I cannot do anything
PS C:\docker> docker container run -it
mcr.microsoft.com/dotnet/framework/aspnet:4.8
Service 'w3svc' has been stopped
Service 'w3svc' started
Are there any good ideas to debug this issue? By the way the installer can work normally on windows 10.

You should never execute the command in the RUN statement that not terminate. I see in Docker build logs that you started exe file in RUN command. This will keep stuck your docker build process and will wait for SIGINT. The same will happen like if you execure RUN npm start so it will hang the build process.
Add your executable at entrypoint or CMD.
Another thing that can be the issue in such cased
Considerations for using CMD with Windows
On Windows, file paths specified in the CMD instruction must use
forward slashes or have escaped backslashes \. The following are
valid CMD instructions:
dockerfile
# exec form
CMD ["c:\\Apache24\\bin\\httpd.exe", "-w"]
# shell form
CMD c:\\Apache24\\bin\\httpd.exe -w
You can further read about Window CMD here
However, the following format without the proper slashes will not work:
dockerfile
CMD c:\Apache24\bin\httpd.exe -w

Related

DotNet 6 fails to run in a Windows container: Permission denied

I am not familiar with Windows containers, and I am getting this error when trying to debug a hello-world DotNet 6 application in VS2022:
Cannot use file stream for [C:\app\bin\Debug\net6.0\WebApplication5.runtimeconfig.json]: Permission denied
Invalid runtimeconfig.json [C:\app\bin\Debug\net6.0\WebApplication5.runtimeconfig.json] [C:\app\bin\Debug\net6.0\WebApplication5.runtimeconfig.dev.json]
The target process exited without raising a CoreCLR started event. Ensure that the target process is configured to use .NET Core. This may be expected if the target process did not run on .NET Core.
The program '[13360] dotnet.exe' has exited with code 2147516563 (0x80008093).
Dockerfile (default):
#See https://aka.ms/containerfastmode to understand how Visual Studio uses this Dockerfile to build your images for faster debugging.
#Depending on the operating system of the host machines(s) that will build or run the containers, the image specified in the FROM statement may need to be changed.
#For more information, please see https://aka.ms/containercompat
FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS base
WORKDIR /app
EXPOSE 80
FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
WORKDIR /src
COPY ["WebApplication5/WebApplication5.csproj", "WebApplication5/"]
RUN dotnet restore "WebApplication5/WebApplication5.csproj"
COPY . .
WORKDIR "/src/WebApplication5"
RUN dotnet build "WebApplication5.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "WebApplication5.csproj" -c Release -o /app/publish
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "WebApplication5.dll"]
I tried to spin the container in an interactive way, to validate if the file was actually there but this command did not work:
docker run -it --entrypoint powershell.exe webapplication5
docker: Error response from daemon: container 8581fd662043dbfad4a2b68e561d21c106e4754b6f21c4d06cfb9b3d5e2b0a39 encountered an error during hcsshim::System::CreateProcess: failure in a Windows system call: The system cannot find the file specified. (0x2).
What can the problem be?
Why I cannot get a PS inside the container?
My docker user needs more permissions. I solved it in an easy way because it is for testing.. don't do this at home, and grant the minimum permissions instead:
FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS base
WORKDIR /app
EXPOSE 80
# Here is the risky trick
USER Administrator

Copy contents from host OS into Docker image without rebuilding image

I'm building a new image and copy contents from host OS folder D:\Programs\scrapy into it like so: docker build . -t scrapy
Dockerfile
FROM mcr.microsoft.com/windows/servercore:ltsc2019
SHELL ["powershell", "-Command", "$ErrorActionPreference = 'Stop'; $ProgressPreference = 'SilentlyContinue';"]
RUN mkdir root
RUN cd root
WORKDIR /root
RUN mkdir scrapy
COPY scrapy to /root/scrapy
Now when I add new contents to the host OS folder "D:\Programs\scrapy" I want to also add it to image folder "root/scrapy", but I DON'T want to build a completely new image (it takes quite a while).
So how can I keep the existing image and just overwrite the contents of the image folder "root/scrapy".
Also: I don't want to copy the new contents EACH time I run the container (so NOT at run-time), I just want to have a SEPARATE command to add more files to an existing image and then run a new container based on that image at another time.
I checked here: How to update source code without rebuilding image (but not sure if OP tries to do the same as me)
UPDATE 1
Checking What is the purpose of VOLUME in Dockerfile and docker --volume format for Windows
I tried the commands below, all resulting in error:
docker: Error response from daemon: invalid volume specification: ''. See 'docker run --help'.
Where <pathiused> is for example D:/Programs/scrapy:/root/scrapy
docker run -v //D/Programs/scrapy:/root/scrapy scrapy
docker run -v scrapy:/root/scrapy scrapy
docker run -it -v //D/Programs/scrapy:/root/scrapy scrapy
docker run -it -v scrapy:/root/scrapy scrapy
UPDATE WITH cp command based on #Makariy's feedback
docker images -a gives:
REPOSITORY TAG IMAGE ID CREATED SIZE
scrapy latest e35e03c8cbbd 29 hours ago 5.71GB
<none> <none> 2089ad178feb 29 hours ago 5.71GB
<none> <none> 6162a0bec2fc 29 hours ago 5.7GB
<none> <none> 116a0c593544 29 hours ago 5.7GB
mcr.microsoft.com/windows/servercore ltsc2019 d1724c2d9a84 5 weeks ago 5.7GB
I run docker run -it scrapy and then docker container ls which gives:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
1fcda458a14c scrapy "c:\\windows\\system32…" About a minute ago Up About a minute thirsty_bassi
If I run docker cp D:\Programs\scrapy scrapy:/root/scrapy I get:
Error: No such container:path: scrapy:\root
So in a separate PowerShell instance I then run docker cp D:\Programs\scrapy thirsty_bassi:/root/scrapy whichs show no output in PowerShell whatsoever, so I think it should've done something.
But then in my container instance when I goto /root/scrapy folder I only see the files that were already added when the image was built, not the new ones I wanted to add.
Also, I think I'm adding files to the container here, but is there no way to add it to the image instead? Without rebuilding the whole image?
UPDATE 2
My folder structure:
D:\Programs
Dockerfile
\image_addons
Dockerfile
\scrapy
PS D:\Programs>docker build . -t scrapybase
Successfully built 95676d084e28
Successfully tagged scrapybase:latest
PS D:\Programs\image_addons> docker build -t scrapy .
Step 2/2 : COPY scrapy to /root/scrapy
COPY failed: file not found in build context or excluded by .dockerignore: stat to: file does not exist
Dockerfile A
FROM mcr.microsoft.com/windows/servercore:ltsc2019
SHELL ["powershell", "-Command", "$ErrorActionPreference = 'Stop'; $ProgressPreference = 'SilentlyContinue';"]
WORKDIR /root/scrapy
Dockerfile B
FROM scrapybase
COPY scrapy to /root/scrapy
You also can use docker cp, to manually copy files from your host to running container
docker cp ./path/to/file containername:/another/path
Docs
answer if you want it quick and dirty
docker run -it -v c:/programs/test:/root/test ubuntu:latest cat /root/test/myTestFile.txt
to update one file quickly:
If you don't have to build your code (I don't know what language you are using) you can build some base image with the initial code and when you want to change only one file (again I'm assuming you don't need to compile your project again for that, otherwise if you do that is not possible to due the nature of compiled programming language):
FROM previous-version-image:latest
COPY myfile dest/to/file
then because your CMD and ENTRYPOINT are saved from the previous stages no need to declare them. (if you don't remember use docker history <docker-image-name> to view virtual dockerfile for image to this stage).
Notice though not to repetitively use this method or you'll get a very big image with many useless layers. Use this only for quick testing and debugging.
explanation
Usually people use it for frontend development on docker containers but the basic idea persists, you create the basic working image with the dependencies installed and the directory layout setup with the last Dockerfile command being the development server start command.
example:
Dockerfile:
# pull the base image
FROM node:slim
# set the working directory
WORKDIR /app
# add `/app/node_modules/.bin` to $PATH
ENV PATH /app/node_modules/.bin:$PATH
# copy dependencies files
COPY package.json ./
COPY package-lock.json ./
# install app dependencies
RUN npm install
# add app
COPY . ./
# start development server
CMD ["npm", "start"]
startup command:
docker run -it --rm \
-v ${PWD}:/app \ <mount current working directory in host to container in path /app>
-v /app/node_modules \ <or other dependency directory if exists>
-p 80:3000 \ <ports if needs exposing>
ps-container:dev
I'm not sure if that use case will 100% work for you because it needs the code to be mounted using bind-mount all the time and when needed to be exported will have to be exported as the image and the source code directory, on the other hand, it allows you to make quick changes without waiting for the image to be built each time you add something new and in the end build the final image that contains all that's needed.
more relatable example to question provided code:
As you can see there is a file on the host machine that contains some text
the command that uses bind-mount to have access to the file:
docker run -it -v c:/programs/test:/root/test ubuntu:latest cat /root/test/myTestFile.txt
hope you find something that works for you from what I've provided here.
thanks to this tutorial and this example for starting examples and information.
EDIT:
Let's say your original Dockerfile looks like this:
FROM python:latest
WORKDIR /app
COPY . .
RUN pip install -r requirements.txt
CMD python /app/app.py
This will build your initial image on top of we'll add layers and change the python files.
The next Dockerfile we'd use (let's call it Dockerfile.fix file) would copy the file we want to change instead of the ones already in the image
FROM previous-image-name
COPY app.py .
Now with after building with this Dockerfile the final image Dockerfile would look (sort of) like so:
FROM python:latest
WORKDIR /app
COPY . .
RUN pip install -r requirements.txt
CMD python /app/app.py
FROM previous-image-name
COPY app.py .
And each time we'll want to change the file we'll use the second Dockerfile
There's no way you can change a Docker image without (at least partially) rebuilding it. But you don't have to rebuild all of it, you can just rebuild the layer copying your scrapy content.
You can optimize your build to have two images:
First image is your static image you don't want to rebuild each time. Let's call it scrapy-base.
Second and final image is based on first image scrapy-base and will only exist for the purpose of copying your dynamic scrapy content
scrapy-base's Dockerfile:
FROM mcr.microsoft.com/windows/servercore:ltsc2019
SHELL ["powershell", "-Command", "$ErrorActionPreference = 'Stop'; $ProgressPreference = 'SilentlyContinue';"]
RUN mkdir root
RUN cd root
WORKDIR /root
RUN mkdir scrapy
And build it like:
docker build -t scrapy-base .
This command only needs to be run once. You won't have to build this image if you only change the content of local scrapy folder. (as you can see, the build does not use it at all)
scrapy's Dockerfile:
FROM scrapy-base
COPY scrapy /root/scrapy
With build command:
docker build -t scrapy .
This second build command will re-use the previous static image and only copy content without having to rebuild the entire image. Even with lots of files it should be pretty quick. You don't need to have a running container.
For your scenario :
docker run -v D:/test:/root/test your-image
A lots of valuable details available in this thread

Docker Build/Deploy using Bash Script

I have a deploy script that I am trying to use for my server for CD but I am running into issues writing the bash script to complete some of my required steps such as running npm and the migration commands.
How would I go about getting into a container bash, from this script, running the commands below and then exiting to finish pulling up the changes?
Here is the script I am trying to automate:
cd /Project
docker-compose -f docker-compose.prod.yml down
git pull
docker-compose -f docker-compose.prod.yml build
# all good until here because it opens bash and does not allow more commands to run
docker-compose -f docker-compose.prod.yml run --rm web bash
npm install # should be run inside of web bash
python manage.py migrate_all # should be run inside of web bash
exit # should be run inside of web bash
# back out of web bash
docker-compose -f docker-compose.prod.yml up -d
Typically a Docker image is self-contained, and knows how to start itself up without any user intervention. With some limited exceptions, you shouldn't ever need to docker-compose run interactive shells to do post-deploy setup, and docker exec should be reserved for emergency debugging.
You're doing two things in this script.
The first is to install Node packages. These should be encapsulated in your image; your Dockerfile will almost always look something like
FROM node
WORKDIR /app
COPY package*.json .
RUN npm ci # <--- this line
COPY . .
CMD ["node", "index.js"]
Since the dependencies are in your image, you don't need to re-install them when the image starts up. Conversely, if you change your package.json file, re-running docker-compose build will re-run the npm install step and you'll get a clean package tree.
(There's a somewhat common setup that puts the node_modules directory into an anonymous volume, and overwrites the image's code with a bind mount. If you update your image, it will get the old node_modules directory from the anonymous volume and ignore the image updates. Delete these volumes: and use the code that's built into the image.)
Database migrations are a little trickier since you can't run them during the image build phase. There are two good approaches to this. One is to always have the container run migrations on startup. You can use an entrypoint script like:
#!/bin/sh
python manage.py migrate_all
exec "$#"
Make this script be executable and make it be the image's ENTRYPOINT, leaving the CMD be the command to actually start the application. On every container startup it will run migrations and then run the main container command, whatever it may be.
This approach doesn't necessarily work well if you have multiple replicas of the container (especially in a cluster environment like Docker Swarm or Kubernetes) or if you ever need to downgrade. In these cases it might make more sense to manually run migrations by hand. You can do that separately from the main container lifecycle with
docker-compose run web \
python manage.py migrate_all
Finally, in terms of the lifecycle you describe, Docker images are immutable: this means that it's safe to rebuild new images while the old ones are running. A minimum-downtime approach to the upgrade sequence you describe might look like:
git pull
# Build new images (includes `npm install`)
docker-compose build
# Run migrations (if required)
docker-compose run web python manage.py migrate_all
# Restart all containers
docker-compose up --force-recreate

"Docker build path not found" in docker support Visual Studio Project

I have C# solution, with 4 projects, 3 being Dlls and 1 is the console application uses those DLLs.
I tried to use docker support on visual studio to build my docker image, it fails
My dockerfile as below:
> FROM microsoft/windowsservercore:ltsc2016
> EXPOSE 80
> EXPOSE 1433
> EXPOSE 29051
>
> COPY bin/x64/debug /root/ ENTRYPOINT
> /root/RmsMainConsole.exe
I CD into directory where my dockerfile is at and execute docker build.
Error:
Docker CLI command : docker build -t rmsmainconsole:self-hosted .
Sending build context to Docker daemon 55.61MB
Step 1/6 : FROM microsoft/windowsservercore:ltsc2016
---> 9dbf7f740334
Step 2/6 : EXPOSE 80
---> Using cache
---> ad0ad85fd107
Step 3/6 : EXPOSE 1433
---> Using cache
---> 81ba13dbd4d4
Step 4/6 : EXPOSE 29051
---> Using cache
---> 1fa3db800abf
Step 5/6 : COPY bin/x64/debug /root/
COPY failed: CreateFile \\?\C:\ProgramData\Docker\tmp\docker-builder043346063\bin\x64\debug: The system cannot find the path specified.
You can see that PATH couldn't be found.
Whereas i tried to create docker file on my .sln level.
I changed one line in my docker file.
COPY RmsMainConsole/bin/x64/debug /root/
Note: "RmsMainConsole" directory has been added.
i executed the docker build at my .sln level and i built it successfully.
Logs are:
Sending build context to Docker daemon 1.15GB
Step 1/6 : FROM microsoft/windowsservercore:ltsc2016
---> 9dbf7f740334
Step 2/6 : EXPOSE 80
---> Running in fe97cf236d5a
Removing intermediate container fe97cf236d5a
---> c31e236353b6
Step 3/6 : EXPOSE 1433
---> Running in f031fce5ecba
Removing intermediate container f031fce5ecba
---> 96c704c68ffb
Step 4/6 : EXPOSE 29051
---> Running in 365e2be43d0e
Removing intermediate container 365e2be43d0e
---> d30c3fb2214b
Step 5/6 : COPY RmsMainConsole/bin/x64/debug /root/
---> b214c1edc256
Step 6/6 : ENTRYPOINT /root/RmsMainConsole.exe
---> Running in 5c819915532a
Removing intermediate container 5c819915532a
---> 247f01bb9b82
Successfully built 247f01bb9b82
Successfully tagged rmsmainconsole:self-hosted
I noticed two difference between the successful and failed build:
1. Size on the docker context
2. The logs at step 5/6:
COPY failed: CreateFile \?\C:\ProgramData\Docker\tmp\docker-builder043346063\bin\x64\debug: The system cannot find the path specified.
and
---> b214c1edc256
How should i use visual studio docker support to build my docker image. Where did i go wrong ?
Not sure, whether I answer your question - first or second :).
I was also wondering how Visual Studio is using Docker, because Dockerfile is created within project folder, but it contains paths for COPY like it was in the root (sln) folder.
I played with it a bit and answer is pretty simple.
Visual Studio builds images from the solution folder using -f switch to point to Dockerfile.
You can watch Output window to see it running following command:
docker build -f "<path to Dockerfile>" -t <name:tag> --target base --label "com.microsoft.created-by=visual-studio" "<solution dir>"
So, I would recommend having Dockerfile in project folder and run it from the root folder using -f option like Visual Studio does it.
Does it answer your question(s)?
After some thoughts, trials and errors, the problems does not lie on docker support visual studio. Docker build has to be run at the root of my solution directory, as docker build need to be able to locate all the dependencies the image file that is depending on.
but what got me curious is, then how is docker support for visual studio come into play ? I can only Right click -> Add Docker support at my console application project.
My current solution is to manually add a docker file at the root of solution. Would be glad if anyone knows visual studio docker support has a better way to solve this ?
Thanks
I think the actual reason for this is much simpler. If you look at the docker ignore (.dockerignore) file, you'll notice that it ignores everything except for a couple of obj folders. Try removing the * at the top of the docker ignore file to test this out. Then you can selectively ignore things you have in the project folder that you'd like to copy over. I ran into this as well and it took lots of trial and error before I thought to look at the ignore file.

Why this docker file is not running with a shell script?

Now, I made a docker file named as (Dockerfile) as follows:
When I build this using the following command:
docker build -f Dockerfile .
I get the following output:
Step 1 : FROM ******.dkr.ecr.us-east-1.amazonaws.com/centos-base:7
---> 9ab68a0dd16a
Step 2 : COPY echo_hello.sh /echo_hello.sh
---> Using cache
---> e7d541f5cf53
Step 3 : RUN bash /echo_hello.sh
---> Running in 4b5518faab28
hello world
hello world
.......
But, when I then begin to run it using the following command:
docker run -it d2cc33b16e8f
This doesn't happen and instead it shows me an error:
the command to run to start the application
Where am I going wrong in this?
You will need to change your Dockerfile to the following:
FROM *******.dkr.ecr.us-east-1.amazonaws.com/centos-base:7
COPY echo_hello.sh /echo_hello.sh
RUN chmod u+x /echo_hello.sh
CMD /echo_hello.sh
This page explains the Dockerfile instructions pretty well:
http://goinbigdata.com/docker-run-vs-cmd-vs-entrypoint/
RUN commands are executed at build time (when you do a docker build). What you need is CMD or ENTRYPOINT. Either of these sets the executable that is run when the container starts (via docker run)
Your Dockerfile should look similar to what #Rezney has in his answer.

Resources