Episode 13: Sharing Images and Containers (Push-Pull in DockerHub)
→ So far we have seen the advantage of using docker i.e. no need to install packages local codebase, docker can install in their container and we can run our application.
→ The second benefit is we can share images or containers with our team and reuse them.
This can be achieved in two ways:
a. Share Dockerfile plus app code:
→ Dockerfile is the raw code where we execute commands and create images and containers.
→ This will be little work for recipients, as they need to run commands
b. Sharing Docker Images Directly:
→ If we directly share images, no code sharing is required recipient users can create Docker containers instantly.
Image = App Code + environment (i.e. all packages)
→ Sharing of images can be done in two ways:
- Docker Hub: https://hub.docker.com/
- Private Repositories (any other docker container managing application)
DockerHub
It is mostly used docker repository (we may think of this as GitHub 😄)
→ We need to do the basic signup and login as shown in the screenshot below. Click Sign in
→ Enter your username. Click Continue.
→ Enter your password. Click Continue
→ We are now logged in and landed on the DockerHub homepage
→ In previous episodes, we have seen, how to search the official image name (such as node, python) to use in our Dockerfile.
→ Their signup was not required. We directly got from search
→ Now we have to push in a repository. For that let us create a repository. Click Repositories.
→ Click Create Repository
→ Here we need to choose repository type — can be public or private
Public repository: Visible to everyone
Private repository: Visible to only the creator
→ Let us select a public type repository. Write repository name (say node-product). Click Create button.
→ Our repository name is:
<username>/<repository-name>eg. amirengg15/node-product
→ Copy the command for pushing (one in black box)
docker push amirengg15/node-product:tagname
A. Pushing image in Docker Hub:
→ Let us go to our project.
Important: For pushing in the DockerHub, we need to create the same name docker image.
docker build -t <username>/<repository-name> .eg. docker build -t amirengg15/node-product
→ Image is created successfully.
→ If we try to use the push command we copied above. We will get authentication error (which is mandatory)
Docker Login:
→ We need to log in first using the below command and try again. It aske
docker login
→ Let us run the same command again
docker build -t amirengg15/node-product
→ Pushing is successful. Let us check the Docker Hub repository.
B. Pulling image from Docker Hub:
→ Once the sender has pushed in DockerHub. They will share repository details with the user.
→ Copy-paste pull command from the repository (one in black box).
→ Let us first delete the image from local project and try pulling.
Deleting image:
docker rmi <username>/<image-name>:<tagname>eg. docker rmi amirengg15/node-product:v1
→ Pulling command:
docker pull <image-name>:tageg. docker pull amirengg15/node-product:v1
→ Image is successfully pulled
NOTE: It is good practice to always give tags to the container. We can see more about tagging here.
Creating containers from the pulled image:
docker run -p 3000:80 -rm amirengg15/node-product:v1
Running our Application:
http://localhost:3000
Video:
Closing Thoughts:
In this article, we have learnt ways we can share docker images. We have seen lots of concepts throughout this journey.
If you have come this far, it means you should have a good understanding of the docker core. From the next videos, we will understand data management and working with volumes.
Thank you for reading till the end 🙌 . If you enjoyed this article or learned something new, support me by clicking the share button below to reach more people and/or give me a follow on Twitter and subscribe Happy Learnings !! to see some other tips, articles, and things I learn about and share there.