参考资料:https://yeasy.gitbooks.io/docker_practice/content/install/centos.html
使用环境为阿里云服务器centos7.2
centos 安装
添加 yum 源
虽然 CentOS 软件源 Extras
中有 Docker,名为 docker
,但是不建议使用系统源中的这个版本,它的版本相对比较陈旧,而且并非 Docker 官方维护的版本。因此,我们需要使用 Docker 官方提供的 CentOS 软件源。
执行下面的命令添加 yum
软件源。
$ sudo tee /etc/yum.repos.d/docker.repo <<-'EOF'
[dockerrepo]
name=Docker Repository
baseurl=https://yum.dockerproject.org/repo/main/centos/7/
enabled=1
gpgcheck=1
gpgkey=https://yum.dockerproject.org/gpg
EOF
更新 yum
软件源缓存,并安装 docker-engine
$ sudo yum update
$ sudo yum install docker-engine
启动 Docker 引擎
$ sudo systemctl enable docker
$ sudo systemctl start docker
#如果容器启动出现问题,不妨重启docker服务
$ sudo systemctl restart docker
安装docker-compose
#安装
pip install docker-compose
#查看安装版本
docker-compose --version
docker-compose version 1.16.1, build 6d1ac219
1.进入root权限
sudo su
2.停止所有的container,这样才能够删除其中的images:
docker stop $(docker ps -a -q)
如果想要删除所有container的话再加一个指令:
docker rm $(docker ps -a -q)
3.查看当前有些什么images
docker images
4.删除images,通过image的id来指定删除谁
docker rmi <image id>
要删除全部image的话
docker rmi $(docker images -q)
1.运行容器
#-d 后台运行,-p 宿主机上选择一个端口映射到容器8080端口上
[root@iZotm71x017bylZ web]docker run -d -p 33892:8080 --name tomcat
chenyaochn/tomcat
d98af46e5e3d30061eec9efd17f21f403b3759f7ef4b3b7c7787ad73f3e2f43c
#查看启动容器
[root@iZotm71x017bylZ web]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
d98af46e5e3d chenyaochn/tomcat "/usr/local/tomcat..." 16 minutes ago Up 16 minutes 0.0.0.0:33892->8080/tcp tomcat
#使用curl访问,查看响应结果
[root@iZotm71x017bylZ web]# curl -I http://localhost:33892
HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
Content-Type: text/html;charset=UTF-8
Transfer-Encoding: chunked
Date: Wed, 30 Aug 2017 12:27:24 GMT
2.进入容器:
sudo docker exec -it 容器id bash
根目录 exit 退出
镜像push到资源库
首先输入docker login 指令,依次输入用户名和密码
#查看镜像
[root@iZotm71x017bylZ test]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
hehe latest 4c9a2f3b237a 6 minutes ago 194MB
python 2.7-slim 451c85955bc2 4 weeks ago 182MB
#创建标签 指令docker tag image(镜像名) username(账号名)/repository(资源库名):tag(标签名)
[root@iZotm71x017bylZ test]# docker tag hehe chenyaochn/hehe:tag1
[root@iZotm71x017bylZ test]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
chenyao/hehe tag1 4c9a2f3b237a 6 minutes ago 194MB
hehe latest 4c9a2f3b237a 6 minutes ago 194MB
python 2.7-slim 451c85955bc2 4 weeks ago 182MB
#将镜像push到dockerhub
[root@iZotm71x017bylZ test]# docker push chenyaochn/hehe:tag1
The push refers to a repository [docker.io/chenyao/hehe]
30976be65f15: Preparing
669912f99445: Preparing
ad85e5f87486: Preparing
735a46068813: Preparing
2323e4886d1c: Preparing
3dd73719e600: Waiting
2c40c66f7667: Waiting
tag1: digest: sha256:501305d4ad9c1b95a26f369cf83ff3002e7564d7270a0d7e8511157219cca883 size: 1787
docker cloud 查看结果
运行资源库images
#docker run -p 3389:80 username/repository:tag
$ docker run -d -p 3389:80 chenyaochn/hehe:tag1
2bfad19330ac19726ac43c4c579e572109897f6c5f2cbf4a725e2ea47b9dda6d
页面访问:
官方提供一些指令:
docker build -t friendlyname . # Create image using this directory's Dockerfile
docker run -p 4000:80 friendlyname # Run "friendlyname" mapping port 4000 to 80
docker run -d -p 4000:80 friendlyname # Same thing, but in detached mode
docker ps # See a list of all running containers
docker stop <hash> # Gracefully stop the specified container
docker ps -a # See a list of all containers, even the ones not running
docker kill <hash> # Force shutdown of the specified container
docker rm <hash> # Remove the specified container from this machine
docker rm $(docker ps -a -q) # Remove all containers from this machine
docker images -a # Show all images on this machine
docker rmi <imagename> # Remove the specified image from this machine
docker rmi $(docker images -q) # Remove all images from this machine
docker login # Log in this CLI session using your Docker credentials
docker tag <image> username/repository:tag # Tag <image> for upload to registry
docker push username/repository:tag # Upload tagged image to registry
docker run username/repository:tag # Run image from a regist