How to add copy files to Docker image - docker

I'm running a python script to manipulate pictures. When I run the test the system obviously does not find the images. I new to docker and don't really understand how to do that.
This is how the structure looks
And this is the dockerfile
FROM ubuntu:latest
RUN apt update
RUN apt install python3 -y
RUN apt-get -y install python3-pip
RUN pip install pillow
RUN pip install wand
RUN DEBIAN_FRONTEND="noninteractive" apt-get install libmagickwand-dev --no-install-recommends -y
WORKDIR /usr/app/src
COPY image_converter.py ./
COPY test_image_converter.py ./
RUN python3 -m unittest test_image_converter.py

COPY image_converter.py ./
COPY test_image_converter.py ./
here you've COPIED ****.py file to ./(== WORKDIR)
so, in the same way copy your image files to your WORKDIR
such as
COPY test_images ./
This should work

IDK if this will help, but try to look its content.
https://www.geeksforgeeks.org/copying-files-to-and-from-docker-containers/

First, you didn't copy the image folder, should have these commands:
WORKDIR /usr/app/src
COPY test_images ./
Second, you should combine same type of Docker commands (to reduce layers, hence reduce docker image's size)
example : instead of saying
COPY image_converter.py ./
COPY test_image_converter.py ./
You can write
COPY *.py ./
Another example :
RUN apt update
RUN apt install python3 -y
You can write
RUN apt update; apt install python3 -y

Related

Workflow for building python wheels in a multistage dockerfile with pipenv

In order to keep final docker image small, my usual approach to building python projects with binary dependencies is to build the pinned dependencies in a first stage and copy them to a final stage lacking the building toolchains. Broadly:
FROM python:3 as builder
RUN apt-get install -y libfoo-dev libbar-dev
COPY constraints.txt /
RUN pip wheel \
--constraint /constraints.txt \
--wheel-dir /wheels \
python-foo pyBar
FROM python:3-slim
RUN apt-get install -y libfoo libbar
COPY requirements.txt constraints.txt /
COPY --from=builder /wheels /wheels
RUN pip install \
--requirement /requirements.txt \
--constraint /constraints.txt \
--only-binary :all: \
--find-links /wheels
Now I am trying to something similar on a project managed with pipenv and I am quite astray on how to achieve the same effect: pre-building the few projects that lack a public wheel in a first stage for the version pinned in the lockfile, and use them in a later pipenv install --deploy in the final stage.
Does this even make sense with the hash checking pipenv does? Is there any alternative to reduce the final image size? I'd like to avoid using a private index where to store prebuilt wheels, I'd rather keep the solution contained in the Dockerfile.
Related question How to make lightweight docker image for python app with pipenv
A solution is to install a full virtualenv and copy it, not only some wheels.
FROM python:3 as builder
RUN apt-get install -y libfoo-dev libbar-dev
RUN pip install pipenv
WORKDIR /app
COPY Pipfile* /app
RUN mkdir /app/.venv
RUN pipenv install --deploy
FROM python:3-slim
RUN apt-get install -y libfoo libbar
WORKDIR /app
COPY --from=builder /app/.venv /app/.venv
ENV PATH=/app/.venv/bin:$PATH

Is it possible create a base image from a file?

repo with a few services and in each service, I have the following base code:
FROM python:3.8.13-slim-bullseye
WORKDIR /usr/app
RUN apt-get update
RUN apt-get install default-libmysqlclient-dev build-essential -y
RUN python -m pip install --upgrade pip
RUN pip install pipenv setuptools
This is a little slow to rebuild each time, and sometimes I need to drop all images, so the idea is to know if is possible, to create Dockerfile as a base image and import this from another docker file in order to build these steps only one time locally.
Thanks

Creating a dockerfile for a .deb file

I want to create a dockerfile for a debian file extension which runs on ubuntu 18.04. So far I've written this
FROM ubuntu:18.04 AS ubuntu
RUN apt-get update
WORKDIR /Downloads/invisily
RUN apt-get install ./invisily.deb
All phases run fine except the last one. It shows this error:
E: Unsupported file ./invisily.deb given on commandline
The command '/bin/sh -c apt-get install ./invisily.deb' returned a non-zero code: 100
I'm new to docker and cloud so any help would be appreciated thanks!
Edit:
I solved it by putting the dockerfile and the debian file in the same directory and using COPY . ./
This is what my dockerfile looks like now:
FROM ubuntu:18.04 AS ubuntu
RUN apt-get update
WORKDIR /invisily
COPY . ./
USER root
RUN chmod +x a.deb && \
apt-get install a.deb
A few things,
WORKDIR is the working directory inside of your container.
You will need to copy the file invisily.deb from locally to your container when building your Docker image.
You can pass multiple bash commands in the RUN field combining them with multilines.
Try something like this
FROM ubuntu:18.04 AS ubuntu
WORKDIR /opt/invisily
#Drop the invisily.deb in to the same directory as your Dockerfile
#This will copy it from local to your container, inside of /opt/invisily dir
COPY invisily.deb .
RUN apt-get update && \
chmod +x invisily.deb && \
apt-get install invisily.deb
in your WORKDIR there isn't any invisly.deb file, so if you have it you can copy it the container like this:
FROM ubuntu ...
WORKDIR /Downloads/invisily
RUN apt-get update
COPY ./your invisly file path ./
RUN chmod +x ./invisily
RUN apt-get install ./invisily.deb

Docker image Package Patch within Dockerfile

I have below docker image, where I need to update patch to curl package, in below Docker image, in Line number 3 I am already doing update, but it is shown up in Vulnerabilities report.
I have added, RUN yum -y update curl at the end of Dockerfile, then it is not showing up in Vulnerabilities report.
Any fix?, All Packages must install with latest version, I dont want to be mention explicitly
or any mistakes in Dockerfile?
FROM centos:7 AS base
FROM base AS build
# Install all dependenticies
RUN yum -y update \
&& yum install -y openssl-devel bzip2-devel libffi-devel \
zlib-devel wget gcc make
# Below compile python from source
FROM base
ENV LD_LIBRARY_PATH=/usr/local/lib64:/usr/local/lib
COPY --from=build /usr/local/ /usr/local/
# Copy Code
COPY . /app/
WORKDIR /app
#Install code dependecies.
RUN /usr/local/bin/python -m pip install --upgrade pip \
&& pip install -r requirements.txt
# Why, I need this step, when I already update RUN in line 3?, If I won't perform I see in compliance report, any fix?
RUN yum -y update curl
# run Application
ENTRYPOINT ["python"]
CMD ["test.py"]
In order to understand what constitutes an image, you need to look at a Dockerfile in a different way:
Every step (with the exception of FROM) creates a new image, with the results of the previous step as a base.
FROM doesn't use the previous step, but an explicitly specified one.
Now, looking at your Dockerfile, you seem to wonder why RUN yum -y update curl doesn't work as expected. For easier understanding, let's trace it backwards:
RUN yum -y update curl
RUN /usr/local/bin/python -m pip install --upgrade pip \ && pip install -r requirements.txt
WORKDIR /app
COPY . /app/
COPY --from=build /usr/local/ /usr/local/
ENV LD_LIBRARY_PATH=/usr/local/lib64:/usr/local/lib
FROM base -- at this point, the previous step is changed to the last step of base
FROM centos:7 AS base -- here, the previous step is changed to centos:7
As you see, nowhere in the earlier steps is yum update -y curl!
BTW: Typing this, I'm wondering what your precise question is, i.e. whether this works or doesn't or whether you wonder why it's necessary. Are you aware of the difference between yum update and yum update curl even?
docker build and friends have a cache system, based on the text of the input. So if the text of the command yum -y update doesn't change, it will continue using the same cached version of the output forever (or until the cache is deleted). Try running the build with --no-cache and see if that helps.

Install ffmpeg package inside docker container

I am trying to install ffmpeg package inside dockerfile, but after that ffmpeg package was not installed.
Here is my dockerfile:
FROM ubuntu:18.04
FROM maven:3.6.0-jdk-11-slim AS build
COPY src /home/app/src
COPY pom.xml /home/app
RUN mvn -f /home/app/pom.xml clean package -DskipTests
RUN apt-get update -y && apt-get install -y ffmpeg
FROM openjdk:11-jdk
COPY --from=build /home/app/target/car-1.0.jar /home/car.jar
EXPOSE 8080:8080
ENTRYPOINT ["java","-jar","/home/car.jar"]
If I enter inside docker container and run command:
apt-get update -y && apt-get install -y ffmpeg
package was successfuly installed and works.
With upper Dockerfile after finished build I typed:
whereis ffmpeg
and it was empty.
I also tried with static ffmpeg inside Dockerfile, but not works https://hub.docker.com/r/mwader/static-ffmpeg/
COPY --from=mwader/static-ffmpeg:4.3.1 /ffmpeg /usr/local/bin/
COPY --from=mwader/static-ffmpeg:4.3.1 /ffprobe /usr/local/bin/

Resources