Ever thought those Docker container images that anyone can use are magical, and thought about making one but don’t know how? Come along, I’ll teach you 😉
To start, you’ll need a Docker Hub account. That’s where all Docker container images live. Once you’ve got your account there, we can move to the command line and start using the Docker CLI.
Before we do anything, we need a project right? The project we’re going to use here is in this GitHub repo. It’s just a Python script that adds two numbers passed on the command line.
Now that we have a project let’s start with the login. Even though you don’t need to login to build your images, once you’re logged in you can forget it exists:

Now let’s build our image:

Here we used the -t parameter to name our image. After building we can check the image in our list of locally available images:

Now that we have an image ready, let’s tag it and check that it was tagged correctly:

This step is basically what lets us distribute our images. So after tagging your image, it’s time to push (send the image to Docker Hub):

And done. Now the image has been sent to Docker Hub, making it possible for anyone to use docker pull to download and use the image:

Something extra
Doing all these steps manually can get tiring if you need to push a lot of images, and beyond that, anything we do by hand can be prone to errors. So the natural path would be to automate these tasks.
There are several ways to do this, but let’s use a shell script I called push.sh for it:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#!/bin/bash
if [ -z ${DOCKER_USERNAME} ]; then
echo "Missing DOCKER_USERNAME variable!"
exit 1
fi
error() {
if [ $? != 0 ]; then
echo "Error!"
exit 122
fi
}
build() {
echo "=> Building ${1}"
docker build -t ${1} .
echo "=> Built ${1}"
}
tag() {
echo "=> Tagging ${1}"
docker tag ${1} $(echo $DOCKER_USERNAME)/${1}
echo "=> Tagged ${1}"
}
push() {
echo "=> Pushing ${1}"
docker push $(echo $DOCKER_USERNAME)/${1}
echo "=> Pushed ${1}"
}
build ${1}
error
tag ${1}
error
push ${1}
error
echo
exit 0
In it I reproduced all the steps we did up to here, and to use it just do:

./push.sh sum You can even put this script in a continuous delivery tool and let the magic happen.
Links
The code and steps demonstrated here are in this GitHub repo, go there and leave a ⭐️
Recent Articles
english The most productive Saturday I've had all year thanks to Grok Bot
I built a six-bot Grok Bot squad on Friday night and spent Saturday shipping. Here's the team, what we closed, and how I kept the quota from vanishing.
english I built my AI squad and I had my most productive Saturday ever
Is this what the future of building with AI will look like?
english Starting a project from zero with AI: begin with the AGENTS.md
How I actually start a project with AI: the first file I write is not code, it is the AGENTS.md rules the agent reads every time. Grounded in my vid-compacter repo.