The post discusses how to build and push docker images on local docker system to the docker hub repository. For the purpose of this post, we will pull a CentOS image from the public Repository in Docker hub. Later we will run a container using this image and add a new file to the container. This new container will then be pushed to the Docker hub as a new docker image.
Building a Docker Image
1. Pull the latest version of the CentOS image from the Docker Hub.
# docker pull centos Using default tag: latest latest: Pulling from library/centos Digest: sha256:4565fe2dd7f4770e825d4bd9c761a81b26e49cc9e3c9631c58cfc3188be9505a Status: Image is up to date for centos:latest
# docker images REPOSITORY TAG IMAGE ID CREATED SIZE centos latest d123f4e55e12 2 weeks ago 197MB
2. Once the CentOS Image is downloaded, we will run docker container based on this image with the name “centos_test”.
# docker run -it --name="centos_test" centos:latest /bin/bash [root@e121d03b20dc /]#
3. Now lets create a new directory in the container “test_dir” with a file in it as “test_file”. Also add some random text in the test_file.
[root@e121d03b20dc /]# mkdir test_dir [root@e121d03b20dc /]# cd test_dir [root@e121d03b20dc test_dir]# echo "This is a sample text" > test_file [root@e121d03b20dc test_dir]# cat test_file This is a sample text [root@e121d03b20dc test_dir]# ls -lrt total 4 -rw-r--r--. 1 root root 22 Nov 19 16:12 test_file
4. Next step is to build the new image with the docker commit command using the newly created docker container. The ‘docker commit’ command is run from docker host and not from the docker container itself.
# docker commit -m="This a test image" centos_test geeklab/test_repo sha256:8a34a37712626012e995e3b02e2179bd38fb646cfc9ceaa30b15a4774d198f67
-m=”This a test image” : is a Commit message.
centos_test : Name of the container from which you are creating the image.
geeklab/test_repo: Name of the Docker Hub repository where you want to push the image. This is the tag taht will be assigned to the new image created.
5. After the above command is run, you would see the new image “centos_image” in the list of docker images available locally on the system.
# docker images REPOSITORY TAG IMAGE ID CREATED SIZE geeklab/test_repo latest 8a34a3771262 About a minute ago 197MB centos latest d123f4e55e12 2 weeks ago 197MB
Pushing the docker image to docker hub repository
1. Next step is login into the docker hub repository you want to push the image to.
# 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: geeklab Password: Login Succeeded
2. Now you can push the image to the docker hub repository.
# docker push geeklab/test_repo The push refers to a repository [docker.io/geeklab/test_repo] 3dbbbdcf3ef9: Pushed cf516324493c: Pushed latest: digest: sha256:c542e22fe3f29f809226d72b6c6d8efd0bec46c2924d66ad8fbb5612972740fe size: 736
3. Now, we’ll login to the Docker Hub and verify the image in Repositories.