Docker Basics (Part 3) : DockerFiles

In the realm of containerization, Dockerfiles serve as the blueprints for creating Docker images. These succinct scripts contain a series of instructions that guide the image-building process. Let's unravel the key instructions and explore the creation of a basic Go application Dockerfile.

Complete Docker Series : View Complete series

Understanding Dockerfile Instructions

FROM: Choosing the Foundation

The FROM instruction specifies the base image to build upon. It serves as the starting point for constructing the image.

FROM ubuntu:latest

WORKDIR: Setting the Stage

The WORKDIR instruction establishes the working directory for subsequent instructions, providing a structured environment for commands.

WORKDIR /app

COPY: Transferring Files

The COPY instruction copies files from the host machine to the container, facilitating the transfer of application code or dependencies.

COPY . /app
  • Tip: Utilize a .dockerignore file to exclude unnecessary files from the copying process.

RUN: Executing Build Commands

The RUN instruction executes commands during the image-building process. It is invaluable for installing software, updating packages, and other setup tasks.

RUN apt-get update && apt-get install -y python3

CMD: Defining Default Container Behavior

The CMD instruction specifies the default command to run when the container starts. It can be overridden at runtime, allowing flexibility.

CMD ["python3", "app.py"]

EXPOSE: Porting Information

The EXPOSE instruction informs Docker that the container will listen on the specified network ports at runtime, facilitating port mapping.

EXPOSE 8080

ENV: Configuring Environment Variables

The ENV instruction sets environment variables within the container, enabling dynamic configuration.

ENV PORT=8080

ENTRYPOINT: Defining Container Execution

The ENTRYPOINT instruction configures a container to run as an executable. It is the first command that executes and cannot be overridden at runtime.

ENTRYPOINT ["java", "-jar", "app.jar"]

Building a Basic Go Dockerfile

Let's delve into creating a Dockerfile for a basic Go application. Follow these steps:

  1. Create a new folder and navigate into it.

  2. Create a main.go file for your Go logic.

  3. Craft a Dockerfile with the following content:

FROM golang:1.12.0-alpine3.9
RUN mkdir /app
ADD . /app
WORKDIR /app
RUN go build -o main .
CMD ["/app/main"]
  1. Use the following commands to build the Docker image and run the container:
docker build -t golang-hi-server .
docker run -d -p 8080:8082 -it golang-hi-server
  1. Open http://localhost:8080 in your browser to witness the Go application in action.

This Dockerfile exemplifies the process of building a Go application image, demonstrating the seamless integration of Go development with Docker. Explore and adapt these Dockerfile instructions to suit your project requirements, ushering in efficiency and scalability to your containerized applications.

Did you find this article valuable?

Support Spotlight by becoming a sponsor. Any amount is appreciated!