如何用docker创建容器,运行自己的代码(ubuntu),以及docker不停重启问题
1、docker ps -a会显示所有正在运行的容器(容器和镜像的区别,一个是死的一个是活的)2、docker images会显示所有的镜像,你可以根据某个镜像创建一个容器REPOSITORYTAGIMAGE IDCREATEDSIZEflow-websetv1.0bd7d1a25de402 months ago1
先说docker 运行自己程序的问题,在docker 启动的时候可以运行自己的脚本,让脚本也运行起来,当容器关闭后程序也关闭,这里注意一个问题,就是当你的脚本执行一个程序后,如果你的程序只是一个后台程序,那么docker 会认为你的程序结束了,也会关闭docker ,那么当你设置了–restart=always那么就会导致docker不停的重启,
很多童鞋在nginx docker 容器部署的时候就会出现这个问题,当你把配置设置如下
worker_processes 1;
daemon on;
master_process on;
daemaon on 就是程序在后台运行,那么你部署到docker 后那么就会导致docker 不停重启重启 所以docker 最好把这个改为off
综合所述,docker 部署的时候至少有一个是前台程序,且你的前台程序决定了你的docker 生命周期。
1、docker ps -a
会显示所有正在运行的容器(容器和镜像的区别,一个是死的一个是活的)
2、docker images
会显示所有的镜像,你可以根据某个镜像创建一个容器
REPOSITORY TAG IMAGE ID CREATED SIZE
flow-webset v1.0 bd7d1a25de40 2 months ago 149MB
flow-center v1.0 5e073ede582c 2 months ago 714MB
3、dockerfile 编写
当你看到镜像后,找到你需要的那个镜像,然后就可以创建容器了,创建容器需要自己写dockerfile。
1)首先,在home工作空间中创建一个文件夹,比如本文mediaserver,然后在这个文件夹下面拷贝自己要运行的程序以及依赖库。
2)vim dockerrun.sh开始编写dockerfile
docker run -itd --restart=always --name mediaserver -p 9970:9970 -p 9975:9975 -p 1935:1935 -v /etc/localtime:/etc/localtime:ro -v /home/project/mediaserver:/mediaserver -w /mediaserver flow-image:v1.5 /bin/bash
#./run-flow-server.sh
docker run -itd --restart=always --name mediaserver9976 -p 9971:9970 -p 9976:9975 -p 1936:1935 -v /etc/localtime:/etc/localtime:ro -v /home/project/mediaserver/9976:/mediaserver/9976 -w /mediaserver/9976 flow-image:v1.5 /bin/bash -c './nginx.sh;./media-server.sh'
解释以下:
--restart=alwanys 一直重启
--name mediaserver 指定容器的名称
-p 9970:9970 -p 9975:9975 -p 1935:1935 就是将端口映射到容器中,因为我的程序是服务器,里面要用到这几个端口
-v /etc/localtime:/etc/localtime:ro 这个是关于时间的映射
-v /home/shenyan/project/mediaserver:/mediaserver 这个是文件夹的映射,就是/home/shenyan/project/mediaserver这是我主机的路径,
映射到容器内部就是/mediaserver
-w /mediaserver 相当于进入容器后直接进入这个文件夹,相当于cd
flow-image:v1.5 指定在那个镜像上运行
/bin/bash -c 这里特别要注意,不要另外起一行,直接跟在命令后面,/bin/bash -c 后面执行的脚本‘ ’括起来,包含ls 等常见命令,命令之间用;f分开。
这样当docker stop 的时候程序就退出了,start 的时候就开始了
可以直接在宿主机上通过ps -ef |grep nginx 这样查看脚本是否执行(一般是启动某个程序)
docker attach 就可以进入容器中查看了
如果你没有/bin/bash
而是启动容器后进入容器启动脚本,那么当你exit退出容器的时候程序也就会退出。因为程序运行在容器虚拟中。
当attach 后可以在另外的窗口 ps -ef |grep attach
然后kill -9 pid即可退出
4、dockerfile编写结束后就可以运行容器了
./dockerrun.sh
5、然后通过第一条的docker ps -a 就可以看到自己的容器运行起来了
6、然后附属到自己容器上
sudo docker attach mediaserver
ls
就可以看到刚才自己拷贝的文件了
7、通过ps -ef 就可以看到运行的进程了
Usage: docker [OPTIONS] COMMAND
A self-sufficient runtime for containers
Options:
--config string Location of client config files (default "/home/shenyan/.docker")
-c, --context string Name of the context to use to connect to the daemon (overrides DOCKER_HOST env var and default context set with "docker context use")
-D, --debug Enable debug mode
-H, --host list Daemon socket(s) to connect to
-l, --log-level string Set the logging level ("debug"|"info"|"warn"|"error"|"fatal") (default "info")
--tls Use TLS; implied by --tlsverify
--tlscacert string Trust certs signed only by this CA (default "/home/shenyan/.docker/ca.pem")
--tlscert string Path to TLS certificate file (default "/home/shenyan/.docker/cert.pem")
--tlskey string Path to TLS key file (default "/home/shenyan/.docker/key.pem")
--tlsverify Use TLS and verify the remote
-v, --version Print version information and quit
Management Commands:
builder Manage builds
config Manage Docker configs
container Manage containers
context Manage contexts
engine Manage the docker engine
image Manage images
network Manage networks
node Manage Swarm nodes
plugin Manage plugins
secret Manage Docker secrets
service Manage services
stack Manage Docker stacks
swarm Manage Swarm
system Manage Docker
trust Manage trust on Docker images
volume Manage volumes
Commands:
attach Attach local standard input, output, and error streams to a running container
build Build an image from a Dockerfile
commit Create a new image from a container's changes
cp Copy files/folders between a container and the local filesystem
create Create a new container
diff Inspect changes to files or directories on a container's filesystem
events Get real time events from the server
exec Run a command in a running container
export Export a container's filesystem as a tar archive
history Show the history of an image
images List images
import Import the contents from a tarball to create a filesystem image
info Display system-wide information
inspect Return low-level information on Docker objects
kill Kill one or more running containers
load Load an image from a tar archive or STDIN
login Log in to a Docker registry
logout Log out from a Docker registry
logs Fetch the logs of a container
pause Pause all processes within one or more containers
port List port mappings or a specific mapping for the container
ps List containers
pull Pull an image or a repository from a registry
push Push an image or a repository to a registry
rename Rename a container
restart Restart one or more containers
rm Remove one or more containers
rmi Remove one or more images
run Run a command in a new container
save Save one or more images to a tar archive (streamed to STDOUT by default)
search Search the Docker Hub for images
start Start one or more stopped containers
stats Display a live stream of container(s) resource usage statistics
stop Stop one or more running containers
tag Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE
top Display the running processes of a container
unpause Unpause all processes within one or more containers
update Update configuration of one or more containers
version Show the Docker version information
wait Block until one or more containers stop, then print their exit codes
更多推荐
所有评论(0)