Guide.
Note that Dockerfile doesn’t have a file extension, simply create a file named “Dockerfile” in the same folder that package.json
lives in.
The commands in a Dockerfile are almost identical to their Linux counterparts.
Step by step
Start with your Base using the syntax: FROM baseImage:tag
Ex:
FROM python:3.12
Next need to set up the file system within the container, this depends on the use case but a general example would look like this:
# set a directory for the app
WORKDIR /usr/src/app
# copy all the files to the container
COPY . .
Best to do this when you’re in your app’s root. If there are files that you don’t want to copy such as node_modules
folder (by God it’s big) include them in a .dockerignore
file just like we do with .gitignore
.
Now to install your app’s dependencies.
# install dependencies
RUN pip install --no-cache-dir -r requirements.txt
If your app runs on a specific port, then expose that port.
EXPOSE 9000
To set ENV variables for the container to use.
ENV API_KEY=VALUE
In case you don’t want the app to run as root user
# Setup an app user so the container doesn't run as the root user
RUN useradd app
USER app
The last step is to write the command that runs the app in the container using CMD ["executable","param1","param2" ...]
. For a Python app it’s something like
CMD ["python", "./app.py", "37"]
With that, your Dockerfile is ready and you can build the Image.
Building the Image
There’s plenty of docs on how to build here. For simple stuff use the command `docker build -t username/appname .
The -t
option is used to specify the name and tag of the Image.