GO-Lang Setup with Docker Environment

A Brief Introduction

Couple of days ago, I started learning GO language, Well it remind me of the days learning C and C++, It’s not difficult to say that those who have a good command over C and C++ can easily get familiar with the GO language. While learning and practicing, I felt like I am programming in C language. First step of learning is setting up the environment which will work independently, Moreover I am not in favor of installation of package and libraries on local system. What I think is to use containerized environment for that using Docker.

Setting up the Environment

Pre-requisites

One must have Docker installed in his local system for deploying this GO Lang setup easily.

Folder and Application Structure

Files folder contains the Docker-File and script to be executed while setting up the container.

FROM golang:1.14
MAINTAINER Syed Saad Ahmed, syedsaadahmed2094.sa@gmail.com
USER root
RUN apt-get update && apt-get install -y sudo
RUN echo "hello ALL=(ALL) NOPASSWD: ALL" >> /etc/sudoers
RUN apt-get install -y openssh-server htop
RUN mkdir /var/run/sshd
RUN useradd --uid 22222 --user-group --create-home --shell /bin/bash hello
RUN echo 'hello:hello' | chpasswd
RUN sed 's@session\s*required\s*pam_loginuid.so@session optional pam_loginuid.so@g' -i /etc/pam.d/sshd
ENV GOPATH /opt/go:$GOPATH
ENV PATH /opt/go/bin:$PATH
ENV PROJECT learningpack
RUN echo "export GOPATH=/opt/go:$GOPATH" >> /home/hello/.bashrc
RUN echo "export PATH=/opt/go/bin:$PATH" >> /home/hello/.bashrc
RUN chown -R hello:hello /opt/go
ADD ./source /opt/go/src/${PROJECT}
WORKDIR /opt/go/src/${PROJECT}
EXPOSE 22
CMD ["/usr/sbin/sshd", "-D"]

FROM golang:1.14

The FROM command is used to state what OS you intend to use as the base image. In this line we are inheriting our image from golang:1.14.

MAINTAINER Syed Saad Ahmed, syedsaadahmed2094.sa@gmail.com

The name or other info of the person who is the maintainer of particular docker file.

USER root

If a service can run without privileges, use USER to change to a non-root user.

WORKDIR /app

WORKDIR allows you change directory while Docker builds the image, and the new directory remains the current directory for the rest of the build instructions.

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

While building a Docker image, you may need to run commands for reasons such as installing applications and packages to be part of the image.

EXPOSE 22

You’ll use the EXPOSE command to choose what ports should be available to communicate with a container. When running Docker containers, you can pass in the -p argument known as publish, which is similar to the EXPOSE command.

CMD [“/usr/sbin/sshd”, “-D”]

Docker can run only one CMD command. Therefore, if you insert two or more CMD instructions, Docker would only run the last one i.e. the most recent one. ENTRYPOINT is similar to CMD, however, you can run commands while launching and it wouldn’t override the instructions you’ve defined at ENTRYPOINT.

docker-compose up -d

Accessing the container

Using SSH

ssh -p 2228 hello@localhost
hello

Inside the container

The source folder present in the repository, which contains the working code is being pushed inside the container at the following address;

/opt/go/src/learningpack

Running the Application

Change Directory to the path mentioned below

/opt/go/src/learningpack 
go run main.go

Python, DevOps, Cryptography, Infra-Structure, Automation. https://syedsaadahmed.com/

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store