参考:https://docs.docker.com/engine/install/centos/
[root@docker ~]# yum instal yum-utils
[root@docker ~]# yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
[root@docker ~]# dnf install docker-ce
[root@docker ~]#
参考:https://docs.docker.com/compose/install/
[root@nginx ~]# cd /usr/local/bin
[root@nginx bin]# ll
total 11940
-rw-r--r--. 1 root root 12218968 Oct 20 14:30 docker-compose-Linux-x86_64
-rw-r--r--. 1 root root 94 Oct 20 14:30 docker-compose-Linux-x86_64.sha256
[root@nginx bin]# shasum -a 256 -c docker-compose-Linux-x86_64.sha256
docker-compose-Linux-x86_64: OK
[root@nginx bin]# mv docker-compose-Linux-x86_64 docker-compose
[root@nginx bin]# chmod +x docker-compose
[root@nginx bin]# rm -f docker-compose-Linux-x86_64.sha256
[root@nginx bin]#
[root@docker ~]# docker search centos
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
centos The official build of CentOS. 6218 [OK]
ansible/centos7-ansible Ansible on Centos7 132 [OK]
consol/centos-xfce-vnc Centos container with "headless" VNC session… 122 [OK]
jdeathe/centos-ssh OpenSSH / Supervisor / EPEL/IUS/SCL Repos - … 115 [OK]
centos/systemd systemd enabled base container. 86 [OK]
centos/mysql-57-centos7 MySQL 5.7 SQL database server 83
imagine10255/centos6-lnmp-php56 centos6-lnmp-php56 58 [OK]
tutum/centos Simple CentOS docker image with SSH access 47
centos/postgresql-96-centos7 PostgreSQL is an advanced Object-Relational … 46
kinogmt/centos-ssh CentOS with SSH 29 [OK]
pivotaldata/centos-gpdb-dev CentOS image for GPDB development. Tag names… 13
guyton/centos6 From official centos6 container with full up… 10 [OK]
drecom/centos-ruby centos ruby 6 [OK]
centos/tools Docker image that has systems administration… 6 [OK]
pivotaldata/centos Base centos, freshened up a little with a Do… 5
mamohr/centos-java Oracle Java 8 Docker image based on Centos 7 3 [OK]
pivotaldata/centos-gcc-toolchain CentOS with a toolchain, but unaffiliated wi… 3
darksheer/centos Base Centos Image -- Updated hourly 3 [OK]
pivotaldata/centos-mingw Using the mingw toolchain to cross-compile t… 3
blacklabelops/centos CentOS Base Image! Built and Updates Daily! 1 [OK]
mcnaughton/centos-base centos base image 1 [OK]
indigo/centos-maven Vanilla CentOS 7 with Oracle Java Developmen… 1 [OK]
pivotaldata/centos6.8-dev CentosOS 6.8 image for GPDB development 0
smartentry/centos centos with smartentry 0 [OK]
pivotaldata/centos7-dev CentosOS 7 image for GPDB development 0
[root@docker ~]#
[root@docker ~]# docker pull centos
Using default tag: latest
latest: Pulling from library/centos
3c72a8ed6814: Pull complete
Digest: sha256:76d24f3ba3317fa945743bb3746fbaf3a0b752f10b10376960de01da70685fbd
Status: Downloaded newer image for centos:latest
docker.io/library/centos:latest
[root@docker ~]# docker pull centos:7
7: Pulling from library/centos
75f829a71a1c: Pull complete
Digest: sha256:19a79828ca2e505eaee0ff38c2f3fd9901f4826737295157cc5212b7a372cd2b
Status: Downloaded newer image for centos:7
docker.io/library/centos:7
[root@docker ~]#
[root@docker ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
ubuntu latest 9140108b62dc 9 days ago 72.9MB
centos 7 7e6257c9f8d8 7 weeks ago 203MB
centos latest 0d120b6ccaa8 7 weeks ago 215MB
[root@docker ~]#
[root@docker ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
ubuntu latest 9140108b62dc 9 days ago 72.9MB
centos 7 7e6257c9f8d8 7 weeks ago 203MB
centos latest 0d120b6ccaa8 7 weeks ago 215MB
[root@docker ~]# docker save 7e6257c9f8d8 -o /tmp/centos_7.docker.tar
[root@docker ~]# ll /tmp/centos_7.docker.tar
-rw-------. 1 root root 211088896 Oct 5 14:47 /tmp/centos_7.docker.tar
[root@docker ~]#
[root@docker ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
ubuntu latest 9140108b62dc 9 days ago 72.9MB
centos 7 7e6257c9f8d8 7 weeks ago 203MB
centos latest 0d120b6ccaa8 7 weeks ago 215MB
[root@docker ~]# docker image rm 7e6257c9f8d8
Untagged: centos:7
Untagged: centos@sha256:19a79828ca2e505eaee0ff38c2f3fd9901f4826737295157cc5212b7a372cd2b
Deleted: sha256:7e6257c9f8d8d4cdff5e155f196d67150b871bbe8c02761026f803a704acb3e9
Deleted: sha256:613be09ab3c0860a5216936f412f09927947012f86bfa89b263dfa087a725f81
[root@docker ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
ubuntu latest 9140108b62dc 9 days ago 72.9MB
centos latest 0d120b6ccaa8 7 weeks ago 215MB
[root@docker ~]#
[root@docker ~]# docker load -i /tmp/centos_7.docker.tar
Loaded image ID: sha256:7e6257c9f8d8d4cdff5e155f196d67150b871bbe8c02761026f803a704acb3e9
[root@docker ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
ubuntu latest 9140108b62dc 9 days ago 72.9MB
centos 7 7e6257c9f8d8 7 weeks ago 203MB
centos latest 0d120b6ccaa8 7 weeks ago 215MB
[root@docker ~]#
命令 | 说明 |
---|---|
docker container run -it [IMAGE ID] | 通过镜像ID创建 |
docker container run -it [REPOSITORY] | 默认最新版本的镜像 |
docker container run -it [REPOSITORY:TAG] | 指定特定版本的镜像 |
[root@docker ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos 7 7e6257c9f8d8 8 weeks ago 203MB
centos latest 0d120b6ccaa8 8 weeks ago 215MB
[root@docker ~]# docker container run -it 7e6257c9f8d8
[root@docker ~]# docker container run -it centos
[root@docker ~]# docker container run -it centos:7
[root@90d6b197c036 /]#
参数 | 说明 |
---|---|
-i | 交互式Keep STDIN open even if not attached |
-t | 加载伪终端Allocate a pseudo-TTY |
参数 | 说明 |
---|---|
–name | 指定容器名称 |
–hostname | 指定容器主机名称 |
[root@docker ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest 7e4d58f0e5f3 3 weeks ago 133MB
centos 7 7e6257c9f8d8 8 weeks ago 203MB
centos latest 0d120b6ccaa8 8 weeks ago 215MB
[root@docker ~]# docker container run -d --name=nginx01 --hostname=nginx nginx
0e7d3aad5b23a2f1635069a5786ab041eef69dce989a6746279f057ea3c8afcc
[root@docker ~]#
列名 | 说明 |
---|---|
CONTAINER ID | 容器的唯一标识 |
IMAGE | 使用的镜像信息 |
NAME | 容器的名字 |
[root@docker ~]# docker container ls
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
0e7d3aad5b23 nginx "/docker-entrypoint.…" 20 seconds ago Up 19 seconds 80/tcp nginx01
[root@docker ~]#
[root@docker ~]# docker container ls -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
0e7d3aad5b23 nginx "/docker-entrypoint.…" 34 seconds ago Up 33 seconds 80/tcp nginx01
90d6b197c036 centos:7 "/bin/bash" 3 minutes ago Exited (0) 35 seconds ago thirsty_gauss
e178d714771c centos "/bin/bash" 3 minutes ago Exited (0) 3 minutes ago practical_gates
990f7231d847 7e6257c9f8d8 "/bin/bash" 4 minutes ago Exited (0) 3 minutes ago affectionate_archimedes
[root@docker ~]#
[root@docker ~]# docker container ls --no-trunc
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
0e7d3aad5b23a2f1635069a5786ab041eef69dce989a6746279f057ea3c8afcc nginx "/docker-entrypoint.sh nginx -g 'daemon off;'" About a minute ago Up About a minute 80/tcp nginx01
[root@docker ~]#
[root@docker ~]# docker container ls -q
0e7d3aad5b23
[root@docker ~]#
交互式容器退出即停止运行,登录前要先启动
[root@docker ~]# docker container ls -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
90d6b197c036 centos:7 "/bin/bash" 14 minutes ago Exited (0) 11 minutes ago thirsty_gauss
[root@docker ~]# docker container start -i thirsty_gauss
[root@90d6b197c036 /]# exit
exit
[root@docker ~]# docker container ls -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
90d6b197c036 centos:7 "/bin/bash" 14 minutes ago Exited (0) 3 seconds ago thirsty_gauss
[root@docker ~]#
守护式容器通过让进程Hang在前台运行保持活动状态,登陆时需要额外起一个子shell来进行交互。
[root@docker ~]# docker container ls
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
0e7d3aad5b23 nginx "/docker-entrypoint.…" 13 minutes ago Up 13 minutes 80/tcp nginx01
[root@docker ~]# docker container exec -it nginx01 /bin/bash
root@nginx:/# exit
exit
[root@docker ~]# docker container ls
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
0e7d3aad5b23 nginx "/docker-entrypoint.…" 15 minutes ago Up 15 minutes 80/tcp nginx01
[root@docker ~]#
登入交互式容器后,使用
Ctrl+P+Q
退出就可以使交互式容器保持活动状态。
[root@docker ~]# docker container start -i thirsty_gauss
[root@90d6b197c036 /]# [root@docker ~]#
[root@docker ~]# docker container ls
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
90d6b197c036 centos:7 "/bin/bash" 20 minutes ago Up 15 seconds thirsty_gauss
[root@docker ~]# docker container exec -it thirsty_gauss /bin/bash
[root@90d6b197c036 /]# exit
exit
[root@docker ~]# docker container ls
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
90d6b197c036 centos:7 "/bin/bash" 20 minutes ago Up 38 seconds thirsty_gauss
[root@docker ~]#
[root@docker ~]# docker container ls
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
0e7d3aad5b23 nginx "/docker-entrypoint.…" 22 minutes ago Up 22 minutes 80/tcp nginx01
90d6b197c036 centos:7 "/bin/bash" 25 minutes ago Up 5 minutes thirsty_gauss
[root@docker ~]# docker container stop thirsty_gauss
thirsty_gauss
[root@docker ~]# docker container ls
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
0e7d3aad5b23 nginx "/docker-entrypoint.…" 22 minutes ago Up 22 minutes 80/tcp nginx01
[root@docker ~]#
[root@docker ~]# docker container ls -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
0e7d3aad5b23 nginx "/docker-entrypoint.…" 23 minutes ago Up 23 minutes 80/tcp nginx01
90d6b197c036 centos:7 "/bin/bash" 26 minutes ago Exited (137) 43 seconds ago thirsty_gauss
[root@docker ~]# docker container rm thirsty_gauss
thirsty_gauss
[root@docker ~]# docker container ls -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
0e7d3aad5b23 nginx "/docker-entrypoint.…" 23 minutes ago Up 23 minutes 80/tcp nginx01
[root@docker ~]# docker container rm nginx01
Error response from daemon: You cannot remove a running container 0e7d3aad5b23a2f1635069a5786ab041eef69dce989a6746279f057ea3c8afcc. Stop the container before attempting removal or force remove
[root@docker ~]# docker container rm -f nginx01
nginx01
[root@docker ~]#
[root@docker ~]# docker container rm -f `docker container ls -a -q`
e178d714771c
990f7231d847
[root@docker ~]#
[root@docker ~]# docker container run -it --rm centos
[root@0e0ae1bea87d /]# exit
exit
[root@docker ~]# docker container ls -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@docker ~]#
格式 | 举例 | 说明说明 |
---|---|---|
hostPort:containerPort | 8080:80 | 监听来自所有IP对本地8080端口的TCP访问请求并转发给容器的80端口 |
hostPort:containerPort | 8080:80/udp | …UDP … |
containerHost | 80 | 随机分配一个本地端口,监听来自所有IP对其发起的TCP访问请求并 … |
ip:hostPort:containerPort | 13.13.7.7:8080:80 | 监听IP地址为13.13.7.7的主机对本地8080端口的TCP访问请求并 … |
[root@docker ~]# docker container run -d -p 8080:80 --name=nginx02 7e4d58f0e5f3
951bc7fa09c9167882ddbabdd997c1d572e44f8cd39e98f570bf22f81e2fea0f
[root@docker ~]# curl localhost:8080
<p><em>Thank you for using nginx.</em></p>
[root@docker ~]#
[root@docker ~]# docker container top nginx02
UID PID PPID C STIME TTY TIME CMD
root 9583 9567 0 16:52 ? 00:00:00 nginx: master process nginx -g daemon off;
101 9632 9583 0 16:52 ? 00:00:00 nginx: worker process
[root@docker ~]#
[root@docker ~]# docker container logs nginx02
172.17.0.1 - - [06/Oct/2020:08:52:56 +0000] "GET / HTTP/1.1" 200 612 "-" "curl/7.61.1" "-"
[root@docker ~]#
[root@docker ~]# echo "Hello World" > /tmp/index.html
[root@docker ~]# docker container cp /tmp/index.html nginx02:/usr/share/nginx/html/
[root@docker ~]# curl localhost:8080
Hello World
[root@docker ~]#
[root@docker ~]# mkdir /media/cdrom
[root@docker ~]# mount /dev/sr0 /media/cdrom/
[root@docker ~]# docker container run -it -v /media/cdrom/:/media/CentOS/ --name=volum centos
[root@a5871c1fab82 /]# cat /etc/yum.repos.d/CentOS-Media.repo
....
[c8-media-BaseOS]
name=CentOS-BaseOS-$releasever - Media
baseurl=file:///media/CentOS/BaseOS
....
[root@a5871c1fab82 /]# dnf --disablerepo=\* --enablerepo=c8-media-BaseOS install openssh-server
Package Architecture Version Repository Size
====================================================================================================================================================================================================================
Installing:
openssh-server x86_64 8.0p1-4.el8_1 c8-media-BaseOS 485 k
[root@docker ~]# docker container run -it --volumes-from=volum centos
[root@30ffe46a6cce /]# dnf --disablerepo=\* --enablerepo=c8-media-BaseOS install openssh-server
Package Architecture Version Repository Size
====================================================================================================================================================================================================================
Installing:
openssh-server x86_64 8.0p1-4.el8_1 c8-media-BaseOS 485 k
[root@nginx ~]# docker container inspect nginx01 | head -5
[
{
"Id": "5d0652cb1510cbc6fe4658ce2ce149567245bd9c4286936f3fb61ba855cfceba",
"Created": "2020-10-20T07:57:10.614033114Z",
"Path": "/docker-entrypoint.sh",
[root@nginx ~]# docker container inspect nginx01 | grep IPAddress
"SecondaryIPAddresses": null,
"IPAddress": "",
"IPAddress": "172.19.0.2",
[root@nginx ~]#