Overview:
(1) Architecture
Docker server: Service process, manages all containers, and accepts client operations.
docker client: remote controller, remote control server behavior
Usually both are on the same machine
(2) noun
Image: A read-only template for container operation, composed of layers, and different containers are generated by submitting corresponding layer updates
General image construction process: basic image - "middleware image -" application image
Container: The program runtime environment created by the image, each operation generates a different layer
Layer layer: Each time docker executes a command, a layer is generated, and images with different operations can be generated based on layer submission.
Repository: A place to store images. There are local warehouses, remote warehouses, public warehouses, and private warehouses.
a mirror image
(1) Search mirror
Command: docker search centos
$ docker search centos
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
centos The official build of CentOS. 2877 [OK]
ansible/centos7-ansible Ansible on Centos7 90 [OK]
jdeathe/centos-ssh CentOS-6 6.8 x86_64 / CentOS-7 7.2.1511 x8... 47 [OK]
jdeathe/centos-ssh-apache-php CentOS-6 6.8 x86_64 - Apache / PHP / PHP M... 23 [OK]
nimmis/java-centos This is docker images of CentOS 7 with dif... 18 [OK]
gluster/gluster-centos Official GlusterFS Image [ CentOS-7 + Glu... 14 [OK]
million12/centos-supervisor Base CentOS-7 with supervisord launcher, h... 12 [OK]
|
(2) Get the image
Command: docker pull centos
(3) View the mirror
Command: docker images
$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos java a10d7da10519 45 hours ago 1.447 GB
centos latest 0584b3d2cf6d 3 weeks ago 196.5 MB
|
(4) Delete the mirror
命令:docker rmi <REPOSITORY>:<TAG>
$ docker rmi centos:datauser
Untagged: centos:datauser
Deleted: sha256:febb9fe5ad20d8ea9c5680807b82c234b0bf8c3c91ca1ac04e33a3d920f6be42
Deleted: sha256:09f1f145834352976cf5433bb549886a05eb6f3b83536bca562af8e6c8fb5ac6
|
two containers
(1) Create a container from the image and start it
命令:docker run -tid —name <CONTAINER NAME> <REPOSITORY>:<TAG>
例如:docker run -tid —name java centos:latest
illustrate:
-t: docker allocates a dimensional terminal and binds it to the standard input of the container
-i: keep the container's standard input open and receive operations
-d: connect as a daemon
Other: Start a container from the image and run the program
docker run centos echo "hello world"
hello world
|
(2) Connect the container
Command: docker exec -ti <container id or name> /bin/bash
(3) View container
a) View running containers
Command: docker ps
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
4712b78f0e5a centos:latest "/bin/bash" 8 hours ago Up 8 hours dataUser
|
b) View all containers
Command: docker ps -a
$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
4712b78f0e5a centos:latest "/bin/bash" 8 hours ago Up 8 hours dataUser
789ab62382eb centos:latest "/bin/bash" 8 hours ago Exited (127) 8 hours ago test
5387312fc962 centos:latest "/bin/bash" 8 hours ago Exited (137) 8 hours ago dataVol
|
(4) Start an existing container
Command: docker start <container id | container name>
(5) Stop the container
Command: docker stop <container id | container name>
(6) Delete the container
Command: docker rm <container id | container name>
(7) Submit the container to the image
Command: docker commit <container ID | container name> <repo name>:<Tag>
$ docker commit dataUser centos:datauser
sha256:febb9fe5ad20d8ea9c5680807b82c234b0bf8c3c91ca1ac04e33a3d920f6be42
|
three-port mapping
Mapping host and container ports at startup
Command: docker run -p <host port>:<container port> <image name>
-p specifies the concentrated form
(1) ip:hostPort:containerPort: Specify the port of the host ip to specify the port mapping container
(2) ip::containerPort: Specify the host ip, the system randomly assigns the port, and maps it to the container port
(3) hostPort:containerPort: any ip of the host, specified port, mapped to the container port
(4) containerPort: The system randomly assigns ports and maps to container ports
Check command:
docker port $container_id | $container_name
Note:
1) To increase the port of the container, it needs to be submitted as an image, and then re-bind the port. The originally bound port needs to be re-bound when the image is started.
2) Use docker port to view the port, docker ps shows unreliable
Open four port mapping example
$ docker run -itd -p 9200:9200 -p 9201:9201 -p 9300:9300 -p 9301:9301 --name search centos:java
e54a6dfc3a8d430fa8b7c2a7bdd65949423cb4da5c7a6013b6871b9e490ac039
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e54a6dfc3a8d centos:java "/bin/bash" 4 seconds ago Up 3 seconds 0.0.0.0:9200-9201->9200-9201/tcp, 0.0.0.0:9300-9301->9300-9301/tcp search
$ docker port search
9200/tcp -> 0.0.0.0:9200
9201/tcp -> 0.0.0.0:9201
9300/tcp -> 0.0.0.0:9300
9301/tcp -> 0.0.0.0:9301
|
四 拷贝数据
命令: docker cp [OPTIONS] CONTAINER:SRC_PATH DEST_PATH|-
docker cp [OPTIONS] SRC_PATH|- CONTAINER:DEST_PATH
1)向容器内拷贝:docker cp [本地路径] [容器名]:[容器路径]
例如:docker cp /tmp/test.txt d8f7c83ba660:/tmp
2)从容器内拷出:docker cp [容器名]:[容器路径] [本地路径]
例如:docker cp d8f7c83ba660:/tmp/test.txt /tmp
五 加载数据卷
(1)挂载目录
启动镜像时,将宿主机指定目录挂载到指定镜像的指定目录上
docker run -tid -v /tmp:/tmp —name data centos:latest
说明:通过-v参数,冒号前为宿主机目录,必须为绝对路径,冒号后为镜像内挂载的路径,此时默认开放读写权限。
需要指定权限如下:
docker run -tid -v /tmp:/tmp:ro —name data centos:latest
如果需要挂载多个目录,调用多次 -v进行挂载
(2)挂在数据卷
数据卷:普通 docker容器,用于供其他容器挂在使用。
a)创建数据卷:同上
docker run -tid -v /tmp:/tmp --name dataVol centos:latest
b)挂载数据卷:
docker run -tid --volumes-from dataVol --name dataUser centos:latest
附录:其他
(1) Docker 容器Root密码
yum install passwd 安装passwd软件;
执行passwd命令,修改root用户密