Docker 笔记

本文记录了学习Docker过程中安装、常用命令、理解等,整理自官网、慕课网视频。

Install In Centos

Install docker-ce in Centos

Docker CE version

Update docker in centos from 1.13.1 to 18.03.0-ce:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
sudo yum remove docker \
docker-client \
docker-client-latest \
docker-common \
docker-latest \
docker-latest-logrotate \
docker-logrotate \
docker-selinux \
docker-engine-selinux \
docker-engine
yum list docker-ce --showduplicates | sort -r
yum install docker-ce18.03.0.ce-1.el7.centos
// If "Unable to install docker docker-ce18.03.0.ce-1.el7.centos", run command below
yum install -y --setopt=obsoletes=0 \
docker-ce-18.03.0.ce-1.el7.centos \
docker-ce-selinux-18.03.0.ce-1.el7.centos

docker -v
1
2
3
// If "options: used when "-bash: warning: setlocale: LC_CTYPE: cannot change locale (UTF-8): No such file or directory"
# echo "export LC_ALL=en_US.UTF-8" >> /etc/pro
// logout and login again

Start

1
systemctl start docker

Introduction

Docker 架构

1
2
3
4
5
6
7
8
docker run [Image] -> container

Dockerfile -> docker build -t [Image-name] -> Images

docker-compose.yml -> docker-composer up -> container
|-> docker swarm init
|-> docker stack deploy -c docker-compose.yml getstartedlab
|-> service (docker stack ps getstartedlab)
1
2
3
4
5
software containerization
解决问题:
1、运行环境(系统,jdk,代码)不一致导致的不能运行
2、隔离性
3、快速扩展

Docker Image

Docker Images are shared, can be copyed to the container but only readable

1
2
3
4
5
6
docker pull hello-world
// list
docker images
// rm
docker rmi <imagename> # Remove the specified image from this machine
docker rmi $(docker images -q) # Remove all images from this machine

docker 原理

Docker Container

The same Image will be copied to different container.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
docker run hello-world
docker run --help

docker ps # See a list of all running containers
docker stop <hash> # Gracefully stop the specified container
docker kill <hash> # Force shutdown of the specified container

docker exec --help
-i Keep STDIN open even if not attached
-t Allocate a pseudo-TTY

docker exec -it f4 bash
(enter docker exec)
ls
ps -ef
exit

docker ps -a # See a list of all containers, even the ones not running
docker container start [containerID]
docker rm <hash> # Remove the specified container from this machine
docker rm $(docker ps -a -q) # Remove all containers from this machine

The difference between docker stop and docker rm

Docker Warehouse

1
2
3
4
5
6
hub.docker.com (default)
c.163.com

// China Warehouse Linux
vim /etc/default/docker
DOCKER_OPTS="--registry-mirror=https://registry.docker-cn.com"

China Warehouse Mac:

Docker Pull

pull image from warehouse

1
docker pull hello-world

Docker Run

  1. copy image to container
  2. if the image is not exist, ‘docker pull’ firstly
1
docker run hello-world

Docker run Nginx

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
docker pull hub.c.163.com/library/nginx:latest

docker run --help
-d, --detach Run container in background and print container

docker run -d hub.c.163.com/library/nginx
f4e......

docker ps
(print containers has runed.)

docker exec --help
-i Keep STDIN open even if not attached
-t Allocate a pseudo-TTY

docker exec -it f4 bash
(enter docker exec)
ls
ps -ef
exit

docker stop f4

Docker Nginx Network

  1. host链接方式是直接链接主机网卡
  2. bridge需要链接一个网桥bridge,然后通过网桥链接主机,container有自己独立等ip和端口
1
2
3
4
5
6
7
docker run --help
-p Publish a container's port(s) to the host (default)
-P Publish all exposed ports to random ports
docker run -d -p 8080:80 hub.c.163.com/library/nginx
(Host:Container)
docker ps
nestat -na|grep 32769

Dockerfile

Define a container with a Dockerfile

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
docker build -t friendlyname .  # Create image using this directory's Dockerfile
docker run -p 4000:80 friendlyname # Run "friendlyname" mapping port 4000 to 80
docker run -d -p 4000:80 friendlyname # Same thing, but in detached mode
docker ps # See a list of all running containers
docker stop <hash> # Gracefully stop the specified container
docker ps -a # See a list of all containers, even the ones not running
docker kill <hash> # Force shutdown of the specified container
docker rm <hash> # Remove the specified container from this machine
docker rm $(docker ps -a -q) # Remove all containers from this machine
docker images -a # Show all images on this machine
docker rmi <imagename> # Remove the specified image from this machine
docker rmi $(docker images -q) # Remove all images from this machine
docker login # Log in this CLI session using your Docker credentials
docker tag <image> username/repository:tag # Tag <image> for upload to registry
docker push username/repository:tag # Upload tagged image to registry
docker run username/repository:tag # Run image from a registry

Docker compose

1
2
3
4
5
6
docker-compose up -d         # start containers in background
docker-compose kill # stop containers
docker-compose up -d --build # force rebuild of Dockerfiles
docker-compose rm # remove stopped containers
docker ps # see list of running containers
docker exec -ti [NAME] bash

Services (containers in production)

https://docs.docker.com/get-started/part3/#understanding-services

1
2
3
4
5
docker stack ls              # List all running applications on this Docker host
docker stack deploy -c <composefile> <appname> # Run the specified Compose file
docker stack services <appname> # List the services associated with an app
docker stack ps <appname> # List the running containers associated with an app
docker stack rm <appname> # Tear down an application

Swarms/Stacks

  1. A swarm is a group of machines that are running Docker and joined into a cluster
  2. A single stack is capable of defining and coordinating the functionality of an entire application (though very complex applications may want to use multiple stacks).

    https://docs.docker.com/get-started/part4/#set-up-your-swarm
    https://docs.docker.com/get-started/part5/stacks

network

1
2
docker network inspect bridge
docker network disconnect bridge [Containers: Name]

Install in centos

  1. Get Docker CE for CentOS | docker official

    Tip: replacing $ sudo yum install docker-ce with $ sudo yum install docker will be faster

  2. Install Docker Compose | docker official

File Sharing in Mac D4m-nfs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// step 1: 在Docker中打开Preferences, 点击File Sharing将其它目录全部删掉只保留/tmp目录

// step 2: 克隆 d4m-nfs 到 ~目录下
$ cd ~
$ git clone https://github.com/IFSight/d4m-nfs ~/d4m-nfs

// step 3: 项目克隆下来后修改~/d4m-nfs/etc/d4m-nfs-mounts.txt文件
$ vim ~/d4m-nfs/etc/d4m-nfs-mounts.txt
/Users:/Users
/Volumes:/Volumes
/private:/private

// step 4 检查系统文件 /etc/exports下是否有内容,编辑文件清空所有内容!(rm)
$ cat /etc/exports
$ sudo rm -rf /etc/exports

// step 5
$ ~/d4m-nfs/d4m-nfs.sh

// success
$ screen -r d4m

d4m-nfs | Laravel 学院

Laravel Development with Docker

Laravel Development with Docker

ek_ik docker
elasticsearch-analysis-ik

TIPS FOR DEPLOYING NGINX (OFFICIAL IMAGE) WITH DOCKER

Docker For PHP Developer

Dockerise your PHP application with Nginx and PHP7-FPM

Vagrant vs Docker

  1. Vagrant 适合用来管理虚拟机,而docker适合用来管理线上环境。
  2. Vagrant 可以让虚拟机各种系统之间快速切换, 并深层运用系统网络,内存等, 用来管理虚拟机
  3. docker 实际上一直是局限于linux的docker 容器里, 在Mac\Windows上也是带虚拟Linux

Docker学习总结之Docker与Vagrant之间的特点比较
Docker与Vagrant