重拾Docker

  • 本篇博文主要是针对 Docker 相关进行一次系统地总结以及一些实战过程中的记录
  • 主要参考 Docker 官方文档,会结合一些简单的实战例子讲解部署的相关流程。
  • 后续可能针对 Docker 和 K8S 容器编排的结合进行讲解,包括一些自动化平台的集成。

Overview

Edition

  • Docker Engine - Community is ideal for individual developers and small teams looking to get started with Docker and experimenting with container-based apps.

  • Docker Engine - Enterprise is designed for enterprise development of a container runtime with security and an enterprise grade SLA in mind.

  • Docker Enterprise is designed for enterprise development and IT teams who build, ship, and run business critical applications in production at scale.

  • This post is built based on Docker Engine - Community

Docker Engine - Community

  • Docker Engine - Community is ideal for developers and small teams looking to get started with Docker and experimenting with container-based apps. Docker Engine - Community has three types of update channels, stable, test, and nightly.
  • Supported platforms: Linux(CentOS/Debian/Fedora/Ubuntu)、Windows、MacOS

How to get it(Docker Engine - Community) for CentOS?

Prerequisites
  • To install Docker Engine - Community, you need a maintained version of CentOS 7. Archived versions aren’t supported or tested.
  • The centos-extras repository must be enabled. This repository is enabled by default, but if you have disabled it, you need to re-enable it.
  • The overlay2 storage driver is recommended.
Uninstall Old Versions
$ sudo yum remove docker \
                  docker-client \
                  docker-client-latest \
                  docker-common \
                  docker-latest \
                  docker-latest-logrotate \
                  docker-logrotate \
                  docker-engine
Install
  • You can install Docker Engine - Community in different ways, depending on your needs: (In this post, only introduce the way of using th repository)
    • Most users set up Docker’s repositories and install from them, for ease of installation and upgrade tasks. This is the recommended approach.
    • Some users download the RPM package and install it manually and manage upgrades completely manually. This is useful in situations such as installing Docker on air-gapped systems with no access to the internet.
    • In testing and development environments, some users choose to use automated convenience scripts to install Docker.
  • Step 1. Install required packages.
$ sudo yum install -y yum-utils \
  device-mapper-persistent-data \
  lvm2
  • Step 2. Set up the stable repository. (yum-config-manager is provided by yum-utils)
$ sudo yum-config-manager \
    --add-repo \
    https://download.docker.com/linux/centos/docker-ce.repo
  • Step 3. Install Docker Engine
# the latest version of Docker Engine - Community and containerd.
$ sudo yum install docker-ce docker-ce-cli containerd.io


-------------------------------------------------------------------
# install a specific version of Docker Engine - Community, list the available versions in the repo, then select and install
$ yum list docker-ce --showduplicates | sort -r

docker-ce.x86_64  3:18.09.1-3.el7                     docker-ce-stable
docker-ce.x86_64  3:18.09.0-3.el7                     docker-ce-stable
docker-ce.x86_64  18.06.1.ce-3.el7                    docker-ce-stable
docker-ce.x86_64  18.06.0.ce-3.el7                    docker-ce-stable

$ sudo yum install docker-ce-<VERSION_STRING> docker-ce-cli-<VERSION_STRING> containerd.io
  • Step 4. Run and Verify
# Start docker
$ sudo systemctl start docker
# Run hello-world demo
$ sudo docker run hello-world
  • Another Step: Uninstall Docker Engine - Community
sudo yum remove docker-ce
# Images, containers, volumes, or customized configuration files on your host are not automatically removed. To delete all images, containers, and volumes:
sudo rm -rf /var/lib/docker

How to get it for Windows And MacOS?

  • Install desktop application and run demo repo.

Get Started

Docker Concepts

  • Docker is a platform for developers and sysadmins to build, share, and run applications with containers. The use of containers to deploy applications is called containerization. Containers are not new, but their use for easily deploying applications is.

Images and Containers

  • Fundamentally, a container is nothing but a running process, with some added encapsulation features applied to it in order to keep it isolated from the host and from other containers. One of the most important aspects of container isolation is that each container interacts with its own, private filesystem; this filesystem is provided by a Docker image. An image includes everything needed to run an application -- the code or binary, runtimes, dependencies, and any other filesystem objects required.

Differences between Docker And VM

  • A container runs natively on Linux and shares the kernel of the host machine with other containers. It runs a discrete process, taking no more memory than any other executable, making it lightweight.
  • A virtual machine (VM) runs a full-blown “guest” operating system with virtual access to host resources through a hypervisor. In general, VMs incur a lot of overhead beyond what is being consumed by your application logic.

Enable K8S

  • Direct Way: In docker-desktop settings, enable the K8S.
  • And you may meet some problems when enable K8S. You can visit Github:k8s-for-docker-desktop for more help info.
  • Since the network problem, you may need to download K8S images mannually. And this script may be useful for you.
#!/bin/bash

set -e 
# Check version in https://kubernetes.io/docs/reference/setup-tools/kubeadm/kubeadm-init/
# Search "Running kubeadm without an internet connection"
# For running kubeadm without an internet connection you have to pre-pull the required master images for the version of choice:
KUBE_VERSION=v1.14.6
KUBE_DASHBOARD_VERSION=v1.10.1
KUBE_PAUSE_VERSION=3.1
ETCD_VERSION=3.1.12
DNS_VERSION=1.14.8
GCR_URL=k8s.gcr.io
ALIYUN_URL=registry.cn-hangzhou.aliyuncs.com/google_containers

images=(kube-proxy-amd64:${KUBE_VERSION}
kube-scheduler-amd64:${KUBE_VERSION}
kube-controller-manager-amd64:${KUBE_VERSION}
kube-apiserver-amd64:${KUBE_VERSION}
pause-amd64:${KUBE_PAUSE_VERSION}
etcd-amd64:${ETCD_VERSION}
k8s-dns-sidecar-amd64:${DNS_VERSION}
k8s-dns-kube-dns-amd64:${DNS_VERSION}
k8s-dns-dnsmasq-nanny-amd64:${DNS_VERSION}
kubernetes-dashboard-amd64:${KUBE_DASHBOARD_VERSION}) 

for imageName in ${images[@]} ; do
docker pull $ALIYUN_URL/$imageName
docker tag $ALIYUN_URL/$imageName $GCR_URL/$imageName
docker rmi $ALIYUN_URL/$imageName
done

docker images

Manage Docker Service

service docker start       # Start docker service,daemon process
service docker stop        # stop docker service
service docker status      # check docker running status
chkconfig docker on        # Enable auto-start with boot

Container Management

# list all running conatiners 
docker container ls
# list all containers including running and stopped
docker container ls --all
docker start [containerID/Names] # Start container
docker stop [containerID/Names]  # Stop container
docker rm [containerID/Names]    # Remove container
docker logs [containerID/Names]  # View the log of container
docker exec -it [containerID/Names] /bin/bash  # Enter in container with bash

# Copy file from remote path of running container to local path(. means cuurent directory)
docker container cp [containID]:[/path/to/file] .

# Exec `echo "hello world"` or other command in container[eg.centos]
docker run centos echo "hello world"
docker run centos yum install -y wget
# List all containers
docker ps                          
docker ps -a                       


docker run -i -t centos /bin/bash   # 启动一个容器
docker inspect centos     # 检查运行中的镜像
docker commit 8bd centos  # 保存对容器的修改
docker commit -m "n changed" my-nginx my-nginx-image # 使用已经存在的容器创建一个镜像
# Get the pid with id 44fc0f0582d9
docker inspect -f {{.State.Pid}} 44fc0f0582d9 
# Pull the image with given name
docker pull gitlab/gitlab-ce:11.2.3-ce.0