參考地址:https://www.imooc.com/article/details/id/25228linux
操做系統Centos7docker
一、替換yum源爲阿里雲yum源;json
//備份yum源 mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup //下載阿里yunyum源 wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo //清除yum緩存 yum clean all //生成yum緩存 yum makecache
二、安裝dockerubuntu
查看yum能夠安裝的docker,命令:yum list |grep dockercentos
執行安裝命令:yum install docker緩存
安裝完成以後,查看已經安裝的docker,命令:yum list installed |grep dockertomcat
能夠看出,安裝docker須要三個包,見上圖bash
三、添加開機啓動app
命令:systemctl enable docker.servicedom
四、啓動docker
命令:systemctl start docker.service
啓動失敗,報錯信息,查看命令:journalctl -xe,具體錯誤信息見下圖:
此linux的內核中的SELinux不支持 overlay2 graph driver ,解決方法有兩個,要麼啓動一個新內核,要麼就在docker裏禁用selinux,--selinux-enabled=false
修改文件,/etc/sysconfig/docker,在--selinux-enabled後面添加=false便可!具體修改見下圖
重啓docker,命令:systemctl restart docker.service
查看docker信息,命令:docker info
[root@localhost ~]# docker info Containers: 1 Running: 0 Paused: 0 Stopped: 1 Images: 1 Server Version: 1.13.1 Storage Driver: overlay2 Backing Filesystem: xfs Supports d_type: true Native Overlay Diff: false Logging Driver: journald Cgroup Driver: systemd Plugins: Volume: local Network: bridge host macvlan null overlay Swarm: inactive Runtimes: docker-runc runc Default Runtime: docker-runc Init Binary: /usr/libexec/docker/docker-init-current containerd version: (expected: aa8187dbd3b7ad67d8e5e3a15115d3eef43a7ed1) runc version: e9c345b3f906d5dc5e8100b05ce37073a811c74a (expected: 9df8b306d01f59d3a8029be411de015b7304dd8f) init version: 5b117de7f824f3d3825737cf09581645abbe35d4 (expected: 949e6facb77383876aeff8a6944dde66b3089574) Security Options: seccomp WARNING: You're not using the default seccomp profile Profile: /etc/docker/seccomp.json Kernel Version: 3.10.0-514.el7.x86_64 Operating System: CentOS Linux 7 (Core) OSType: linux Architecture: x86_64 Number of Docker Hooks: 3 CPUs: 2 Total Memory: 2.731 GiB Name: localhost.localdomain ID: NDA4:FMH4:OVC2:HARD:3Q4V:ZWIE:UTJU:YVR3:AFEE:FOBN:54NL:GXGU 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 Registries: docker.io (secure)
下面,咱們經過最簡單的 image 文件"hello world",感覺一下 Docker。
由於國內鏈接 Docker 的官方倉庫很慢,所以咱們在平常使用中會使用Docker 中國加速器。經過 Docker 官方鏡像加速,中國區用戶可以快速訪問最流行的 Docker 鏡像。該鏡像託管於中國大陸,本地用戶如今將會享受到更快的下載速度和更強的穩定性,從而可以更敏捷地開發和交付 Docker 化應用。
Docker 中國官方鏡像加速可經過registry.docker-cn.com
訪問。該鏡像庫只包含流行的公有鏡像,私有鏡像仍須要從美國鏡像庫中拉取。
修改系統中docker對應的配置文件便可,以下:
vi /etc/docker/daemon.json #添加後 { "registry-mirrors": ["https://registry.docker-cn.com"], "live-restore": true}
也能夠替換成網易的鏡像地址
{ "registry-mirrors": ["http://hub-mirror.c.163.com"] }
咱們選擇用網易的鏡像地址。
運行下面的命令,將 image 文件從倉庫抓取到本地。
docker pull library/hello-world
上面代碼中,docker image pull是抓取 image 文件的命令。library/hello-world是 image 文件在倉庫裏面的位置,其中library是 image 文件所在的組,hello-world是 image 文件的名字。
抓取成功之後,就能夠在本機看到這個 image 文件了。
查看下載的鏡像,命令:docker images
[root@localhost docker]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE docker.io/hello-world latest e38bc07ac18e 7 weeks ago 1.85 kB
運行hello-world鏡像,命令:docker run hello-world
[root@localhost docker]# docker run hello-world 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://hub.docker.com/ For more examples and ideas, visit: https://docs.docker.com/engine/userguide/
除過以上咱們使用的Docker命令外,Docker還有一些其它經常使用的命令
拉取docker鏡像
docker pull image_name
查看宿主機上的鏡像,Docker鏡像保存在/var/lib/docker目錄下:
docker images
刪除鏡像
docker rmi docker.io/tomcat:7.0.77-jre7 或者 docker rmi b39c68b7af30
查看當前有哪些容器正在運行
docker ps
查看全部容器
docker ps -a
啓動、中止、重啓容器命令:
docker start container_name/container_iddocker stop container_name/container_iddocker restart container_name/container_id
後臺啓動一個容器後,若是想進入到這個容器,可使用attach命令:
docker attach container_name/container_id
刪除容器的命令:
docker rm container_name/container_id
查看當前系統Docker信息
docker info
從Docker hub上下載某個鏡像:
docker pull centos:latestdocker pull centos:latest
執行docker pull centos會將Centos這個倉庫下面的全部鏡像下載到本地repository。