Views: 22

Creating a Dockerfile
Usual steps of creating a Dockerfile of your application:
- Specify Base Image
- Specify any environment variables
- Specify commands to run on the top of OS layer such as:
- downloading or updating dependencies
- add groups or users to run process instead of root
- Copy the application files into the image.
- Specify which port(s) for container to listen
- Specify the command to start the application
Dockerfile Instructions | Arguments |
FROM | Base Image or other Container Image |
RUN | 1. Passing commands (runs in shell) 2. Passing exec form: [“executable”, “p1”, “p2”] Execute command in a new layer. Examples: – Updating dependencies |
COPY | COPY is preferred to ADD as we know clearly that COPY will just copy files from local directory. ADD is useful if you wanna copy the content in a tar file. |
ENTRYPOINT | Define the command that will always be executed when the container starts. By default, Docker is using /bin/sh -c as the entrypoint. |
CMD | Define the arguments that will be appended to the ENTRYPOINT |
WORKDIR | Define where your commands should run. Saves the trouble of running to many cd ..... |
EXPOSE | Define the ports that a container listens. |
ENV | Setting environment variables that can be used in other instructions such as RUN |
FROM node:12-alpine
RUN apk add --no-cache python2 g++ make
EXPOSE 9091
WORKDIR /app
COPY . .
RUN yarn install --production
CMD ["node", "src/index.js"]
References:
- https://docs.docker.com/develop/develop-images/dockerfile_best-practices/#dockerfile-instructions
- https://docs.docker.com/engine/reference/builder/#ru
- https://stackoverflow.com/questions/21553353/what-is-the-difference-between-cmd-and-entrypoint-in-a-dockerfile
Running a container
Make sure the port publish setting (
https://stackoverflow.com/questions/66316400/cannot-reach-docker-container-port-not-bound-p
) is positioned before the image tag so that it can override the image default settings.
docker run -p 9091:9091 -t spring-boot-docker
References:
Be First to Comment