Docker Magic: From First Image to Publishing on Docker Hub!

Learn Docker Commands Today and Deploy Tomorrow

Hello Connections! ?

Welcome back to my blog. I’m Malarvizhi, a Cybersecurity Professional with a strong passion for DevSecOps and web application security. In my last blog we talked about the role of docker between DevOps and DevSecOps. Today, let’s explore key Docker concepts and commands. By the end of this post, you’ll learn how to:

  • Define essential Docker concepts like containers, images, Docker Daemon, Docker Client, and Docker Hub.
  • Work with foundational Docker commands.
  • Use Play with Docker for hands-on practice.
  • Run a static site and create your very first Docker image.
Image from Unsplash

Docker: It is a platform to test, develop, ship and run applications inside a lightweight containers. To test an application, we can simply use the ID of the container to run and it will start in a second.

Image: An Image is a blueprint used for creating containers. It contains everything needed to run an application: the application code, libraries, dependencies, configurations, and the base operating system.

Assume like, image is a architecture for the house, where they have complete template/blueprint to build a container.

Container: It is a unit that package an application and its dependencies, includes libraries, configurations or binaries. It ensures consistent behavior across environment and makes flawless for development, testing, and deployment.

Assume like, it is a house (container), where you have all the furniture (dependencies) and extra stuff already in place. No need to buy anything separately.

Image serves as basis for the container when its run. As a result, the container is isolated and runs as a version of the image.

Docker Daemon: The Docker Daemon (often referred to as dockerd) is a background service that runs on a Docker host (the machine where Docker is installed) and is responsible for managing containers. Daemon handles the entire Docker lifecycle, from creating and running containers to handling images and networks.

We can communicates with Docker CLI (Command Line Interface) commands and performs the actual work of starting, stopping, and managing containers.

Docker Client: Nothing much but a command-line tool used to interact with the Docker Daemon through commands like docker run or docker pull.

Docker Hub: It is a centralized repository, where we can share and find container images.

SetUp: Utilize Play with Docker Lab (PWD)

Why I’m using Play with Docker? — It is a separate lab where I can access it via browser itself. No need to download Docker Desktop or anything in your system. You can visit Play with Docker website (a browser-based lab to test Docker commands), give start, and there you go, a command line page appears to play around.

Bonus tip: You will have totally 3 hours for each session to test your skills and it is cost free. Below is the link :)

Play with Docker

Let’s know the Docker Commands

  1. Docker pull

Purpose: Pull an image from Docker Hub.

Example: I’m pulling a nginx image from the docker hub.

docker pull nginx

It’ll pull the nginx from the hub and completes in a second. Now I want to check the pulled image and what are all the other images I have. Well, let’s check.

2. Docker Image

Purpose: It will show all the images that you currently have on the local system.

docker images

Image is here, then let’s build a container. How?

3. Docker run

Purpose: Create and start a container from a pulled image.

docker run -d -p 8080:80 nginx

docker : CLI command line tool.

run : tells docker to create and start a new container.

-d (detached mode) : This flag runs the container in the background (detached mode), so it doesn’t lock up your terminal. Without this flag, the container logs would display in the terminal.

-p 8080:80 (port mapping) : This maps the container’s port 80 (which nginx uses by default) to the host machine’s port 8080.

  • 8080: The port on the host machine.
  • 80: The port inside the container where nginx is running.
    This means you can access the nginx server on http://localhost:8080 (or your host IP address and port).

nginx : Specifies the Docker image to use.

If the nginx image is not already present on your machine, Docker will pull it from the Docker Hub (Docker’s default registry).

Note: In the top of the lab, nect to open port you can see the port 8080 just popped up, click on that and there you go, the nginx page :)

4. Docker ps

Purpose: Show running containers.

docker ps

5. Docker stop

Purpose: Stop a running container

docker stop <container_id>

once you stop, try to check “docker ps” command, you will not able to see the running container

OK, let’s have a little more bit fun :)

Run a Static site and create your very first Docker image

Let’s try running a basic web server that serves static files.

  1. Here I’m using the same nginx web-server. So no need to pull again.
  2. Run a container and map a local directory to serve your site:
docker run -d -p 8080:80 -v $(pwd):/usr/share/nginx/html nginx

Important : give your port as any 4 digit number except 8080, as we already used 8080 to view the nginx webpage. Everytime you give run command along with host machine port, change the post number and execute.

docker run -d -p 8081:80 -v $(pwd):/usr/share/nginx/html nginx

here, the command starts an nginx web server in a Docker container and sets up your local directory as the source for the website content. Here’s a simplified explanation of each part:

Breakdown:

  • v $(pwd):/usr/share/nginx/html : volume mapping

-v: Binds (links) a directory from your host machine to the container.

$(pwd): Refers to the current directory on your local machine (from where you run the command).

/usr/share/nginx/html: The default directory in the nginx container where website files (HTML, CSS, etc.) are served.

In simple:
Your local directory becomes the source of the website content served by nginx.
Any files (e.g., index.html) in your local directory will be displayed as the website when accessed via http://localhost:8081.

Creating your First Docker Image

Building a custom Docker image is a key step toward mastering Docker.

  1. Creating a simple web page

Create a file called index.html in a sample text.

Step1 : vi index.html

Step2 : Type below HTML Lines

<h1> Hello from Docker!</h1>
<h2>Myself Malarvizhi :). Great, You've made it this far. Good luck!</h2>

To exit vi, give click “esc”; now type :wq; and enter :)

2. Write a Dockerfile

  • The Dockerfile defines instructions for building your image. Save this as Dockerfile in the same directory as your index.html:

Step1 : vi Dockerfile

Step2 : Type below lines

FROM nginx
COPY index.html /usr/share/nginx/html/index.html

3. Build the image

docker build -t my-static-site .

Important: Don’t forget to mention the dot after space. The . tells Docker to use the current directory as the build context. Docker will look for the Dockerfile in this directory and include all necessary files (e.g., index.html

4. Now run your container

docker run -d -p 8082:80 my-static-site

Let’s Publish it in our Docker Hub

Step 1: Login to Docker Hub

Before pushing your Docker image, you need to log in to your Docker Hub account. Run:

docker login

Here you need to activate your account. Once done, it’ll confirm it in you CLI prompt as Logged in successfully.

If you don’t have docker hub account, I suggest you to create it and then proceed for docker login in CLI :)

Step 2: Tag Your Docker Image

Docker Hub requires images to have a repository name format like username/repository_name. You can tag your image using:

docker tag my-static-site your_dockerhub_username/my-static-site:latest
  • my-static-site – The name of your local image.
  • your_dockerhub_username/my-static-site – The name of the image for Docker Hub.
  • latest – (Optional) Specifies the version tag. By default, it's latest

Step 3: Push the Image to Docker Hub

Push the tagged image to your Docker Hub repository:

docker push your_dockerhub_username/my-static-site:latest

Docker will upload the image to Docker Hub. Once completed, your image will be available for public or private use based on your repository’s settings. Check your docker hub account now :)

:)

Bonus: Pull the Image

To ensure your image was successfully uploaded, pull it from Docker Hub using:

docker pull your_dockerhub_username/my-static-site:latest

This confirms the image is publicly accessible (if you made it public). Also, with your image published on Docker Hub, you can easily share it, pull it from other systems, or integrate it into CI/CD pipelines!

Closing Thoughts:

I hope this write-up has provided you with valuable insights into Docker commands, from creating your first image to publishing it in a Docker Hub repository. In the next blog, I have an exciting topic lined up to share with you. Meanwhile, I’d love to hear your thoughts on this post — let me know in the comments section below!

Image from Unsplash

Docker Magic: From First Image to Publishing on Docker Hub! was originally published in InfoSec Write-ups on Medium, where people are continuing the conversation by highlighting and responding to this story.

原始链接: https://infosecwriteups.com/docker-magic-from-first-image-to-publishing-on-docker-hub-89ba39df08ae?source=rss----7b722bfd1b8d---4
侵权请联系站方: [email protected]

相关推荐

换一批