Docker workflow

Category : Kubernetes | Sub Category : Kubernetes With Java | By Prasad Bonam Last updated: 2023-11-21 10:12:59 Viewed : 217


The Docker workflow typically involves several steps, from creating a Docker image to running and managing containers. Here is a high-level overview of the Docker workflow:


  1. Install Docker:

    • Before you can use Docker, you need to install the Docker engine on your system. Docker provides installation packages for various operating systems, including Windows, macOS, and Linux.
  2. Write a Dockerfile:

    • The Dockerfile is a text file that contains instructions for building a Docker image. It specifies the base image, sets up the environment, installs dependencies, and defines how the application should run.
  3. Build Docker Image:

    • Use the docker build command to build a Docker image based on the Dockerfile. This involves creating layers for each instruction in the Dockerfile, resulting in a complete image that encapsulates the application and its dependencies.
    bash
    docker build -t your-image-name:tag .
    • The docker build command is used to build a Docker image from a specified Dockerfile. The -t option allows you to tag the resulting image with a name and optional tag. Here is a breakdown of the command:

      docker build: This is the command to build a Docker image.

    • -t your-image-name:tag: This option is used to tag the resulting Docker image. The -t flag stands for "tag," and it allows you to provide a name and an optional tag for the image.

      • your-image-name: Replace this with the desired name for your Docker image. It is a user-defined name that identifies the image.

      • :tag: This is an optional tag for versioning your image. It helps you manage different versions of the same image. For example, you might use a tag like latest, v1.0, or a commit hash. The colon (:) is used to separate the image name and the tag.

    • .: This specifies the build context. The build context is the set of files located in the current directory (.) and its subdirectories. It includes the Dockerfile and any files specified in the Dockerfile with commands like COPY or ADD.

    So, when you run the docker build -t your-image-name:tag . command, Docker reads the instructions from the Dockerfile in the current directory and its subdirectories, creates a set of layers, and builds an image. The resulting image is then tagged with the specified name and tag.

    For example:

    bash
    docker build -t my-java-app:1.0 .

    This command builds a Docker image from the Dockerfile in the current directory and tags it with the name my-java-app and the tag 1.0.

    Run Docker Container:

    • Once the image is built, you can create and run containers based on that image. The docker run command is used for this purpose.
    bash
    docker run -p 8080:80 your-image-name:tag
    • This example maps port 8080 on the host to port 80 on the container, allowing you to access the application running inside the container at http://localhost:8080.
    • The docker run command is used to create and start a Docker container based on a specific image. The -p option is used to map ports between the host and the container. Here is a breakdown of the command:

      bash
      docker run -p 8080:80 your-image-name:tag
      • docker run: This is the command to run a Docker container.

      • -p 8080:80: This option is used to map ports between the host and the container. It specifies that port 8080 on the host should be mapped to port 80 on the container.

        • 8080: This is the port on the host machine that you want to use to access the container.

        • 80: This is the port inside the container that you want to expose.

      • your-image-name:tag: This specifies the name and tag of the Docker image you want to use for creating the container.

      For example:

      bash
      docker run -p 8080:80 my-java-app:1.0

      This command creates and starts a Docker container based on the my-java-app:1.0 image. It maps port 8080 on the host to port 80 on the container. As a result, you can access the application running inside the container at http://localhost:8080 on your host machine.

      The -p option is crucial for exposing services running inside the container to the host or the external world. It allows you to specify how network traffic is directed between the host and the container, making your containerized applications accessible from the host machine or external networks.

  4. Manage Docker Containers:

    • Use various Docker commands to manage containers. Some common commands include:
      • docker ps: List running containers.
      • docker ps -a: List all containers, including stopped ones.
      • docker stop <container-id>: Stop a running container.
      • docker start <container-id>: Start a stopped container.
      • docker restart <container-id>: Restart a running or stopped container.
      • docker logs <container-id>: View logs of a container.
  5. Push and Pull Docker Images:

    • If you want to share your Docker image with others or deploy it on remote servers, you can push it to a container registry (e.g., Docker Hub). Use the docker push command.
    bash
    docker push your-image-name:tag
    • The docker push command is used to push a Docker image to a container registry, making it available for deployment on other systems or for sharing with others. Here is a breakdown of the command:
      • docker push: This is the command to push a Docker image to a container registry.

      • your-image-name:tag: This specifies the name and tag of the Docker image that you want to push to the container registry.

        • your-image-name: Replace this with the name of your Docker image.

        • :tag: Replace this with the tag associated with your Docker image. Tags are optional but are commonly used to version images. For example, latest, v1.0, etc.

      Before using the docker push command, ensure that you are logged in to the container registry using the docker login command. This command prompts you for your registry credentials (username and password or token).

      For example:

      bash
      docker push my-java-app:1.0

      This command pushes the Docker image named my-java-app with the tag 1.0 to the default container registry. If you are using a private registry or a different registry, you would specify the full registry path as part of the image name.

      After executing the docker push command, the Docker image and its associated layers are uploaded to the specified container registry, making it accessible for deployment on other systems or for sharing with others who have the necessary registry credentials.


    • To use an image from a registry, pull it using the docker pull command.
    bash
    docker pull your-image-name:tag
  6. The docker pull command is used to pull or download a Docker image from a container registry to your local machine.

    • docker pull: This is the command to pull a Docker image from a container registry.

    • your-image-name:tag: This specifies the name and tag of the Docker image that you want to pull from the container registry.

      • your-image-name: Replace this with the name of the Docker image.

      • :tag: Replace this with the tag associated with the Docker image. Tags are optional but are commonly used to version images. For example, latest, v1.0, etc.

    For example:

    bash
    docker pull my-java-app:1.0

    This command pulls the Docker image named my-java-app with the tag 1.0 from the default container registry. If you are using a private registry or a different registry, you would specify the full registry path as part of the image name.

    After executing the docker pull command, the Docker image and its associated layers are downloaded to your local machine. This allows you to use the pulled image to create and run containers locally.

    The docker pull command is commonly used when you want to deploy an application using an image that is stored in a container registry. It ensures that you have the latest version of the image available on your local machine before running containers based on that image.

    Clean Up:

    • Use the docker system prune command to remove unused resources such as stopped containers, dangling images, and networks.
    bash
    docker system prune

The docker system prune command is used to clean up and remove unused Docker resources, including stopped containers, dangling images, and networks. It helps to free up disk space on your machine by removing resources that are no longer in use. Here is a breakdown of the command:

  • docker system prune: This is the command to clean up and remove unused Docker resources.

When you execute this command, Docker will prompt you with a confirmation message, and if you confirm (usually by typing `y` and pressing Enter), it will perform the following cleanup:

  1. Stopped Containers: Remove all containers that are not currently running. This includes containers that were stopped manually or those that exited after running.

  2. Dangling Images: Remove image layers that have no relationship to any tagged images. Dangling images are often created during the build process when a new image is built, and the old image layers become unused.

  3. Unused Networks: Remove networks that are not connected to any containers.

  4. Unused Volumes: This option is not included by default in the command but can be added using the --volumes flag (docker system prune --volumes). It removes volumes that are not associated with any containers.

By running docker system prune, you can reclaim disk space by removing these unnecessary resources. It is a good practice to periodically clean up unused resources, especially if you frequently build and run Docker containers.

Note: Be cautious when using this command, as it permanently removes resources. Make sure you understand the potential impact on your containers and data before confirming the cleanup.

This workflow provides a basic outline of Docker usage. Depending on your specific use case, you might need additional steps, such as working with Docker Compose for multi-container applications, creating Docker networks, or managing volumes for data persistence. Additionally, Docker can be integrated into continuous integration and deployment (CI/CD) pipelines for automated testing and deployment of applications.

Search
Related Articles

Leave a Comment: