docker基礎筆記

系統版本
[root@linux-node3 ~]# cat /etc/redhat-release
CentOS Linux release 7.4.1708 (Core) php

安裝docker
[root@linux-node3 ~]# yum install docker-io -ynode

啓動docker
[root@linux-node3 ~]# systemctl start dockerpython

搜索docker鏡像
[root@linux-node3 ~]# docker search centos
INDEX NAME DESCRIPTION STARS OFFICIAL AUTOMATED
docker.io docker.io/centos The official build of CentOS. 4564 [OK]
docker.io docker.io/ansible/centos7-ansible Ansible on Centos7 115 [OK]
docker.io docker.io/jdeathe/centos-ssh CentOS-6 6.9 x86_64 / CentOS-7 7.4.1708 x8... 98 [OK]
docker.io docker.io/consol/centos-xfce-vnc Centos container with "headless" VNC sessi... 59 [OK]
docker.io docker.io/imagine10255/centos6-lnmp-php56 centos6-lnmp-php56 44 [OK]
docker.io docker.io/tutum/centos Simple CentOS docker image with SSH access 43
docker.io docker.io/centos/mysql-57-centos7 MySQL 5.7 SQL database server 35
docker.io docker.io/gluster/gluster-centos Official GlusterFS Image [ CentOS-7 + Glu... 31 [OK]
docker.io docker.io/openshift/base-centos7 A Centos7 derived base image for Source-To... 31
docker.io docker.io/centos/python-35-centos7 Platform for building and running Python 3... 28
docker.io docker.io/centos/postgresql-96-centos7 PostgreSQL is an advanced Object-Relationa... 25
docker.io docker.io/kinogmt/centos-ssh CentOS with SSH 22 [OK]
docker.io docker.io/centos/httpd-24-centos7 Platform for running Apache httpd 2.4 or b... 15
docker.io docker.io/openshift/jenkins-2-centos7 A Centos7 based Jenkins v2.x image for use... 14
docker.io docker.io/pivotaldata/centos-gpdb-dev CentOS image for GPDB development. Tag nam... 7
docker.io docker.io/openshift/mysql-55-centos7 DEPRECATED: A Centos7 based MySQL v5.5 ima... 6
docker.io docker.io/openshift/jenkins-1-centos7 DEPRECATED: A Centos7 based Jenkins v1.x i... 4
docker.io docker.io/openshift/wildfly-101-centos7 A Centos7 based WildFly v10.1 image for us... 4
docker.io docker.io/darksheer/centos Base Centos Image -- Updated hourly 3 [OK]
docker.io docker.io/pivotaldata/centos Base centos, freshened up a little with a ... 2
docker.io docker.io/blacklabelops/centos CentOS Base Image! Built and Updates Daily! 1 [OK]
docker.io docker.io/pivotaldata/centos-mingw Using the mingw toolchain to cross-compile... 1
docker.io docker.io/jameseckersall/sonarr-centos Sonarr on CentOS 7 0 [OK]
docker.io docker.io/pivotaldata/centos-gcc-toolchain CentOS with a toolchain, but unaffiliated ... 0
docker.io docker.io/smartentry/centos centos with smartentry 0 [OK]mysql

將鏡像拉到本地來
[root@linux-node3 ~]# docker pull centos
Using default tag: latest
Trying to pull repository docker.io/library/centos ...
latest: Pulling from docker.io/library/centos
Digest: sha256:6f6d986d425aeabdc3a02cb61c02abb2e78e57357e92417d6d58332856024faf
Status: Image is up to date for docker.io/centos:latestlinux

查看本地鏡像
[root@linux-node3 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
docker.io/centos latest 5182e96772bf 4 days ago 200 MBnginx

運行docker容器
[root@linux-node3 ~]# docker run centos /bin/echo 'hello world'
hello worldsql

查看容器
[root@linux-node3 ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e3d682a89841 centos "/bin/echo 'hello ..." 47 seconds ago Exited (0) 46 seconds ago mystifying_heisenbergdocker

運行容器並進入終端
[root@linux-node3 ~]# docker run --name mydocker -it centos /bin/bash
[root@743b7ce7e900 /]#
--name 給容器取名
-i 讓容器輸入終端保持打開的狀態
-t 開一個僞終端ttycentos

[root@743b7ce7e900 ~]#ls
另開一個終端差段docker狀態
[root@linux-node3 ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
743b7ce7e900 centos "/bin/bash" 13 minutes ago Up 13 minutes mydocker
e3d682a89841 centos "/bin/echo 'hello ..." 22 minutes ago Exited (0) 22 minutes ago mystifying_heisenbergbash

退出容器再查看狀態
[root@743b7ce7e900 ~]# exit
exit
[root@linux-node3 ~]#
另開一個終端差段docker狀態
[root@linux-node3 ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
743b7ce7e900 centos "/bin/bash" 13 minutes ago Exited (0) 2 seconds ago mydocker
e3d682a89841 centos "/bin/echo 'hello ..." 23 minutes ago Exited (0) 23 minutes ago mystifying_heisenberg

[root@linux-node3 ~]# docker run -d --name mynginx nginx
後臺啓動名爲mynginx的docker容器
-d 後臺啓動
--name 指定名稱
nginx 指定鏡像

刪除docker容器(刪除對應的容器ID便可)
[root@linux-node3 ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
4d219df0f742 nginx "nginx -g 'daemon ..." 2 minutes ago Up 2 minutes 80/tcp mynginx
e3d682a89841 centos "/bin/echo 'hello ..." 39 minutes ago Exited (0) 39 minutes ago mystifying_heisenberg
[root@linux-node3 ~]# docker rm e3d682a89841
e3d682a89841
[root@linux-node3 ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
4d219df0f742 nginx "nginx -g 'daemon ..." 3 minutes ago Up 2 minutes 80/tcp mynginx

查看最後一個建立的容器
[root@linux-node3 ~]# docker ps -l
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
4d219df0f742 nginx "nginx -g 'daemon ..." 9 minutes ago Up 9 minutes 80/tcp mynginx

進入容器(docker bug)
[root@linux-node3 ~]# docker attach 4d219df0f742
直接經過 attach 進入容器,沒法進去,須要用到nsenter命令

獲取容器的PID
[root@linux-node3 ~]# docker inspect --format "{{.State.Pid}}" mynginx
7784

進入容器:[root@linux-node3 ~]# nsenter --target 7784 --mount --uts --ipc --net --pid

相關文章
相關標籤/搜索