Dockerfile 👷🏼♂️👷🏼♀️
Small "program" to create an image. Run with...
docker build -t [name of result] .
The . at the end is important! It specifies the location of the dockerfile
Result will be added to your local registry
Each step in the dockerfile produces a new image. You don't want large files to span lines, or your image will be huge. Best not to span lines with large images.
Caching - docker skips lines that have not changed since the last build, e.g. if your first line is "download latest file" it might not always run.
Tip:- The parts that change the most belong at the end of the Dockerfile
Note: dockerfiles are not shell scripts - they look like shell scripts, but they aren't
Processes do not run across lines. only moves onto the next line when the process for the previous line is finished.
Environment variables you set will be set on the next line - if you use the ENV command, remember that each line is its own call to docker run
Building a dockerfile
The most Basic Dockerfile
FROM busybox
RUN echo "building simple docker image."
CMD echo "Hello Container"
Can build for an image already built from a previous dockerfile. Thereby chaining dockerfiles together.
FROM
The FROM statement - must always be the first command (ith the exception of ARG) in your file (can have more than one FROM)
MAINTAINER
defines the author of this docker file
RUN
run a command through the shell
ADD
adds local files, add contents from a tar archive
ADD run.sh /run.sh
ADD project.tar.gz /install/
uncompresses the tar file into the image's install folder
ADD https://project.example.com/download/1.0/rpm /project
- works with URLs too!
COPY
(same as ADD, but without the auto extraction of archives, also doesn't have the URL functionality)
ENV
sets environment variables during the build and when running the result
ENV DB_HOST = db.production.example.com
ENTRYPOINT
specifies the start of the command to run (e.g. ls & user can add options)
makes your containers look like command line programs
CMD
specifies the whole of the command to run. There are two types
Type | Example | Notes |
---|---|---|
Shell form | nano notes.txt | is run through a shell (such as bash) |
Exec form | ["/bin/nano", "notes.txt"] | is run directly - so slightly more efficient. |
EXPOSE
maps a port into the container
EXPOSE 8080
- is the same as -p 8080:8080
VOLUME
VOLUME ["/host/path/" "/container/path/"]
VOLUME ["/shared-data"]
avoid defining shared folders in Dockerfiles
VOLUME
means that this docker image will only work on your host
WORKDIR
sets the directory the container starts in
USER
sets which user the container will run as
Check out the dockerfile reference on docker.com