Dockerfile error when running apt-get install - docker

Im new to docker and tried a few different setups in my Dockerfile and it works great. Now when I try to use apt-get install inside my docker file (docker build .) it just breaks and I get the following error:
=> ERROR [ 6/14] RUN apt-get install -y php8.0-gd
------
> [ 6/14] RUN apt-get install -y php8.0-gd:
#11 0.213 Reading package lists...
#11 0.670 Building dependency tree...
#11 0.767 Reading state information...
#11 0.846 E: Unable to locate package php8.0-gd
#11 0.846 E: Couldn't find any package by glob 'php8.0-gd'
#11 0.846 E: Couldn't find any package by regex 'php8.0-gd'
I've tried to change to other apt-get install packages but same result no matter what package I try to install. I've also tried to install all packages in one RUN command, bud rewrote the code as shown below, to see if it was one specific package that crashed it.
Here is my code:
FROM php:8.0-apache-buster
RUN mkdir /workdir
WORKDIR /workdir
COPY . .
RUN apt-get update -y
RUN apt-get install -y php8.0-gd
RUN apt-get install -y php8.0-imagick
RUN apt-get install -y php8.0-pgsql
RUN apt-get install -y php-gettext
RUN apt-get install -y php8.0-curl
RUN apt-get install -y php8.0-soap
RUN apt-get install -y php-bcmath
RUN apt-get install -y language-pack-ko-base
RUN a2enmod rewrite
EXPOSE 80
CMD ["/usr/sbin/apache2ctl","-DFOREGROUND"]
I've searched and search but cannot find someone with the same problem. I am really new to Docker and I havn't figgured out if it is better to set this up in the docker-composer.yml file?

Please try this
FROM php:8.0-apache-buster
RUN mkdir /workdir
WORKDIR /workdir
COPY . .
RUN apt-get -y update && apt-get upgrade
RUN apt-get install -y lsb-release ca-certificates apt-transport-https software-properties-common
RUN apt-get install -y php8.0-gd php8.0-imagick php8.0-pgsql php-gettext php8.0-curl php8.0-soap php-bcmath language-pack-ko-base
RUN a2enmod rewrite
EXPOSE 80
CMD ["/usr/sbin/apache2ctl","-DFOREGROUND"]
Please refer this SO thread about using RUN multiple lines.

Related

Docker Compose: Apt-utils installation problem

I am trying to build a new image in Docker Compose but the following problem occurs
E: Failed to fetch http://archive.ubuntu.com/ubuntu/pool/main/a/apt/apt-utils_2.4.7_amd64.deb 404 Not Found [IP: ]
E: Unable to fetch some archives, maybe run apt-get update or try with --fix-missing?
The command '/bin/sh -c apt-get install -y apt-utils' returned a non-zero code: 100
ERROR: Service 'nginx-service' failed to build : Build failed
In my Dockerfile I'm running: RUN apt-get install -y apt-utils and with --fix-missing.
None of the related questions or other solutions helped me and I've been stuck for quite a while. What am I missing?
Thanks!
EDIT: The whole Dockerfile
FROM ubuntu:latest
RUN apt-get update
RUN apt-get upgrade -y
RUN apt-get install -y nginx
RUN apt-get clean
RUN apt-get install -y curl
RUN apt-get install -y unzip
RUN apt-get install -y wget
RUN DEBIAN_FRONTEND=noninteractive TZ=Etc/UTC apt-get -y install tzdata
RUN apt-get install -y php php-xml php-curl php-fpm php-mysql
RUN apt-get install -y apt-utils --fix-missing
RUN apt-get install -y php-zip --fix-missing
RUN apt-get install -y php-gd --fix-missing
RUN apt-get install -y mysql-server
RUN curl -sS https://getcomposer.org/installer -o composer-setup.php
RUN php composer-setup.php --install-dir=/usr/local/bin --filename=composer
RUN apt-get install -y nano
RUN apt-get install -y mc
RUN apt-get install -y systemctl
RUN systemctl start nginx.service
usually , when we are building a new image, we use update then install like this
RUN apt-get update && apt-get install -y apt-utils
This will update apt source list and will make the package available to install.
Addibg this to your Dockerfile should fix the issue.
If you want to install many at once you can add the packages like the following :
RUN apt-get update && apt-get install -y \
bzr \
cvs \
git \
mercurial \
subversion \
Building the image like this:
docker-compose build --no-cache
worked for me

Docker image size is coming up to 1.7 G for Ubuntu with Python packages

Following is my Dockerfile :-
FROM ubuntu:18.04 AS builder
RUN apt update -y
RUN apt install python3.8 -y && apt install python3-pip -y
RUN apt install build-essential automake pkg-config libtool libffi-dev libgmp-dev -y
RUN apt install libsecp256k1-dev -y
RUN apt install openjdk-8-jre -y
RUN apt install git -y
RUN apt install libkrb5-dev -y
RUN apt install vim -y
RUN mkdir /opt/app
RUN chown -R root:root /opt/app
COPY ["requirements.txt","/opt/app/requirements.txt"]
SHELL ["/bin/bash", "-c"]
WORKDIR /opt/app
RUN pip3 install -r requirements.txt && apt-get -y clean all
RUN mkdir /opt/app/
RUN chown -R root:root /opt/app/
RUN cd /opt/app/
RUN git clone -b master https://bitbucket.org/heroes/test.git
CMD ["bash","/opt/app/bin/connect.sh"]
Docker image is generating with an image file size of 1.7G. I need to have OpenJDK hence cannot use a standard python package as a base package. When I perform docker history , I can see 2 or 3 layers (installing packages above like Python3.8, OpenJDK and libsecp256k1-dev) taking up to 400MB to 500MB in size. Ubuntu as a base image takes only 64 MB however rest of size is taking by my dockerfile layers.
I believe I need to re-write the dockerfile in order to reduce the file size which I did but nothing happened concrete.
Please assist me on reducing the image less than 1 GB at least.
[Update]
Below is my updated Dockerfile:-
FROM ubuntu:18.04 AS builder
WORKDIR /opt/app
COPY requirements.txt /opt/app/aws/requirements.txt
RUN mkdir -p /opt/app/aws \
&& apt-get update -yq \
&& apt-get install -y python3.8 python3-pip openjdk-8-jre -yq && apt-get -y clean all \
&& chown -R root:root /opt/app && cd /opt/app/aws && pip3 install -r requirements.txt
FROM alpine
COPY --from=builder /opt/app /opt/app
SHELL ["/bin/bash", "-c"]
CMD ["bash","/opt/app/aws/bin/connector/connect.sh"]
Screenshot of image size:-
After removing unwanted libraries like git, etc and using the multi-stage build, the image is now approx 1.7 GB which I believe is a lot. Any suggestion to improve this?
You have multiple issues going on.
First, each of your RUN apt install is increasing your image size, you should have them all in the same RUN stage, and at the end of the stage, delete all cached apt files.
Second, you're installing unnecessary stuff. Why would you need vim and git for instance? Why are you installing build-essential and other build-related stuff if you're not building anything?
Third, it seems you tried to do a multi-stage build but ended up adding everything to the same image. Read up on python multi-stage builds.
If we consider best practices instead of multiple RUN use single RUN.
For example
RUN apt-get update -yq \
&& apt-get install -y python3-dev build-essential -yq \
&& apt-get install curl -yq \
&& pip install -r requirements.txt \
&& apt-get purge -y --auto-remove gcc python3-dev build-essential
you can use multistage builds if you don't require git in your final image you can remove in final stage
Also if possible you can use alpine version also.
Try disabling recommended packages of APT with --no-install-recommends, you can read more about it from here.
Now the image is smaller:
FROM ubuntu:18.04 AS builder
RUN apt update -y
RUN apt install python3-pip -y
RUN apt install build-essential automake pkg-config libtool libffi-dev libgmp-dev -y
RUN apt install libsecp256k1-dev -y
RUN apt install openjdk-8-jre-headless -y
RUN apt install git -y
RUN apt install libkrb5-dev -y
RUN apt install vim -y
RUN mkdir /opt/app
RUN chown -R root:root /opt/app
COPY ["requirements.txt","/opt/app/requirements.txt"]
SHELL ["/bin/bash", "-c"]
WORKDIR /opt/app
RUN pip3 install -r requirements.txt && apt-get -y clean all
RUN mkdir /opt/app/
RUN chown -R root:root /opt/app/
RUN cd /opt/app/
RUN git clone -b master https://bitbucket.org/heroes/test.git
CMD ["bash","/opt/app/bin/connect.sh"]

How to convert an Ubuntu package/repository to Alpine in a Dockerfile?

Currently I have this Dockerfile
FROM ubuntu:18.04
# https://github.com/tesseract-shadow/tesseract-ocr-re
RUN apt-get update && apt-get install -y software-properties-common && add-apt-repository -y ppa:alex-p/tesseract-ocr
RUN apt-get update && apt-get install -y tesseract-ocr-all
RUN apt-get install -y git build-essential cmake
RUN apt-get install -y ffmpeg
# Install Node and NPM
RUN apt-get install nodejs -y && apt-get install npm -y
The size of the image is too big so I searched for alternatives and found about Alpine.
I'm stuck with this one
FROM alpine
RUN apk add --update ffmpeg cmake nodejs npm
Looking at the aline edge repository, I can't seem to find tesseract-ocr-all and no idea how to do apt-get install -y software-properties-common && add-apt-repository -y ppa:alex-p/tesseract-ocr in alpine.
Are there any resources that can help me through this? Should I make my own Alpine image for those packages/repositories?
The alpine package name is tesseract-ocr, you can check here the releases or alpine repository.
FROM alpine
RUN apk add --update --no-cache ffmpeg cmake nodejs npm tesseract-ocr
If you are interested in the beta version you may check here.
Always try to add --no-cache option allows to not cache the index locally, which keeps containers small.

E: Unable to locate package in multistage Docker build

When I build just the main image, all the packages instead. But as soon as I turn it into a multi-stage build and it gets to RUN apt-get install -y python3-pip, I get "E: Unable to locate package in multistage Docker build"
FROM gcc:8.2.0 as builder
# FROM ownyourbits/debiandev:latest
RUN apt-get update
# RUN apt-get install -y libxerces-c-dev automake cmake libboost-all-dev build-essential
RUN apt-get install -y libxerces-c-dev automake cmake libboost-all-dev build-essential
RUN git clone https://github.com/mypackage/mypackage-d.git
WORKDIR /mypackage-d/
RUN autoreconf -if
RUN ./configure --enable-silent-rules 'CFLAGS=-g -O0 -w' 'CXXFLAGS=-g -O0 -w' 'LDFLAGS=-g -O0 -w'
RUN make
RUN make install
RUN ls .
# Main Image
FROM library/python:3.7-stretch
COPY --from=builder /mypackage-d/mypackaged.bin /mypackage-d
RUN apt-get update
RUN apt-get install -y python3
RUN apt-get install -y python3-pip
RUN apt-get install -y postgresql-client
RUN apt-get install -y libxerces-c-dev
# For VIM
RUN apt-get install -y apt-file
RUN apt-file update
RUN apt-get install -y vim
RUN pip install --upgrade pip
COPY requirements.txt /
RUN pip3 install --trusted-host pypi.org -r /requirements.txt
WORKDIR /code
ENTRYPOINT ["/bin/bash", "start.sh"]
Moving the COPY --from=builder command below the apt-get install and pip install statements worked for me.

Dockerfile error: /bin/sh: 1: [“python”,: not found

here is my Dockerfile tried to build:
FROM ubuntu:latest
# install flask server
RUN apt-get update -y
RUN apt-get install -y python-pip python-dev build-essential
COPY app.py /
RUN pip install flask
# install ruby
RUN \
apt-get install -y ruby ruby-dev ruby-bundler && \
rm -rf /var/lib/apt/lists/*
# install lua
RUN apt-get update -y && apt-get install -y luajit luarocks
# Define default command.
CMD [“python”, “app.py”]
However, it shown up with error
/bin/sh: 1: [“python”,: not found
I have no idea why this happened. Could someone please help me with it?
Make sure to use the right CMD syntax with "", not “”:
CMD ["executable","param1","param2"] (exec form, this is the preferred form)

Resources