docker学习笔记

2018-07-13 15:56:32       xhs
docker run ubuntu echo hello docker
docker pull nginx
#本地的8080端口映射到容器的80端口
docker run -p 8080:80 -d nginx
curl 127.0.0.1:8080
docker ps
#文件拷贝到容器
vi index.html
docker ps
docker cp index.html 05f67f3f819d://usr/share/nginx/html
docker stop 05f67f3f819d
docker run -p 8080:80 -d nginx
curl http://localhost:8080

docker commit -m 'fun' 24dc41701283 nginx-fun
docker images
删除镜像
docker rmi 9f139d4af874

docker stop 24dc41701283

docker ps -a
docker rm 2cbeef6445dc
基础镜像,类比基本类
vi Dockerfile
    FROM alpine:latest
    MAINTAINER xhs
    CMD echo "Hello Docker!"
docker pull alpine
docker build -t hello_docker .
docker images
docker run hello_docker
#建Dockerfile 自己创建镜像
vi Dockerfile 
    FROM ubuntu
    MAINTAINER xhs
    RUN apt-get update
    RUN apt-get install -y nginx
    COPY index.html /var/www/html
    ENTRYPOINT ["/usr/sbin/nginx", "-g", "daemon off;"]
    EXPOSE 80
    
docker build -t xhs/hello-nginx .
docker images
docker run -d -p 80:80 xhs/hello-nginx
curl localhost

FROM 基础镜像
RUN 执行命令
ADD  包括网络文件
COPY 拷贝文件或目录
CMD 执行入口
ENTRYPOINT
没有指定ENTRYPOINT,就用CMD,指定了,CMD后字符串成为ENTRYPOINT的参数
WORKDIR 路径
ENV 环境变量
USER 
VOLUME 挂载卷
EXPOSE 暴露端口
Dockerfile每一行都产生一个新层

容器层可读可写

镜像层只读

相同层可以共享

volume
独立容器之外的持久化存储

docker run -d --name nginx -v /usr/share/nginx/html nginx
docker inspect nginx

ls /var/lib/docker/volumes/e1b94b6addbba7267594c8dbbc6c83534da02d292090e3781f49447fc4e02727/_data
cd /var/lib/docker/volumes/e1b94b6addbba7267594c8dbbc6c83534da02d292090e3781f49447fc4e02727/_data
cat index.html
[root@localhost _data]# echo "it's 2018" > index.html

[root@localhost ~]# docker exec -it nginx /bin/bash
root@88840db4e4c2:/# cd /usr/share/nginx/html/
root@88840db4e4c2:/usr/share/nginx/html# cat index.html 
it's 2018
root@88840db4e4c2:/usr/share/nginx/html# 

#本地的目录挂载到容器里
docker run -p 80:80 -d -v $PWD/html:/usr/share/nginx/html nginx

mkdir vol3
cd vol3/
mkdir data
docker create -v $PWD/data:/var/mydata --name data_container ubuntu
docker run -it --volumes-from data_container ubuntu /bin/bash

root@775022803f20:/# mount | grep mydata
/dev/mapper/centos-root on /var/mydata type xfs (rw,relatime,seclabel,attr2,inode64,noquota)
root@775022803f20:/# cd /var/mydata/
root@775022803f20:/var/mydata# ls
ls: cannot open directory '.': Permission denied
root@775022803f20:/var/mydata# cd data
bash: cd: data: No such file or directory
root@775022803f20:/var/mydata# touch whatever.txt
touch: cannot touch 'whatever.txt': Permission denied

解决Permission denied 
su -c "setenforce 0"
chcon -Rt svirt_sandbox_file_t /path/to/volume
https://stackoverflow.com/questions/24288616/permission-denied-on-accessing-host-directory-in-docker

Registry
镜像库

host 宿主机
registry 仓库

docker search 
docker pull
docker push

#国内的库
daocloud
时速云
阿里云

[root@localhost ~]# docker search whalesay
INDEX       NAME                                      DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
docker.io   docker.io/docker/whalesay                 An image for use in the Docker demo tutorial    632                  
docker.io   docker.io/mendlik/docker-whalesay         Docker whalesay image from training materi...   7                    [OK]
docker.io   docker.io/caibar/whalesay                 Builds automatizados.                           1                    [OK]
docker.io   docker.io/milanfort/whalesay              Modified docker/whalesay image that output...   1                    
docker.io   docker.io/nikovirtala/whalesay            Tiny Go web service to print Moby Dock ASC...   1                    [OK]
docker.io   docker.io/ojenge/whalesay                 from docker/whalesay                            1                    
docker.io   docker.io/sabs1117/whalesay               Whalesay with fortune phrases.                  1                    


docker run docker.io/docker/whalesay cowsay 上午好

docker tag docker.io/docker/whalesay xhs/whalesay

docker images

docker push xhs/whalesay

#登录
docker login 



发表评论