docker's Introduction
docker's People
docker's Issues
Dockfile的编写
基本语法
编译go报错解决方案
standard_init_linux.go:178: exec user process caused “no such file or directory”
出现该问题的原因是编译的环境和运行的环境不同,可能有动态库的依赖
1.默认go使用静态链接,在docker的golang环境中默认是使用动态编译。
2.如果想使用docker编译+alpine部署,可以通过禁用cgoCGO_ENABLED=0来解决。(export CGO_ENABLED=0)
3.如果要使用cgo可以通过go build --ldflags “-extldflags -static” 来让gcc使用静态编译。
docker运行
功能划分 | 命令 |
---|---|
环境信息相关 | info version |
系统运维相关 | attach build commit cp diff export images import/save/load inspect kill port pause/unpause ps rm rmi run start/stop/restart tag top wait |
日志信息相关 | events history logs Docker |
Hub服务相关 | login pull/push search |
docker run
run是最常用的命令,他是docker create和docker start命令的组合,创建容器并启动它。
- -i
-i参数是keep STDIN open even if not attached,意思就是会把交互界面保留着。但是要看容器的PID1,或者说看容器的CMD是什么,有些CMD程序并不会理会任何输入,也就是说它就没有等着你输入命令,这时候你输入什么都是无效的。并且,因为linux对pid1的特殊处理,它也不会理会信号,所以你只能按ctrl+p + ctrl+q,把程序放到后台。(但通常都要run -it一起使用才有效)
- -t
allocate a pseudo-TTY.作用是分配一个虚拟的终端,我的理解就是,有了-t参数,这个docker就会被分配一个终端,在docker中启动bash就会有提示符。不过,-it总是一起出现的。
- -d
run container in background and print container ID,就是在后台运行容器。相当于进去之后,按了ctrl+p + ctrl+q。所以可以docker attch回去。在后台运行的容器,只能通过网络和它交互了。
- -a
可以定义到底是attach STDIN、STDOUTSTDERR,如果没有定义,则默认是 attach to both stdout and stderr。
- --rm
容器退出后,自动删除容器
- --name
为容器取一个名字
- --p
hostport:containerport,设置端口转发
总结
要启动一个容器,这个容器的pid1应用是一个不需要与用户命令行交互的,比如一个什么web应用服务,那么就用-d参数,然容器运行在后台,好好服务。比如:
docker run --name mywebserver -d -p 80:80 my_image nginx -g 'daemon off;'
要启动一个容器并且用这个容器来做命令操作,他的pid1进程是一个和用户交互的程序,比如启动一台busybox(pid1是bash),那么用 -it。比如:
docker run -a stdin -a stdout -i -t ubuntu /bin/bash
或者docker run --name myubuntu -it ubuntu /bin/bash,如果要工作后即焚毁,那么就带上--rm就好了。
- 注意,如果容器要从管道中获取输入,就不允许带-t了。比如:$ echo test | docker run -i busybox cat
docker start
start是启动一个已经存在的容器,他有两个参数-a 和-i
- -a
attach STDOUT/STDERR。 -a参数带上了的话,那么会把pid1程序的输出都带到终端上
- -i
attach container's STDIN。 -i参数是为了让容器相应输入,作用其实和run中的-i一样。
总结
启动一个容器作为服务,就什么参数都不要带,让他运行在后台。启动一个容器作为交互应用,就带上-i。
docker attach
attach命令把一个后台运行的容器搞到前台来,这样你就可以看到他们的输出,并且使用输入了。
- --no-stdin
关闭输入,这样你就不能使用输入了
- --sig-proxy
信号转发,默认开启,这样DMD进程就可以收到你的信号了。
总结
把后台边前台,基本上就是一种单一的用法docker attach container_name了。
(1)因为attach是进入了和CMD程序交互,但是CMD程序未必就会相应你的输入(一般程序都不会相应你的输入吧,因为他有没有动标准流中读取什么东西),所以不要以为程序死了。
(2)因为linux对PID1程序的特殊照顾,所以它本身不会相应任何信号,除非程序自己定义了相应信号的处理方案。也就是说,你按ctrl+c可能没什么反应哦。
(3)因为attach对stdout和stderr做了1M的缓存,如果缓存占满了,输出就会有卡顿感,所以如果程序有大量的输出,就不要用attach去看,而应该用docker logs去看log哦。
(4)如果容器建立的时候是tty-enabled container (i.e.: launched with -t),那么就不允许重定向输入哦。
(5) attach的退出码是容器退出后的退出码。
docker exec
这个命令是用已经运行的容器执行命令。需要注意的是,如果容器关闭后再次运行,那么用exec命令执行的命令是不会自己再次执行的。
- -d
让命令在后台执行。
- -e
设置环境变量
- -i -t
运行交互命令的时候用
- -u -w
分别设置命令运行的用户和work directory。
docker内部使用宿主机ip方案
1、可以在使用localhost的地方替换host.docker.internal
对于mac和windows,可以使用host.docker.internal替换127.0.0.1,如 mongodb://host.docker.internal:27017
2、对于linux可以使用:
创建一个桥接网络
下面的localNet是网络名字,可自行修改;关于192.168.0.0这个子网,也可以自行定义.
默认按照下面的命令,执行后将可以通过192.168.0.1访问宿主机.
docker network create -d bridge --subnet 192.168.0.0/24 --gateway 192.168.0.1 localNet
使用192.168.0.1替换127.0.0.1,如mongodb://192.168.0.1:27017
docker配置
配置文件问题
1.修改/etc/docker/daemon.json配置文件容器日志大小后,systemctl docker restart不生效
原因:配置文件冲突(可能与docker版本有关)
解决:修改/etc/sysconfig/docker,删除--log-driver=Journald,systemctl daemon-reload再重启
挂载目录修改文件,容器内部未同步
原因:linux内一切皆文件,一开始挂载目录和容器内部是同一inode,操作挂载目录后inode变更,容器内inode并未变更
解决:使用mount,--mount type=bind,source="$(pwd)"/target,target=/app2,readonly,bind-propagation=rslave
propagation类型(详见官网:https://docs.docker.com/storage/bind-mounts/)
- shared-双向目录同步,slave-挂载目录单向同步容器 ,private-不同步
- rshared-双向目录及子目录同步,slave-挂载目录及子目录单向同步容器 ,rprivate-父子目录不同步
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.