[英]How to launch Docker Containers using Ansible
I am writing a playbook where I want to build and image and start some containers for that image.我正在写一本剧本,我想在其中构建和镜像并为该镜像启动一些容器。 I was able to build an image but my containers are not getting started.
我能够构建一个图像,但我的容器没有启动。 They are being created successfully but not in running state.
它们正在成功创建,但没有在运行 state。
Can someone help what am i missing?有人可以帮助我错过什么吗?
- name: Manage Docker instances via Ansible
hosts: shashank-VM
connection: local
become: yes
become_method: sudo
tasks:
- name: Building an image from Dockerfile
docker_image:
build:
path: .
pull: yes
name: web
source: build
- name: Starting Docker Containers
docker_container:
name: my-app-{{ item }}
image: web:latest
state: started
with_sequence: count=4
From the below you can see the containers are created but if i do sudo docker ps I don't see anything.从下面您可以看到容器已创建,但如果我执行 sudo docker ps,我什么也看不到。
TASK [Starting Docker Containers] ************************************************************************************************************
/usr/lib/python2.7/dist-packages/requests/__init__.py:80: RequestsDependencyWarning: urllib3 (1.25.9) or chardet (3.0.4) doesn't match a supported version!
RequestsDependencyWarning)
changed: [shashank-VM] => (item=1)
changed: [shashank-VM] => (item=2)
changed: [shashank-VM] => (item=3)
changed: [shashank-VM] => (item=4)
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
shashank@shashank-VM:~/docker_project$ sudo docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
d5b586266f15 web_new "/bin/bash" 2 minutes ago Exited (0) 2 minutes ago myvm-app-4
ea45cc1fcf3c web_new "/bin/bash" 2 minutes ago Exited (0) 2 minutes ago myvm-app-3
1e685f3d1368 web_new "/bin/bash" 2 minutes ago Exited (0) 2 minutes ago myvm-app-2
5d03750c3fd2 web_new "/bin/bash" 2 minutes ago Exited (0) 2 minutes ago myvm-app-1
shashank@shashank-VM:~/docker_project$ sudo docker run -d web_new
6e937d7a8e75c04ea9dc6769fa41b00f5bf455e6f947f002d181d105b94c7265
shashank@shashank-VM:~/docker_project$ sudo docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
shashank@shashank-VM:~/docker_project$ sudo docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
6e937d7a8e75 web_new "/bin/bash" 14 seconds ago Exited (0) 13 seconds ago agitated_bartik
Dockerfile content - Dockerfile 内容 -
FROM debian:8.5
RUN apt-get update
RUN apt-get install -y nginx
RUN echo 'Default page. Nginx is in your container. ' \
>/usr/share/nginx/html/index.html
EXPOSE 8085
This has nothing to do with Ansible.这与Ansible无关。
Your Docker image is based on debian:8.5
, which when run simply starts a shell. If the shell isn't connected to a terminal, it will exit immediately.您的 Docker 图像基于
debian:8.5
,它在运行时仅启动 shell。如果 shell 未连接到终端,它将立即退出。 Eg, try running:例如,尝试运行:
docker run debian:8.5
If you expect your image to run a persistent service (like nginx
), you need to arrange for nginx
to run when you start a container from your image.如果您希望您的镜像运行持久服务(如
nginx
),则需要安排nginx
在您从您的镜像启动容器时运行。 You do this suing the CMD
or ENTRYPOINT
directives in your Dockerfile.您在 Dockerfile 中使用
CMD
或ENTRYPOINT
指令执行此操作。
For example:例如:
FROM debian:8.5
RUN apt-get update
RUN apt-get install -y nginx
RUN echo 'Default page. Nginx is in your container. ' \
>/usr/share/nginx/html/index.html
EXPOSE 8085
CMD ["nginx", "-g", "daemon off"]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.