Basic things about Docker for developer

Duy KN
5 min readJan 25, 2019

As a developer, I’m so curious about Docker because I’ve heard my friends, my colleagues, my boss, people in my network they talk about it. Actually, three years ago, I had got lost in the first time, too many new complicated terms and too many questions run in my mind. This year, once again I try with docker, it is even more complicated with large scale orchestration. So, I try to write this story to introduce it in my way, as a developer. It may be not “deep” enough, not accurate enough, but my point is we can accept it and go on. We have time to get back and dig into it when we have enough experience of it.

The first thing, why we need it?

Let imagine that you need to run two projects on the same server. One needs to run with Apache and PHP5, and the other one needs Nginx and PHP7. It is actually quite hard to put them together. For Docker, it does not matter. You can package two projects into two docker images and that’s it.

The second thing is you can avoid the situation like “It runs on my PC but it does not run as well on the server”. Because now the configurations for both environments are almost the same.

What is it? and How?

You can say it is a Virtual machine (actually not, but yes, just say that). Like any machine, you need 2 things to develop software: environment and storage. The environment I mean is the OS, the service/framework such as Node JS, Nginx, PHP, MySQL, etc. The Storage is where you put your code, your documents, your assets.

For the virtual machine, you have to put your code on the virtual machine, and use that virtual machine to code. It is sometimes a nightmare because the virtual machine performance is quite poor. By working around, you can find out the way to mount the physical disk to virtual disk somehow, but it is still a nightmare.

An interesting thing is Docker supports you extremely well those things. You can use the internal storage of that container — Virtual machine, or bind the storage to your real physic device. So in development, you can use your code editor to write down the code on your real PC. It’s actually flexible and fast.

In the Docker world, they call storage is “volume”. Basically, there are three types of volume: (1) volume inside a container, (2) external volume managed by Docker — kind of virtual storage, and (3) physical volume — bound to your PC/server files/folders.

1. Preparation

For example, let build a docker that use Nginx to serve static files.

First, install Docker on your laptop. See https://docs.docker.com/install/. I’m using Macbook, so it is quite easy with this guide.

Then, build the sample project structure

$ my_project && cd "$_"
$ mkdir source
$ echo "Hey dude. Welcome to Docker world." > source/test.txt

Then, create the Dockerfile with the following line:

FROM nginx:alpine
Your project now looks like this.

2. Build and run

A little bit explanation about the line FROM nginx:alpine. That means your “image” is cloned/used the pre-built Nginx image here, no customization need. Actually, for the “real” project, you always need to customize it with built-in Dockerfile command syntax, but no need now.

Build your image using the following command: (let think about the ISO file).

$ docker build -t your_name/your_image_name .

Unless you need to make some changes in Dockerfile, you do not need to build the image again. You can find out your images by using:

$ docker images ps

Now, you can start the image as a virtual machine — a container.

$ docker run -p 8080:80 -v $PWD/source:/usr/share/nginx/html your_name/your_image_name
  • -p 8080:80 means you map the port 80 of the container — virtual machine to real port 8080
  • -v $PWD/source:/user/share/nginx/html : bind your project folder to the container storage.

By using this way you can put your source code outside the Docker and keep working as traditional way.

You can find the list of running docker by using

docker ps

Your server is running at http://localhost:8080/test.txt

3. Some more actions

You can ask the Docker run in background by using -td

$ docker run -td -p 8080:80 -v $PWD/source:/usr/share/nginx/html your_name/your_image_name

You can kill the running container docker stop

$ docker stop <container_image_id>

Takeaway

  • Docker is a term, no need to think much about it. It’s OK if you see it like VMware, Virtual Box or Vagrant.
  • An Image is a way you configure/setup your virtual machine environment as you need. Like you buy a PC, write down the progress what you install and what you do settings to a paper — Dockerfile. When you build an Image, that means you follow your paper note above — the Dockerfile — step-by-step automatically by Docker. It is similar to the ISO file
  • The Container is an instance of an image. You can run multiple instances of an image like you can build & start multiple virtual machines from an ISO file. For each container, you can basically config two things: the mapping port and the binding volume, that gives more flexibility to develop or execute your project.

(To be continued)

--

--