How to Containerize Golang app with Docker

How to containerize Golang app using Docker


To containerize a Golang app is similar to any other project. We need to use the base image for golandg and build the app.

Install Docker desktop , if you are on windows and create an account first. Then in the project create Dockerfile ( no extension required) with following command.

Save the file in root of the project

FROM  golang:1.18beta2-bullseye
RUN mkdir /app
ADD . /app
WORKDIR /app
RUN go build -o main .
CMD ["/app/main"]

Building the Docker Image

Go to the terminal and CD into the project folder and build the image. Docker image can be used to run different process.

docker build -t my-go-app . 

try to list image using docker images command.

Using the image

To use the image we created withrun command.

docker run -d -p 8080:8080 --name test1 my-go-app

The above command will run the image in detached mode, leaving console reedy for another execution.

It also exposed to port internal 8080:8080. Go to the browser and try localhost:8080 and it should have working.

Stopping, Starting and removing containers

Using docker stop <container-name/id>. Once it stopped can delete using docker rm container-name/id command.

Need to start again ? use the docker start command.

Wanna know running processes/ containers ? Try docker ps.

How to create a strapi project with Docker on Windows

How to create a strapi project with Docker in Windows


Starpi is new gen CMS for Node based projects, and it help us to build and manage content using customizable Admin UI.

Docker : When start using different technologies and database, Docker can make dev work easier. Developer usually need to work with different databases,frameworks, languages etc, which require super awesome computer.

Docker container help us to run development environment within a disposable,light weight container, it can include, Mysql, Mongo, Strapi and more etct .. Each container working based on Docker Images. You can explore more about Docker Images @ Docker Hub

How to start

First we need Docker for desktop get installed in Windows machine and then we need to use the Docker image to create strapi project. You need a Linux Subsystem get installed on Windows for proper working of Docker.

On the terminal please issue the following command create a project at the drive E named starpiProject.

docker run -it -p 1337:1337 -v E:\starpiProject:/srv/app strapi/strapi

This will run a docker container with interactive terminal mode (-it) and use local storage or volume which will create a strapi project starpiProject (on drive e) folder and run at the port of localhost:1337.

You can visit the the local host in a web brower which welcome you with user signup form.

Run the existing Project ?

The same command can be work for us and in case of non existed project, new one will be created.

docker run -it -p 1337:1337 -v E:\starpiProject:/srv/app strapi/strapi

Have a look at these docker posts

Docker-compose for projects

How use docker-compose to run MySQL database base for development


In the past docker posts we used docker run command to run containers using docker image and also the Dockerfile to create project image.

Sometimes we need to use/run a database in container during development, in such cases docker-compose came into light.

The Docker-Compose file

Docker-compose is a file at the root of your project, which has no extension and carry some special script. In our example we are configuring MySQL database.

version: '3.1'
services:
  db: 
    image: mysql
    command: --default-authentication-plugin=mysql_native_password
    restart: always
    environment: 
      MYSQL_ROOT_PASSWORD: example
      MYSQL_USER: development
      MYSQL_PASSWORD: development
      MYSQL_DATABASE: blog
    ports: 
      - 127.0.0.1:3306:3306

Up and Down

To start the docker-compose use the up command from the project root

docker-compose up -d

The down command used to remove and stop for stopping the container .

Checking the docker-compose containers

For listing the containers we can use

docker-compose ps

Accessing MySQL CLI

Login into the database and create some database ( as root)

docker exec -i some-id sh -c "exec -uroot -p password"

You can also use -u username for login as normal user and can execute basic sql operations such create database, tables and CURD operations on objects etc. Note that the database uses the default port of MySQL

We can stop running a container at any time using docker stop <id/name> and resume with docker start <id/name>

Following Docker Post may deserve a good read

Create a docker image of Node-Express API – Part II

How to build Node-Express project Docker Image using Node base image


In this post which is the continuation of Node-API Docker Image ( the Links are at the end of the post), create and run container using the image we build.

Lets check the existence of the image in docker using

docker image

Create the container

To build a container to run the API we need only the Image , all the depending Image will be automatically download by Docker.

docker run --name customer -d -p 3000:3000 user-api:latest

The above command will run, customer API in detached mode on port 3000.

Stop and Resume the container

We can stop running a container at any time using docker stop <id/name> and resume with docker start <id/name>

Following Docker Post may deserve a good read

Create a docker image of Node-Express API – Part I

How to build Node-Express project Docker Image.


In this post we are create Docker Image of a NodeExpress app which can be used to deploy and run the app. The image can be used to deploy multiple container as well. Following are the requirements for this tutorial

Requirements

  • NPM
  • Nodejs
  • VSCode/Atom

Node-Express API

Lets create a folder and create file index.js and paste the following API site content

const express = require('express')
const app=express()
require("dotenv").config();

app.get('/',(req,res)=>
  res.json([{name:'manoj',email:'codehat@outlook.com'
  },
  {name:'mariah',email:'codehat@outlook.com'
  }])
)
app.listen(process.env.PORT || 3000,()=>console.log(`Server running on port ${process.env.PORT}`))

Our API is a simple USER API which shows a list of users. Let’s setup the package.json file by issuing npm command and install dependencies

Create portfolio app with nginx in 5 minute

How to create and run bootstrap portfolio site in docker container using nginx in 5 minute


nginx is a docker image where you can run a local website. I have post regarding Docker blow the post, in case you need them. We can nginx it to create a static site on localhost with Docker container.

Static/Dynamic web page

As the name indicates the content of our web app remain the same. Grab a beautiful bootstrap for quick start, I have on from https://startbootstrap.com/previews/stylish-portfolio

  • Download the complete code
  • Place them in a local folder (portfolio)
  • Access Powers-hell/CLI/Terminal

Docker Volume

Docker volume is allow us to host data from local system. In our case we want to copy the content of our portfolio web app to /usr/share/nginx/html (nginx folder) in the docker container.

volume can added to container using the -v or –volume label in a docuker run command. Without the :ro the data will be updated when ever a change made the source folder.

Can I dive into nginx folder ?

Yes you can explore the Linux shell using the following command with container ID/name

 docker exec -it 9a99a1ad28e1  bash
 cd /usr/share/nginx/html 
 ls  // see all the files

Let’s create and run the container

Read only
docker run --name myapp -v e:/portfolio:/usr/share/nginx/html:ro  -d -p 8080:80 nginx:latest
Dynamic
docker run --name myapp -v e:/portfolio:/usr/share/nginx/html  -d -p 8080:80 nginx:latest

:ro stands for read only, you can remove it and the data will change as you edit the content in the source folder, in a limited sense it can be dynamic.

Go to the browser and try http:///localhost:8080

You can use the id/name to stop/start/remove the container.

docker stop 6ec62466d248  //stop container with id
docker start 6ec62466d248  //start container with id
docker rm    6ec62466d248  //remove the container with id

Other Docker posts you may interested in

Start a nginx app container in Docker

How to run a nginx web app in Docker container


nginx is a docker image where you can run a local website. I have post regarding Docker, in case you need them.

What is a container ?

A container is consisting of copy of Docker Image and an app. You can create as many containers and start/stop/remove them and link them together. Lets create a nginx server.

Run the following code in case don’t have the nginx docker image in your computer.

docker pull nginx //download the image
docker images //list images

Crate a container

Let’s create container and run a sample web app

docker run --name myapp -t latest -d -p 8080:80 nginx:latest

This will run an nginx app on the local host port 8080 which is mapped to 80. To list all containers you can use either on of the command

Go to the browser and try http:///localhost:8080

Other commands

docker ps // list of containers
docker ps -a //stopped containers
docker container ls // list of containers

You can use the id/name to stop/start/remove the container.

docker stop 6ec62466d248  //stop container with id
docker start 6ec62466d248  //start container with id
docker rm    6ec62466d248  //remove the container with id

Wanna remove an image ? use docker rmi <image-id>

Other Docker posts you may interested in

Howto download images in Docker

How to download docker images


Docker is a developer tool which help us to run applications in an isolated environment, where you can start a service, app,local host, MySQL, SQL server, mongodb etc and they can can communicate each.

Images

Images are used to create container which are used to run apps and services. You can find image of Redis, Mongo, MySQL, Ubuntu and everything needed for a develop and test app @ https://hub.docker.com/

Find the image you want and copy the docker pull code.

Open the command prompt. Before start downloading I will show you how you can view a list of Images you had

docker images

This will list all the docker images in your computer. Lets download nginx web server

docker pull nginx
docker images