How to optimize a docker image?

I am building a webapp with several docker images via docker-compose.
One of the Docker images is webserver_web.
The size of the image (~1.2 GB) looks pretty big to me and, I wonder what tricks I can apply to reduce the size.
I suspect that I’m not using the Dockerfile efficiently.
For example, almost half of the image size (510 MB) is coming from the need to install sudo, vim, and nano into the container:

RUN apt-get update && apt-get install -y vim nano sudo

The webserver_web image is using FROM python:3.7.6 for the first layer.
The size of the python 3.7.6 image is 919MB
Why don’t I see this size in docker history webserver_web? (the first layer in the history is 114MB)

docker images
REPOSITORY                   TAG                 IMAGE ID            CREATED             SIZE
webserver_web                latest              ec306049da3e        15 minutes ago      1.11GB
python                       3.7.6               879165535a54        3 months ago        919MB

Also, I tried to build the image from scratch, by deleting the related containers and the webserver_web image, just to start fresh, but I still see in the history layers from 4 months ago.
How can I completely remove the layers of the image and start completely fresh?

Here are the Dockerfile, and the layers of the docker image.
Any ideas what can be done to reduce the size of the webserver_web Docker image?
Is size of more than 1GB acceptable for a docker image?


The Dockerfile:

cat Dockerfile
FROM python:3.7.6
MAINTAINER User4 <[email protected]>

# Create the group and user to be used in this container
RUN groupadd flaskgroup && useradd -m -g flaskgroup -s /bin/bash flask

RUN apt-get update && apt-get install -y vim nano sudo

# Add user flask to sudoers
RUN echo "flask ALL=(root) NOPASSWD:ALL" > /etc/sudoers.d/flask && 
    chmod 0440 /etc/sudoers.d/flask

# Create the working directory (and set it as the working directory)
RUN mkdir -p /home/flask/app/web
WORKDIR /home/flask/app/web

# Install the package dependencies (this step is separated
# from copying all the source code to avoid having to
# re-install all python packages defined in requirements.txt
# whenever any source code change is made)
COPY requirements.txt /home/flask/app/web
RUN pip install --no-cache-dir -r requirements.txt

# Copy the source code into the container
COPY . /home/flask/app/web

RUN chown -R flask:flaskgroup /home/flask

USER flask

Layers of the docker image (with size for each layer):

[email protected]:~$ docker history webserver_web
IMAGE               CREATED              CREATED BY                                      SIZE                COMMENT
0688e195012d        About a minute ago   /bin/sh -c #(nop)  USER flask                   0B                  
3052d2e91fd0        About a minute ago   /bin/sh -c chown -R flask:flaskgroup /home/f…   35.3MB              
81a5cf0a37b9        About a minute ago   /bin/sh -c #(nop) COPY dir:668861ff8c7f55004…   35.3MB              
24341ee304ae        About a minute ago   /bin/sh -c pip install --no-cache-dir -r req…   64.2MB              
925e12e74fe8        About a minute ago   /bin/sh -c #(nop) COPY file:4e7a69dabf52b355…   1.07kB              
0419bbb589d3        About a minute ago   /bin/sh -c #(nop) WORKDIR /home/flask/app/web   0B                  
b87691531f4a        About a minute ago   /bin/sh -c mkdir -p /home/flask/app/web         0B                  
251001545b6f        About a minute ago   /bin/sh -c echo "flask ALL=(root) NOPASSWD:A…   30B                 
980fc4c0dd9c        About a minute ago   /bin/sh -c apt-get update && apt-get install…   58.2MB              
9015bd3d1e3b        2 minutes ago        /bin/sh -c groupadd flaskgroup && useradd -m…   334kB               
7ad23eec9ab4        2 minutes ago        /bin/sh -c #(nop)  MAINTAINER User4 <[email protected]…   0B                  
879165535a54        3 months ago         /bin/sh -c #(nop)  CMD ["python3"]              0B                  
<missing>           3 months ago         /bin/sh -c set -ex;   wget -O "$P…   6.34MB              
<missing>           3 months ago         /bin/sh -c #(nop)  ENV PYTHON_GET_PIP_SHA256…   0B                  
<missing>           3 months ago         /bin/sh -c #(nop)  ENV PYTHON_GET_PIP_URL=ht…   0B                  
<missing>           4 months ago         /bin/sh -c #(nop)  ENV PYTHON_PIP_VERSION=20…   0B                  
<missing>           4 months ago         /bin/sh -c cd /usr/local/bin  && ln -s idle3…   32B                 
<missing>           4 months ago         /bin/sh -c set -ex   && wget -O python.tar.x…   91MB                
<missing>           4 months ago         /bin/sh -c #(nop)  ENV PYTHON_VERSION=3.7.6     0B                  
<missing>           4 months ago         /bin/sh -c #(nop)  ENV GPG_KEY=0D96DF4D4110E…   0B                  
<missing>           4 months ago         /bin/sh -c apt-get update && apt-get install…   17.9MB              
<missing>           4 months ago         /bin/sh -c #(nop)  ENV LANG=C.UTF-8             0B                  
<missing>           4 months ago         /bin/sh -c #(nop)  ENV PATH=/usr/local/bin:/…   0B                  
<missing>           4 months ago         /bin/sh -c set -ex;  apt-get update;  apt-ge…   510MB               
<missing>           4 months ago         /bin/sh -c apt-get update && apt-get install…   145MB               
<missing>           4 months ago         /bin/sh -c set -ex;  if ! command -v gpg > /…   17.5MB              
<missing>           4 months ago         /bin/sh -c apt-get update && apt-get install…   16.5MB              
<missing>           4 months ago         /bin/sh -c #(nop)  CMD ["bash"]                 0B                  
<missing>           4 months ago         /bin/sh -c #(nop) ADD file:a5ec219cbfc4e0c31…   114MB 

Source: StackOverflow