Using Docker for the first time

Yuil Tripathee
4 min readJun 2, 2019
Image result for docker

I am a web developer specializing in back-end technologies. My area of work is targeted to be setting up systems engineering, developing micro-services, automation, CRUD operations, setting up APIs with REST or GraphQL and so on. So, here’s how I got started with docker.

Docker is a computer program that performs OS level virtualization also known as containerization. It has been an industry standard for few years because of merits of using docker in your system from startup level up to enterprises. According to survey by stack overflow, it was ranked #1 in “Most Loved Platform”, #2 “Most Wanted Platform” and #3 “Platform in Use”.

Here are few of the benefits of using Docker:

  • Flexibility
  • Lightweight
  • Interchangeable
  • Portable
  • Scalable
  • Stackable

Setup (for the first)

My setup runs under Parrot Security OS. This Linux distro runs under a branch of Debian called Buster with a rolling release deployment. Currently, the system has Linux Kernel 4.19, and Debian 10 Buster with a MATE desktop environment. This Linux distribution though been made for security engineering, it is much helpful even for software development purposes.

I had a RESTful API made with Go which serves JSON data along with music and picture files.

How I got docker installed?

My docker installation is followed by going through the official documentation.

I recommend installing packages from the repositories referenced by the Docker community rather than using shortcuts like snap or by other means.

For Debian : installation guide

Firing up docker itself

Let’s check if docker is installed properly.

>> docker -v
Docker version 18.09.6, build 481bc77
>> docker info
Containers: 2
Running: 0
Paused: 0
Stopped: 2
Images: 1
Server Version: 18.09.6
Storage Driver: overlay2
Backing Filesystem: xfs
Supports d_type: true
Native Overlay Diff: true
Logging Driver: json-file
...

Managing docker as a non-root user

To create a docker group and add your user:

  1. Create the docker group. sudo groupadd docker.
  2. Add you user to the docker group.
  3. Log out and log back in so that your group membership is re-evaluated.

If testing on a virtual machine, it may be necessary to restart the virtual machine for changes to take effect. While on a desktop Linux environment such as X Windows, log out of your session completely and then log back in.

4. Verify that you can run docker commands without sudo.

docker run hello-world

You may encounter an error, which indicates that your ~/.docker/ directory was created with incorrect permissions due to the sudo commands. To fix this, either remove the ~/.docker/ directory or change its ownership and permissions using the following commands.

$ sudo chown “$USER”:”$USER” /home/”$USER”/.docker -R
$ sudo chmod g+rwx “$HOME/.docker” -R

This command worked at my final try, since the permission was denied to access files at /var/run/docker.sock.

$ sudo chmod 666 /var/run/docker.sock

My setup in IDE

I use Visual Studio Code as my daily driver, where I installed official Docker extension. I got this idea in the image below to generate all the docker files necessary for my environment which were:

.dockerignore
docker.compose.debug.yml
docker-compose.yml
Dockerfile
Docker extension in Visual Studio Code

My Dockerfile

The docker file and other support files were generated from the VS Code Docker extension.


#build stage
FROM golang:alpine AS builder
WORKDIR /go/src/app
COPY . .
RUN apk add — no-cache git
RUN go get -d -v ./…
RUN go install -v ./…
#final stage
FROM alpine:latest
RUN apk — no-cache add ca-certificates
COPY — from=builder /go/bin/app /app
ENTRYPOINT ./app
LABEL Name=children_app_api Version=0.0.1
EXPOSE 8085

Building the app

After the Docker setup in my system, I ran the following command:

$ docker build — tag=TAGNAME .

The tag is defaulted to latest. The full syntax for the tag option would be like

— tag=TAGNAME:v0.0.1.

Running the app

First, it is advised to check if image is prepared properly, here’s the command for that purpose:

$ docker image ls # for viewing images
$ docker ps -a # for viewing containers

You can run the image you prepared with the following command.

$ docker run -p 4000:8000 TAGNAME
Running docker app

References

  1. https://docs.docker.com/
  2. https://www.docker.com/get-started

--

--