Docker docker部署以及运行你的第一个容器
程序员文章站
2024-02-22 09:40:10
...
安装 Docker
本节我们将在centos7.4中安装 Docker。
Docker 支持几乎所有的 Linux 发行版,也支持 Mac 和 Windows。各操作系统的安装方法可以访问:https://docs.docker.com/engine/installation/
Docker 分为开源免费的 CE(Community Edition)版本和收费的 EE(Enterprise Edition)版本。下面我们将按照文档,通过以下步骤安装 Docker CE 版本。
配置docker ce版本的repo源
[aaa@qq.com yum.repos.d]# cat docker-ce.repo
[docker-ce-stable]
name=Docker CE Stable - $basearch
baseurl=https://download.docker.com/linux/centos/7/$basearch/stable
enabled=1
gpgcheck=1
gpgkey=https://download.docker.com/linux/centos/gpg
[docker-ce-stable-debuginfo]
name=Docker CE Stable - Debuginfo $basearch
baseurl=https://download.docker.com/linux/centos/7/debug-$basearch/stable
enabled=0
gpgcheck=1
gpgkey=https://download.docker.com/linux/centos/gpg
[docker-ce-stable-source]
name=Docker CE Stable - Sources
baseurl=https://download.docker.com/linux/centos/7/source/stable
enabled=0
gpgcheck=1
gpgkey=https://download.docker.com/linux/centos/gpg
[docker-ce-edge]
name=Docker CE Edge - $basearch
baseurl=https://download.docker.com/linux/centos/7/$basearch/edge
enabled=0
gpgcheck=1
gpgkey=https://download.docker.com/linux/centos/gpg
[docker-ce-edge-debuginfo]
name=Docker CE Edge - Debuginfo $basearch
baseurl=https://download.docker.com/linux/centos/7/debug-$basearch/edge
enabled=0
gpgcheck=1
gpgkey=https://download.docker.com/linux/centos/gpg
[docker-ce-edge-source]
name=Docker CE Edge - Sources
baseurl=https://download.docker.com/linux/centos/7/source/edge
enabled=0
gpgcheck=1
gpgkey=https://download.docker.com/linux/centos/gpg
[docker-ce-test]
name=Docker CE Test - $basearch
baseurl=https://download.docker.com/linux/centos/7/$basearch/test
enabled=0
gpgcheck=1
gpgkey=https://download.docker.com/linux/centos/gpg
[docker-ce-test-debuginfo]
name=Docker CE Test - Debuginfo $basearch
baseurl=https://download.docker.com/linux/centos/7/debug-$basearch/test
enabled=0
gpgcheck=1
gpgkey=https://download.docker.com/linux/centos/gpg
[docker-ce-test-source]
name=Docker CE Test - Sources
baseurl=https://download.docker.com/linux/centos/7/source/test
enabled=0
gpgcheck=1
gpgkey=https://download.docker.com/linux/centos/gpg
[docker-ce-nightly]
name=Docker CE Nightly - $basearch
baseurl=https://download.docker.com/linux/centos/7/$basearch/nightly
enabled=0
gpgcheck=1
gpgkey=https://download.docker.com/linux/centos/gpg
[docker-ce-nightly-debuginfo]
name=Docker CE Nightly - Debuginfo $basearch
baseurl=https://download.docker.com/linux/centos/7/debug-$basearch/nightly
enabled=0
gpgcheck=1
gpgkey=https://download.docker.com/linux/centos/gpg
[docker-ce-nightly-source]
name=Docker CE Nightly - Sources
baseurl=https://download.docker.com/linux/centos/7/source/nightly
enabled=0
gpgcheck=1
gpgkey=https://download.docker.com/linux/centos/gpg
使用yum源安装docker
[aaa@qq.com ~]# yum install epel-release -y
[aaa@qq.com ~]# ll /etc/yum.repos.d/epel*
-rw-r--r-- 1 root root 1050 Sep 17 2019 /etc/yum.repos.d/epel.repo
-rw-r--r-- 1 root root 1149 Sep 17 2019 /etc/yum.repos.d/epel-testing.repo
yum安装软件包,我这里安装CE版本不安装1.13版本
[aaa@qq.com ~]# yum install docker* -y 1.13版本
[aaa@qq.com ~]# yum install docker-ce* -y CE版本社区版本
检擦docker软件包是否部署成功
[aaa@qq.com ~]# rpm -qa | grep docker
[aaa@qq.com ~]# yum list -C docker
启动docker引擎服务,并且查看版本
[aaa@qq.com ~]# systemctl start docker
[aaa@qq.com ~]# docker version
Server: Docker Engine - Community
Engine:
Version: 19.03.12
API version: 1.40 (minimum version 1.12)
Go version: go1.13.10
Git commit: 48a66213fe
Built: Mon Jun 22 15:45:28 2020
OS/Arch: linux/amd64
Experimental: false
containerd:
Version: 1.2.13
GitCommit: 7ad184331fa3e55e52b890ea95e65ba581ae3429
runc:
Version: 1.0.0-rc10
GitCommit: dc9208a3303feef5b3839f4323d9beb36df0a9dd
docker-init:
Version: 0.18.0
GitCommit: fec3683
可以看到和mysql一样C/S架构,有客户端和服务端。通过下面socket文件来进行本地通信
[aaa@qq.com ~]# ps -ef | grep docker
root 23788 1 0 05:25 ? 00:00:00 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock
[aaa@qq.com ~]# docker search nginx
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
nginx Official build of Nginx. 13433 [OK]
将镜像拉取到本地
[aaa@qq.com ~]# docker pull nginx
Using default tag: latest
latest: Pulling from library/nginx
8559a31e96f4: Downloading 1.948MB/27.1MB
8d69e59170f7: Download complete
3f9f1ec1d262: Download complete
d1f5ff4f210d: Download complete
1e22bfa8652e: Download complete
或者将nginx.tar镜像包导入到你的镜像列表里面,因为拉取镜像很慢,我将tar包上传了
如果nginx镜像是通过windows上传nginx.tar,导入的nginx镜像命令:
[aaa@qq.com ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
[aaa@qq.com ~]# ls
anaconda-ks.cfg nginx.tar
[aaa@qq.com ~]# docker load <nginx.tar
1c95c77433e8: Loading layer 72.47MB/72.47MB
002a63507c1c: Loading layer 57.31MB/57.31MB
12fdf55172df: Loading layer 3.584kB/3.584kB
Loaded image: nginx:latest
[aaa@qq.com ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest 5a3221f0137b 10 months ago 126MB
[aaa@qq.com ~]# docker run -itd -p 80:80 nginx
314d1d01c94136a35dcb07016814437666331d8f7b441f5d0a1f66863848c413
[aaa@qq.com ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
314d1d01c941 nginx "nginx -g 'daemon of?? 6 seconds ago Up 4 seconds 0.0.0.0:80->80/tcp happy_mirzakhani
或者这种方式导入镜像也可以,但是不建议,创建容器报错
[aaa@qq.com ~]# cat nginx.tar | docker import - nginx:v1
sha256:0dd1a59f537a0760b91897283e7e568bbc29b3971337c31a630042a1a049fd7c
[aaa@qq.com ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx v1 0dd1a59f537a 2 seconds ago 130MB
nginx latest b2ebe65d87c7 2 minutes ago 130MB
[aaa@qq.com ~]# docker run -itd -p 81:80 nginx:v1
docker: Error response from daemon: No command specified.
Run,全新创建一台容器;
-i,interactive交互模式;
-t,tty打开登陆终端;
-d,detach后台运行、启动;
-p,publish发布端口,将宿主机80(第一个)DNAT映射至容器的80;第一个80端口是宿主机的端口,这个端口是宿主机上面需要开启的,如果宿主机上80端口被占用了是不行的,通过DNAT映射