Docker by Example: Container Registries
Container registries like Docker Hub allow you to download containers and share your own containers with others. Push images to the registry to make them available.
web-app container. If you haven't done that, do it now before continuing.I've mentioned Docker Hub (https://hub.docker.com/) in previous tutorials, and we've pulled container images from there for Python, NodeJS and PostgreSQL to do the previous tutorials.
Docker Hub is one example of a container registry. Container registry is a bit of a misnomer, because really it is an image registry, but I didn't come up with the term. In a container registry, container images can be pushed and pulled, updated and tagged, and shared with others.
Exploring Docker Hub
Let's explore a bit what's available on Docker Hub. Go to https://hub.docker.com and scroll down to see various lists of container images that you can download. Check out the categories on the left side of the page, including databases, web servers, data science, and others. If you click on a container's page, it will take you to documentation for how to run that image.
If you find something you'd like to try, give it a try. Otherwise, We'll start up a favorite application of mine: Ghost (https://ghost.org/), the software that runs this website.
Running an application
Ghost is website/blog software that I use to run several websites because it looks good, and is relatively easy to set up. You can pay $30/month to host your blog at ghost.org, or you can be a cool tech person and run it yourself. Ghost is open source software, so anybody can host their own for free.
Visit https://hub.docker.com/_/ghost to see the documentation for the official Ghost image. You'll notice the command listed to run the image is (Don't run it yet!)
docker run -d --name some-ghost -e NODE_ENV=development ghost
Notice any problems with this? This is a website that you'll be running on your machine. To connect to it, you'll want to pull up your browser and go to http://localhost:2368 (2368 is Ghost's default port).
The problem is, this doesn't publish the ports, so you won't actually be able to get to the website. To run it for reals, run:
docker run -d --name some-ghost -p 2368:2368 -e NODE_ENV=development ghost:6.8.0-alpine
Notice we made a few changes - (1) we published the port so we can go to http://localhost:2368 now, and (2) we've specified the image tag we want to use. It's good practice to be specific about the tag so that the software version you're using doesn't change without your permission.
Now you can go to http://localhost:2368/ghost to set up your local website. And just like that you have an application ready to go. Near the bottom of the documentation page, you can find an example Docker Compose file for running the site in production mode.
Pushing to a registry
Container registries are not just for companies to publish their software - you can use them, too. Most container registries allow you to push both public and private containers, with limited usage for a free tier. Docker Hub, for example, allows you to have 1 private Docker Hub repo (set of images) for free. Other popular container registries include Amazon's ECR, Github's container registry, Microsoft's Azure Container Registry, and probably even more that I don't know about.
If you are building and deploying applications, often you'll push the image to one of these container registries, then pull onto your server from there. This is a similar process to packaging up and deploying your code, but with the added benefit that you're also setting up the entire deployment environment through the configuration of your Docker image.
We're going to push a container to Docker Hub, but the process is basically the same for any of these registries. You'll need to (1) Log in to the registry, (2) create the repo on the registry, (3) push the image to the registry.
Step 1: Log in
You'll need to create a Docker Hub account to push your image. Once you have the account, in the terminal you can run the following command, replacing YOUR-USER-NAME with your actual Docker username:
docker login YOUR-USER-NAMEStep 2: Create the repository
Once you're logged in on the Docker Hub website, go to https://hub.docker.com/repositories/. Click the "Create a repository" button. For the Repository Name, call it web-app, and choose Public visibility.
Step 3: Tag and push your image
Now we need to tag the image for pushing to Docker Hub. The following command will take your local web-app:v1 image and tag it as YOUR-USER-NAME/web-app:latest, the default tag for the latest image.
docker tag web-app:v1 YOUR-USER-NAME/web-appOne more command and we're there:
docker push YOUR-USER-NAME/web-appNow anybody can go to https://hub.docker.com/r/YOUR-USER-NAME/web-app, or type
docker run -p 3000:3000 YOUR-USER-NAME/web-appto run your application.