如何在 CentOS 7 上安裝和使用 Docker 及容器
本文將學習如何安裝 Docker。Docker 是一個非常好的工具,它可以在自己的容器中執行應用程式。它的工作原理類似於虛擬機器,但更便攜、更節省資源,並且對主機作業系統的依賴性更小。在 CentOS 7 上安裝 Docker 有兩種方法。第一種方法是使用現有作業系統進行安裝,或者使用名為 Docker Machine 的工具啟動伺服器並進行自動安裝。
此處出於演示目的,我們將在現有作業系統上進行安裝。
在 CentOS 7 上安裝 Docker
在開始安裝之前,我們需要 64 位 CentOS 7 系統,並且需要一個具有 sudo 許可權的非 root 使用者。Docker 安裝程式可在 CentOS 7 官方儲存庫中找到,但不是最新版本。為了獲得最新版本,我們將從 Docker 官方儲存庫安裝 Docker。
$ sudo yum check-update
現在執行以下命令以新增 Docker 官方儲存庫,下載最新版本的 Docker 並安裝它。
$ curl -fsSL https://get.docker.com/ | sh
請記住,您需要登出並重新登入才能使更改生效!
安裝完成後,我們需要使用以下命令啟動 Docker 守護程序
$ sudo systemctl start docker
要檢視 Docker 服務是否正在執行,我們可以執行以下命令
$ sudo systemctl status docker
我們需要啟用 Docker 在啟動時自動啟動,需要執行以下命令
$ sudo systemctl enable docker
將當前使用者新增到 Docker 組
我們需要將當前使用者新增到系統的 Docker 組,儘管安裝過程中已啟用此功能,但如果沒有此步驟,我們無法使用非 root 使用者執行 Docker 命令列工具。
$ sudo usermod –aG docker $(whoami)
或者
$ sudo usermond –aG docker username
使用 Docker 命令
由於我們剛剛安裝了 Docker 並確認其正在執行,我們需要學習一些命令列工具及其子命令和引數。
$ docker [option] [command] [arguments]
要檢視所有子命令,我們需要輸入以下命令
$ docker CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES [root@centos-linux-1 ~]# docker Usage: docker [OPTIONS] COMMAND [arg...] docker daemon [ --help | ... ] docker [ --help | -v | --version ] A self-sufficient runtime for containers. Options: --config=~/.docker Location of client config files -D, --debug Enable debug mode -H, --host=[] Daemon socket(s) to connect to -h, --help Print usage -l, --log-level=info Set the logging level --tls Use TLS; implied by --tlsverify --tlscacert=~/.docker/ca.pem Trust certs signed only by this CA --tlscert=~/.docker/cert.pem Path to TLS certificate file --tlskey=~/.docker/key.pem Path to TLS key file --tlsverify Use TLS and verify the remote -v, --version Print version information and quit Sub Commands: attach Attach to a running container build Build an image from a Dockerfile commit Create a new image from a container's changes cp Copy files/folders between a container and the local filesystem create Create a new container diff Inspect changes on a container's filesystem events Get real time events from the server exec Run a command in a running container export Export a container's filesystem as a tar archive history Show the history of an image images List images import Import the contents from a tarball to create a filesystem image info Display system-wide information inspect Return low-level information on a container or image kill Kill a running container load Load an image from a tar archive or STDIN login Log in to a Docker registry logout Log out from a Docker registry logs Fetch the logs of a container network Manage Docker networks pause Pause all processes within a container port List port mappings or a specific mapping for the CONTAINER ps List containers pull Pull an image or a repository from a registry push Push an image or a repository to a registry rename Rename a container restart Restart a container rm Remove one or more containers rmi Remove one or more images run Run a command in a new container save Save one or more images to a tar archive search Search the Docker Hub for images start Start one or more stopped containers stats Display a live stream of container(s) resource usage statistics stop Stop a running container tag Tag an image into a repository top Display the running processes of a container unpause Unpause all processes within a container update Update configuration of one or more containers version Show the Docker version information volume Manage Docker volumes wait Block until a container stops, then print its exit code
使用 Docker 映象
Docker 映象用於執行 Docker 容器,這些映象是從 Docker Hub 中拉取的。
要檢查我們是否有權訪問 Docker Hub,請執行以下命令
$ docker run hello-world
我們可以使用 docker 命令及其 search 子命令搜尋 Docker Hub 上可用的映象,例如,我們將搜尋 CentOS 映象,以下是搜尋 Docker Hub 上 CentOS 映象的命令。
[root@centos-linux-1 ~]# docker search centos
NAME DESCRIPTION STARS OFFICIAL AUTOMATED centos The official build of CentOS. 2417 [OK] ansible/centos7-ansible Ansible on Centos7 80 [OK] jdeathe/centos-ssh CentOS-6 6.8 x86_64 / CentOS-7 7.2.1511 x8... 26 [OK] nimmis/java-centos This is docker images of CentOS 7 with dif... 13 [OK] million12/centos-supervisor Base CentOS-7 with supervisord launcher, h... 12 [OK] consol/centos-xfce-vnc Centos container with "headless" VNC sessi... 10 [OK] torusware/speedus-centos Always updated official CentOS docker imag... 8 [OK] jdeathe/centos-ssh-mysql CentOS-6 6.7 x86_64 / MySQL. 7 [OK] nickistre/centos-lamp LAMP on centos setup 4 [OK] centos/mariadb55-centos7 3 [OK] nathonfowlie/centos-jre Latest CentOS image with the JRE pre-insta... 3 [OK] consol/sakuli-centos-xfce Sakuli end-2-end testing and monitoring co... 2 [OK] blacklabelops/centos CentOS Base Image! Built and Updates Daily! 1 [OK] darksheer/centos Base Centos Image -- Updated hourly 1 [OK] timhughes/centos Centos with systemd installed and running 1 [OK] grayzone/centos auto build for centos. 0 [OK] ustclug/centos USTC centos 0 [OK] januswel/centos yum update-ed CentOS image 0 [OK] kz8s/centos Official CentOS plus epel-release 0 [OK] grossws/centos CentOS 6 and 7 base images with gosu and l... 0 [OK] harisekhon/centos-scala Scala + CentOS (OpenJDK tags 2.10-jre7 - 2... 0 [OK] jsmigel/centos-epel Docker base image of CentOS w/ EPEL installed 0 [OK] ericuni/centos centos dev 0 [OK] dmglab/centos CentOS with some extras - This is for the ... 0 [OK] repositoryjp/centos Docker Image for CentOS. 0 [OK]
將 Docker 映象下載到本地
我們可以從 Docker Hub 下載映象,以下是使用 pull 命令下載 Docker 映象的命令
$ docker pull centos docker pull centos Using default tag: latest latest: Pulling from library/centos 0653bff3c5cf: Extracting 29.52 MB/70.6 MB Message from syslogd@centos-linux-1 at Jul 11 08:06:44 ... 0653bff3c5cf: Pull complete Digest: sha256:81e4f2f663eaa1bf46ff9be348396dd7053734b257ef4147d7133d6f25bbf7cf Status: Downloaded newer image for centos:latest
我們可以使用 run 子命令執行我們下載的映象
$ docker run centos
並使用以下命令檢視下載到我們計算機的映象
$ docker images docker images REPOSITORY TAG IMAGE ID CREATED SIZE centos latest 05188b417f30 9 days ago 196.8 MB hello-world latest c54a2cc56cbb 9 days ago 1.848 kB
執行 Docker 容器
由於我們已將容器映象下載到本地系統,我們將使用 CentOS 的最新映象以及 –i 和 –t 開關執行容器,這些開關將為容器提供互動式 shell
$ docker run –it centos [root@0aac55bf786f /]#
提示符將更改為容器 ID,“0aac55bf786f”是下載容器的 ID
我們可以使用以下命令在容器中安裝一些軟體,我們將在此容器中安裝 MySQL 伺服器
[root@0aac55bf786f /]# yum install mysql-server
在容器中安裝 MySQL 後,我們可以使用以下命令將更改提交到 Docker Hub,其中 –m 開關用於提交訊息,–a 用於新增作者姓名
General Syntax $ docker commit -m "What did you do to the image" -a "Author Name" container-id repository/new_image_name
Example $ docker commit -m "added MySQL-server" -a "Chandra Prakash" 0aac55bf786f finid/centos-mysql_db sha256:77993fb3b476e02324b8bf7ebfebe1e792b93df2eecc70208997ac95455c65e8
要確認映象已提交併列在本地儲存庫中,您可以執行以下命令。
$ docker images REPOSITORY TAG IMAGE ID CREATED SIZE finid/centos-mysql_db latest 77993fb3b476 28 seconds ago 268.4 MB centos latest 05188b417f30 9 days ago 196.8 MB hello-world latest c54a2cc56cbb 9 days ago 1.848 kB
在完成設定和配置後,我們學習了 Docker 及其容器的基礎知識,以及如何拉取映象、使用映象以及安裝一些軟體。藉助 Docker,我們可以做更多的事情,在後續文章中,我們將學習更多內容。