Skip to main content

Command Palette

Search for a command to run...

Docker Concept

Published
7 min readView as Markdown
Docker Concept
S

*Shreyash Bhise | Aspiring Mern Stack Developer and DevOps enthusiast,

Docker's multi-stage builds allow you to create more efficient and smaller Docker images by using multiple "stages" within a single Dockerfile. Each stage can include its own set of instructions, and you can copy files from one stage to another. This is particularly useful for building applications that require compilation or additional dependencies during the build process. Here's how you can use multi-stage builds in a Dockerfile:

Dockerfile
# Stage 1: Build the application
FROM golang:1.16 AS build
WORKDIR /app
COPY . .
RUN go build -o myapp

# Stage 2: Create a lightweight runtime image
FROM alpine:3.14
WORKDIR /app
COPY --from=build /app/myapp .
CMD ["./myapp"]

In this example:

  1. Stage 1 (Build):

    • The first stage uses the golang:1.16 image as the base.

    • It sets the working directory to /app and copies the application source code.

    • It runs the go build command to compile the application and generate an executable named myapp.

  2. Stage 2 (Runtime Image):

    • The second stage uses the alpine:3.14 image as the base, which is a lightweight Linux distribution.

    • It sets the working directory to /app and copies the compiled myapp executable from the previous stage using COPY --from=build.

    • The CMD instruction specifies the default command to run when a container based on this image starts.

When you build an image using this Dockerfile, only the necessary files and the final executable from the build stage are included in the runtime image. This results in a smaller image size and eliminates unnecessary dependencies from the runtime image.

To build an image using this Dockerfile, navigate to the directory containing the Dockerfile and run:

bash
docker build -t myapp-image .

Here, myapp-image is the name you're giving to the built image. The build process will create two stages: the build stage and the runtime stage. Only the files needed for the runtime image will be included, reducing the image size.

Distroless container images are minimalistic Docker images designed to be as secure and lightweight as possible. These images are stripped down to include only the essential components required to run a specific application, and they exclude unnecessary libraries and tools commonly found in traditional Linux distributions.

Distroless images are primarily used for running single applications in a containerized environment, with a focus on reducing the attack surface and minimizing potential security vulnerabilities. They are particularly suitable for production deployments and scenarios where you only need to run a specific application without the overhead of a full operating system.

Here's how you can create a Distroless container image for a simple Go application:

  1. Create Your Go Application:

    Let's assume you have a simple Go application named myapp.go.

  2. Write a Dockerfile for Distroless:

    Create a Dockerfile to build a Distroless container image for your Go application.

     Dockerfile
    
  •   # Build stage
      FROM golang:1.16 AS build
      WORKDIR /app
      COPY . .
      RUN CGO_ENABLED=0 GOOS=linux go build -o myapp
    
      # Distroless runtime image
      FROM gcr.io/distroless/static-debian10
      COPY --from=build /app/myapp /
      CMD ["/myapp"]
    

    In this example, we're using the golang:1.16 base image to build the Go application and then copying the compiled binary to a Distroless image named gcr.io/distroless/static-debian10. We're using a static version of Distroless that is based on Debian 10 and optimized for static binaries.

  • Build the Distroless Image:

    Navigate to the directory containing your Dockerfile and the Go application, and run the following command to build the Distroless image:

      bash
    
  •   docker build -t myapp-distroless .
    

    Replace myapp-distroless with the desired image name.

  • Run the Distroless Container:

    Once the image is built, you can run the Distroless container:

      bash
    
  1.  docker run --rm myapp-distroless
    

    This command will start a container based on the Distroless image you created and execute your Go application.

Distroless images help you maintain a more secure and lightweight container environment, as they exclude unnecessary components that could potentially introduce vulnerabilities. However, they may not be suitable for all use cases, especially if you require additional tools or libraries within your container. Always consider the specific requirements of your application when deciding whether to use Distroless images.

Container Management:

  1. Run a Container:

     css
    
  •   docker run [options] image_name [command]
    
  • List Running Containers:

  •   docker ps
    
  • List All Containers (including stopped ones):

      css
    
  •   docker ps -a
    
  • Stop a Container:

      arduino
    
  •   docker stop container_id
    
  • Start a Stopped Container:

      sql
    
  •   docker start container_id
    
  • Remove a Container:

      bash
    
  •   docker rm container_id
    
  • Execute Command in a Running Container:

      bash
    
  •   docker exec [options] container_id command
    
  • Inspect Container Details:

  1.  docker inspect container_id
    

Image Management:

  1. List Images:
  •   docker images
    
  • Pull an Image from Docker Hub:

      css
    
  •   docker pull image_name[:tag]
    
  • Build an Image from a Dockerfile:

  •   docker build -t image_name:tag path_to_dockerfile
    
  • Remove an Image:

      css
    
  •   docker rmi image_name[:tag]
    
  • Tag an Image:

      css
    
  •   docker tag source_image[:tag] target_image[:tag]
    
  • Push an Image to Docker Hub:

      css
    
  1.  docker push image_name[:tag]
    

Docker Compose:

Docker Compose is a tool for defining and running multi-container Docker applications. It uses a YAML file to define services, networks, and volumes for your application.

  1. Run Containers Using Docker Compose:

     css
    
  •   docker-compose up [options]
    
  • Stop and Remove Containers Defined in Docker Compose:

      css
    
  •   docker-compose down [options]
    
  • View Docker Compose Logs:

      css
    
  •   docker-compose logs [service_name]
    
  • Build and Start Containers Defined in Docker Compose:

      css
    
  1.  docker-compose up --build
    

Cleaning Up:

  1. Remove All Stopped Containers:
  •   docker container prune
    
  • Remove All Dangling Images:

      arduino
    
  •   docker image prune
    
  • Remove All Unused Resources:

      perl
    
  1.  docker system prune
    

These are just a subset of Docker commands. Docker has many more commands and options to manage containers, images, volumes, networks, and more. You can always refer to the official Docker documentation for comprehensive information on each command and its options: https://docs.docker.com/

ChatGPT

To check the status of the Docker daemon (the Docker service), you can use various commands depending on your operating system. Here are some common commands to check the status of Docker:

Linux:

  1. Using systemctl: If you're using a Linux distribution that uses systemd (such as Ubuntu), you can use the following command to check the status of the Docker service:

     bash
    
  •   systemctl status docker
    

    This command will provide detailed information about the Docker service, including whether it's active (running) or not.

  • Using service: On Linux distributions that use the traditional service command, you can use:

      bash
    
  1.  service docker status
    

macOS:

On macOS, Docker uses a lightweight virtual machine (VM) to run containers. To check the status of the Docker Desktop application:

  1. GUI: You can check the status of Docker Desktop by looking at the Docker icon in your system tray (menu bar). The icon should indicate whether Docker is running.

  2. Terminal: To check the status using the terminal, you can use the following command:

     bash
    
  1.  docker info
    

    This command will provide information about the Docker Desktop application and the Docker daemon running within the VM.

Windows:

On Windows, Docker also uses a lightweight VM to run containers. To check the status of Docker Desktop:

  1. GUI: Similar to macOS, you can check the status of Docker Desktop by looking at the Docker icon in your system tray. The icon will indicate whether Docker is running.

  2. PowerShell: To check the status using PowerShell, you can use the following command:

     powershell
    
  1.  docker info
    

    This command will provide information about the Docker Desktop application and the Docker daemon running within the VM.

In general, running docker info on any platform will provide you with a detailed overview of the Docker daemon, including its current status, version, containers, images, and more.

Please note that the specific commands may vary slightly based on your system configuration and the version of Docker you are using.

More from this blog

Shreyash Bhise's blog

55 posts