Where the new folder is created when using Dockerfile - docker

Please help me understand where the new folder is created. When I docker exec -it <mycontainer> bash the container, the created folder is not there.
Dockerfile:
FROM python:3.7-alpine
WORKDIR /app
RUN pip install -r requirements.txt
RUN mkdir -p /new_folder
COPY . .
CMD ["gunicorn", "-w 4", "main:app"]
I also tried copying the local stuff before creating a new folder, still can't see the folder created in the container.

your working directory is /app and you are copying files from your current dierctory to /app. when your container is running do this
docker exec -it <container_id> pwd
you'll see /app in output
but you are creating new_folder as a root directory. so you can't see it inside /app
to see your root directories you can run this:
docker exec -it container_id /bin/sh -c "ls -lah ..
also my Dockerfile is this:
FROM python:3.7-alpine
WORKDIR /app
RUN mkdir -p /new_folder
COPY . .
CMD ["python" , "-c" , "import time; time.sleep(10000)"]

Your folder is created at the filesystem root : /
try docker exec -it <mycontainer> bash and then, in the container, ls /

Related

Module Not found after attaching volume in docker

This is my dockerfile
FROM node:15
# sets the folder structure to /app directory
WORKDIR /app
# copy package.json to /app folder
COPY package.json .
RUN npm install
# Copy all files from current directory to current directory in docker(app)
COPY . ./
EXPOSE 3000
CMD ["node","index.js"]
I am using this command in my powershell to run the image in a container
docker run -v ${pwd}:/app -p 3000:3000 -d --name node-app node-app-image
${pwd}
returns the current directory.
But as soon as I hit enter, somehow node_modules isn't being installed in the container and I get "express not found" error in the log.
[![Docker log][1]][1]
I can't verify if node_modules isn't being installed because I can't get the container up to run the exec --it command.
I was following a freecodecamp tutorial and it seems to work in his pc and I've tried this command in command prompt too by replacing ${pwd} by %cd%.
This used to work fine before I added the volume flag in the command.
[1]: https://i.stack.imgur.com/4Fifu.png
Your problem was you build your image somewhere and then try to map another folder to it.
|_MyFolder/
|_ all-required-files
|_ all-required-folders
|_ Dockerfile
docker build -t node-app-image .
docker run -p 3000:3000 -d --name node-app node-app-image
Simplified Dockerfile
FROM node:15
# sets the folder structure to /app directory
WORKDIR /app
# Copy all files from current directory to current directory in docker(app)
COPY . ./
RUN npm install
EXPOSE 3000
CMD ["node","index.js"]

Docker build "no such file or directory" error

RUN adduser -D appUser
RUN mkdir /usr/share/app
RUN mkdir /logs
ADD Function/target/foo.jar /usr/share/app
WORKDIR /usr/share/app
RUN chown -R appUser /usr/share/app
RUN chown -R appUser /logs
USER appUser
EXPOSE 8080
ENTRYPOINT ["java", "-jar", "foo.jar"]`
I've got this weird issue I can't seem to work my head around.
My root folder contains two directories, (both with subdirectories) 'Demo/Dockerfile' and 'Function/target/foo.jar'
I have a copy command in my Dockerfile that reads
COPY Function/target/foo.bar /usr/share/app
but when I run docker build -f Demo/Dockerfile from the root folder, I get an error
stat /var/lib/docker/tmp/docker-builder238823934/Function/target/foo.jar: no such file or directory
I find this a bit strange because when I edit the copy command to read COPY /target/foo.bar /usr/share/app and then I cd into the Function directory and run
docker build -f ../Demo/Dockerfile
it builds successfully, or if I edit the Dockerfile to read COPY foo.bar /usr/share/app and then cd into the target directory and run docker build -f ../../Demo/Dockerfile, this also works.
Is there an explanation for this sort of behavior?
This is what my dockerignore file looks like
!**/Dockerfile
!DockerServiceDescription/**
!Function/target/*.war
!server.xml
!tomcat-users.xml
Docker uses context directory and children only and does not allow using any files outside for security reasons.
You should show context directory to docker using '.' or so:
cd myproject
docker build -f Demo/Dockerfile .

Server Tomcat using Docker on Windows PC

I made a webapp in Java and I would like to test the site locally using Docker.
The war file I created works perfectly but to be read correctly it must be inserted inside this path:
/tomcat/webapps/ROOT/
For these reasons I decided to use this form:
https://hub.docker.com/_/tomcat
In particular I decided to use this Dockerfile:
https://github.com/docker-library/tomcat/blob/ec2d88f0a3b34292c1693e90bdf786e2545a157e/9.0/jre11-slim/Dockerfile
I added this code towards the end:
...
EXPOSE 8080
CMD ["cd /usr/local/tomcat/webapps/"]
CMD ["mv ROOT ROOT.old"]
CMD ["mkdir ROOT"]
COPY ./esercitazione.1.maven/ /usr/local/tomcat/webapps/ROOT/
CMD ["catalina.sh", "run"]
I used this code at the Windows 10 prompt:
D:
cd "D:\DATI\Docker-Tomcat-Win10"
docker build -t tomcat-9-java-11:v2.0 .
docker run -it --rm --name tomcat-9-java-11-container -p 8888:8080 tomcat-9-java-11:v2.0
When I enter this link on the browser:
http://192.168.99.103:8888/
I see this:
https://prnt.sc/n2vti1
I'm a beginner with both Docker and Tomcat and I need a little help.
Inside this path I put my unzipped .war file:
D:\DATI\Docker-Tomcat-Win10\esercitazione.1.maven
Thank you
%%%%%%%%%%%%%%%%%%%%%%%%%%
#Shree Tiwari
%%%%%%%%%%%%%%%%%%%%%%%%%%
First of all thank you for your help!
I deleted all the containers and all the images on Docker and used your Dockerfile (I only changed the name of the folder containing the .war files to be tested).
FROM tomcat:9-jre8
ENV JAVA_OPTS="-Xms512m -Xmx1024m -XX:MaxPermSize=256m -XX:MaxMetaspaceSize=128m"
WORKDIR /usr/local/tomcat/webapps/
RUN rm -rf /usr/local/tomcat/webapps/*
COPY ./webapps/*.war /usr/local/tomcat/webapps
EXPOSE 8080
CMD ["catalina.sh", "run"]
I placed the .war file at this address:
D:\DATI\Docker-Tomcat-Win10\webapps\esercitazione.1.maven.war
I opened the Windows prompt and I typed:
D:
cd "D:\DATI\Docker-Tomcat-Win10"
docker build -t tomcat:v1.0 .
docker run -it --rm --name tomcat-container -p 8888:8080 tomcat:v1.0
I entered this URL in the browser:
http://192.168.99.103:8888/esercitazione.1.maven/
then this other:
http://192.168.99.103:8888/
Unfortunately none of them went well.
The only mistake I encountered when creating the image is this:
"SECURITY WARNING: You are building to Docker image from Windows against a non-Windows Docker host. Files and directories added to build context will have '-rwxr-xr-x' permissions. It is recommended to double check and reset permissions for sensitive files and directories."
%%%%%%%%%%%%%%%%%%%%%%%%%%
#Miq
%%%%%%%%%%%%%%%%%%%%%%%%%%
First of all thank you for your help!
I also tested your code but it doesn't work.
Dockerfile:
FROM tomcat:9-jre11-slim
RUN mv webapps/ROOT webapps/ROOT.old && mkdir webapps/ROOT
COPY ./esercitazione.1.maven/ webapps/ROOT/
Code:
D:
cd "D:\DATI\Docker-Tomcat-Win10"
docker build -t tomcat:v2.0 .
docker run -it --rm --name tomcat-container tomcat:v2.0
Browser:
http://192.168.99.103:8080/
Other tests:
FROM tomcat:9-jre11-slim
ENV JAVA_OPTS="-Xms512m -Xmx1024m -XX:MaxPermSize=256m -XX:MaxMetaspaceSize=128m"
RUN mv webapps/ROOT webapps/ROOT.old && mkdir webapps/ROOT
WORKDIR /usr/local/tomcat/webapps/
RUN rm -rf /usr/local/tomcat/webapps/ROOT/*
COPY ./webapps/*.war /usr/local/tomcat/webapps/ROOT/
EXPOSE 8080
CMD ["catalina.sh", "run"]
docker ps -a
docker images
docker stop tomcat-container
docker rmi tomcat:v3.0
D:
cd "D:\DATI\Docker-Tomcat-Win10"
docker build -t tomcat:v3.0 .
docker run -d --name tomcat-container -p 8888:8080 tomcat:v3.0
http://192.168.99.103:8888/
%%%%%%%%%%%%%%%%%%%%%%%%%%
Other tests: (8 April 2019)
FROM tomcat:9.0.17-jre11-slim
LABEL Author="Nome Cognome"
EXPOSE 8080
RUN rm -fr /usr/local/tomcat/webapps/ROOT
COPY ./esercitazione.1.maven.war /usr/local/tomcat/webapps/ROOT.war
CMD ["catalina.sh", "run"]
>
docker build -t tomcat-eb:v.9.0.17 .
docker run -it --rm -p 8888:8080 tomcat-eb:v.9.0.17
>
I'm going here:
http://192.168.99.103:8888
and the browser sends me here:
https://192.168.99.103:8443
%%%%%%%%%%%%%%%%%%%%%%%%%%
Other tests: (I choose another image)
FROM tomee:8-jre-8.0.0-M2-webprofile
LABEL Author="Nome Cognome"
EXPOSE 8080
RUN rm -fr /usr/local/tomcat/webapps/ROOT
COPY ./esercitazione.1.maven.war /usr/local/tomcat/webapps/ROOT.war
CMD ["catalina.sh", "run"]
>
docker build -t tomcat-eb:v.9.0.17 .
docker run -it --rm -p 8888:8080 tomcat-eb:v.9.0.17
>
If I go here:
http://192.168.99.103:8888
I see Tomcat home, non my webapp.
Is this a problem without a solution?
The biggest problem I see here is that you use CMD instead RUN in your dockerfile. CMD is to define a command that will be run when container is ran. With the Dockerfile you have now, only the last one is executed when you start your container and all of those mkdirs, moves, etc. are never executed. As said, you need to use RUN keywords to indicate command that build process should execute and commit as image layer. You probably also do not need the catalina.sh CMD as it might come from the image you base on, but you need to check it on doc page for the base image or take a peek to it's Dockerfile or use docker history imagename To see the layers and commands used to create them.
I took a deeper look into this and the base image you use. In addition to the CMD's your problem is that you change the workdir by running cd commands. catalina.sh exists in $CATALINA_HOME dir, which then is marked as workdir in base image. When you change the active directory by executing cd it breaks the image runtime.
I'd suggest you try with following dockerfile:
FROM tomcat:9-jre11-slim
RUN mv webapps/ROOT webapps/ROOT.old && mkdir webapps/ROOT
COPY ./esercitazione.1.maven/ webapps/ROOT/
No need to use EXPOSE and CMD as they are defined in base image. also, base image defines WORKDIR to $CATALINA_HOME and that's where you will be when executing any following commands (treat WORKDIR as cd but in docker style).
Hope that helps.
First you need to compile your Code to a war file then you can use below Dockerfile
FROM tomcat:9-jre8
ENV JAVA_OPTS="-Xms512m -Xmx1024m -XX:MaxPermSize=256m -XX:MaxMetaspaceSize=128m"
WORKDIR /usr/local/tomcat/webapps/
RUN rm -rf /usr/local/tomcat/webapps/*
COPY ./esercitazione.1.maven/*.war /usr/local/tomcat/webapps
EXPOSE 8080
CMD ["catalina.sh", "run"]

How to create a file using touch in Dockerfile or docker-compose?

I want to create an empty DB file using touch by Dockerfile or docker-compose.yml and volume it. Actually, I'm able to create it manually within the container as follows:
docker exec -it <container-name> bash
# touch /app/model/modbus.db
Whereas, when I use the following procedure it throws exited with code 0 and stops:
version: '3'
services:
collector:
build: .
image: collector:2.0.0
command: bash -c "touch /app/model/modbus.db" # Note
# command: bash /app/bashes/create_an_empty_db.sh
volumes:
- "./model/modbus.db:/app/model/modbus.db:rw"
tty: true
As well as this, I tried that via Dockerfile without any success either:
FROM python:3.6-slim
ENV PYTHONUNBUFFERED 1
RUN mkdir /app
WORKDIR /
ADD . /app
RUN touch /app/model/modbus.db # Note
CMD python app
[NOTE]:
Also without the command: bash -c "touch /app/model/modbus.db" in the docker-compose.yml which was the cause of exited with code 0; a directory will be created named modbus.db instead of a file due to the following section:
volumes:
- "./model/modbus.db:/app/model/modbus.db:rw"
TL;DR:
How to send a new file from the container to the host which does not exist in the host? (In other words, it is done inside of the container, not in the host)
I am not sure about the docker-compose.yml but the dockerfile that you have seems to be working for me.
The Dockerfile looks like this,
FROM python:3.6-slim
RUN mkdir /app
WORKDIR /
RUN touch /app/modbus.db
Build the dockerfile,
docker build -t test .
Sending build context to Docker daemon 2.048kB
Step 1/4 : FROM python:3.6-slim
---> 903e8a0f0681
Step 2/4 : RUN mkdir /app
---> Using cache
---> c039967bf463
Step 3/4 : WORKDIR /
---> Using cache
---> c8c81ac01f50
Step 4/4 : RUN touch /app/modbus.db
---> Using cache
---> 785916fe4cea
Successfully built 785916fe4cea
Successfully tagged test:latest
Build the container,
docker run -dit test
52cde500cda015f170140ae9e7174a0367b29265a49a3742173946b686179fb3
I ssh'ed into the container and was able to find the file.
docker exec -it 52cde500cda015f170140ae9e7174a0367b29265a49a3742173946b686179fb3 /bin/bash
root#52cde500cda0:/# ls
app bin boot dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var
root#52cde500cda0:/# cd app
root#52cde500cda0:/app# ls
modbus.db
Put the following in your docker-compose.yml
volumes:
- "./model:/app/model"
This will create a /app/model folder inside of your container. Its contents (which you will create inside the container) will be available on ./model on your host.
If you put the touch command in the CMD of your Dockerfile, that file will be created after starting the container when the volume is also initialized. So the following Dockerfile should work:
FROM python:3.6-slim
ENV PYTHONUNBUFFERED 1
RUN mkdir /app
WORKDIR /
ADD . /app
CMD touch /app/model/modbus.db && python app

Mount docker volume to host machine path

I need to access test result files in the host from the container. I know that I need to create a volume which maps between host and container, like below, but I get nothing written to the host.
docker run --rm -it -v <host_directory_path>:<container_path> imagename
Dockerfile:
FROM microsoft/dotnet:2.1-sdk AS builder
WORKDIR /app
COPY ./src/MyApplication.Program/MyApplication.Program.csproj ./src/MyApplication.Program/MyApplication.Program.csproj
COPY nuget.config ./
WORKDIR ./src/MyApplication.Program/
RUN dotnet restore
WORKDIR /app
COPY ./src ./src
WORKDIR ./src/MyApplication.Program/
RUN dotnet build MyApplication.Program.csproj -c Release
FROM builder as tester
WORKDIR /app
COPY ./test/MyApplication.UnitTests/MyApplication.UnitTests.csproj ./test/MyApplication.UnitTests/MyApplication.UnitTests.csproj
WORKDIR ./test/MyApplication.UnitTests/
RUN dotnet restore
WORKDIR /app
COPY ./test ./test
WORKDIR ./test/MyApplication.UnitTests/
RUN dotnet test /p:CollectCoverage=true /p:CoverletOutputFormat=cobertura
ENTRYPOINT ["dotnet", "reportgenerator", "-reports:coverage.cobertura.xml", "-targetdir:codecoveragereports", "-reportTypes:htmlInline"]
The command at the entry point is working correctly. It is writing the output to the MyApplication.UnitTests/codecoveragereports directory, but not to the host directory.
My docker run looks as follows:
docker run --rm -it -v /codecoveragereports:/app/test/MyApplication.UnitTests/codecoveragereports routethink.tests:latest
What could I be doing wrong?
Looks like a permission issue.
-v /codecoveragereports:/app/***/codecoveragereports is mounting a directory under the root / which is dangerous and you may not have the permission.
It's better to mount locally, like -v $PWD/codecoveragereports:/app/***/codecoveragereports, where $PWD is an environment variable equal to the current working directory.

Resources