Docker Beginner Notes

Saturday, August 29, 2015 🌐中文

logo

I was totally drawn in by this whale logo—loved it the first time I saw it. Docker is an open-source engine that makes it easy to create lightweight, portable, self-sufficient containers for any application. A container that a developer compiles and tests on a laptop can be deployed in batches to production environments, including VMs (virtual machines), bare metal, OpenStack clusters, and other infrastructure platforms. Compared with ordinary VM images, the biggest difference is that a Docker container image does not include an operating system kernel.

diff

Docker is commonly used in these scenarios:

  • Automated packaging and deployment of web applications;
  • Automated testing and continuous integration / delivery;
  • Deploying and tuning databases or other backend applications in service environments;
  • Building your own PaaS environment by compiling from scratch or extending existing OpenShift or Cloud Foundry platforms.

Linux version: ubuntu-15.04-server-amd64
Environment: VMware Workstation 11

Install Docker:

apt-get install docker.io

Check Docker version:

root@ubuntu:~# docker version
Client version: 1.6.2
Client API version: 1.18
Go version (client): go1.3.3
Git commit (client): 7c8fca2
OS/Arch (client): linux/amd64
Server version: 1.6.2
Server API version: 1.18
Go version (server): go1.3.3
Git commit (server): 7c8fca2
OS/Arch (server): linux/amd64

root@ubuntu:~# docker info
Containers: 13
Images: 3
Storage Driver: aufs
 Root Dir: /var/lib/docker/aufs
 Backing Filesystem: extfs
 Dirs: 29
 Dirperm1 Supported: true
Execution Driver: native-0.2
Kernel Version: 3.19.0-15-generic
Operating System: Ubuntu 15.04
CPUs: 2
Total Memory: 1.938 GiB
Name: ubuntu
ID: 5P2D:CFMQ:5WWP:U7RE:3SLE:Z2D3:L7D3:UMCI:LHZD:WNQK:Q6DD:ZRPA
WARNING: No swap limit support

Search for available Docker images. Here we search for an image named tutorial:

root@ubuntu:~# docker search learn/tutorial
NAME             DESCRIPTION   STARS     OFFICIAL   AUTOMATED
learn/tutorial                 11

Download the image:

root@ubuntu:~# docker search learn/tutorial
NAME             DESCRIPTION   STARS     OFFICIAL   AUTOMATED
learn/tutorial                 11
root@ubuntu:~# docker pull learn/tutorial
Pulling repository learn/tutorial
8dbd9e392a96: Download complete Status: Downloaded newer image for learn/tutorial:latest

Let’s do a hello, gorgias!

root@ubuntu:~# docker run learn/tutorial echo "hello gorgias"
hello gorgias

Install a new program inside the container:

docker run learn/tutorial apt-get install -y ping

When running apt-get, you must add the -y flag. Without -y, apt-get enters interactive mode and requires user confirmation, but you can’t respond to that kind of interaction in a Docker environment.

Save changes to a container After you modify a container (by running a command inside it), you can save that modified state so next time you can run the container from the latest saved state. In Docker, this is called “committing”. It saves the difference between the old and new states, producing a new version. First use docker ps -l to get the container ID after installing ping. Then save the image as learn/ping.

root@ubuntu:~# docker ps -l
CONTAINER ID        IMAGE                   COMMAND              CREATED              STATUS              PORTS               NAMES
824490fb9db6        learn/tutorial:latest   "ping 192.168.1.1"   About a minute ago
// only take enough to distinguish; here it's 824                                          suspicious_yalow
root@ubuntu:~# docker commit 824 learn/ping
ae5aa76cc0f84a7a3ebb77e8c7557df0d89564dd5cb9830cb4d35424290a71ca // returned version ID

Run the new image Make sure you run the ping command using the new image name learn/ping. (Translator’s note: the original learn/tutorial image does not include ping.)

# docker run learn/ping ping www.baidu.com
PING www.a.shifen.com (115.239.210.27) 56(84) bytes of data.
64 bytes from 115.239.210.27: icmp_req=1 ttl=53 time=23.7 ms
64 bytes from 115.239.210.27: icmp_req=2 ttl=53 time=23.2 ms
64 bytes from 115.239.210.27: icmp_req=3 ttl=53 time=23.4 ms

Stop a running container:

docker stop id  // gracefully stop the container
docker kill id  // force stop the container

Inspect a running container First use docker ps to view containers, then run:

docker inspect dc8 // here dc8 is the first 3 chars of the container ID

inspect

Publish your own image First register an account on Docker Hub. Create your own repository.

repostitory

You can also log in before pushing:

docker login

Here we try to publish learn/ping to Docker’s index. You can only publish images under your own namespace. This tutorial uses the learn account in the simulator.

root@ubuntu:~# docker images // list all installed images
REPOSITORY          TAG                 IMAGE ID            CREATED              VIRTUAL SIZE
learn/ping          latest              db66cd8a04ae        55 minutes ago       139.5 MB
learn/tutorial      latest              8dbd9e392a96        2.380344 years ago   128 MB
root@ubuntu:~# docker tag db6 gorgiaxx/learn-ping // retag the image
root@ubuntu:~# docker push gorgiaxx/learn-ping // publish an image to the official registry
The push refers to a repository [gorgiaxx/learn-ping] (len: 1)
Sending image list
Pushing repository gorgiaxx/learn-ping (1 tags)
8dbd9e392a96: Image already pushed, skipping
db66cd8a04ae: Image successfully pushed
Pushing tag for rev [db66cd8a04ae] on {https://cdn-registry-1.docker.io/v1/repositories/gorgiaxx/learn-ping/tags/latest}

Successfully published to Docker Hub:

pushed

VirtualizationDocker

Deploying a Hexo Blog on GitCafe

Kali NetHunter: First Impressions