Sharing A Docker Image

Docker logo

I have created a Docker image and now it is time to share a Docker image. I created an image called geektechstuff_flask_hello and it is this image that I am going to share.

Log into Docker

Via a terminal or command prompt enter:

docker login

Docker may ask for your Docker account credentials at this point.

Logging into Docker

Tag The Image

As the image is going to be shared in a repository it will need tagging appropriately. To tag an image the following command is used:

docker tag image username/repository:tag

for me this is:

docker tag geektechstuff_flask_hello geektechstuff/flask:flask_hello

With this command I am recreating geektechstuff_flask_hello into a repository called geektechstuff/flask with the tag flask_hello.

Docker Repository Tagging


Push The Image To Docker

To push the image up to Docker the following command is used:

docker push username/repository:tag

for more example this is:

docker push geektechstuff/flask:flask_hello

Docker Push

The image is now in the Docker repository. You may be wondering why this is a good thing, hopefully this situation will explain:

  • You want to create a Python program in a test environment
  • You spend weeks creating the program and installing modules so that it runs
  • You then have to transplant the program from the test environment to a live environment

You could have to spend time reinstalling Python modules on the live system (hoping they don’t conflict with other versions of modules on the server) or resetting up your Python virtual environment on the live system OR you could do the whole thing in a Docker container, upload it from the test system and then pull it onto the live system.

Pull The Image From Docker

To pull and run the image from Docker the run command can be used.

docker run username/repository:tag

From my demostration this would be:

docker run -p 4000:80 geektechstuff/flask:flask_hello

I have used -p 4000:80 as I am mapping the containers port 80 to my devices port 4000. I have removed geektechstuff/flask:flask_hello from my device (using docker image rm -f  imageID) to show what this run command would look like on a new device/host.

Docker run & pull
Docker run & pull

As the image doesn’t exist locally (i.e. on the device) Docker goes and finds it from the repository, downloads it and then runs it.

Docker Running Container In Background

If you want to run a container in the background make sure to add -d to the command e.g. docker run -d -p 4000:80 geektechstuff/flask:flask_hello

Docker run -d
Docker run -d