docker的原理:基于Cgroup,namespace,联合文件系统
https://testerhome.com/topics/9522
安装过程参考https://docs.docker.com/install/linux/docker-ce/ubuntu/#prerequisites
遇到的问题:
我安装之后输入
docker run hello-world
提示我docker deamon XXXXXXX 反正就是不行。
修改配置文件/etc/systemd/system/multi-user.target.wants/docker.service(可能有问题,需要以后继续观察)
ExecStart=/usr/bin/dockerd -H tcp://0.0.0.0:4243 -H unix:///var/run/docker.sock
重启docker
service docker start
root@Yzz:/home/yindongzi# 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/get-started/
docker的一些名词,参考:https://github.com/docker/labs/blob/master/beginner/chapters/alpine.md
- Images - The file system and configuration of our application which are used to create containers. To find out more about a Docker image, run
docker inspect alpine
. In the demo above, you used thedocker pull
command to download the alpine image. When you executed the commanddocker run hello-world
, it also did adocker pull
behind the scenes to download the hello-world image. - Containers - Running instances of Docker images — containers run the actual applications. A container includes an application and all of its dependencies. It shares the kernel with other containers, and runs as an isolated process in user space on the host OS. You created a container using
docker run
which you did using the alpine image that you downloaded. A list of running containers can be seen using thedocker ps
command. - Docker daemon - The background service running on the host that manages building, running and distributing Docker containers.
- Docker client - The command line tool that allows the user to interact with the Docker daemon.
- Docker Store - A registry of Docker images, where you can find trusted and enterprise ready containers, plugins, and Docker editions. You'll be using this later in this tutorial.
get一个statict site的docker image,启动他的镜像。
参考:https://github.com/docker/labs/blob/master/beginner/chapters/webapps.md
docker run --name static-site -e AUTHOR="YIN_DONGZI" -d -P dockersamples/static-site
因为run的时候会现在本地找image,如果找不到就会自动下载,所以不用事先下载。
执行完毕后看一下容器的情况
root@Yzz:/home/yindongzi# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 9c16433bbee4 dockersamples/static-site "/bin/sh -c 'cd /usr…" 22 minutes ago Up 22 minutes 0.0.0.0:32769->80/tcp, 0.0.0.0:32768->443/tcp static-site d77eba4ad21e alpine "echo 'hello from al…" 41 minutes ago Up 41 minutes tender_sinoussi
这里可以看到我的static-site的端口号
0.0.0.0:32769->80/tcp, 0.0.0.0:32768->443/tcp
要去访问这个网站,可以在命令行访问:
curl http://localhost:32769
或者ifconfig获取本地IP地址后,在其他机器上访问该IP地址。
比如我的ubuntu的IP地址是172.24.174.126
那我在windows上访问http://172.24.174.126:32769/
一些常用命令:
#给容器起名字,并后台运行 docker run --name myNewWorld -d hello-world #查看容器 ,并显示大小 docker ps -s
#删除镜像
docker rmi imageName
#删除容器 强制删除
docker rm -f containerName
#链接容器和宿主机的网络--端口映射,将宿主机的端口号映射到容器,通过访问宿主机的端口访问到容器
#将宿主机的8099映射到容器的8080
docker run --name myNewWorld -d -p 8099:8080 jenkins/jenkins
#查看容器的日志
docker log -f myNewWorld
#可以访问 宿主机IP:8099,来访问容器的8080端口
启动一个nginx,并将容器中的conf文件导出到宿主机
yindongzi@Yzz:~$ mkdir -p ~/docker/nginx yindongzi@Yzz:~$ cd /home/yindongzi/docker/nginx yindongzi@Yzz:~/docker/nginx$ pwd /home/yindongzi/docker/nginx yindongzi@Yzz:~/docker/nginx$ docker run --name myNG -d nginx e1f20462f1a01c5b1efbb0cc93b6c28b022206a588219a00a55d4069aeca799d yindongzi@Yzz:~/docker/nginx$ docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES e1f20462f1a0 nginx "nginx -g 'daemon of…" 11 seconds ago Up 10 seconds 80/tcp myNG yindongzi@Yzz:~/docker/nginx$ docker ps -s CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES SIZE e1f20462f1a0 nginx "nginx -g 'daemon of…" 34 seconds ago Up 33 seconds 80/tcp myNG 2B (virtual 127MB) yindongzi@Yzz:~/docker/nginx$ docker cp myNG:/etc/nginx/nginx.conf ~/docker/nginx yindongzi@Yzz:~/docker/nginx$ ls nginx.conf yindongzi@Yzz:~/docker/nginx$ docker rm -f myNG myNG yindongzi@Yzz:~/docker/nginx$ docker ps -s CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES SIZE yindongzi@Yzz:~/docker/nginx$
上面的操作内容,可以直接通过-v参数,将容器的文件映射到本地:
-p将容器内的ng默认80端口,映射到本地的8099端口
-v
yindongzi@Yzz:~/docker/nginx$ docker run -d --name myNG2 -p 8099:80 -v ~/docker/nginx/html:/usr/yindongzi/nginx/html:ro -v ~/docker/nginx/nginx.conf:/etc/nginx/nginx.conf:ro nginx ebdcd7d27e8369aa163e7f3441f21f4565e7db2f1b957fbf1bb852fbce5d44d8 yindongzi@Yzz:~/docker/nginx$ docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES ebdcd7d27e83 nginx "nginx -g 'daemon of…" 5 seconds ago Up 2 seconds 80/tcp, 0.0.0.0:8099->8080/tcp myNG2