Skip to content

Instantly share code, notes, and snippets.

@wdhowe
Created November 28, 2020 19:36
Show Gist options
  • Save wdhowe/fd58191bfc81f5eeca854201f24624ec to your computer and use it in GitHub Desktop.
Save wdhowe/fd58191bfc81f5eeca854201f24624ec to your computer and use it in GitHub Desktop.
Docker commands to build, push, and run containers.

Docker Image Running/Building Examples

The docker images can all be run/built similar to the following.

Variables

Setup the variables per image.

GitHub Example

registry="docker.io/"
image_path="myuser/"
image_name="myimage"
tag_name="latest"
image="${registry}${image_path}${image_name}:${tag_name}"

GitLab Example

registry="mygitlab.example.com:4567/"
image_path="mygroup/myproject/"
image_name="myimage"
tag_name="latest"
image="${registry}${image_path}${image_name}:${tag_name}"

Run the image

To run an image manually from the command line:

docker run -it --rm --name testing ${image} /bin/sh

To run an image and mount your current working directry into the image for use (at /mnt)

docker run -it --rm --name testing -v ${PWD}:/mnt ${image} /bin/sh

How to Build a Docker Image Manually

  • Build image using a Dockerfile (in the current directory)
docker build -t ${image_name}:${tag_name} .
  • Run a test container from the built image to verify
docker run -it --rm --name testing ${image_name}:${tag_name} /bin/sh
  • Tag the local image in preparation for a remote registry push
docker tag ${image_name}:${tag_name} ${image}
  • Login to the remote registry (prompted for registry username/password)
docker login https://${registry}
  • Push to the remote registry
docker push ${image}
  • Optional: Additional tag latest and push
docker tag ${image_name}:${tag_name} ${registry}${image_path}${image_name}:latest
docker push ${registry}${image_path}${image_name}:latest

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment