Two ways to create an image
Commit changes made to a container (but not pushed yet to dockerhub.com)
- Example: Create Debian Linux base with git installed
- $ docker run -it debian:jessie (-it = interactive so get terminal)
- /# apt-get update && apt-get install -y git (apt-get is package installer like npm)
- Commit changes to your DockerHub account
- $ docker commit <cont_ID> <user_Name>/<image_Name>
- $ docker commit [OPTIONS] <cont_ID> [REPOSITORY/img_name:TAG
-m, --message string Commit message
From a Dockerfile
- Dockerfile is merely a text file w/ instructions for building an image
- Dockerfile must start with capital “D” (or end in .dockerfile)
- Great tutorial on Dockerfile commands by Digital Ocean
- Example: Dockerfile
- Dockerfile is merely a text file w/ instructions for building an image
FROM debian:jessie
RUN apt-get update
RUN apt-get install -y git
RUN apt-get install -y vim
Build image from Dockerfile
- $ docker build -t <image_name> <Dockerfile relative location . = cwd>
$ docker build -t dougwells/debian . → . = cwd (works if Dockerfile is in cwd)
$ docker build -t dougwells/debian Dropbox/1-Code/Docker/DockerBasics2
- Since image built with Dockerfile, no version #. Instead tagged w/”latest”
- Docker images → dougwells/debian:latest
- Avoid temptation to use :latest. Rather, give version #
- Since image built with Dockerfile, no version #. Instead tagged w/”latest”
- Rename image, use $ docker tag
$ docker tag <image_id> <new_image_name>:tag
- Remember, try to avoid leaving off tag (defaults to “latest”)
- But, if building new image with this image as a layer, if your existing layer in cache already has “latest”, it won’t update b/c docker thinks you have the latest (which you might or might not)
Image Naming cautions:
- Build manually (ie, $ docker build -t dougwells/debian .)
- Image name = Image name given
- Example node or dougwells/node
- Build image locally with docker-compose.yml
- Image name = <folder of docker-compose.yml>_<service name given in docker-compose-yml>
- Build image in CircleCI - (built from github repo)
- Image name = <gh repo name (with dashes [-]removed) >_<service name given in docker-compose.yml
- Build manually (ie, $ docker build -t dougwells/debian .)
Pushing image to DockerHub.com (James Lee)
- Make sure image has format <DockerHub_Username>/<image_Name>
- If not, rename it using “docker tag” (‘tagged” w/ a name)
- $ docker tag <current_img_id> <new_img_name>:tag
- Login to Docker Hub from CLI
- $ docker login --username=dougwells -- or --
- docker login
- Then push to Docker Hub
- $ docker push <repository_name>/<image_name>:tag
- Example: $ docker push dougwells/debian:1.01
- Make sure image has format <DockerHub_Username>/<image_Name>
Publishing images to Docker Hub (Dan Wahlin)
- $ docker login → must be logged in first
- $ docker push <your username>/<image-name>
- Must use this format <username>/<image-name> or will not push
- If need to rename image to follow naming convention
- docker tag <image_id> <new_image_name>:tag
Remember, try to avoid leaving off tag (defaults to “latest”)
But, if building new image with this image as a layer, if your existing layer in cache already has “latest”, it won’t update b/c docker thinks you have the latest (which you might or might not)
- $ docker pull <filename>
- $ docker run <filename> → (docker will download image if not on machine yet)