What is Docker
Docker is an open platform that provides the ability to package and run an application in a loosely isolated environment called a container. The isolation and security lets you run many containers simultaneously on a given host. Containers are lightweight and contain everything needed to run the application, so you don't need to rely on what's installed on the host. You can share containers while you work, and be sure that everyone you share with gets the same container that works in the same way.
What is Docker Hub
Docker Hub is a service provided by Docker for finding and sharing container images. It's the world’s largest repository of container images with an array of content sources including container community developers, open source projects, and independent software vendors (ISV) building and distributing their code in containers.
Having understood what Docker, container and Docker Hub brings to the table, you can now move on to what it takes to build, Push and deploy docker images.
Prerequisite
DOCKER HUB ACCOUNT: You can follow the steps here to create your own if you don't have one
VIRTUAL SERVER: Launch a server (Ubuntu) of version (22.04). Note you can use any Linux Distro of your choice.
HARDWARE REQUIREMENTS FOR THE SERVER : It should be minimum of 1vCPU (Recommended), 1GB (Recommended) of RAM for memory and Port 22 (SSH), 80 (HTTP) should be opened in your security group.
When all this are set, you can now proceed to the getting our hands dirty.
STEP 1: INSTALLING DOCKER
Here you would be installing Docker on your virtual machine. But before that you need to make sure that the server apt
repository for Docker is updated.
- Installing docker: Create a
docker.sh
file to automate the installation of docker.sudo vim docker.sh
, Then paste this commands.
# Add Docker's official GPG key:
sudo apt-get update -y
sudo apt-get install ca-certificates curl
sudo install -m 0755 -d /etc/apt/keyrings
sudo curl -fsSL https://download.docker.com/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.asc
sudo chmod a+r /etc/apt/keyrings/docker.asc
# Add the repository to Apt sources:
echo \
"deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/ubuntu \
$(. /etc/os-release && echo "$VERSION_CODENAME") stable" | \
sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
sudo apt-get update -y
# Adding Ubuntu user to the docker group. Replace ubuntu with the current user
sudo usermod -aG docker ubuntu
sudo su - ubuntu
sudo apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin -y
# Verify that the Docker Engine installation
sudo docker run hello-world
Now, make the
docker.sh
script executable to enable faster installation process using this command.sudo chmod +x docker.sh
Then you can now run the
docker.sh
script using the./docker.sh
command and check docker version usingdocker --version
STEP 2: CREATE A CUSTOM DOCKER IMAGE
Here, you are going to be creating a custom docker image (Nginx) by first creating a Dockerfile. sudo vim Dockerfile
, Paste this custom Nginx Dockerfile.
# Use the official Nginx base image from the Docker Hub
FROM nginx:latest
# Expose port 80 to the host
EXPOSE 80
# Start Nginx when the container launches
CMD ["nginx", "-g", "daemon off;"]
- Since the Dockerfile is created successfully, you can go ahead to building our custom docker image to be pushed to Docker Hub.
docker build -t my-nginx .
- You can now run
docker images
to view the created docker image.
STEP 3: PUSHING THE DOCKER IMAGE TO DOCKER HUB
Since you have a Docker Hub Account [If you don't have one, you can createhere], Start pushing the custom docker image to your Docker Hub Account. But before that there is need to be authenticated using your Docker Hub Login credentials like this,
docker login -u <dockerHub-username>
You will be prompted to enter your Docker Hub password. Go to your Docker Hub account and create a PAT (Personal Access Token) and enter it.
Note: Create a PAT and choose read & write to enable you to Push.
- Add a Tag to your docker image to correspond to your Docker Hub
docker tag dockerHub-username/docker-image-name:tag-name
and Push custom docker image to your Docker Hub Account.docker push dockerHub-username/docker-image-name:tag-name
- The Docker image was recently pushed to Docker Hub Registry.
STEP 4: REDEPLOYING THE DOCKER IMAGE
- Note: To successfully pull the image from Docker Hub, you must first delete the initially tagged image in your environment.
You can easily pull the custom docker image from Docker Hub to be able to reuse it using
docker pull <dockerHub-username/image-name:tag-name>
To deploy the pulled custom docker image, run this command.
docker run -d -p 80:80 <dockerHub-username/image-name:tag-name>
- You can view your running docker container (Nginx) from the web browser using
http://server-PublicIP:80
Thank you for following through to the end, I hope you were able to BUILD, PUSH A CUSTOM DOCKER IMAGE TO DOCKER HUB AND REDEPLOY IT.
References: