Video in progress
What do the FROM, WORKDIR, COPY, RUN and CMD instructions in a Dockerfile do?
FROM picks the base image, WORKDIR the working directory, COPY copies files into the image, RUN executes a command at build time, and CMD says what runs when the container starts.
📖 Answer
FROM eclipse-temurin:21-jre # base: Java 21 runtime
WORKDIR /app # the next commands run in this directory
COPY target/shop-api.jar app.jar # jar on the host -> into the image
RUN mkdir -p /app/tmp # runs once, at build time
EXPOSE 8080 # documentation only: the app listens on this port
CMD ["java", "-jar", "app.jar"] # when the container starts
The most important difference is between RUN and CMD. RUN executes while the image is being built, and its result stays as a new layer. CMD sits in the image as a "reminder" and is executed every time you do docker run.
EXPOSE does not open the port by itself. It is only metadata. Publishing a port to the outside is done with docker run -p.
Sign up to mark studied questions and save your progress.