EXPOSE and CMD are ignored when running docker build

Viewed 1236

I'm trying to create a docker image by running docker build . The below is codes in my Dockerfile.

FROM node

WORKDIR /app

COPY . /app

RUN npm install

EXPOSE 80

CMD ["node", "server.js"]

but I only got the log up to the RUN command:

 => [internal] load build definition from Dockerfile                                                                   0.1s 
 => => transferring dockerfile: 128B                                                                                   0.0s 
 => [internal] load .dockerignore                                                                                      0.0s 
 => => transferring context: 2B                                                                                        0.0s 
 => [internal] load metadata for docker.io/library/node:latest                                                         0.0s 
 => [1/4] FROM docker.io/library/node                                                                                  0.0s 
 => [internal] load build context                                                                                      0.1s 
 => => transferring context: 14.62kB                                                                                   0.0s 
 => CACHED [2/4] WORKDIR /app                                                                                          0.0s 
 => [3/4] COPY . /app                                                                                                  0.1s 
 => [4/4] RUN npm install                                                                                              5.0s 
 => exporting to image                                                                                                 0.3s 
 => => exporting layers                                                                                                0.2s 
 => => writing image sha256:b7949d3a83e365d0c55690d4fd5c6d9f0e0e20fe88c53aebc3e33b067e600231                           0.0s

I'm expecting both EXPOSE and CMD work as well as the other 4 lines. Where should I check in order to do this? Thank you so much for your help!

2 Answers

EXPOSE doesn't really publish the port (highlighted is mine):

The EXPOSE instruction informs Docker that the container listens on the specified network ports at runtime. You can specify whether the port listens on TCP or UDP, and the default is TCP if the protocol is not specified.

The EXPOSE instruction does not actually publish the port. It functions as a type of documentation between the person who builds the image and the person who runs the container, about which ports are intended to be published. To actually publish the port when running the container, use the -p flag on docker run to publish and map one or more ports, or the -P flag to publish all exposed ports and map them to high-order ports.

When running the container, you should use the -p option in order to actually publish and map the port. For example:

docker run [params] -p 8080:9090 image

Buildkit doesn't log anything for those steps even though it still performed them. Most likely they elected to not log the step because there's no filesystem change that is being created, it's only some metadata that's adjusted. The result of the EXPOSE, CMD, along with other steps like ENTRYPOINT, LABEL, and ENV is a change to the image's json config.

This config is shipped with the image and tells the runtime the defaults for running the container. Many of these defaults may be overridden when creating the container, like the command and entrypoint. And specifically for the exposed port, it's documentation of what ports are listening by default inside the container.

To see the output from the classic build tooling, you can disable buildkit by setting an environment variable:

DOCKER_BUILDKIT=0 docker build ...
Related