(This post assumes you have a DockerHub account.)
In the previous post, we built a Docker image. Now, we're going to "push", or upload it to DockerHub, and also see how to run it in a container!
DockerHub
DockerHub is a library for container images. By pushing our image (with an appropriate tag), we can make our image available to others (or keep it private if we so require), or use the same image on different systems.
Before we can push our image, we need to give a proper name to it. This name will include our DockerHub username, the image name, and a version tag.
Consider a local image named "python-helloworld". We run the following command.
docker tag python-helloworld importhuman/python-helloworld:v1.0.0
Let's break this down. With the docker tag
command, we take our local image, python-helloworld
, and give it the name importhuman/python-helloworld:v1.0.0
. Here,
-
importhuman
is the name of my DockerHub account. This is needed to decide which account the image is pushed to. Replace this with your account name. -
python-helloworld
is the image I want to push. This is also the name that will be given to the DockerHub repository. Replace this with the name you gave your image (see previous post in this series). -
v1.0.0
is the tag name. Tags are used to version images.
Now, if we run docker images
, we should see both our local image and the newly tagged image.
(If you haven't logged in to DockerHub in the terminal, run docker login
, and input your username and password.)
To push the image, we simply run
docker push importhuman/python-helloworld:v1.0.0
and we should have a new repository on DockerHub with our newly tagged image!
To pull this image to our system, we would simply run
docker pull importhuman/python-helloworld:v1.0.0
Running the image
Let's see the command to run our image in a container, and then we'll break it down.
docker run -d -p 8080:5000 importhuman/python-helloworld:v1.0.0
-
-d
runs the container in "detached mode". This essentially starts the container while leaving you free to run other commands in the terminal window. Without this flag (foreground mode), the terminal attaches to the process’s standard input, output, and standard error. (Source: Docker) -
-p 8080:5000
specifies that host port 8080 is connected to container port 5000. The container port here is 5000 as it is the default port exposed by Flask. Thus, if we open "localhost:8080" in a browser, we'll see the expected "Hello World!" output. -
importhuman/python-helloworld:v1.0.0
is the image we want to run in a container (a local image can be used rather than the tagged version as well).
(For more information on the above explanation, or on running images in Docker containers, please see here)
To see more information about currently running containers, run
docker ps
To stop a running container, run the command
docker stop <container-id>
OR
docker stop <container-name>
Congratulations! You now know how to
- tag Docker images
- push and pull Docker images from DockerHub
- run Docker images in containers
Please feel free to ask any questions you have in the comments or on Twitter.
Top comments (0)