Dockerfile
ALWAYS FOLLOWS THIS FORMAT: INSTRUCTION ARGUMENT
FROM
FROMInherits and sets Base Image (like Base class) or OS
eg.
python:3.63.6is the tag/version
RUN apt-get update
RUN apt-get updateRuns this linux command and downloads all OS package related updates in the container
COPY ./<your-directory> /<your-directory>
COPY ./<your-directory> /<your-directory>Copies local folder into the container's folder
i.e.
./appin local folder copies to container's/app
For local folder, the
.is the same directory as the Dockerfile
WORKDIR
WORKDIRStarts container in thie
workdirdirectoryi.e.
mkdir workdircd workdir
Note: These 2 ways are equivalent:
ENV
ENVSets environment variable (global variable in shell) in the container
Will do
export MYSQL_USER=ml
CMD
CMDCommand at startup of container (if there is another command in
docker run..., this command replaces the one in the Dockerfile)
ENTRYPOINT
ENTRYPOINTCommand at startup of container (if there is another command in
docker run..., this command is APPENDED to the one in the Dockerfile)
Using both CMD and ENTRYPOINT
CMD and ENTRYPOINTYou do this when you want a default command, but the argument can be changed
So here, if no parameter is specified in docker run... , then sleep 5 will run. Otherwise, sleep <argument> will run.
colon :
colon :This means port
If you do
google:80, just goes to google80 is the default port for websites
22 is the default port for ssh
gunicorn
gunicorn-w #Tells you number of CPU cores/threads (1 CPU core handles 1 request at a time)-b :#: bind to port # of container-t #: timeout in seconds
Formatting
These are all equivalent: Array: RUN/CMD/... [a, b, c, d] Shell: RUN/CMD/... a b c d
Running commands inside a Docker container
Caching
Caching when building Docker images can be very useful as it saves a lot of time when installing dependencies.
In what cases do Docker commands not use their cache?
If the contents that the command is applying itself on changes.
e.g.
COPY ./example /copy. If./exampleis changed in any way, this line in the Dockerfile will not use the cache.
If anything before the command is installed/changed that wasn't installed/changed before.
Running another program before starting container
Last updated
Was this helpful?