Home > Blockchain >  Docker compose with Dockerfile creates multiple images instead of one
Docker compose with Dockerfile creates multiple images instead of one

Time:03-20

I have a docker-compose.yml and Dockerfile in the same folder. When running docker compose build this should result in one image and one container, but somehow I'm left with two images and two containers. The same docker-compose.yml and Dockerfile on my desktop however results in one image. What is happening here?

docker-compose.yml

    version: '3.3'
    services:
        nginx-proxy: 
            build: .
            restart: always
            ports:
                - '80:80'
                - '443:443'
            volumes:
                - /etc/nginx/certs/domain.nl.crt:/etc/letsencrypt/live/domain.nl/cert.pem:ro
                - /etc/nginx/certs/domain.nl.key:/etc/letsencrypt/live/domain.nl/privkey.pem:ro
                - /var/run/docker.sock:/tmp/docker.sock:ro

Dockerfile:

FROM nginxproxy/nginx-proxy
EXPOSE 80
EXPOSE 443

The output of docker compose build looks completely different on the server and on my desktop too.

Running docker compose build Linux server:

Sending build context to Docker daemon     404B
Step 1/3 : FROM nginxproxy/nginx-proxy
latest: Pulling from nginxproxy/nginx-proxy
f7a1c6dad281: Pull complete
4d3e1d15534c: Pull complete
9ebb164bd1d8: Pull complete
59baa8b00c3c: Pull complete
a41ae70ab6b4: Pull complete
e3908122b958: Pull complete
3016ffcb703f: Pull complete
d0b58d19b229: Pull complete
e75e1b46ae51: Pull complete
a1b8f07fa83d: Pull complete
b1ff9eda0cc4: Pull complete
d334f8d44841: Pull complete
4f4fb700ef54: Pull complete
Digest: sha256:5e0be3b1bb035301c5bb4edbe0dee5bc0f133d26866e3165f1912acb126e15d4
Status: Downloaded newer image for nginxproxy/nginx-proxy:latest
 ---> 65d9e0769695
Step 2/3 : EXPOSE 80
 ---> Running in 4bdc00f75332
 ---> 0ca795ce85fb
Step 3/3 : EXPOSE 443
 ---> Running in 3530eba2d3cf
 ---> 9440ccab1790
Successfully built 9440ccab1790
Successfully tagged nginx-proxy_nginx-proxy:latest

Result:

docker image ls -a
REPOSITORY                TAG       IMAGE ID       CREATED          SIZE
<none>                    <none>    0ca795ce85fb   46 minutes ago   156MB
nginx-proxy_nginx-proxy   latest    9440ccab1790   46 minutes ago   156MB
nginxproxy/nginx-proxy    latest    65d9e0769695   4 days ago       156MB

docker container ls -a
CONTAINER ID   IMAGE          COMMAND                  CREATED          STATUS    PORTS     NAMES
3530eba2d3cf   0ca795ce85fb   "/app/docker-entrypo…"   25 seconds ago   Created             affectionate_vaughan
4bdc00f75332   65d9e0769695   "/app/docker-entrypo…"   25 seconds ago   Created             fervent_varahamihira

Running docker compose build Desktop:

[ ] Building 2.4s (5/5) FINISHED
 => [internal] load build definition from Dockerfile                                                                                         0.1s
 => => transferring dockerfile: 31B                                                                                                          0.0s
 => [internal] load .dockerignore                                                                                                            0.1s
 => => transferring context: 2B                                                                                                              0.0s
 => [internal] load metadata for docker.io/nginxproxy/nginx-proxy:latest                                                                     2.1s
 => CACHED [1/1] FROM docker.io/nginxproxy/nginx-proxy@sha256:5e0be3b1bb035301c5bb4edbe0dee5bc0f133d26866e3165f1912acb126e15d4               0.0s
 => exporting to image                                                                                                                       0.1s
 => => exporting layers                                                                                                                      0.0s
 => => writing image sha256:f66ed6ec69a319ce3a15b1f1948720bdd8174d13299513cc162a812616874793                                                 0.0s
 => => naming to docker.io/library/nginx-proxy_nginx-proxy

Result:

docker image ls -a
REPOSITORY                TAG       IMAGE ID       CREATED       SIZE
nginx-proxy_nginx-proxy   latest    f66ed6ec69a3   4 days ago    156MB

docker container ls -a
CONTAINER ID   IMAGE                    COMMAND         CREATED       STATUS       PORTS                                          NAMES

CodePudding user response:

You're looking at two different build tools. The classic docker build performs each step using a container that gets committed into an dangling image. For some of those changes, the container isn't even run, it's just created. These are visible in the container and image listings. Deleting them may delete your build cache which will force a rebuild of the entire image, and while they report the size of all their layers, those layers are shared with the final created image, so deleting the dangling images often won't save much space (maybe a few kb for some json metadata). Because of that, people would leave them around.

The other build is using buildkit, which runs directly on containerd and runc, so you don't see the build artifacts in the docker container and image list. This is the preferred builder and enabled by default on newer versions of docker.

  • Related