Building Images
docker build -t ImageName:TagName <context-directory>
| Element | Meaning |
|---|---|
-t |
Tags the image with a name and optional tag |
ImageName |
The name given to the image |
TagName |
The version label; defaults to latest if omitted |
| context | Directory sent to the daemon; . means the current directory |
docker build -t mongodbimage:1.0 .
Because the Dockerfile sits in the working directory, . is used to denote it. Use -f when the
Dockerfile lives elsewhere or has a non-default name.
Creating an image manually (and why you shouldn't)
It is instructive to build an image by hand once, because it shows what docker build automates.
Start a base container, copy an artifact in, and commit the result:
docker run -dit openjdk:8-jdk-alpine
docker ps # note the generated container name
docker container exec <container> ls /tmp
docker container cp build/libs/hello-world-rest-api.jar <container>:/tmp
docker container exec <container> ls /tmp
# commit the container's current state as a new image
docker container commit <container> pravat/hello-world-rest-api:manual1
# commit, and set the default command at the same time
docker container commit \
--change='CMD ["java","-jar","/tmp/hello-world-rest-api.jar"]' \
<container> pravat/hello-world-rest-api:manual2
docker run -p 8080:8080 pravat/hello-world-rest-api:manual2
This works, but it is not reproducible — nothing records how the image was made. Use a Dockerfile
for anything that matters. docker commit is best reserved for capturing the state of a container
mid-debug.