GOlang con Docker | GOlang with Docker | GO con Docker | GO with Docker

GOlang con Docker | GOlang with Docker | GO con Docker | GO with Docker


Un contenedor básicamente es una unidad de software estándar que empaqueta el código y dependencias para una aplicación, permitiendo que esta se ejecute de manera rápida en un entorno.


Objetivo


Ejecutar una aplicación desarrollada con GOlang en Docker.


Requisitos:


  • go (versión go1.14.4 o superior)

  • Docker (versión 19.03.12)

  • Docker-compose (versión 1.25.0)



En Acción:

Estructura del proyecto:

Código del ejemplo con su respectivo Dockerfile.


Dockerfile:
FROM golang:alpine
# Add Maintainer Info
LABEL maintainer="Jose Mejia"
# Install git.
# Git is required for fetching the dependencies.
RUN apk add git
# RUN apk update && apk add --no-cache git
# Create folder app
RUN mkdir /app
# Build Args
ARG LOG_DIR=/logs
# Create Log Directory
RUN mkdir -p ${LOG_DIR}
# Environment Variables
ENV LOG_FILE_LOCATION=${LOG_DIR}/
ENV ENVIRONMENT=dev
# Copy sources
COPY . /app
# Set the Current Working Directory inside the container
WORKDIR /app
# get dependencies
RUN go get -d -v
# Build the Go app
RUN go build -o main .
# Declare volumes to mount
VOLUME [${LOG_DIR}]
# This container exposes port
EXPOSE 4443
# Run
CMD ["./main"]

Descripción del dockerfile, si usas dependencias dentro del proyecto GOlang, el comando 
"RUN go get -d -v" las descarga automáticamente.

docker-compose.yml
FROM golang:alpine
# Add Maintainer Info
LABEL maintainer="Jose Mejia"
# Install git.
# Git is required for fetching the dependencies.
RUN apk add git
# RUN apk update && apk add --no-cache git
# Create folder app
RUN mkdir /app
# Build Args
ARG LOG_DIR=/logs
# Create Log Directory
RUN mkdir -p ${LOG_DIR}
# Environment Variables
ENV LOG_FILE_LOCATION=${LOG_DIR}/
ENV ENVIRONMENT=dev
# Copy sources
COPY . /app
# Set the Current Working Directory inside the container
WORKDIR /app
# get dependencies
RUN go get -d -v
# Build the Go app
RUN go build -o main .
# Declare volumes to mount
VOLUME [${LOG_DIR}]
# This container exposes port
EXPOSE 4443
# Run
CMD ["./main"]


Resultados


* El ejecutar el comando "docker-compose -f docker-compose.yml build":

Building proxy Step 1/15 : FROM golang:alpine ---> 30df784d6206 Step 2/15 : LABEL maintainer="Jose Mejia" ---> Running in 48432e50076e Removing intermediate container 48432e50076e ---> 69885a211b65 Step 3/15 : RUN apk add git ---> Running in 4c4f9dcd38c7 fetch http://dl-cdn.alpinelinux.org/alpine/v3.12/main/x86_64/APKINDEX.tar.gz fetch http://dl-cdn.alpinelinux.org/alpine/v3.12/community/x86_64/APKINDEX.tar.gz (1/5) Installing nghttp2-libs (1.41.0-r0) (2/5) Installing libcurl (7.69.1-r0) (3/5) Installing expat (2.2.9-r1) (4/5) Installing pcre2 (10.35-r0) (5/5) Installing git (2.26.2-r0) Executing busybox-1.31.1-r16.trigger OK: 22 MiB in 20 packages Removing intermediate container 4c4f9dcd38c7 ---> 6d1c57a93241 Step 4/15 : RUN mkdir /app ---> Running in fc75d7d768a9 Removing intermediate container fc75d7d768a9 ---> 7e2de823c76a Step 5/15 : ARG LOG_DIR=/logs ---> Running in 3c70764533be Removing intermediate container 3c70764533be ---> 2b5359b69184 Step 6/15 : RUN mkdir -p ${LOG_DIR} ---> Running in aec51112be5e Removing intermediate container aec51112be5e ---> 0ddd3b3730c2 Step 7/15 : ENV LOG_FILE_LOCATION=${LOG_DIR}/ ---> Running in 13511e480bff Removing intermediate container 13511e480bff ---> 8ae48f78d345 Step 8/15 : ENV ENVIRONMENT=dev ---> Running in c0ade334fe86 Removing intermediate container c0ade334fe86 ---> f6fd849b7dc0 Step 9/15 : COPY . /app ---> 587e00e5b08c Step 10/15 : WORKDIR /app ---> Running in 6947e65e4878 Removing intermediate container 6947e65e4878 ---> 02e9b0d5fe40 Step 11/15 : RUN go get -d -v ---> Running in 75840c642913 Removing intermediate container 75840c642913 ---> 7ab53e8bf047 Step 12/15 : RUN go build -o main . ---> Running in cd8f2cacc2cf Removing intermediate container cd8f2cacc2cf ---> e092e409b7ca Step 13/15 : VOLUME [${LOG_DIR}] ---> Running in e2c62bbed547 Removing intermediate container e2c62bbed547 ---> b6e38b09b8a4 Step 14/15 : EXPOSE 4443 ---> Running in 777f70a9c16c Removing intermediate container 777f70a9c16c ---> 2e64363894e1 Step 15/15 : CMD ["./main"] ---> Running in 314ea97d14cd Removing intermediate container 314ea97d14cd ---> fd16563b7905 Successfully built fd16563b7905 Successfully tagged go-reverse-proxy_proxy:latest


* El ejecutar el comando "docker-compose -f docker-compose.yml up":

Creating network "go-reverse-proxy_fullstack" with driver "bridge" Creating proxy ... done Attaching to proxy


* El ejecutar el comando "sudo docker stats":

CONTAINER ID NAME CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O PIDS 65b0c47bd3f1 proxy 0.00% 1.625MiB / 7.684GiB 0.02% 12.2kB / 0B 0B / 0B 7

Se muestra el detalle del contenedor iniciado. Consumo de memoria, cpu, red, entre otros.

Código fuentes:


Notas finales:

Con Docker y Docker-Compose es mucho más fácil la administración y despliegues de aplicaciones en cualquier ambiente. Adicionalmente Docker se encarga de administrar el contenedor.


No olvides compartir y dar +1

-

Próximos eventos:

* Habilitar https en GOlang
* Ejecutar código HTML estático en DOCKER
* Microservicios en Python
* Python+MongoDB: CRUD
* Python+MongoDB+GridFS: Almacenamiento de archivos
* Colas - Queue Python: Envío de mensajes
* Python+AWS+SNS: Envío de mensajes de textos.
* Python3: Creación de un Chatbot básico
* NodeJS+MongoDB: CRUD

El orden de los eventos puede cambiar

Comentarios

Entradas más populares de este blog

Ejemplo Log4j 2 en JAVA | Log4j 2 en Springboot | Configuración Log4j 2 | Log4j 2 in SpringBoot| Example Log4j 2 in SpringBoot | Configuring Log4j 2

Python: Inyección de dependencias