Day 32: Docker

Day 32: Docker

Docker Installation & Multi-Stage

Docker Installation

Prerequisites for installing Docker on Ubuntu:

  1. Operating System: Use a recent, stable version of Ubuntu, preferably an LTS release.

  2. User Privileges: You need sudo access or root user privileges.

  3. Update APT: Update the APT package index with sudo apt update.

  4. Uninstall Old Docker: Remove older Docker versions if they exist.

  5. 64-bit System: Ensure you're using a 64-bit Ubuntu version.

Installation Steps:

To install Docker, including the Docker Engine, on Ubuntu, you can follow these steps:

1. Update the APT package index:

Open a terminal window and run the following commands to ensure that the package database is up-to-date and also upgraded:

sudo apt update
sudo apt upgrade -y

2. Install dependencies to allow APT to use a repository over HTTPS:

sudo apt install apt-transport-https ca-certificates curl software-properties-common

3. Add Docker's official GPG key:

This step ensures that the software you download is from a trusted source:

curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg

4. Set up the Docker stable repository:

Use the following command to add the Docker repository to your system:

echo "deb [arch=amd64 signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null

5. Update the APT package index again:

sudo apt update

6. Install Docker:

Install Docker with the following command:

sudo apt install docker.io -y

7. Start and enable Docker:

To start the Docker service and enable it to start on boot, run these commands:

sudo systemctl start docker
sudo systemctl enable docker
sudo systemctl status docker

8. Verify the installation:

You can confirm that Docker is installed and running by running a simple test:

sudo docker --version

This should display the installed version of Docker.

You have successfully installed Docker, including the Docker Engine, on your Ubuntu system. You can now use Docker to manage containers and run various applications.


Defination of Multi-Stage Builds

Multi-stage builds in Docker is a feature that allows you to create more efficient and smaller Docker images by using multiple build stages within a single Dockerfile. With multi-stage builds, you can separate the build environment from the runtime environment and selectively copy only the necessary files from one stage to another. This approach has several advantages:


Working of a Multi-Stage Builds

The syntax for a multi-stage Dockerfile typically involves using multiple FROM statements, where each FROM represents a different build stage. The stages are defined with unique names, and you can selectively copy files or directories between these stages using the COPY command.

Here's a basic example of a multi-stage Dockerfile for a Golang application:

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

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

In this example, the first stage (named builder) builds the Go application, and the second stage creates a minimal runtime image using Alpine Linux, containing only the built binary. This results in a smaller and more efficient final image.

Multi-stage builds are a powerful feature in Docker that can help streamline the image creation process and improve the overall efficiency of Docker containers.


Advantages

  1. Reduce Redundant Build Effort

In the spirit of Docker layers, multi-stage builds can be a good advanced tool to reduce the amount of duplicate work needed. Especially for production builds, where you just need the final artifacts.

  1. Make Intermediate Image Layers Shareable

Multi-stage builds can help to share intermediate image layers with your team. You’ll need to tag them right, and make sure to push them. It can pay off though!

  1. Keep Your Secrets Safe

Multi-stage builds were a popular tool to keep secrets out of final images. They still are! But there are better ways to handle build-time secrets and SSH credentials by now if you want to give BuildKit a try.


Use Cases

  1. Building Images: Multi-stage builds are specifically useful for use cases where we build an artifact, binary or executable. Usually, there are lots of dependencies required for building the binary – for example – GCC, Maven, build-essentials, etc.

  2. Language Runtimes: Create images that contain only the runtime components needed to execute your application, without including development tools.

  3. CI/CD pipelines: Multi-stage builds can seamlessly integrate with your CI/CD pipelines, enabling efficient and consistent image creation.


Conclusion:

In conclusion, this blog covered the prerequisites and installation steps for Docker on Ubuntu, ensuring a smooth setup process for users. The multi-stage builds feature in Docker was also explored, offering a method to optimize and streamline the creation of Docker images, reducing size and redundancy. By following these guidelines, users can efficiently set up Docker and harness the power of multi-stage builds to create more compact and functional containers.

Hope you like my post. Don't forget to like, comment, and share.

Note
Last Updated 28 Oct 2023