Docker学习

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Docker学习相关的知识,希望对你有一定的参考价值。

# Docker学习


从仓库获取所需要的镜像


```

sudo docker pull ubuntu:12.04

sudo docker pull registry.hub.docker.com/ubuntu:12.04

sudo docker pull dl.dockerpoll.com:5000/ubuntu:12.04

```


- 入门命令


```sudo docker run -i -t ubuntu /bin/bash```


```sudo docker run -t -i ubuntu:12.04 /bin/bash```


- 后台运动


```sudo docker run -d ubuntu:12.04 /bin/bash -c "while true; do sleep 1; done"```


- 进入已经运行的容器


```sudo docker exec -i -t $CONTAINER_ID /bin/bash```

```sudo docker attach ubuntu```


- 列出当前运行的容器


```sudo docker ps```


- 查看容器详细信息


```sudo docker inspect $CONTAINER_ID```


- 列出所有容器


```sudo docker ps -a```


- 查看本地安装的所有镜像


```sudo docker images```


- 创建自定义网络


```

//注:shadown为自定义网桥的名字,可自己任意取名。

docker network create --subnet=192.168.88.0/24 mynet88

```


- 在你自定义的网段选取任意IP地址作为你要启动的container的静态IP地址


```

docker run -d --name mydocker -it --net mynet88 --ip 192.168.88.10 mytest/ubuntu

```


- 删除 none 的镜像


```docker rmi $(docker images | awk ‘/^<none>/ { print $3 }‘)```


- docker save命令-打包image


```docker save -o nginx.tar mytest/nginx```


- docker load命令-加载image


```docker load -i nginx.tar```



- 用容器ID把容器提交到了仓库中


```

sudo docker commit 4fe870047979 mytest/ubuntu

sudo docker run -i -t mytest/ubuntu /bin/bash

```


### 使用Dockerfile构建镜像


```

FROM ubuntu:12.04

MAINTAINER bstdn <[email protected]>


RUN echo "deb http://mirrors.163.com/ubuntu/ precise main restricted\n" \

  "deb-src http://mirrors.163.com/ubuntu/ precise main restricted\n" \

  "deb http://mirrors.163.com/ubuntu/ precise-updates main restricted\n" \

  "deb-src http://mirrors.163.com/ubuntu/ precise-updates main restricted\n" \

  "deb http://mirrors.163.com/ubuntu/ precise universe\n" \

  "deb-src http://mirrors.163.com/ubuntu/ precise universe\n" \

  "deb http://mirrors.163.com/ubuntu/ precise-updates universe\n" \

  "deb-src http://mirrors.163.com/ubuntu/ precise-updates universe\n" \

  "deb http://mirrors.163.com/ubuntu/ precise multiverse\n" \

  "deb-src http://mirrors.163.com/ubuntu/ precise multiverse\n" \

  "deb http://mirrors.163.com/ubuntu/ precise-updates multiverse\n" \

  "deb-src http://mirrors.163.com/ubuntu/ precise-updates multiverse\n" \

  "deb http://mirrors.163.com/ubuntu/ precise-backports main restricted universe multiverse\n" \

  "deb-src http://mirrors.163.com/ubuntu/ precise-backports main restricted universe multiverse\n" \

  "deb http://mirrors.163.com/ubuntu/ precise-security main restricted\n" \

  "deb-src http://mirrors.163.com/ubuntu/ precise-security main restricted\n" \

  "deb http://mirrors.163.com/ubuntu/ precise-security universe\n" \

  "deb-src http://mirrors.163.com/ubuntu/ precise-security universe\n" \

  "deb http://mirrors.163.com/ubuntu/ precise-security multiverse\n" \

  "deb-src http://mirrors.163.com/ubuntu/ precise-security multiverse\n" \

  "deb http://extras.ubuntu.com/ubuntu precise main\n" \

  "deb-src http://extras.ubuntu.com/ubuntu precise main" > /etc/apt/sources.list

RUN apt-get update

RUN apt-get install -y openssh-server

#RUN apt-get install -y python2.7-dev

RUN apt-get install -y vim

RUN mkdir /var/run/sshd

RUN echo "root:root" | chpasswd

#VOLUME ["/media/psf/Home/pan_baidu/wwwroot", "/wwwroot"]

VOLUME ["/data/wwwroot"]

WORKDIR /data/wwwroot

EXPOSE 22 80

CMD /usr/sbin/sshd -D

```


- 构建镜像


```sudo docker build -t mytest/ssh .```

```sudo docker build -t="mytest/ssh:v2" .```


-t  标记 来添加 tag


- 修改镜像的标签


```sudo docker tag $CONTAINER_ID mytest/ssh:v3```


- 实例化


```sudo docker run -d -p 80:80 -p 2222:22 -v /wwwroot:/data/wwwroot mytest/ssh```


- ssh登录


```ssh [email protected] -p 2222```


- kill 正在运行的容器


```sudo docker kill $(sudo docker ps -aql)```



- 调试


```

FROM ubuntu:12.04

RUN echo "deb http://mirrors.163.com/ubuntu/ precise main restricted universe multiverse" > /etc/apt/sources.list

RUN apt-get update

RUN apt-get install -y openssh-server

RUN mkdir /var/run/sshd

RUN echo "root:root" | chpasswd

EXPOSE 22 80 3306

CMD /usr/sbin/sshd -D

```




```sudo docker build -t mytest/base .```


```

sudo docker run -i -t -p 2222:22 -p 8080:80 -p 3306:3306 -v /wwwroot:/home/vagrant/wwwroot --name=base mytest/base /bin/bash

```



## 建立容器之间的连接 - 以Nginx+php为例


```


# phpfpm

docker run -d --name phpfpm mytest/php-5.3 /bin/bash -c "while true;do sleep 1;done"


docker exec -it phpfpm /bin/bash


# nginx

docker run -d --name nginx-1.1 -p 80:80 --link phpfpm:phpfpm --volumes-from phpfpm mytest/nginx-1.1 /bin/bash -c "while true;do sleep 1;done"


docker exec -it nginx-1.1 /bin/bash


```


```

docker run --name some-mysql -e MYSQL_ROOT_PASSWORD=my-secret-pw -d mysql:tag

```


## docker 实例


```

//mysql

docker run -d --name mysql-5.5 -h mysql-5-5 -p 3306:3306 -v /wwwroot:/home/vagrant/wwwroot -v /Downloads:/home/vagrant/Downloads mytest/mysql-5.5


//redis

docker run -d -p 6379:6379 -h redis-2-2 --name redis-2.2 -v /wwwroot:/home/vagrant/wwwroot mytest/redis-2.2


//memcached

docker run -d -p 45001:11211 --name memcached -h memcached -v /wwwroot:/home/vagrant/wwwroot mytest/memcached

```


- 清理docker for mac [占用本地空间](https://github.com/docker/for-mac/issues/371)


```

停用 docker

docker rm $(docker ps -a -q)

docker rmi $(docker images -q)

docker volume rm $(docker volume ls |awk ‘{print $2}‘)

rm -rf ~/Library/Containers/com.docker.docker/Data/*

启用 docker 会自动生成 Data 数据

```


本文出自 “技术蓝” 博客,请务必保留此出处http://bstdn.blog.51cto.com/8667020/1895745

以上是关于Docker学习的主要内容,如果未能解决你的问题,请参考以下文章

Docker学习目标

docker学习 docker-compose使用示例

Linux学习--Docker学习

docker 学习路线

从零开始学习Docker (初识Docker)

十分钟了解Docker(我的Docker学习笔记)