Docker hub sign up and pull image from docker hub & push customize image to run the container

Barun Kumar
5 min readDec 22, 2020

Sign up with docker hub account- by using personal ID (Gmail/outlook/yahoo..etc or by using corporate email ID if in case you are setting up docker hub account for enterprise solution)

Step 1: Click on link to sign up docker hub account

Once you are able to create docker hub account then login to docker engine server.

In my scenario, I have a server with docker installed.

[root@ip-172-10-206-10 ~]# docker version
Client:
Version: 1.13.1
API version: 1.26
Package version: docker-1.13.1-203.git0be3e21.el7.centos.x86_64
Go version: go1.10.3
Git commit: 0be3e21/1.13.1
Built: Thu Nov 12 15:11:46 2020
OS/Arch: linux/amd64
Server:
Version: 1.13.1
API version: 1.26 (minimum version 1.12)
Package version: docker-1.13.1-203.git0be3e21.el7.centos.x86_64
Go version: go1.10.3
Git commit: 0be3e21/1.13.1
Built: Thu Nov 12 15:11:46 2020
OS/Arch: linux/amd64
Experimental: false

Step 2: Now login to docker hub account by executing “docker login” command, it will prompt for username and password, use the one which you have used to sign up docker hub account, by authorizing this

[root@ip-172-10-206-10 ~]# docker login
Login with your Docker ID to push and pull images from Docker Hub. If you don’t have a Docker ID, head over to https://hub.docker.com to create one.
Username: XXXXX
Password: XXXXX
Login Succeeded

Step 3: Pull docker image from docker hub #docker pull nginx

Note: Image name will be available on docker hub

[root@ip-172-10-206-10 ~]# docker pull nginx
Using default tag: latest
Trying to pull repository docker.io/library/nginx ...
latest: Pulling from docker.io/library/nginx
6ec7b7d162b2: Pull complete
cb420a90068e: Pull complete
2766c0bf2b07: Pull complete
e05167b6a99d: Pull complete
70ac9d795e79: Pull complete
Digest: sha256:4cf620a5c81390ee209398ecc18e5fb9dd0f5155cd82adcbae532fec94006fb9
Status: Downloaded newer image for docker.io/nginx:latest

Step 4: Check the downloaded Image # sudo docker images

[root@ip-172-10-206-10 ~]# docker images 
REPOSITORY TAG IMAGE ID CREATED SIZE
docker.io/nginx latest ae2feff98a0c 5 days ago 133 MB

Step 5: Run application in container using downloaded docker image.

[root@ip-172-10-206-10 ~]# docker run --name app1 -p 80:8080 -d nginx
8adde23ed9adf9490e54b591d2db5005841edbf9fdf0047972049313148cfa61
[root@ip-172-10-206-10 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b83dec6acdf4 nginx "/docker-entrypoin..." 18 seconds ago Up 17 seconds 80/tcp, 0.0.0.0:80->8080/tcp app1

Find the container ID #docker ps -a -q

[root@ip-172-10-206-10 ~]# docker ps -a -q
8adde23ed9ad

Step 6: Login to app1 container # docker exec -it /bin/sh

[root@ip-172-10-206-10 ~]# docker exec -it app1 /bin/sh# ls 
bin docker-entrypoint.d home media proc sbin tmp
boot docker-entrypoint.sh lib mnt root srv usr
dev etc lib64 opt run sys var
# ls -la
total 12
drwxr-xr-x. 1 root root 39 Dec 16 12:16 .
drwxr-xr-x. 1 root root 39 Dec 16 12:16 ..
-rwxr-xr-x. 1 root root 0 Dec 16 12:16 .dockerenv
drwxr-xr-x. 2 root root 4096 Dec 9 23:22 bin
drwxr-xr-x. 2 root root 6 Nov 22 12:37 boot
drwxr-xr-x. 5 root root 340 Dec 16 12:16 dev
drwxr-xr-x. 1 root root 41 Dec 15 20:20 docker-entrypoint.d
-rwxrwxr-x. 1 root root 1202 Dec 15 20:20 docker-entrypoint.sh
drwxr-xr-x. 1 root root 19 Dec 16 12:16 etc
drwxr-xr-x. 2 root root 6 Nov 22 12:37 home
drwxr-xr-x. 1 root root 56 Dec 15 20:20 lib
drwxr-xr-x. 2 root root 34 Dec 9 23:22 lib64
drwxr-xr-x. 2 root root 6 Dec 9 23:22 media
drwxr-xr-x. 2 root root 6 Dec 9 23:22 mnt
drwxr-xr-x. 2 root root 6 Dec 9 23:22 opt
dr-xr-xr-x. 104 root root 0 Dec 16 12:16 proc
drwx------. 2 root root 37 Dec 9 23:22 root
drwxr-xr-x. 1 root root 38 Dec 16 12:16 run
drwxr-xr-x. 2 root root 4096 Dec 9 23:22 sbin
drwxr-xr-x. 2 root root 6 Dec 9 23:22 srv
dr-xr-xr-x. 13 root root 0 Dec 16 10:38 sys
drwxrwxrwt. 1 root root 6 Dec 15 20:20 tmp
drwxr-xr-x. 1 root root 66 Dec 9 23:22 usr
drwxr-xr-x. 1 root root 19 Dec 9 23:22 var

Now, Create an image by downloading image form docker hub and upload modified image to image repository (docker hub).

Step 1: Create a folder called ‘nginx-image’ and create a file called ‘Dockerfile’ under folder ‘nginx-image’ and copy the customize ‘index.html’

[root@ip-172-10-206-10 nginx-image]# cat Dockerfile
FROM nginx
COPY index.html /usr/share/nginx/html

Step 2: Create an index.html file under ‘nginx-image’ folder

[root@ip-172-10-206-10 nginx-image]#cat index.htmlWelcome to custom docker image 

Step3: Build customize docker image

# docker build -t <docker hub acc ID>/<image name>:<image version tag> .

. = represent the current working directory

[root@ip-172-10-206-10 nginx-image]# docker build -t baruniitt06/mynginx_image:v1 .
Sending build context to Docker daemon 3.072 kB
Step 1/2 : FROM nginx
— -> ae2feff98a0c
Step 2/2 : COPY index.html /usr/share/nginx/html
— -> 2cca3b23bb83
Removing intermediate container e38fc009532c
Successfully built 2cca3b23bb83

Validate the custom image

[root@ip-172-10-206-10 nginx-image]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
baruniitt06/mynginx_image v1 2cca3b23bb83 About an hour ago 133 MB
docker.io/nginx latest ae2feff98a0c 5 days ago 133 MB

Step 4: Create a container using customize docker image

[root@ip-172-10-206-10 nginx-image]# docker run — name mynginx_app1 -p 80:80 -d baruniitt06/mynginx_image:v1
42ee05fffc493b02163b5211a1e29f3caa096682811f7e74a6b5ad5b37dd863e

validate newly created container

[root@ip-172-10-206-10 nginx-image]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
42ee05fffc49 baruniitt06/mynginx_image:v1 “/docker-entrypoin…” 3 minutes ago Up 3 minutes 0.0.0.0:80->80/tcp mynginx_app1

Step 5: Tag and push customize docker image to docker hub

List out available image

[root@ip-172-10-206-10 nginx-image]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
baruniitt06/mynginx_image v1 2cca3b23bb83 About an hour ago 133 MB

Tag the image

#docker tag SOURCE_IMAGE[:TAG] TARGET_IMAGE[:TAG]

[root@ip-172-10-206-10 nginx-image]# docker tag baruniitt06/mynginx_image:v1 baruniitt06/mynginx_image:v1-release[root@ip-172–10–206–10 nginx-image]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
baruniitt06/mynginx_image v1 2cca3b23bb83 About an hour ago 133 MB
baruniitt06/mynginx_image v1-release 2cca3b23bb83 About an hour ago 133 MB

Push image to docker hub

before executing ‘docker push command — make sure to login docker hub account’

[root@ip-172-10-206-10 nginx-image]# docker login
Login with your Docker ID to push and pull images from Docker Hub. If you don’t have a Docker ID, head over to https://hub.docker.com to create one.
Username: baruniitt06
Password:
Login Succeeded
[root@ip-172-10-206-10 nginx-image]# docker push baruniitt06/mynginx_image:v1-release
The push refers to a repository [docker.io/baruniitt06/mynginx_image]
fcc677e0ac81: Pushed
4eaf0ea085df: Pushed
2c7498eef94a: Pushed
7d2b207c2679: Pushed
5c4e5adc71a8: Pushed
87c8a1d8f54f: Pushed
v1-release: digest: sha256:9fb5e02d8e191dc5adbafcbb0374fac3859f113fccc99c0a6abac66d73ada66b size: 1569

Docker command cheat sheet

  1. docker ps — List all running container
  2. docker ps -a — List all stop and running container
  3. docker start/stop/restart container-id — to start/stop/restart container
  4. docker port container id — List port mapped to the container
  5. docker rm conatiner-id/name — delete the stopped container
  6. docker rm -f container-d/name — delete the running container
  7. docker pull image-info — pull the image from docker hub repo
  8. docker exec -it container -name /bin/sh — to login to the container
  9. docker rm image-id — remove the docker image
  10. docker login — to login to docker docker hub account
  11. docker version — display the docker version
  12. docker logout — to logout from docker hub account
  13. docker top container-id/name — display the running process of container
  14. docker kill conatiner-id/$(docker ps -q) — kill container
  15. docker rm $(docker ps -a -q) — remove all stopped container

--

--