Creating an image without dockerfile

  • Even though the conventional way to create a docker image would be through a dockerfile, it is possible to create an image from an existing container.
  • Basically the image will be a snapshot of the container along with changes that have been made to it.
  • Let’s pull latest Alpine to start with:
1
docker pull alpine:latest
  • Create a container from the Alpine:latest image, name it chan-alpine and start a bash shell into the container
1
docker container run -it --name chan-alpine alpine:latest /bin/sh
  • Container shows up in the list of started containers minimal-api
  • Let’s install git on this container by running the command below on the shell:
1
apk add git

minimal-api

  • Now that we have a container running alpine and git installed, let’s create an image of this set up using docker container commit and passing it the name of the alpine container and a name for the resulting image which we will call chan-alpine-image
1
docker container commit chan-alpine chan-alpine-image
  • Listing the images now show our custom image: minimal-api

  • At this stage, let’s create a container from our custom image and confirm that it comes pre-installed with git by simple running the git CLI:

1
docker container run chan-alpine-image git

And the output shows the help menu of the Git CLI: minimal-api