Docker 是一個開源的應用容器引擎,讓開發者能夠打包他們的應用以及依賴包到一個可移植的容器中,而後發佈到任何流行的 Linux 機器上,也能夠實現虛擬化。容器是徹底使用沙箱機制,相互之間不會有任何接口。php
此處主要講解在CentOS-7.3中安裝Docker環境python
CentOS Linux release 7.3.1611mysql
最新版本 Docker 19.03.1linux
注意 yum默認安裝的docker爲 1.13 版本,比較舊並且並非開源社區維護的版本,目前開源社區最新版本爲 19.03.1,社區分爲開源版本(docker-ce)和企業版本(docker-ee)。 須要配置Docker官方源才能夠安裝最新版本,此處咱們安裝開源社區版本 docker-ce。sql
yum remove docker \ docker-common \ docker-selinux \ docker-engine
首先添加依賴,以下:docker
yum install -y yum-utils device-mapper-persistent-data lvm2
鑑於國內網絡問題,強烈建議使用國內源,以下:json
yum-config-manager \ --add-repo \ https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
安裝最新版docker,此處安裝社區開源版,以下:ubuntu
[root@localhost yum.repos.d]# yum install -y docker-ce
執行以下命令可查看Docker版本:centos
docker -v
顯示版本信息以下:ruby
Docker version 19.03.1, build 74b1e89
systemctl start docker && systemctl enable docker
docker run hello-world
此時會出現沒法鏈接的狀況,由於Docker的官網被牆,默認的鏡像源地址沒法鏈接,會提示以下信息:
[root@localhost docker]# docker run hello-world /usr/bin/docker-current: Cannot connect to the Docker daemon. Is the docker daemon running on this host?. See '/usr/bin/docker-current run --help'.
解決此問題則須要修改鏡像源地址,修改 /etc/docker/daemon.json 文件,填寫Docker 官方中國區鏡像源地址,修改後/etc/docker/daemon.json 內容以下:
{ "registry-mirrors": ["https://registry.docker-cn.com"] }
也可以使用國內其餘Docker鏡像源地址
修改完成後從新執行 測試hello-world鏡像,本地沒有的話則會從遠端鏡像庫下載,成功執行信息以下:
[root@localhost docker]# docker run hello-world Unable to find image 'hello-world:latest' locally Trying to pull repository docker.io/library/hello-world ... latest: Pulling from docker.io/library/hello-world ca4f61b1923c: Pull complete Digest: sha256:445b2fe9afea8b4aa0b2f27fe49dd6ad130dfe7a8fd0832be5de99625dad47cd Hello from Docker! This message shows that your installation appears to be working correctly. To generate this message, Docker took the following steps: 1. The Docker client contacted the Docker daemon. 2. The Docker daemon pulled the "hello-world" image from the Docker Hub. (amd64) 3. The Docker daemon created a new container from that image which runs the executable that produces the output you are currently reading. 4. The Docker daemon streamed that output to the Docker client, which sent it to your terminal. To try something more ambitious, you can run an Ubuntu container with: $ docker run -it ubuntu bash Share images, automate workflows, and more with a free Docker ID: https://cloud.docker.com/ For more examples and ideas, visit: https://docs.docker.com/engine/userguide/
看到此信息,則說明安裝Docker成功。
Compose 是一個用戶定義和運行多個容器的 Docker 應用程序。在 Compose 中你可使用 YAML 文件來配置你的應用服務。而後,只須要一個簡單的命令,就能夠建立並啓動你配置的全部服務。
使用 Compose 基本會有以下三步流程:
dcoker-compose up
,Compose 將啓動並運行整個應用程序。
安裝python-pip
yum -y install epel-release yum -y install python-pip
安裝docker-compose
pip install docker-compose
裝完成後,執行查詢版本的命令,便可安裝docker-compose
[root@localhost ~]# docker-compose version docker-compose version 1.21.2, build a133471 docker-py version: 3.3.0 CPython version: 2.7.5 OpenSSL version: OpenSSL 1.0.2k-fips 26 Jan 2017
須要首先清晰的認識Docker中鏡像與容器之間的關係,鏡像和容器就至關於Java中的類和對象的關係,鏡像是對容器的初始定義,運行鏡像以後會獲得容器,容器提交版本後會生成鏡像,Docker 的大部分操做都是針對容器的。
查看本地有哪些鏡像文件
[root@localhost docker]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE docker.io/hello-world latest f2a91732366c 4 weeks ago 1.848 kB
可經過搜索查找本身想要的鏡像,此處咱們搜索centos
[root@localhost docker]# docker search centos INDEX NAME DESCRIPTION STARS OFFICIAL AUTOMATED docker.io docker.io/centos The official build of CentOS. 3893 [OK] docker.io docker.io/ansible/centos7-ansible Ansible on Centos7 103 [OK] docker.io docker.io/jdeathe/centos-ssh CentOS-6 6.9 x86_64 / CentOS-7 7.4.1708 x8... 90 [OK] docker.io docker.io/consol/centos-xfce-vnc Centos container with "headless" VNC sessi... 37 [OK] docker.io docker.io/imagine10255/centos6-lnmp-php56 centos6-lnmp-php56 35 [OK] docker.io docker.io/tutum/centos Simple CentOS docker image with SSH access 34 docker.io docker.io/gluster/gluster-centos Official GlusterFS Image [ CentOS-7 + Glu... 21 [OK] docker.io docker.io/kinogmt/centos-ssh CentOS with SSH 18 [OK] docker.io docker.io/centos/python-35-centos7 Platform for building and running Python 3... 14 docker.io docker.io/openshift/base-centos7 A Centos7 derived base image for Source-To... 13 docker.io docker.io/openshift/jenkins-2-centos7 A Centos7 based Jenkins v2.x image for use... 9 docker.io docker.io/openshift/mysql-55-centos7 DEPRECATED: A Centos7 based MySQL v5.5 ima... 6 docker.io docker.io/darksheer/centos Base Centos Image -- Updated hourly 3 [OK] docker.io docker.io/openshift/ruby-20-centos7 DEPRECATED: A Centos7 based Ruby v2.0 imag... 3 docker.io docker.io/blacklabelops/centos CentOS Base Image! Built and Updates Daily! 1 [OK] docker.io docker.io/indigo/centos-maven Vanilla CentOS 7 with Oracle Java Developm... 1 [OK] docker.io docker.io/miko2u/centos6 CentOS6 日本語環境 1 [OK] docker.io docker.io/openshift/php-55-centos7 DEPRECATED: A Centos7 based PHP v5.5 image... 1 docker.io docker.io/pivotaldata/centos-gpdb-dev CentOS image for GPDB development. Tag nam... 1 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/openshift/wildfly-101-centos7 A Centos7 based WildFly v10.1 image for us... 0 docker.io docker.io/pivotaldata/centos Base centos, freshened up a little with a ... 0 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]
可按照搜索結果名稱進行獲取,此處咱們獲取 發佈版本Centos:The official build of CentOS
[root@localhost docker]# docker pull docker.io/centos Using default tag: latest Trying to pull repository docker.io/library/centos ... latest: Pulling from docker.io/library/centos 85432449fd0f: Pull complete Digest: sha256:3b1a65e9a05f0a77b5e8a698d3359459904c2a354dc3b25ae2e2f5c95f0b3667
此處運行剛纔獲取的鏡像,鏡像運行以後成爲容器。
[root@localhost docker]# docker run -i -t docker.io/centos /bin/bash [root@d86eed7c4bf0 /]# ls anaconda-post.log bin dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var
此時已進入鏡像文件內部,此時咱們能夠對此容器進行更改,好比在 /home目錄下建立test.txt文件,內容爲 Hello my test file ! 。
[root@d86eed7c4bf0 /]# exit exit [root@localhost docker]#
exit 命令會退出容器而且關閉容器,若是隻須要退出容器但不須要關閉則可按快捷鍵:Ctrl+P+Q
此處定義提交的容器的Tab爲 v1.1
[zhpt@localhost ~]$ docker commit -m ="Commit message" -a="Author" 22fd7c4c0668 docker.io/centos:v1.1 sha256:acef7126a6ac4e5abd3acfcd92655d1e41a45b9cc57b7d60eea7d75dc175667e
但是用 docker images 命令查看新提交的鏡像,以下:
[zhpt@localhost ~]$ docker images REPOSITORY TAG IMAGE ID CREATED SIZE docker.io/centos v1.1 acef7126a6ac 4 minutes ago 271.3 MB docker.io/centos latest 3fa822599e10 2 weeks ago 203.5 MB docker.io/hello-world latest f2a91732366c 4 weeks ago 1.848 kB
刪除剛纔新提交的鏡像文件:docker.io/centos:v1.1
[zhpt@localhost ~]$ docker rmi docker.io/centos:v1.1 Untagged: docker.io/centos:v1.1 Deleted: sha256:acef7126a6ac4e5abd3acfcd92655d1e41a45b9cc57b7d60eea7d75dc175667e Deleted: sha256:47744657ffff5c29bc5f73c0bc8ad9677688a50d89dda5777ef7467603fe2469 [zhpt@localhost ~]$ docker images REPOSITORY TAG IMAGE ID CREATED SIZE docker.io/centos latest 3fa822599e10 2 weeks ago 203.5 MB docker.io/hello-world latest f2a91732366c 4 weeks ago 1.848 kB
在鏡像運行後,關閉以前能夠查看正在運行的鏡像
[zhpt@localhost ~]$ docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES d86eed7c4bf0 docker.io/centos "/bin/bash" About a minute ago Up About a minute elated_wescoff
[zhpt@localhost ~]$ docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES d86eed7c4bf0 docker.io/centos "/bin/bash" 15 hours ago Up 4 minutes elated_wescoff [zhpt@localhost ~]$ docker kill d86eed7c4bf0 d86eed7c4bf0 [zhpt@localhost ~]$ docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES [zhpt@localhost ~]$
也可中止全部正在執行的容器
[zhpt@localhost ~]$ docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES d86eed7c4bf0 docker.io/centos "/bin/bash" 15 hours ago Up 2 minutes elated_wescoff [zhpt@localhost ~]$ docker kill $(docker ps -q) d86eed7c4bf0 [zhpt@localhost ~]$ docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES [zhpt@localhost ~]$
只可刪除已經中止的容器,正在運行的容器是沒法刪除的。
[zhpt@localhost ~]$ docker rm 22fd7c4c0668 22fd7c4c0668 [zhpt@localhost ~]$ docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES d86eed7c4bf0 docker.io/centos "/bin/bash" 39 minutes ago Exited (127) 38 minutes ago elated_wescoff bda7d545ac4c docker.io/centos "/bin/bash" 2 hours ago Exited (0) About an hour ago clever_mestorf e1c6121e4845 hello-world "/hello" 2 hours ago Exited (0) 2 hours ago suspicious_ardinghelli
刪除全部已經中止的容器
[root@localhost zhpt]# docker rm $(docker ps -a -q) 2853ae52f76d 5c721cd6810b fd2123aa64a2 84a6405887b4 bda7d545ac4c e1c6121e4845
[zhpt@localhost ~]$ docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES d86eed7c4bf0 docker.io/centos "/bin/bash" 15 hours ago Exited (137) 47 seconds ago elated_wescoff bda7d545ac4c docker.io/centos "/bin/bash" 17 hours ago Exited (0) 16 hours ago clever_mestorf e1c6121e4845 hello-world "/hello" 17 hours ago Exited (0) 6 minutes ago suspicious_ardinghelli [zhpt@localhost ~]$ docker start -i d86eed7c4bf0 [root@d86eed7c4bf0 /]#
由於容器的ID是隨機碼,而容器的名字又是看似無心義的命名,咱們可使用命令重命名,可以使用新名稱對容器進行操做。
[zhpt@localhost ~]$ docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES d86eed7c4bf0 docker.io/centos "/bin/bash" 15 hours ago Exited (137) 4 minutes ago elated_wescoff bda7d545ac4c docker.io/centos "/bin/bash" 17 hours ago Exited (0) 16 hours ago clever_mestorf e1c6121e4845 hello-world "/hello" 17 hours ago Exited (0) 14 minutes ago suspicious_ardinghelli [zhpt@localhost ~]$ docker rename elated_wescoff my_centos [zhpt@localhost ~]$ docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES d86eed7c4bf0 docker.io/centos "/bin/bash" 15 hours ago Exited (137) 4 minutes ago my_centos bda7d545ac4c docker.io/centos "/bin/bash" 17 hours ago Exited (0) 16 hours ago clever_mestorf e1c6121e4845 hello-world "/hello" 17 hours ago Exited (0) 14 minutes ago suspicious_ardinghelli [zhpt@localhost ~]$ docker start -i my_centos [root@d86eed7c4bf0 /]#
[zhpt@localhost ~]$ docker start -i my_centos [root@d86eed7c4bf0 /]# [zhpt@localhost ~]$ [zhpt@localhost ~]$ docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES d86eed7c4bf0 docker.io/centos "/bin/bash" 16 hours ago Up 14 minutes my_centos [zhpt@localhost ~]$ docker attach d86eed7c4bf0 [root@d86eed7c4bf0 /]#
推薦使用以下方式:
[root@localhost es]# docker exec -it 0f19337a8ac9 /bin/bash root@0f19337a8ac9:/usr/src/app#
一、導出鏡像爲文件
二、導入文件爲鏡像
三、重命名鏡像