一、簡介
- 本文主要介紹如何使用docker安裝centos鏡像。
- 有了centos鏡像之後,就可以在該鏡像中安裝一些環境,軟件等等,方便複製。
- 一般有倆種方式安裝,一種是直接拉取開源的鏡像,一種是使用dockerfile定製安裝。
二、拉取開源鏡像安裝
2.1 查詢開源centos鏡像
- 使用docker search 命令來查詢開源鏡像;
[root@yuanshushu ~]# docker search centos
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
centos The official build of CentOS. 5649 [OK]
ansible/centos7-ansible Ansible on Centos7 125 [OK]
jdeathe/centos-ssh OpenSSH / Supervisor / EPEL/IUS/SCL Repos - … 114 [OK]
consol/centos-xfce-vnc Centos container with "headless" VNC session… 100 [OK]
centos/mysql-57-centos7 MySQL 5.7 SQL database server 64
imagine10255/centos6-lnmp-php56 centos6-lnmp-php56 57 [OK]
tutum/centos Simple CentOS docker image with SSH access 44
centos/postgresql-96-centos7 PostgreSQL is an advanced Object-Relational … 39
kinogmt/centos-ssh CentOS with SSH 29 [OK]
pivotaldata/centos-gpdb-dev CentOS image for GPDB development. Tag names… 10
guyton/centos6 From official centos6 container with full up… 9 [OK]
drecom/centos-ruby centos ruby 6 [OK]
centos/tools Docker image that has systems administration… 4 [OK]
darksheer/centos Base Centos Image -- Updated hourly 3 [OK]
mamohr/centos-java Oracle Java 8 Docker image based on Centos 7 3 [OK]
pivotaldata/centos Base centos, freshened up a little with a Do… 3
pivotaldata/centos-mingw Using the mingw toolchain to cross-compile t… 2
miko2u/centos6 CentOS6 日本語環境 2 [OK]
pivotaldata/centos-gcc-toolchain CentOS with a toolchain, but unaffiliated wi… 2
indigo/centos-maven Vanilla CentOS 7 with Oracle Java Developmen… 1 [OK]
blacklabelops/centos CentOS Base Image! Built and Updates Daily! 1 [OK]
mcnaughton/centos-base centos base image 1 [OK]
pivotaldata/centos7-dev CentosOS 7 image for GPDB development 0
smartentry/centos centos with smartentry 0 [OK]
pivotaldata/centos6.8-dev CentosOS 6.8 image for GPDB development 0
2.2 下載官方開源鏡像
- 下載的默認是最新的,我也不知道是不是centos7版本。。。。
[root@yuanshushu ~]# docker pull centos
- 查看本地鏡像
[root@yuanshushu ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos latest 0f3e07c0138f 5 weeks ago 220MB
2.3 創建容器並運行
- 基於鏡像centos:latest鏡像創建一個名稱爲centos-01的容器,並進入;
- -t: 在新容器內指定一個僞終端或終端;
- -i: 允許你對容器內的標準輸入 (STDIN) 進行交互;
- --name: 爲命名容器名稱;
- centos爲源鏡像名稱;
- /bin/bash 爲 進入新的容器centos-01;
- 注意:進入之後,@後面的名稱就改爲了容器id;
[root@yuanshushu ~]# docker run -it --name centos-01 centos /bin/bash
[root@8e2b1edba516 /]#
- 也可以使用-d命令來後臺啓動容器,然後使用attach進入
[root@yuanshushu ~]# docker run -itd --name centos-002 centos /bin/bash
aeb0617e6a98d9282ae4cb00ef2de38e0f172f832db5d3ac72fa7cfe680aef92
[root@yuanshushu ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
aeb0617e6a98 centos "/bin/bash" 14 seconds ago Up 13 seconds centos-002
[root@yuanshushu ~]# docker attach aeb0617e6a98
[root@aeb0617e6a98 /]#
- 此時可以查看centos-01內部文件結構,如下:
[root@8e2b1edba516 /]# ls
bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var
- 可以使用exit命令退出容器
- 注意此時@已經回退到宿主機上了
[root@737b37aa53df /]# exit
exit
[root@yuanshushu ~]#
- 查看所有的容器
[root@yuanshushu ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
ab7dc6a5f4ae centos:latest "/bin/bash" 8 minutes ago Exited (0) 8 minutes ago centos-001
8e2b1edba516 centos "/bin/bash" 13 minutes ago Exited (127) 10 minutes ago centos-01