The problem:
The problem is that the files and directories created during one of the RUN steps do not make it to the image. They're not even available from one step to the other.
My Dockerfile
I have a setup something like this...
COPY . /app
VOLUME /app
WORKDIR /app
RUN mkdir _TEST_ && \
echo "hello" >> ___FILE_CREATED.txt && \
echo $(ls)
RUN echo $(ls)
I can see that in the first run echo $(ls) has the file and directory, and on the second run, they are missing.
The problem was that I've defined a volume for the folder
COPY . /app
# VOLUME /app <== remove this line
WORKDIR /app
The important bit about volumes (why the files are not persistent) is mentioned in the docs:
Changes to a data volume will not be included when you update an image.
Related
I want to spin up a low configuration containerized service for which I created a Dockerfile as below:
docker build -t apache/druid_nano:0.20.2 -f Dockerfile .
FROM ubuntu:16.04
Install Java JDK 8
RUN apt-get update
&& apt-get install -y openjdk-8-jdk
RUN mkdir /app
WORKDIR /app
COPY apache-druid-0.20.2-bin.tar.gz /app
RUN tar xvzf apache-druid-0.20.2-bin.tar.gz
WORKDIR /app/apache-druid-0.20.2
EXPOSE <PORT_NUMBERS>
ENTRYPOINT ["/bin/start/start-nano-quickstart"]
When I start the container using the command "docker run -d -p 8888:8888 apache/druid_nano:0.20.2, I get an error as below:
/bin/start-nano-quickstart: no such file or directory
I removed the ENTRYPOINT command and built the image again just to check if the file exists in the bin directory inside the container. There is a file start-nano-quickstart under the bin directory inside the container.
Am I missing anything here? Please help.
I'm using Jason Taylor's Clean Architecture template and I wonder why his WebUI Dockerfile is generated by Visual Studio just like below.
It sets WORKDIR to /src and then copies each project from /src/ProjectDir/Project.csproj. What's the point of WORKDIR in that case? Look at WORKDIR "/src/src/WebUI". Why /src/src? There is only one src directory.
FROM mcr.microsoft.com/dotnet/aspnet:5.0 AS base
ENV ASPNETCORE_URLS=https://+:5001;http://+:5000
WORKDIR /app
EXPOSE 5000
EXPOSE 5001
FROM mcr.microsoft.com/dotnet/sdk:5.0 AS build
RUN curl -sL https://deb.nodesource.com/setup_12.x | bash -
RUN apt install -y nodejs
WORKDIR /src
COPY ["src/WebUI/WebUI.csproj", "src/WebUI/"]
COPY ["src/Application/Application.csproj", "src/Application/"]
COPY ["src/Domain/Domain.csproj", "src/Domain/"]
COPY ["src/Infrastructure/Infrastructure.csproj", "src/Infrastructure/"]
RUN dotnet restore "src/WebUI/WebUI.csproj"
COPY . .
WORKDIR "/src/src/WebUI"
RUN dotnet build "WebUI.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "WebUI.csproj" -c Release -o /app/publish
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "CleanArchitecture.WebUI.dll"]
I suspect this is because WORKDIR /src will create a directory called /src AND change directories into it. After changing into it, it will copy files from the host's relative src folder and into another subdirectory called src (on the container). So the full absolute path becomes /src/src. You can confirm this by adding RUN pwd && ls before the WORKDIR and after the first COPY.
FROM mcr.microsoft.com/dotnet/aspnet:5.0 AS base
ENV ASPNETCORE_URLS=https://+:5001;http://+:5000
WORKDIR /app
EXPOSE 5000
EXPOSE 5001
FROM mcr.microsoft.com/dotnet/sdk:5.0 AS build
RUN curl -sL https://deb.nodesource.com/setup_12.x | bash -
RUN apt install -y nodejs
RUN pwd && ls /
WORKDIR /src
COPY ["src/WebUI/WebUI.csproj", "src/WebUI/"]
RUN pwd && ls
COPY ["src/Application/Application.csproj", "src/Application/"]
COPY ["src/Domain/Domain.csproj", "src/Domain/"]
COPY ["src/Infrastructure/Infrastructure.csproj", "src/Infrastructure/"]
RUN dotnet restore "src/WebUI/WebUI.csproj"
COPY . .
WORKDIR "/src/src/WebUI"
RUN dotnet build "WebUI.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "WebUI.csproj" -c Release -o /app/publish
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "CleanArchitecture.WebUI.dll"]
In my CodeBuild step I have an environment variable called VUE_APP_BACKEND defined as a URL. I confirmed that this part is OK by adding echo $VUE_APP_BACKEND to my buildspec.yml.
That one will output
[Container] 2020/11/26 13:22:25 Running command echo $VUE_APP_BACKEND
https://my-url-here
However when I try to forward this argument into the docker build it is lost. Am I doing it incorrectly?
I have a Dockerfile that looks like this. The echo part outputs nothing.
#build stage
FROM node:12.18.2-alpine3.10 as build-stage
ARG VUE_APP_BACKEND
ENV VUE_APP_BACKEND $VUE_APP_BACKEND
RUN echo $VUE_APP_BACKEND
# Create app directory
RUN mkdir -p /app
WORKDIR /app
# Bundle app source
COPY package*.json ./
COPY .npmrc ./
RUN npm install
COPY . .
RUN npm run build
#production stage
FROM nginx:1.14.1-alpine as production-stage
COPY --from=build-stage /app/dist/spa /usr/share/nginx/html
COPY nginx.conf /etc/nginx/conf.d/default.conf
EXPOSE 80
CMD [ "nginx", "-g", "daemon off;" ]
i have the next problem:
i try to build a docker image with ros2, in which a code package is downloaded which will be built using the colcon build method.
but when I try to run last of. install / setup.bash doesn't work for me.
I already tried to put it in a script and copy it to the dockerfile but it didn't work
any ideas
here I leave the docker file
FROM osrf/ros:dashing-desktop
WORKDIR /home
COPY mobilidad.sh .
RUN bash mobilidad.sh
ENV ROS2_WS cleanmyway/test_ws
RUN mkdir -p ${ROS2_WS}/src/demo_py
COPY ./ ${ROS2_WS}/src/demo_py
WORKDIR ${ROS2_WS}
SHELL ["/bin/bash", "-c"]
RUN colcon build
RUN . install/setup.bash
note: mobilidad.sh is a script that dowload de code from github, this works fine
I think I managed to find a solution,
building the dockerfile as follows:
FROM osrf/ros:dashing-desktop
WORKDIR /home
COPY mobilidad.sh .
RUN bash mobilidad.sh
ENV ROS2_WS cleanmyway/test_ws
RUN mkdir -p ${ROS2_WS}
WORKDIR ${ROS2_WS}
RUN colcon build
RUN echo "source install/setup.bash" >> /opt/ros/dashing/setup.bash
and when i run the ros2 command works fine
but anyway thanks for the help. :)
note: i'm not sure if the better form but works for me
I have a dockerfile like this:
FROM python:3
ENV PYTHONUNBUFFERED 1
RUN mkdir /code
WORKDIR /code
ADD requirements.txt /code/
ADD reports /code/
RUN pip install -r requirements.txt
ADD . /code/
RUN ls -l /code/reports/report/manage.py # gives expected result
RUN ls -l /code/reports/build_static/ # gives expected result
RUN python /code/reports/report/manage.py build full_report.views.RenderView # does not work
Everything works fine except for the last command which runs a python package (django-bakery) through manage.py build. I don't get any errors.
This command should output some files inside build_static directory in the container.
If I ssh into the container and run the command manually then it is working. I inserted the full path with /code/ to make sure that they match and created all necessary directories beforehand.
This is how I build the container:
docker-compose run django /bin/bash
This is my docker-compose:
version: '3'
services:
django:
build: .
volumes:
- .:/code
ports:
- "8000:8000"
I wonder how come it is working when I run the command manually through bash inside the container, but not working with the command in the dockerfile.
Thanks!
Update (it seems that the files are created, but then if I check on them they aren't there):
Step 12/12 : RUN ls -l /code/reports/build_static/
---> Running in e294563d26d5
total 11080
-rw-r--r-- 1 root root 11339956 Apr 30 10:53 index.html
drwxr-xr-x 7 root root 4096 Apr 30 10:53 static
Removing intermediate container e294563d26d5
---> b8e72da8ee5c
Successfully built b8e72da8ee5c
Successfully tagged image_django:latest
WARNING: Image for service django was built because it did not already exist. To rebuild this image you must use `docker-compose build` or `docker-compose up --build`.
root#7483853ecc45:/code# ls -l reports/build_static/
total 0
Try following steps and let me know output:
FROM python:3
ENV PYTHONUNBUFFERED 1
RUN mkdir /code
WORKDIR /code
ADD requirements.txt /code/
ADD reports /code/
RUN pip install -r requirements.txt
ADD . /code/
RUN ls -l /code/reports/report/manage.py # gives expected result
RUN ls -l /code/reports/build_static/ # gives expected result
RUN python /code/reports/report/manage.py build full_report.views.RenderView
RUN ls -l /code/reports/build_static/ # should give you expected list of files
Give me output for the last step.
I'll help you out based on output.
The following Dockerfile, copies your current directory content to a code folder (if it doesn't exist, it creates it), then sets it as the workdir.
The WORKDIR instruction sets the working directory for any RUN, CMD,
ENTRYPOINT, COPY and ADD instructions that follow it in the Dockerfile
Then, in order to reduce you docker image size to the maximum, we regroup all your commands in one RUN command so that it reduces the number of layers.
FROM python:3
ENV PYTHONUNBUFFERED 1
COPY . /code
WORKDIR /code
RUN pip install -r requirements.txt && \
ls -l reports/report/manage.py && \
ls -l reports/build_static/ && \
python reports/report/manage.py build full_report.views.RenderView
I haven't tried it with a full Django app example, but it should help you narrow down the problem !