2018年1月18日 星期四

VirtualBox is strong, but Docker is quick. Let’s build with Docker!

All credit belongs to Tommy from Synaptics. Thanks a lot for his help to set up Docker for me.

For years we have talked about local build environment on the customer side. There are several ways to deploy it i.e. cygwin+toolbox, VirtualBox. However, those environments cannot directly clone to other user’s PC and we always have a lot of “My PC doesn’t work as yours” problems.

So, can we have a better solution to to eliminate “works on my machine” problems when collaborating on code with co-workers? Now we have the Docker to solve this issue. This makes for efficient, lightweight, self-contained systems and guarantees that software will always run the same, regardless of where it’s deployed.

2 Easy Steps to Deploy your own container

Docker for Windows

  • Install Docker for Windows
  • Virtualization must be enabled. Typically, virtualization is enabled by default. (Note that this is different from having Hyper-V enabled.) For more detail see Virtualization must be enabled in Troubleshooting.
  • Increase RAM to 4GB and change image location to disk of data strorage

enter image description here

Tutorial for hello-world

There are some basic docker commands for hello-world.
docker version
docker info
docker images
docker ps
docker run hello-world
docker stop hello-world
docker ps -a
docker rm hello-world
docker rmi hello-world

Here is the result showing how we see all containers(running, exited) and delete it via docker ps -a.

enter image description here

Build an image of application container

Now we are going to build a container to run development environment. Here is the layer description of my container and steps to build them from Dockerfile.1

Container Layer Image Comment
User ubuntu-hchang Create user account and .ssh
Dev ubuntu-packages Install packages are required for your development enviroment
Base ubuntu Use an official ubuntu as a parent image
  • CreateDokcerfile of ubuntu-packages and ubuntu-$USER. Then, put files with your ssh.tar.gz under D:\Docker as follows enter image description here
  • Ready for launch! Execute these commands via PowerShell. It takes some time to build those images from base ubuntu
docker build -t ubuntu-packages ubuntu-packagesdocker build -t ubuntu-hchang ubuntu-hchang
  • Done! Execute docker images and 2 built images are seen. ubuntu-hchang is a final container for SYNA FW build. enter image description here
Note: You can find more information from Docker\ubuntu-packages\Dockerfile and understand what packages are installed for your development environment from offical ubuntu.
# Use an official ubuntu as a parent image
FROM ubuntu
# Install packages
RUN apt-get install --assume-yes zsh
RUN apt-get install --assume-yes git
# Install packages for comm2
RUN apt-get install --assume-yes bc
RUN apt-get install --assume-yes default-jre
RUN apt-get install --assume-yes libspreadsheet-parseexcel-perl
# Many lines skipped...Docker\ubuntu-packages\Dockerfile

Note: You can find more information from Docker\ubuntu-hchang\Dockerfile and understand how we set up account and .ssh from ubuntu-packages.

# Use ubuntu-packages as a parent image
FROM ubuntu-packages
# Add user account
RUN useradd -m -s /bin/zsh -G sudo hchang
# Config ssh
COPY ssh.tar.gz ./
RUN tar xvfz ssh.tar.gz
RUN rm ssh.tar.gz
# Many lines skipped...Docker\ubuntu-hchang\Dockerfile

Docker is running!

Run Container with port forwarding

  • Create shared volume2 and run container with port forwarding3 automatically as default. Then you can access your guest VM via SSH
docker volume create comm2
docker volume ls
docker run -d -it --restart always --mount source=myshare,destination=/home/hchang/myshare -p 2222:22 --name ubuntu ubuntu-hchang
enter image description here
Tip: To run container, you can directly use docker run -it ubuntu-hchang for root login. Or you can use docker run -it -d -p 2222:22 –-name ubuntu ubuntu-hchang as user ssh login.

Connect to container

  • Access a container via SSH ssh -p 2222 MYACCOUNT@localhost.

  1. Dockerfile: Docker can build images automatically by reading the instructions from a Dockerfile. A Dockerfile is a text document that contains all the commands a user could call on the command line to assemble an image. ↩︎
  2. Docker volume: Multiple containers can use the same volume in the same time period. This is useful if two containers need access to shared data. For example, if one container writes and the other reads the data. ↩︎
  3. You will use the Port Forwarding network feature of Docker, and configure it to access your guest VM OS via SSH. What we are going to do is pick a port on our Host, for example 2222, and forward TCP connections received on this port, to port 22/TCP (SSH) on our guest. ↩︎