Diving into Docker: Exploring Containerization and Docker Hub for Seamless Development
Table of contents
- What is Docker?
- Benefits of using Docker
- Docker terminologies
- Installation of Docker
- Generating Docker Images
- Some helpful Docker commands
What is Docker?
Docker is a technology that helps developers overcome the “it works on my machine” problem by enabling them to create, ship, and operate applications inside isolated environments known as containers. Through speeding deployment, increasing effectiveness, and promoting cooperation across the software development lifecycle, this cutting-edge solution maintains consistency across diverse environments.
Benefits of using Docker?
Reproducibility: Docker containers are reproducible, so you can be sure that an application will function the same on any computer to which it is installed. This is because each container holds all of the components, such as the operating system, libraries, and application code that an application needs to function.
Cost savings: By utilizing fewer resources and by making it simpler to distribute and deploy programs, Docker can assist to lower the cost of running applications.
Security: The ability to segregate Docker containers from one another helps shield programs from security flaws.
Efficiency: Because Docker containers are lightweight, they are more resource-efficient than conventional virtual computers. They are therefore perfect for use with mobile or cloud-based apps.
Docker Terminologies
Container: A small, independent, executable software package that contains all of the components required to operate a piece of software, such as the code, runtime, libraries, and system utilities.
Image: A filesystem that includes all the code, libraries, runtime, and dependencies needed to run an application. Containers are made with the use of images.
Dockerfile: A text file containing a set of instructions for building a Docker image. It defines the base image, environment variables, application code, and configurations.
Repository: A collection of Docker images with the same name, distinguished by their tags. For example, the repository might be “ubuntu” and the tags could be versions like “18.04”, and “20.04”, this is where images are pulled from
Containerization: The process of packaging an application and its dependencies together in a single container, making it easy to deploy and run consistently across different environments
Installation of Docker
Feel free to visit Docker’s website to download the software and simply follow the provided instructions tailored to your specific operating system, via https://www.docker.com/get-started , an image below will appear.
Generating Docker Images
Let us Dockerize a static website using NGINX
Step 1: Set Up Your Project Directory, my-website
Step 2: Inside the my-website directory, create an index.html file, paste the content below
<!DOCTYPE html>
<html>
<head>
<title>My Dockerized Website</title>
</head>
<body>
<h1>Hello, Docker with NGINX!</h1>
</body>
</html>
Step 3: Create a Dockerfile
FROM nginx:latest
COPY html /usr/share/nginx/html
Step 4: Build and Run the Docker Container
i. Open a terminal in the project directory.
ii. Build the Docker image using the following command:
docker build -t docker-nginx .
iii. Run the Docker container
docker run --name docker-nginx -p 80:80 docker-nginx
Step 5: Access the Website, in your browser, and paste the link http://localhost You should see the “Hello, Docker with NGINX!” message(image below).
iv. The running container can be viewed in Docker Desktop, click on Containers.
or in your terminal, type the below command
docker ps # To see the containers running
v. To stop the container from running, you can use Docker Desktop, by clicking on delete
or in your terminal, type the below command docker stop <container_name _or_id>, in our case, we type the below command
docker stop docker-nginx # Stops the container<docker-nginx> from running
Some Helpful Docker Commands
docker pull <image_name> # Pull an image from a Docker Hub
docker build -t <image_name> <path_to_dockerfile> # Build an image from a Dockerfile.
docker run -d --name <container_name> # Create and start a container from an image
docker images # List available Docker images.
docker rm <container name> # To remove a container
Happy coding!