Wait for file over ssh - ssh

I would like to do something like:
- name: Wait until the file is touched
ansible.builtin.wait_for:
path: 192.168.1.1:/home/test.txt
timeout: 300
where 192.168.1.1 is some remote host I am connected to. Is this possible?

I don't believe it's possible with wait_for module.
My workaround is to use until:
- name: Wait until file exists on remote.
shell: ssh $USER#192.168.1.1 ls /home/test.txt
register: filecheck
until: filecheck.stdout == "/home/test.txt"
retries: 10
delay: 1
I copied the file to the host while the playbook was running through the 9th attempt:
changed: [localhost] => {
"attempts": 9,
"changed": true,
"cmd": "ssh root#192.168.1.1 ls /home/test.txt",
"delta": "0:00:01.548091",
"end": "2022-08-11 15:31:05.276277",
"invocation": {
"module_args": {
"_raw_params": "ssh root#192.168.1.1 ls /home/test.txt",
"_uses_shell": true,
"argv": null,
"chdir": null,
"creates": null,
"executable": null,
"removes": null,
"stdin": null,
"stdin_add_newline": true,
"strip_empty_ends": true,
"warn": false
}
},
"msg": "",
"rc": 0,
"start": "2022-08-11 15:31:03.728186",
"stderr": "",
"stderr_lines": [],
"stdout": "/home/test.txt",
"stdout_lines": [
"/home/test.txt"
]

Related

Ansible: Print path and contents of multiple files

I am trying to write a playbook that scans a host for the authorized_keys file, and then prints out the path and contents of the file.
Every attempt I have made and all the researching I have done and I am still unable to get the behaviour that I want, which is the following:
For each instance of the found file, print:
path to file
contents of file
Here is the playbook:
---
- name: Show File Contents
vars_prompt:
- name: host
prompt: Enter host(s) to search
private: no
- name: file
prompt: Enter file to show the contents of
private: no
hosts: "{{ host }}"
tasks:
- name: Find Files
find:
patterns: "{{ file }}"
recurse: true
paths: /
register: files_matched
- name: Cat authorized keys
shell: cat {{ item.path }}
register: cat
loop: "{{ files_matched.files }}"
- name: Print output
loop: "{{ cat | json_query('results[*].stdout') }}"
debug:
msg: "{{ item.results }}"
become: true
...
Here is the output when the above playbook is run against a single host:
TASK [Print output] ****************************************************************************************************************
ok: [ansible-testinghost.local] => {
"msg": [
{
"ansible_loop_var": "item",
"changed": true,
"cmd": "cat /root/.ssh/authorized_keys",
"delta": "0:00:00.003527",
"end": "2022-04-10 14:56:27.867035",
"failed": false,
"invocation": {
"module_args": {
"_raw_params": "cat /root/.ssh/authorized_keys",
"_uses_shell": true,
"argv": null,
"chdir": null,
"creates": null,
"executable": null,
"removes": null,
"stdin": null,
"stdin_add_newline": true,
"strip_empty_ends": true,
"warn": true
}
},
"item": {
"atime": 1649599428.4008615,
"ctime": 1648947238.992,
"dev": 64768,
"gid": 0,
"gr_name": "root",
"inode": 661181,
"isblk": false,
"ischr": false,
"isdir": false,
"isfifo": false,
"isgid": false,
"islnk": false,
"isreg": true,
"issock": false,
"isuid": false,
"mode": "0600",
"mtime": 1648947238.992,
"nlink": 1,
"path": "/root/.ssh/authorized_keys",
"pw_name": "root",
"rgrp": false,
"roth": false,
"rusr": true,
"size": 0,
"uid": 0,
"wgrp": false,
"woth": false,
"wusr": true,
"xgrp": false,
"xoth": false,
"xusr": false
},
"rc": 0,
"start": "2022-04-10 14:56:27.863508",
"stderr": "",
"stderr_lines": [],
"stdout": "",
"stdout_lines": []
},
{
"ansible_loop_var": "item",
"changed": true,
"cmd": "cat /home/ansible/.ssh/authorized_keys",
"delta": "0:00:00.003548",
"end": "2022-04-10 14:56:28.138414",
"failed": false,
"invocation": {
"module_args": {
"_raw_params": "cat /home/ansible/.ssh/authorized_keys",
"_uses_shell": true,
"argv": null,
"chdir": null,
"creates": null,
"executable": null,
"removes": null,
"stdin": null,
"stdin_add_newline": true,
"strip_empty_ends": true,
"warn": true
}
},
"item": {
"atime": 1649587940.6308339,
"ctime": 1649458786.3099895,
"dev": 64768,
"gid": 1001,
"gr_name": "ansible",
"inode": 661196,
"isblk": false,
"ischr": false,
"isdir": false,
"isfifo": false,
"isgid": false,
"islnk": false,
"isreg": true,
"issock": false,
"isuid": false,
"mode": "0600",
"mtime": 1649458786.3099895,
"nlink": 1,
"path": "/home/ansible/.ssh/authorized_keys",
"pw_name": "ansible",
"rgrp": false,
"roth": false,
"rusr": true,
"size": 561,
"uid": 1001,
"wgrp": false,
"woth": false,
"wusr": true,
"xgrp": false,
"xoth": false,
"xusr": false
},
"rc": 0,
"start": "2022-04-10 14:56:28.134866",
"stderr": "",
"stderr_lines": [],
"stdout": "ssh key",
"stdout_lines": [
"ssh key"
]
},
{
"ansible_loop_var": "item",
"changed": true,
"cmd": "cat /home/primary-admin/.ssh/authorized_keys",
"delta": "0:00:00.003515",
"end": "2022-04-10 14:56:28.408766",
"failed": false,
"invocation": {
"module_args": {
"_raw_params": "cat /home/primary-admin/.ssh/authorized_keys",
"_uses_shell": true,
"argv": null,
"chdir": null,
"creates": null,
"executable": null,
"removes": null,
"stdin": null,
"stdin_add_newline": true,
"strip_empty_ends": true,
"warn": true
}
},
"item": {
"atime": 1649587950.2066748,
"ctime": 1649495080.4039176,
"dev": 64768,
"gid": 1000,
"gr_name": "primary-admin",
"inode": 661199,
"isblk": false,
"ischr": false,
"isdir": false,
"isfifo": false,
"isgid": false,
"islnk": false,
"isreg": true,
"issock": false,
"isuid": false,
"mode": "0600",
"mtime": 1649495080.4039176,
"nlink": 1,
"path": "/home/primary-admin/.ssh/authorized_keys",
"pw_name": "primary-admin",
"rgrp": false,
"roth": false,
"rusr": true,
"size": 277,
"uid": 1000,
"wgrp": false,
"woth": false,
"wusr": true,
"xgrp": false,
"xoth": false,
"xusr": false
},
"rc": 0,
"start": "2022-04-10 14:56:28.405251",
"stderr": "",
"stderr_lines": [],
"stdout": "ssh key",
"stdout_lines": [
"ssh key"
]
}
]
}
Which I have summarised to be a list of 3 dictionaries:
"msg": [
{},
{},
{}
]
How do I get my playbook to print just the path and contents?
Thanks for your time!
Fix the last task "Print output". Otherwise, your code is working as expected. For example, given the files at the remote host for testing
shell> ssh admin#test_11 'find /tmp -name authorized_keys'
/tmp/root/.ssh/authorized_keys
/tmp/admin/.ssh/authorized_keys
shell> ssh admin#test_11 'cat /tmp/root/.ssh/authorized_keys'
ssh-rsa 111111111111111111
22222222222222222222222222
333333333333 admin#test_12
ssh-rsa 111111111111111111
22222222222222222222222222
333333333333 admin#test_13
shell> ssh admin#test_11 'cat /tmp/admin/.ssh/authorized_keys'
ssh-rsa 111111111111111111
22222222222222222222222222
333333333333 admin#test_12
ssh-rsa 111111111111111111
22222222222222222222222222
333333333333 admin#test_13
Running the playbook
shell> ansible-playbook playbook.yml
Enter host(s) to search: test_11
Enter file to show the contents of: authorized_keys
Set a list
- name: Print output
set_fact:
path_content: "{{ cat.results|
json_query('[].{path: item.path, content: stdout}') }}"
- debug:
var: path_content
gives
path_content:
- content: |-
ssh-rsa 111111111111111111
22222222222222222222222222
333333333333 admin#test_12
ssh-rsa 111111111111111111
22222222222222222222222222
333333333333 admin#test_13
path: /tmp/root/.ssh/authorized_keys
- content: |-
ssh-rsa 111111111111111111
22222222222222222222222222
333333333333 admin#test_12
ssh-rsa 111111111111111111
22222222222222222222222222
333333333333 admin#test_13
path: /tmp/admin/.ssh/authorized_keys
, or a dictionary
- name: Print output
set_fact:
path_content: "{{ cat.results|
json_query('[].{path: item.path, content: stdout}')|
items2dict(key_name='path', value_name='content') }}"
- debug:
var: path_content
gives
path_content:
/tmp/admin/.ssh/authorized_keys: |-
ssh-rsa 111111111111111111
22222222222222222222222222
333333333333 admin#test_12
ssh-rsa 111111111111111111
22222222222222222222222222
333333333333 admin#test_13
/tmp/root/.ssh/authorized_keys: |-
ssh-rsa 111111111111111111
22222222222222222222222222
333333333333 admin#test_12
ssh-rsa 111111111111111111
22222222222222222222222222
333333333333 admin#test_13
If you run the code on multiple hosts collect all results in a dictionary. For example, running the playbook
shell> ansible-playbook playbook.yml
Enter host(s) to search: test_11,test_12
Enter file to show the contents of: authorized_keys
Set the dictionary of all hosts
- name: Print output of all hosts
set_fact:
host_path_content: "{{ dict(ansible_play_hosts|
zip(ansible_play_hosts|
map('extract', hostvars, 'path_content'))) }}"
run_once: true
- debug:
var: host_path_content
gives
host_path_content:
test_11:
/tmp/admin/.ssh/authorized_keys: |-
ssh-rsa 111111111111111111
22222222222222222222222222
333333333333 admin#test_12
ssh-rsa 111111111111111111
22222222222222222222222222
333333333333 admin#test_13
/tmp/root/.ssh/authorized_keys: |-
ssh-rsa 111111111111111111
22222222222222222222222222
333333333333 admin#test_12
ssh-rsa 111111111111111111
22222222222222222222222222
333333333333 admin#test_13
test_12:
/tmp/admin/.ssh/authorized_keys: |-
ssh-rsa 111111111111111111
22222222222222222222222222
333333333333 admin#test_11
ssh-rsa 111111111111111111
22222222222222222222222222
333333333333 admin#test_13
/tmp/root/.ssh/authorized_keys: |-
ssh-rsa 111111111111111111
22222222222222222222222222
333333333333 admin#test_11
ssh-rsa 111111111111111111
22222222222222222222222222
333333333333 admin#test_13

Get data from ping output in ansible

I can't extract the ip from the register var after ping command from "shell" module.
ping.yml
---
- name: "Ping computers"
shell:
cmd: "ping -c1 -w 2 {{ pinging_host }}"
register: pingged_host
ignore_errors: yes
with_items:
- 192.168.1.27
- 192.168.1.42
loop_control:
loop_var: pinging_host
- name: "Result ping"
debug:
var: pingged_host
- name: " ***Ip ping"
debug:
var: pingged_host.results.value.pinging_host
...
output from jenkins:
TASK [ Result ping] **********************
ok: [computer1] => {
"pingged_host": {
"changed": true,
"failed": true,
"msg": "All items completed",
"results": [
{
"ansible_loop_var": "pinging_host",
"changed": true,
"cmd": "ping -c1 -w 2 192.168.1.27",
"delta": "0:00:00.003363",
"end": "2021-02-25 17:08:48.994930",
"failed": false,
"invocation": {
"module_args": {
"_raw_params": "ping -c1 -w 2 192.168.1.27",
"_uses_shell": true,
"argv": null,
"chdir": null,
"creates": null,
"executable": null,
"removes": null,
"stdin": null,
"stdin_add_newline": true,
"strip_empty_ends": true,
"warn": true
}
},
"pinging_host": "192.168.1.27",
"rc": 0,
"start": "2021-02-25 17:08:48.991567",
"stderr": "",
"stderr_lines": [],
"stdout": "PING 192.168.1.27 (192.168.1.27) 56(84) bytes of data.\n64 bytes from 192.168.1.27: icmp_seq=1 ttl=128 time=0.337 ms\n\n--- 192.168.1.27 ping statistics ---\n1 packets transmitted, 1 received, 0% packet loss, time 0ms\nrtt min/avg/max/mdev = 0.337/0.337/0.337/0.000 ms",
"stdout_lines": [
"PING 192.168.1.27 (192.168.1.27) 56(84) bytes of data.",
"64 bytes from 192.168.1.27: icmp_seq=1 ttl=128 time=0.337 ms",
"",
"--- 192.168.1.27 ping statistics ---",
"1 packets transmitted, 1 received, 0% packet loss, time 0ms",
"rtt min/avg/max/mdev = 0.337/0.337/0.337/0.000 ms"
]
},
{
"ansible_loop_var": "pinging_host",
"changed": true,
"cmd": "ping -c1 -w 2 192.168.1.42",
"delta": "0:00:02.003313",
"end": "2021-02-25 17:08:51.269047",
"failed": true,
"invocation": {
"module_args": {
"_raw_params": "ping -c1 -w 2 192.168.1.42",
"_uses_shell": true,
"argv": null,
"chdir": null,
"creates": null,
"executable": null,
"removes": null,
"stdin": null,
"stdin_add_newline": true,
"strip_empty_ends": true,
"warn": true
}
},
"msg": "non-zero return code",
"pinging_host": "192.168.1.42",
"rc": 1,
"start": "2021-02-25 17:08:49.265734",
"stderr": "",
"stderr_lines": [],
"stdout": "PING 192.168.1.42 (192.168.1.42) 56(84) bytes of data.\n\n--- 192.168.1.42 ping statistics ---\n2 packets transmitted, 0 received, 100% packet loss, time 10ms",
"stdout_lines": [
"PING 192.168.1.42 (192.168.1.42) 56(84) bytes of data.",
"",
"--- 192.168.1.42 ping statistics ---",
"2 packets transmitted, 0 received, 100% packet loss, time 10ms"
]
}
]
}
}
TASK [ ***Result ping] *******************
ok: [computer1] => {
"pingged_host.results.value.pinging_host": "VARIABLE IS NOT DEFINED!"
}
Nota: "VARIABLE IS NOT DEFINED!" or "...template error while templating string: expected name...", I have this result when I put other path's:
pingged_host.results.value.pinging_host
pingged_host.results.pinging_host
pingged_host.results.[pinging_host]
pingged_host.results[pinging_host]
pingged_host.results['pinging_host']
How I can read "pinging_host" or "rc" from my register var???????
Thank's
Your data structure in yaml looks like this
pingged_host:
results:
- pinging_host: 192.168.1.27
rc: 0
- pinging_host: 192.168.1.42
rc: 0
Since result contains an array, you have to reference exact element of the array.
i.e to get first result you should use
- name: " {{ role_name }} | ***Ip ping"
debug:
var: pingged_host.results[0].pinging_host
To get every attribute you can use map and list filters, as explained here https://ansiblemaster.wordpress.com/2017/02/24/debug-properly-data-registered-with-a-loop/ , if you want to get all rc's, you can use the following:
- name: " {{ role_name }} | ***Ip ping"
debug:
msg: "{{ pingged_host.results|map(attribute='rc')|list }}"
resulting in:
TASK [{{ role_name }} | ***Ip ping] **********************************************************************************************************************************************
ok: [localhost] => {
"msg": [
0,
0
]
}

Ansible Register access invocation_module?

I have a playbook that downloads a list of files from S3, this list can be set dynamically by utilizing with_items and |default([]).
After pulling this I need to get a list of destinations of the stored files and perform other actions. I registered the var output and see that invocation module_args has the value "Dest" which is what i want to access.
I've tried things like:
debug: msg="{{ item }}"
with_items: "{{ output.results }}
Or even accessing output.invocation but get undefined variable
Task:
- name: "Download Apps from S3"
aws_s3:
bucket: "{{ resource_bucket_name }}"
object: "{{ s3_apps[item].src }}"
dest: "{{ s3_apps[item].dest }}"
mode: get
with_items: "{{ s3_apps_decl |default([]) }}"
register: output
My output variable with debug:
"msg": [
{
"ansible_loop_var": "item",
"changed": true,
"failed": false,
"invocation": {
"module_args": {
"aws_access_key": null,
"aws_secret_key": null,
"bucket": "bucket-resources",
"debug_botocore_endpoint_logs": false,
"dest": "/tmp/test_app.tgz",
"dualstack": false,
"ec2_url": null,
"encrypt": true,
"encryption_kms_key_id": null,
"encryption_mode": "AES256",
"expiry": 600,
"headers": null,
"ignore_nonexistent_bucket": false,
"marker": "",
"max_keys": 1000,
"metadata": null,
"mode": "get",
"object": "test_app.tgz",
"overwrite": "always",
"permission": [
"private"
],
"prefix": "",
"profile": null,
"region": null,
"retries": 0,
"rgw": false,
"s3_url": null,
"security_token": null,
"src": null,
"validate_certs": true,
"version": null
}
},
"item": "test_app",
"msg": "GET operation complete"
},
{
"ansible_loop_var": "item",
"changed": true,
"failed": false,
"invocation": {
"module_args": {
"aws_access_key": null,
"aws_secret_key": null,
"bucket": "bucket-resources",
"debug_botocore_endpoint_logs": false,
"dest": "/tmp/testanotherapp.spl",
"dualstack": false,
"ec2_url": null,
"encrypt": true,
"encryption_kms_key_id": null,
"encryption_mode": "AES256",
"expiry": 600,
"headers": null,
"ignore_nonexistent_bucket": false,
"marker": "",
"max_keys": 1000,
"metadata": null,
"mode": "get",
"object": "testanotherapp.spl",
"overwrite": "always",
"permission": [
"private"
],
"prefix": "",
"profile": null,
"region": null,
"retries": 0,
"rgw": false,
"s3_url": null,
"security_token": null,
"src": null,
"validate_certs": true,
"version": null
}
},
"item": "testanotherapp",
"msg": "GET operation complete"
}
]
}
My expected output would be to define a variable that outputs:
['/tmp/testanotherapp.spl'.'/tmp/test_app.tgz']
I've tried set_fact with the similar syntax as my task above however that only saves the last value...
What you should expect in output is exactly what you get since you are receiving the return values from an aws_s3 module call which you are registering in a loop.
Now, if you want to get a list of only paths of all dest you have saved on your target host, you have to extract the corresponding attribute from your data structure.
- name: Show a list of dest paths from previous run
debug:
msg: "{{ output.results | map(attribute='invocation.module_args.dest') | list }}"

Docker: Cannot launch .Net Core 3.1 Web Api on browser after docker run

I'm new to Docker and, as a start, I'm trying to accomplish a basic task, dockerize a .Net Core 3.1 Web Api and run it from the command line (not From Visual Studio where it actually works).
I create my project image using the following Dockerfile and with the next command:
docker build -t concepttest_crud1 .
Dockerfile (created by Visual Studio):
FROM mcr.microsoft.com/dotnet/core/aspnet:3.1-buster-slim AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443
FROM mcr.microsoft.com/dotnet/core/sdk:3.1-buster AS build
WORKDIR /src
COPY ["Gfi_ConceptTest_CRUD1/Gfi_ConceptTest_CRUD1.csproj", "Gfi_ConceptTest_CRUD1/"]
RUN dotnet restore "Gfi_ConceptTest_CRUD1/Gfi_ConceptTest_CRUD1.csproj"
COPY . .
WORKDIR "/src/Gfi_ConceptTest_CRUD1"
RUN dotnet build "Gfi_ConceptTest_CRUD1.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "Gfi_ConceptTest_CRUD1.csproj" -c Release -o /app/publish
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "Gfi_ConceptTest_CRUD1.dll"]
4) I run my image with the following command:
docker run -d -p 8080:44390 --name crud1 concepttest_crud1
where 44390 is the port I have my api configured on in Visual Studio.
When debugging I use to access the api through:
https://localhost:44390/api/Authors
5) I'm trying to test my dockerized api in Chrome with the next url:
https://localhost:8080/api/Authors
to no avail. No matter with url I try, my api won't start when the docker run command executes with no errors.
docker ps output:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
8df6c92c6678 concepttest_crud1 "dotnet Gfi_ConceptT…" 22 minutes ago Up 22 minutes 0.0.0.0:8080->44390/tcp crud1
docker images output:
REPOSITORY TAG IMAGE ID CREATED SIZE
concepttest_crud1 latest 878e4c4845f6 About an hour ago 228MB
<none> <none> 36a29990113c About an hour ago 1GB
In the Docker images output I don't know why I see two images (a second with no name) when I expect only one.
I've also tried http://192.168.99.100:8080/api/Authors but it is not working either.
Edit 1: Adding docker inspect.
[
{
"Id": "0f0bf4b4de30ef5dd222016db639abe45e5e70bc25270b74df45eec64e319b57",
"Created": "2019-12-23T12:30:06.3947619Z",
"Path": "dotnet",
"Args": [
"Gfi_ConceptTest_CRUD1.dll"
],
"State": {
"Status": "running",
"Running": true,
"Paused": false,
"Restarting": false,
"OOMKilled": false,
"Dead": false,
"Pid": 83477,
"ExitCode": 0,
"Error": "",
"StartedAt": "2019-12-23T12:30:08.9701219Z",
"FinishedAt": "0001-01-01T00:00:00Z"
},
"Image": "sha256:92b9217abf5dc6a7fc6c10aac2ac5549d6873adfc1b3ed30264d6e1fd4a971c7",
"ResolvConfPath": "/var/lib/docker/containers/0f0bf4b4de30ef5dd222016db639abe45e5e70bc25270b74df45eec64e319b57/resolv.conf",
"HostnamePath": "/var/lib/docker/containers/0f0bf4b4de30ef5dd222016db639abe45e5e70bc25270b74df45eec64e319b57/hostname",
"HostsPath": "/var/lib/docker/containers/0f0bf4b4de30ef5dd222016db639abe45e5e70bc25270b74df45eec64e319b57/hosts",
"LogPath": "/var/lib/docker/containers/0f0bf4b4de30ef5dd222016db639abe45e5e70bc25270b74df45eec64e319b57/0f0bf4b4de30ef5dd222016db639abe45e5e70bc25270b74df45eec64e319b57-json.log",
"Name": "/crud1",
"RestartCount": 0,
"Driver": "overlay2",
"Platform": "linux",
"MountLabel": "",
"ProcessLabel": "",
"AppArmorProfile": "",
"ExecIDs": null,
"HostConfig": {
"Binds": null,
"ContainerIDFile": "",
"LogConfig": {
"Type": "json-file",
"Config": {}
},
"NetworkMode": "default",
"PortBindings": {
"443/tcp": [
{
"HostIp": "",
"HostPort": "8080"
}
]
},
"RestartPolicy": {
"Name": "no",
"MaximumRetryCount": 0
},
"AutoRemove": false,
"VolumeDriver": "",
"VolumesFrom": null,
"CapAdd": null,
"CapDrop": null,
"Capabilities": null,
"Dns": [],
"DnsOptions": [],
"DnsSearch": [],
"ExtraHosts": null,
"GroupAdd": null,
"IpcMode": "private",
"Cgroup": "",
"Links": null,
"OomScoreAdj": 0,
"PidMode": "",
"Privileged": false,
"PublishAllPorts": false,
"ReadonlyRootfs": false,
"SecurityOpt": null,
"UTSMode": "",
"UsernsMode": "",
"ShmSize": 67108864,
"Runtime": "runc",
"ConsoleSize": [
28,
165
],
"Isolation": "",
"CpuShares": 0,
"Memory": 0,
"NanoCpus": 0,
"CgroupParent": "",
"BlkioWeight": 0,
"BlkioWeightDevice": [],
"BlkioDeviceReadBps": null,
"BlkioDeviceWriteBps": null,
"BlkioDeviceReadIOps": null,
"BlkioDeviceWriteIOps": null,
"CpuPeriod": 0,
"CpuQuota": 0,
"CpuRealtimePeriod": 0,
"CpuRealtimeRuntime": 0,
"CpusetCpus": "",
"CpusetMems": "",
"Devices": [],
"DeviceCgroupRules": null,
"DeviceRequests": null,
"KernelMemory": 0,
"KernelMemoryTCP": 0,
"MemoryReservation": 0,
"MemorySwap": 0,
"MemorySwappiness": null,
"OomKillDisable": false,
"PidsLimit": null,
"Ulimits": null,
"CpuCount": 0,
"CpuPercent": 0,
"IOMaximumIOps": 0,
"IOMaximumBandwidth": 0,
"MaskedPaths": [
"/proc/asound",
"/proc/acpi",
"/proc/kcore",
"/proc/keys",
"/proc/latency_stats",
"/proc/timer_list",
"/proc/timer_stats",
"/proc/sched_debug",
"/proc/scsi",
"/sys/firmware"
],
"ReadonlyPaths": [
"/proc/bus",
"/proc/fs",
"/proc/irq",
"/proc/sys",
"/proc/sysrq-trigger"
]
},
"GraphDriver": {
"Data": {
"LowerDir": "/var/lib/docker/overlay2/3781dd694db551736c551397e9a0834ebd6fa653e0b78b6d6244e15ef7b8c291-init/diff:/var/lib/docker/overlay2/025232b693c4a6470e0b26e5302e5165757dbc4a44d5af5b7b8aa8dacd77ee03/diff:/var/lib/docker/overlay2/bcca2356fe199bbaa471c3590a3f55df343eecd84899cf570197d97ed8111d95/diff:/var/lib/docker/overlay2/5f2d77acbd50bc4a8cde011128fb43b2a0cff888e716c5ba882d4dd906bc1901/diff:/var/lib/docker/overlay2/ca57ac72ac1d48b622745bb473fd71c18d67392b02cdf938835f05cf9b547681/diff:/var/lib/docker/overlay2/0b894b31b26df6e40b57a86dc66bbd4eda71d0ebf6d5755c4c43bc296e9a24de/diff:/var/lib/docker/overlay2/b6ca0109d2718605ccba2585e52ae1718cbecd2fcd62197f1f7fd505cf2be358/diff:/var/lib/docker/overlay2/e0ff62d02fe725053674ce9e3b59a4004c101fd2e98f919e5037a412f1c3a4e8/diff",
"MergedDir": "/var/lib/docker/overlay2/3781dd694db551736c551397e9a0834ebd6fa653e0b78b6d6244e15ef7b8c291/merged",
"UpperDir": "/var/lib/docker/overlay2/3781dd694db551736c551397e9a0834ebd6fa653e0b78b6d6244e15ef7b8c291/diff",
"WorkDir": "/var/lib/docker/overlay2/3781dd694db551736c551397e9a0834ebd6fa653e0b78b6d6244e15ef7b8c291/work"
},
"Name": "overlay2"
},
"Mounts": [],
"Config": {
"Hostname": "0f0bf4b4de30",
"Domainname": "",
"User": "",
"AttachStdin": false,
"AttachStdout": false,
"AttachStderr": false,
"ExposedPorts": {
"443/tcp": {},
"8080/tcp": {}
},
"Tty": true,
"OpenStdin": false,
"StdinOnce": false,
"Env": [
"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
"ASPNETCORE_URLS=http://+:80",
"DOTNET_RUNNING_IN_CONTAINER=true"
],
"Cmd": null,
"Image": "crud1",
"Volumes": null,
"WorkingDir": "/app",
"Entrypoint": [
"dotnet",
"Gfi_ConceptTest_CRUD1.dll"
],
"OnBuild": null,
"Labels": {}
},
"NetworkSettings": {
"Bridge": "",
"SandboxID": "ed889f3303ca17ad4e79ae917d8b17c5e590af502adb8049dbd7e0fcd46b323c",
"HairpinMode": false,
"LinkLocalIPv6Address": "",
"LinkLocalIPv6PrefixLen": 0,
"Ports": {
"443/tcp": [
{
"HostIp": "0.0.0.0",
"HostPort": "8080"
}
],
"8080/tcp": null
},
"SandboxKey": "/var/run/docker/netns/ed889f3303ca",
"SecondaryIPAddresses": null,
"SecondaryIPv6Addresses": null,
"EndpointID": "001cd49640c4211e5eeb1eedacedd6de07022cc3362acaca59b82f0e537f6238",
"Gateway": "172.17.0.1",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"IPAddress": "172.17.0.2",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"MacAddress": "02:42:ac:11:00:02",
"Networks": {
"bridge": {
"IPAMConfig": null,
"Links": null,
"Aliases": null,
"NetworkID": "ccea227190a1f04d0043015cf3d9932ba41f4a96dfb9bc2a175a06483a9ca66e",
"EndpointID": "001cd49640c4211e5eeb1eedacedd6de07022cc3362acaca59b82f0e537f6238",
"Gateway": "172.17.0.1",
"IPAddress": "172.17.0.2",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"MacAddress": "02:42:ac:11:00:02",
"DriverOpts": null
}
}
}
}
]
Edit 2: Added curl.
docker exec -it 0f0bf4b4de30 /bin/bash . root#4237f947b2b0:/app# curl https://localhost:8080/api/Authors
.: .: Is a directory
Edit 3: New curl
docker exec -it 0f0bf4b4de30 /bin/bash . root#4237f947b2b0:/app# curl https://localhost/api/Authors
.: .: Is a directory
In my case it was due to having SSL enabled inthe project, but after removing everything related to https it started working as expected.
Having SSL enabled implies installing a SSL certificate inside the docker container and enabling it, things I wasn't able to do as I don't have a certificate and I'm just learning docker.
Steps I've done:
In Startup.cs I've disabled https redirection
//app.UseHttpsRedirection();
And in project properties under "Debug" section I unchecked "Enable SSL".
Then I was able to run the container as expected.

Unable to connect to dockerized redis instance from outside docker

I have the latest docker installation (without boot2docker) and I am unable to connect to a dockerized redis instance running locally. Could you please tell me what I'm doing wrong here?
Created the docker, mapped port 6379 to 127.0.0.1:6379
bash-3.2$ docker run -p 127.0.0.1:6379:6379 --name webmonitor-redis -d redis
3291541d58ab16c362f9e0cd7017d179c0bc9aef3a1323e79f1e1ca075e171c9
docker ps output:
bash-3.2$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
3291541d58ab redis "/entrypoint.sh redis" 14 seconds ago Up 6 seconds 127.0.0.1:6379->6379/tcp webmonitor-redis
Tried connecting from outside container (but the same host where container is running), connection failed:
bash-3.2$ ./src/redis-cli
Could not connect to Redis at 127.0.0.1:6379: Connection refused
not connected> exit
It works if I try to connect from another container though..
bash-3.2$ docker run -it --link webmonitor-redis:redis --rm redis sh -c 'exec redis-cli -h "$REDIS_PORT_6379_TCP_ADDR" -p "$REDIS_PORT_6379_TCP_PORT"'
172.17.0.8:6379>
Here's the docker inspect for the container:
bash-3.2$ docker inspect 3291541d58ab
[
{
"Id": "3291541d58ab16c362f9e0cd7017d179c0bc9aef3a1323e79f1e1ca075e171c9",
"Created": "2015-10-03T15:48:17.818355794Z",
"Path": "/entrypoint.sh",
"Args": [
"redis-server"
],
"State": {
"Running": true,
"Paused": false,
"Restarting": false,
"OOMKilled": false,
"Dead": false,
"Pid": 7769,
"ExitCode": 0,
"Error": "",
"StartedAt": "2015-10-03T15:48:17.954436198Z",
"FinishedAt": "0001-01-01T00:00:00Z"
},
"Image": "2f2578ff984f013c9a5d6cbb6fe061ed3f73a17380a4c9b53b76d4b8da3eda7d",
"NetworkSettings": {
"Bridge": "",
"EndpointID": "b787e46d1219f36d4f1b1ea35c5f750f7174221137fb01889a26a3bc1e1c6aee",
"Gateway": "172.17.42.1",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"HairpinMode": false,
"IPAddress": "172.17.0.8",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"LinkLocalIPv6Address": "",
"LinkLocalIPv6PrefixLen": 0,
"MacAddress": "02:42:ac:11:00:08",
"NetworkID": "30176c9c7c14a6a052af784014832a0c52b5966089d7bcfe535041569e6bb1c9",
"PortMapping": null,
"Ports": {
"6379/tcp": [
{
"HostIp": "127.0.0.1",
"HostPort": "6379"
}
]
},
"SandboxKey": "/var/run/docker/netns/3291541d58ab",
"SecondaryIPAddresses": null,
"SecondaryIPv6Addresses": null
},
"ResolvConfPath": "/mnt/sda1/var/lib/docker/containers/3291541d58ab16c362f9e0cd7017d179c0bc9aef3a1323e79f1e1ca075e171c9/resolv.conf",
"HostnamePath": "/mnt/sda1/var/lib/docker/containers/3291541d58ab16c362f9e0cd7017d179c0bc9aef3a1323e79f1e1ca075e171c9/hostname",
"HostsPath": "/mnt/sda1/var/lib/docker/containers/3291541d58ab16c362f9e0cd7017d179c0bc9aef3a1323e79f1e1ca075e171c9/hosts",
"LogPath": "/mnt/sda1/var/lib/docker/containers/3291541d58ab16c362f9e0cd7017d179c0bc9aef3a1323e79f1e1ca075e171c9/3291541d58ab16c362f9e0cd7017d179c0bc9aef3a1323e79f1e1ca075e171c9-json.log",
"Name": "/webmonitor-redis",
"RestartCount": 0,
"Driver": "aufs",
"ExecDriver": "native-0.2",
"MountLabel": "",
"ProcessLabel": "",
"AppArmorProfile": "",
"ExecIDs": null,
"HostConfig": {
"Binds": null,
"ContainerIDFile": "",
"LxcConf": [],
"Memory": 0,
"MemorySwap": 0,
"CpuShares": 0,
"CpuPeriod": 0,
"CpusetCpus": "",
"CpusetMems": "",
"CpuQuota": 0,
"BlkioWeight": 0,
"OomKillDisable": false,
"MemorySwappiness": -1,
"Privileged": false,
"PortBindings": {
"6379/tcp": [
{
"HostIp": "127.0.0.1",
"HostPort": "6379"
}
]
},
"Links": null,
"PublishAllPorts": false,
"Dns": null,
"DnsSearch": null,
"ExtraHosts": null,
"VolumesFrom": null,
"Devices": [],
"NetworkMode": "default",
"IpcMode": "",
"PidMode": "",
"UTSMode": "",
"CapAdd": null,
"CapDrop": null,
"GroupAdd": null,
"RestartPolicy": {
"Name": "no",
"MaximumRetryCount": 0
},
"SecurityOpt": null,
"ReadonlyRootfs": false,
"Ulimits": null,
"LogConfig": {
"Type": "json-file",
"Config": {}
},
"CgroupParent": "",
"ConsoleSize": [
0,
0
]
},
"GraphDriver": {
"Name": "aufs",
"Data": null
},
"Mounts": [
{
"Name": "643a80cbd7a50cfd481acc48721b34030c8ce55ba64ac3bc161d5b330c9374d2",
"Source": "/mnt/sda1/var/lib/docker/volumes/643a80cbd7a50cfd481acc48721b34030c8ce55ba64ac3bc161d5b330c9374d2/_data",
"Destination": "/data",
"Driver": "local",
"Mode": "",
"RW": true
}
],
"Config": {
"Hostname": "3291541d58ab",
"Domainname": "",
"User": "",
"AttachStdin": false,
"AttachStdout": false,
"AttachStderr": false,
"ExposedPorts": {
"6379/tcp": {}
},
"PublishService": "",
"Tty": false,
"OpenStdin": false,
"StdinOnce": false,
"Env": [
"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
"REDIS_VERSION=3.0.3",
"REDIS_DOWNLOAD_URL=http://download.redis.io/releases/redis-3.0.3.tar.gz",
"REDIS_DOWNLOAD_SHA1=0e2d7707327986ae652df717059354b358b83358"
],
"Cmd": [
"redis-server"
],
"Image": "redis",
"Volumes": {
"/data": {}
},
"VolumeDriver": "",
"WorkingDir": "/data",
"Entrypoint": [
"/entrypoint.sh"
],
"NetworkDisabled": false,
"MacAddress": "",
"OnBuild": null,
"Labels": {}
}
}
]
Am I missing something here?
make sure the port 6379 on the host is forwarded to port 6379 on docker.
use -p 6379:6379 to fixed the problem:
docker run -d --name redisDev -p 6379:6379 redis
The VM has it's own IP 192.168.99.100 so I was able to connect by binding to 192.168.99.100:6379:6379 and then connecting as below.
0c4de9a25467:redis-stable electron$ ./src/redis-cli -h 192.168.99.100
192.168.99.100:6379>
docker run -p 6379:6379 -host 0.0.0.0 --name webmonitor-redis -d redis