Docker


Issues we face when developing softwares

  • For the development of different softwares, we usually need different environment. Therefore, environment configuration becomes a complex and troublesome issuse, especially when environment have conflicts.
  • Virtual Machine becomes a solution for this issue by providing a new virtual system on the original system you have. However, this solution is not always ideal as it will takes up a large part of your memory resources, leading to a much slower operation. Besides, because Virtual Machine is a complete system, it contains many redundant operations which we actually don’t need.
  • Basing on these drawbacks of Virtual Machine, Linux Containers (LXC) were developed. LXC dose not provide a complete operating system as Virtual Machine, it just isolates applications from the infrastructure can provide an environment container for it. Compared with Virtual Machine, it takes much less resources and ensures a faster start-up.

Docker

  • Docker is a popular Linux container solution. It helps to package the application together with its dependencies within a image file. By runing this image file, a virtual container will be created and an isolated runing environment will be created.

docker

Image ref: https://i2.wp.com/blog.docker.com/wp-content/uploads/Are-containers-..-vms-image-2-1024x759.png?ssl=1

How to setup Docker

  • Docker is platform-agnostic, so the setting up process will be very similar among Ubuntu/Mac/Windows etc.

  • Here is the example of setting up docker on a Google Cloud Engine instance (with Ubuntu 18.04 installed).

  • First we need to install Docker CE within the cloud terminal.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    $ sudo apt-get update
    $ sudo apt-get install \
    apt-transport-https \
    ca-certificates \
    curl \
    gnupg-agent \
    software-properties-common
    $ curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
    $ sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu bionic test"
    $ sudo apt-get update
    $ sudo apt-get install docker-ce docker-ce-cli containerd.io
    $ sudo docker run hello-world
  • Change direction to the directory where your project locats. Create a Dockerfile for configuration.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    # Lightweight alpine OS, weight only 5mb, everything else is Go # # environment
    FROM golang:1.11.5

    # Define working directory
    WORKDIR <YOUR_DOCKER_WORKDIR>

    # Add files from your laptop to WORKDIR inside the docker image
    ADD . <YOUR_DOCKER_WORKDIR>

    # Install all dependencies needed for your project
    RUN go get -v \
    cloud.google.com/go/bigtable \
    cloud.google.com/go/storage \
    github.com/auth0/go-jwt-middleware \
    github.com/dgrijalva/jwt-go \
    github.com/gorilla/mux \
    github.com/pborman/uuid \
    golang.org/x/oauth2/google \
    gopkg.in/olivere/elastic.v6

    # Expose port
    EXPOSE 8080

    # Entrypoint
    CMD ["/usr/local/go/bin/go", "run", "user.go", "main.go", "ml.go"]
  • Save and quit. Then we could build an image by runing $ sudo docker build -t <IMAGE_NAME>.

  • To now, the docker image has been created. Then, we could run the image locally by $ sudo docker run -d -p 8080:8080 <IMAGE_NAME>. From port 8080 to port 8080 locally.

  • Check all existing images using sudo docker images.

  • We could check all runing contianers by using $ sudo docker ps.

  • Stop a running container $ sudo docker stop <container_id>.

  • Remove a docker continer $ sudo docker rm <container_id>.

  • Remove an docker image $ sudo docker rmi <IMAGE_NAME>.

Publish the Docker Image

  • Only after we publishing the docker image can we use it on GKE.
  • To publish it, first we need to Tag it $ sudo docker tag <IMAGE_NAME> <your_docker_id>/<REPO_NAME>:<TAG>>.
  • Then we login to our DockerHub accound through cloud terminal $ sudo docker login.
  • And then, publish it! $ sudo docker push <your_docker_id>/<REPO_NAME>

Hypervisor

  • Hypervisors are softwares that manages virtual machines. e.g. VMWare/ Virtualbox/ Parallel…

Kubernetes


Author: Luchen
Reprint policy: All articles in this blog are used except for special statements CC BY 4.0 reprint polocy. If reproduced, please indicate source Luchen !
  TOC