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

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

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

source folder contains all the code that is to be put inside the container, all the go code files.

Getting to Know Docker-File

Setting up the containerized environment for GO Language using Docker is quite simple, first we have to write Docker-file and docker-compose for setting up the Docker image and container respectively.

Docker-file for development including initial GO setup, ready to go environment in a Docker 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 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}
CMD ["/usr/sbin/sshd", "-D"]

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.

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

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

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.

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.

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.

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.

Getting to Know Docker-Compose

Image that will be created using the docker-compose file will be named as “learningpack” as described in the file. The image will be created by docker-compose file using the Docker-file inside the Files/ folder. Moreover the line EXPOSE 2228:22 will expose the host port 2228 to the port 22 of the docker container. Furthermore the Environment variable and Volumes are also being set by docker-compose file which can be easily understandable by any user.

Running the docker-compose setup

docker-compose up -d

It will deploy the container named as learningpack, no need of any manual or human intervention.

Using SSH

ssh -p 2228 hello@localhost

And the password is also


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


Change Directory to the path mentioned below


Now run the command

go run main.go

It will run the code and gives you the output.

Furthermore here is the URL for the github repository for this complete application and setup.

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