CentOs7 docker 安裝與基本操做

本篇介紹在 centos7 操做系統下 docker 的安裝與基本操做。java

安裝 dockerlinux

yum remove -y docker docker-common container-selinux docker-selinux docker-engine docker-engine-selinux && \

yum install -y yum-utils device-mapper-persistent-data lvm2 && \

yum-config-manager --enable extras && \

yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo && \

yum makecache fast && \

yum install -y docker-ce

基本上是先刪除以前安裝過的 docker 模塊,再安裝一些必要的工具,添加 docker 的 yum 源,最後使用yum 安裝 docker。web

啓動 docker 服務spring

systemctl start docker

中止 docker 服務docker

systemctl stop docker

查看 docker 基本信息apache

docker info
[root@localhost ~]# docker info
Containers: 0
 Running: 0
 Paused: 0
 Stopped: 0
Images: 0
Server Version: 17.06.1-ce
Storage Driver: overlay
 Backing Filesystem: xfs
 Supports d_type: true
Logging Driver: json-file
Cgroup Driver: cgroupfs
Plugins: 
 Volume: local
 Network: bridge host macvlan null overlay
 Log: awslogs fluentd gcplogs gelf journald json-file logentries splunk syslog
Swarm: inactive
Runtimes: runc
Default Runtime: runc
Init Binary: docker-init
containerd version: 6e23458c129b551d5c9871e5174f6b1b7f6d1170
runc version: 810190ceaa507aa2727d7ae6f4790c76ec150bd2
init version: 949e6fa
Security Options:
 seccomp
  Profile: default
Kernel Version: 3.10.0-514.el7.x86_64
Operating System: CentOS Linux 7 (Core)
OSType: linux
Architecture: x86_64
CPUs: 1
Total Memory: 992.7MiB
Name: localhost.localdomain
ID: PSVE:UJSY:QW2G:FE23:KFCI:FYXR:XJBX:XP6U:2TYN:P54I:QMWZ:MIZB
Docker Root Dir: /var/lib/docker
Debug Mode (client): false
Debug Mode (server): false
Registry: https://index.docker.io/v1/
Experimental: false
Insecure Registries:
 127.0.0.0/8
Live Restore Enabled: false

docker 基本操做json

查看鏡像ubuntu

docker images
[root@localhost ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
centos              latest              328edcd84f1b        10 days ago         193MB
[root@localhost ~]#

當前有一個 centos 的鏡像centos

刪除全部鏡像bash

docker rmi $(docker images -q)
[root@localhost ~]# docker rmi $(docker images -q)
Untagged: centos:latest
Untagged: centos@sha256:26f74cefad82967f97f3eeeef88c1b6262f9b42bc96f2ad61d6f3fdf544759b8
Deleted: sha256:328edcd84f1bbf868bc88e4ae37afe421ef19be71890f59b4b2d8ba48414b84d
[root@localhost ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
[root@localhost ~]#

centos 鏡像已被刪除

運行容器

docker run xxx(鏡像名)
[root@localhost ~]# docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
b04784fba78d: Pull complete 
Digest: sha256:f3b3b28a45160805bb16542c9531888519430e9e6d6ffc09d72261b0d26ff74f
Status: Downloaded newer image for hello-world:latest

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.
 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 run 發現本地沒有名爲 hello-world 的鏡像, 自動從 docker hub 上拉取到本地

[root@localhost ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
hello-world         latest              1815c82652c0        2 months ago        1.84kB

查看正在運行的容器

docker ps
[root@localhost ~]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                    NAMES
565ebd954fd6        registry            "/entrypoint.sh /e..."   12 minutes ago      Up 12 minutes       0.0.0.0:5000->5000/tcp   peaceful_fermat
7c2b28c1c8b2        sample_img          "java -jar /data/s..."   23 minutes ago      Up 23 minutes       0.0.0.0:9000->8080/tcp   c_sample

這有2個容器在運行,ID 分別爲 565ebd954fd6 和  7c2b28c1c8b2

查看全部容器

docker ps -a
[root@localhost ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED              STATUS                          PORTS               NAMES
7a7386a0c148        hello-world         "/hello"            About a minute ago   Exited (0) About a minute ago                       objective_clarke

刪除全部容器

docker rm $(docker ps -a -q)

從 docker hub 上面拉取一個 centos 鏡像

docker pull xxx (鏡像名)
docker pull centos:latest

根據Dockfile(稍後會介紹)構建鏡像

docker build -t xxx(鏡像名) .

根據Dockfile(稍後會介紹)構建好的鏡像啓動一個容器

docker run --name xxx(容器名) -d -p xxx(docker端口):xxx(程序端口) xxx(鏡像名)

查看容器日誌

docker logs -f xxx(容器ID)

進入正在運行的容器內部

docker exec -it xxx (容器ID) /bin/bash

提交容器

docker commit -m "註釋內容" xxx(容器ID) xxx(新的鏡像名)

鏡像重命名

docker tag centos(原鏡像名) cnetos_jdk:latest(新鏡像名)

搭建私有倉庫

docker pull registry
docker run -d -p 5000:5000 registry

默認狀況下,會將倉庫存放於容器內的/tmp/registry目錄下,這樣若是容器被刪除,則存放於容器中的鏡像也會丟失,因此咱們通常狀況下會指定本地一個目錄掛載到容器內的/tmp/registry下

docker run -d -p 5000:5000 -v /data/registry:/tmp/registry registry

重命名鏡像

[root@localhost ~]# docker tag sample_img 127.0.0.1:5000/sample_img:latest
[root@localhost ~]# docker images
REPOSITORY                  TAG                 IMAGE ID            CREATED             SIZE
127.0.0.1:5000/sample_img   latest              33c863b35655        17 minutes ago      584MB
sample_img                  latest              33c863b35655        17 minutes ago      584MB
...
[root@localhost ~]#

上傳鏡像到私有倉庫

[root@localhost ~]# docker push 127.0.0.1:5000/sample_img
The push refers to a repository [127.0.0.1:5000/sample_img]
3935bdccc733: Pushed 
0dd42e651e88: Pushed 
b36ecdce0393: Pushed 
b362758f4793: Pushed 
latest: digest: sha256:605d2cade53a657e32e1fc33e483c81591e1e8afb85e4ce0deefac54e94206c8 size: 1161

檢查倉庫鏡像

curl 127.0.0.1:5000/v2/_catalog
[root@localhost ~]# curl 127.0.0.1:5000/v2/_catalog
{"repositories":["sample_img"]}

下次用到的話能夠直接從本地鏡像下載,沒必要再去 docker hub 上面下載。

如遇到 相似的問題

http: server gave HTTP response to HTTPS client

在 /etc/docker/ 目錄下,建立 daemon.json 文件。在文件中寫入:

{ "insecure-registries":["127.0.0.1:5000"] }

保存退出後,重啓docker

 

注意:由於某種緣由 docker 私有倉庫沒有刪除鏡像的功能。當前時間是 2017-08-22, docker 版本是 17.06.1-ce,不知道之後會不會添加這個功能。

導出容器

docker export xxx(容器ID) > xxx.tar

導入容器

docker import - xxx.tar

導出鏡像

docker save xxx(鏡像ID) > xxx.tar

導入鏡像

docker load < xxx.tar

 

 

下面演示使用 docker 啓動一個 spring boot 應用

使用 Dockerfile 建立一個鏡像。在任意目錄建立一個 Dockerfile

vi dockerfile

輸入如下內容

FROM centos:latest

LABEL author="mike" version="1.0"

ENV JAVA_HOME /data/jdk1.8.0_131
ENV PATH $JAVA_HOME/bin:$PATH
ENV TZ Asia/Shanghai

ADD sample.jar /data/
ADD jdk-8u144-linux-x64.tar.gz /data/

RUN cp /usr/share/zoneinfo/$TZ /etc/localtime

EXPOSE 8080

ENTRYPOINT ["java", "-jar", "/data/sample.jar"]

FROM centos:latest 先檢索本地有沒有名字爲 centos 標籤爲 latest 的鏡像,若是沒有檢測到會去 docker hub 上面拉取鏡像到本地。命令等同於 docker pull centos:latest, 鏡像名後面不加標籤默認拉取最新的版本(latest),不然拉取指定版本的鏡像。例如我還能夠拉取 centos:6 centos:6.9 ubuntu:16.04 之類的版本的鏡像。

LABEL author="mike" version="1.0" 添加 author, version 變量。

ENV JAVA_HOME /data/jdk1.8.0_131 設置 JAVA_HOME 環境變量

ENV PATH $JAVA_HOME/bin:$PATH 設置 PATH 環境變量

ENV TZ Asia/Shanghai 設置時區環境變量

ADD sample.jar /data/ 添加 spring boot 應用到 docker 容器內部 /data/ 文件夾

ADD jdk-8u144-linux-x64.tar.gz /data/ 添加 jdk1.8 到 docker 容器內部 /data/ 文件夾

RUN cp /usr/share/zoneinfo/$TZ /etc/localtime 複製宿主機的時區到 docker 容器內(解決 docker 內部時間和宿主機不一致的問題)

EXPOSE 8080 docker 內部暴露 8080 端口, 實際爲 spring boot 啓動的端口號

ENTRYPOINT ["java", "-jar", "/data/sample.jar"] 程序入口,啓動 docker 容器的時候實際執行的是這裏的命令

具體關於 Dockerfile 如何使用,官網上面提供了詳細的教程。有興趣能夠仔細看下。(https://docs.docker.com/engine/reference/builder/)

 

構建鏡像, jdk 和 sample.jar 必須和 Dockerfile 放在同一個目錄下

docker build -t sample_img .
[root@localhost ~]# docker build -t sample_img .
Sending build context to Docker daemon  200.8MB
Step 1/10 : FROM centos:latest
 ---> 328edcd84f1b
Step 2/10 : LABEL author "mike" version "1.0"
 ---> Using cache
 ---> 296971e5a18b
Step 3/10 : ENV JAVA_HOME /data/jdk1.8.0_131
 ---> Using cache
 ---> adbafff9ec20
Step 4/10 : ENV PATH $JAVA_HOME/bin:$PATH
 ---> Using cache
 ---> a82606788be7
Step 5/10 : ENV TZ Asia/Shanghai
 ---> Using cache
 ---> ecdbca9ad394
Step 6/10 : ADD sample.jar /data/
 ---> 2eb7bf1d4518
Removing intermediate container bd8c795f540c
Step 7/10 : ADD jdk-8u144-linux-x64.tar.gz /data/
 ---> a28d4094a526
Removing intermediate container f2915c259563
Step 8/10 : RUN cp /usr/share/zoneinfo/$TZ /etc/localtime
 ---> Running in 989ba6843fb9
 ---> 18caa6df7026
Removing intermediate container 989ba6843fb9
Step 9/10 : EXPOSE 8080
 ---> Running in 184105d158a2
 ---> 6ba63148d968
Removing intermediate container 184105d158a2
Step 10/10 : ENTRYPOINT java -jar /data/sample.jar
 ---> Running in a91fae2179ff
 ---> 6debaae62f6d
Removing intermediate container a91fae2179ff
Successfully built 6debaae62f6d
Successfully tagged sample_img:latest

查看鏡像能夠看到 sample_img 已經生成

[root@localhost ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
sample_img          latest              6debaae62f6d        22 seconds ago      584MB
centos              latest              328edcd84f1b        10 days ago         193MB
hello-world         latest              1815c82652c0        2 months ago        1.84kB

啓動容器

[root@localhost ~]# docker run --name c_sample -d -p 9000:8080 sample_img
7c2b28c1c8b24a5423e65851b6fc851d96ecdd9fdc4edc2d493023eab4ce4526

docker run 運行容器命令

--name 指定容器名是 c_sample

-d 後臺運行

-p 指定容器的 8080 端口對外映射到 9000 端口 

sample_img 鏡像名

 

查看容器

[root@localhost ~]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                    NAMES
7c2b28c1c8b2        sample_img          "java -jar /data/s..."   31 seconds ago      Up 30 seconds       0.0.0.0:9000->8080/tcp   c_sample

容器在 31 秒前建立

查看容器日誌

docker logs -f 7c2b
docker logs -f 7c2b28c1c8b2
docker logs -f c_sample

以上3個命令等價,能夠經過容器ID或者容器的名字或者容器ID前4位查看。

[root@localhost ~]# docker logs -f c_sample

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v1.5.4.RELEASE)

2017-08-14 15:32:40.334  INFO 1 --- [           main] com.ccm.Application                      : Starting Application on 7c2b28c1c8b2 with PID 1 (/data/sample.jar started by root in /)
2017-08-14 15:32:40.343  INFO 1 --- [           main] com.ccm.Application                      : No active profile set, falling back to default profiles: default
2017-08-14 15:32:40.559  INFO 1 --- [           main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@4b9af9a9: startup date [Mon Aug 14 15:32:40 CST 2017]; root of context hierarchy
2017-08-14 15:32:44.999  INFO 1 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8080 (http)
2017-08-14 15:32:45.037  INFO 1 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2017-08-14 15:32:45.048  INFO 1 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet Engine: Apache Tomcat/8.5.15
2017-08-14 15:32:45.354  INFO 1 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2017-08-14 15:32:45.355  INFO 1 --- [ost-startStop-1] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 4806 ms
....

docker logs -f 效果等同於 linux 中 tail -f

進入容器內部

[root@localhost ~]# docker exec -it c_sample /bin/bash
[root@7c2b28c1c8b2 /]#

能夠看到 /data/下面是咱們剛纔添加的文件

[root@7c2b28c1c8b2 /]# ll /data/
total 14932
drwxr-xr-x. 8   10  143      255 Aug 14 15:31 jdk1.8.0_144
-rw-r--r--. 3 root root 15287879 Aug 14 15:18 sample.jar
[root@7c2b28c1c8b2 /]#

測試容器是否啓動成功

bingaos-MacBook-Pro:Desktop bingao$ curl -i http://192.168.0.106:9000/hello
HTTP/1.1 200 
Content-Type: text/plain;charset=ISO-8859-1
Content-Length: 12
Date: Mon, 14 Aug 2017 07:50:46 GMT

Hello World!bingaos-MacBook-Pro:Desktop bingao$

對 9000 端口(咱們剛纔設置的)發送了 /hello 請求,成功返回 Hello World!

相關文章
相關標籤/搜索