Skip to content

Instantly share code, notes, and snippets.

@dev-sareno
Last active August 19, 2020 02:22
Show Gist options
  • Save dev-sareno/3aae5d8b57ab1bbd975f9ffdc61b7994 to your computer and use it in GitHub Desktop.
Save dev-sareno/3aae5d8b57ab1bbd975f9ffdc61b7994 to your computer and use it in GitHub Desktop.

Creating a Custom Docker Image and Pushing it to Docker Hub

Login to Docker Hub

$ docker login

Pull a Base Docker Image

$ docker run -ti -d --name my-custom-image ubuntu:latest bash
  • -ti - Enable Interactive and Terminal Typing
  • -d - Dettach to container after running
  • --name my-custom-image ubuntu:latest - Docker image definition --name <your-image-name> <image[:TAG]>
  • bash - Corollary to -ti

Check if Container is Running

$ docker ps

Note: Copy the CONTAINER ID for later use

Attach to Container's Terminal

$ docker exec -ti <CONTAINER-ID> bash

Once successfully accessed, you should see root@<CONTAINER-ID>:/#

Make Changes to Container

$ apt-get update
$ apt-get install htop
$ exit
  • apt-get update - download package information from all configured sources
  • apt-get install htop - Install a new package htop
  • exit - Exit the terminal

Commit The Changes

Documentation: https://docs.docker.com/engine/reference/commandline/commit/

$ docker commit <CONTAINER-ID> <image-name[:TAG]>

Note: This will create a new image with an additional layer.

Add Tag to Docker Image (Optional)

Documentation: https://docs.docker.com/engine/reference/commandline/tag/

Get the Image Id first
$ docker images

Note: See IMAGE ID.

Add Tag
$ docker tag <image-id> <my-custom-image:latest>
  • docker tag <image-id> <my-custom-image:latest> - Image tag definition docker tag <image-id> <image-name:TAG>

Push to Docker Hub

$ docker push dockerhubusername/my-custom-image

Reference/s

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