How to connect with container inside VM - docker

I have issue to connect from my local machine through SSH with Docker container on which I have openssh-server installed and exposed on port 22 (default for openssh server) and this container is on virtual machine.
Here is my dockerfile:
FROM ubuntu:latest
RUN apt-get -y update
RUN apt-get -y install openssh-server
EXPOSE 22
After expose 22 in dockerfile shouldn't I be able to connect for example through ssh://user#vmIP:22?

First of all, it is not ideal to connect via SSH to a running docker container, read this so you can understand why https://www.cloudbees.com/blog/ssh-into-a-docker-container-how-to-execute-your-commands, now if you really want to do that, the EXPOSE instruction is a way to document to the Dockerfile maintainer or another dev that you are most likely to have a service running on that port, it will not map that port to the host were you are running the container.
In order to map a port from the container to the VM you can do this:
#Build the container in the same directory where the Dockerfile is located
docker build . -t mycontainer:latest
#Run it mapping port 22 to the VMs port 2222 in detached mode
docker run -d -p 2222:22 mycontainer:latest
Now, you have port 2222 of the VM mapped to the running container port 22, so ssh://user#vmip:222 would work

Related

Unable to Run the Fast API Application deployed in Docker Container

I have create a Docker Image based on the following DockerFile
FROM tiangolo/uvicorn-gunicorn-fastapi:python3.7
COPY . /usr/app/
EXPOSE 80
WORKDIR /usr/app/
RUN pip install -r requirements.txt
CMD ["uvicorn", "app_Testing_Praveen:app", "--host", "0.0.0.0", "--port", "80"]
following the documentation available at
https://fastapi.tiangolo.com/deployment/docker/
After running the command
docker run -p 80:80 image_name
My docker image is running but giving the address as 0.0.0.0:80
But I am not able to find the absolute link to open the application. I know, due to virtualization there will be different external IP address for docker.
I found that IP on my docker network interface as "docker subnet mask" but that value is also not opening the applicatiln on browser.
My docker version is Docker version 20.10.5, build 55c4c88 and I am running this on windows.
You reach your services inside Docker containers, via the IP of the host machine
So you either access your service by http://localhost:80 or, from another machine, with http://<docker_host_ip>:80.

Starting Tomcat8 in docker doesnt work as in native ubuntu 16.04 environment

Following docker image starts tomcat8 in a fresh ubuntu 16.04 in a virtualbox but doesnt in a docker container. Is this a problem with docker, tomcat or am I missing on something?
Dockerfile:
FROM ubuntu:16.04
RUN apt update
RUN apt install -y openjdk-8-jdk
RUN apt-get install -y tomcat8
CMD service tomcat8 start
I assume that the image is built correctly (docker build command ends without errors)
While running the docker container just connect to it and check its logs:
docker logs <CONTAINER_ID> -f
You should see what happens there and why does tomcat fail to start. Maybe Java is not mapped correctly, maybe the ports are busy (unlikely but who knows).
And maybe tomcat starts correctly but you can't access it from outside because the 8080 port is not exposed / mapped (EXPOSE 8080 in docker file / -p 8080:8080 option while running a docker container)

How do I advertise AND browse mDNS from within docker container?

I'm trying to create a ubuntu 17.04 based docker container that can browse mDNS on my network (outside of the docker network) AND advertise on mDNS to my network (outside of docker network).
I want to be able to run this docker container on a macOS host (during my development) AND a Linux (Debian) host for production.
https://github.com/ianblenke/docker-avahi seems to have solved this for Linux hosts (utilizing avahi daemon and mapping the /var/run/dbus volume to the host). When I'm developing on my macbook, I would like to use mDNSResponder.
How do I create a container that can advertise and browse on my local network, that will also run on my macOS laptop and on a Linux server?
Here is what I have so far.
Dockerfile
FROM ubuntu:17.04
WORKDIR /app
RUN apt-get update && apt-get install -yq avahi-daemon avahi-utils libnss-mdns \
&& apt-get -qq -y autoclean \
&& apt-get -qq -y autoremove \
&& apt-get -qq -y clean
RUN update-rc.d avahi-daemon enable
COPY docker/etc/nsswitch.conf /etc/nsswitch.conf
COPY docker/etc/avahi-daemon.conf /etc/avahi/avahi-daemon.conf
COPY docker/start.sh /app
CMD ["/bin/bash","start.sh"]
start.sh
#!/bin/bash
service avahi-daemon restart
service avahi-daemon status
avahi-browse -a
nsswitch.conf
hosts: files mdns_minimal [NOTFOUND=return] dns
avahi-daemon.conf
...
enable-dbus=no
...
Running
docker run --net=host -it mdns1
* Restarting Avahi mDNS/DNS-SD Daemon avahi-daemon [ OK ]
Avahi mDNS/DNS-SD Daemon is running
Failed to create client object: Daemon not running
As you can see avahi-daemon is running, but avahi-browse doesn't think it is. Is this because I disabled dbus?
Running the same commands (except I keep enable-dbus=yes) inside a 17.04 virtualbox image on my mac things work just fine.
Update: it looks like you can not do bridged networking on a macOS host. So is what I am trying to do impossible?
I'm currently trying to get avahi working inside a docker container and in my research came across this:
you can in the Avahi settings configuration disable dbus so it won't
use it. Then when you run Avahi in Docker you must pass it the
--no-rlimits flag and it'll work without compromising your containers security.
https://www.reddit.com/r/docker/comments/54ufz2/is_there_any_way_to_run_avahi_in_docker_without/
Hopefully this can help with your situation.
For mdns advertising/listening we run
dnssd
inside docker containers.
But! In order to be discoverable on a local network
the docker container should have an IP address from the network, proper routes from the network to docker container should be configured.
If you do not have control over the default router of the network,
you can try to use macvlan/ipvlan network driver.
It will allows you to assign multiple mac/IP addresses on the same network interface.
In our case, the network is wifi, so we had to use the ipvlan, because macvlan does not works with wifi. In a wired case you should prefer macvlan.

Rails app docker container not accessible from Windows host

I am trying to make a simple docker container that runs the Rails app from the directory that I launch it in.
Everything appears to be fine except when I run the container and try to access it from my Windows host at the IP address that Docker Machine gives me, it responds with a connection refused error message.
I even used the Nginx Dockerfile as a reference, because the Nginx Dockerfile actually builds a container that is accessible for me.
Here is my Dockerfile so far:
FROM ruby:2.3.1
RUN gem install rails && \
apt-get update -y && \
apt-get install -y nodejs
VOLUME ["/web_app"]
ADD . /web_app
WORKDIR /web_app
RUN bundle install
CMD rails s -p 80
EXPOSE 80
I build the image using this command
docker build -t rails_server .
I then run it using this command
docker run -d -p 80:80 rails_server
And here is what I try to access the webpage:
curl $(docker-machine ip)
And this is what I get back:
curl: (7) Failed to connect to 192.168.99.100 port 80: Connection refused
And this is how it makes me feel:
The problem here seems to be that the app is listening on 127.0.0.1:80, so the service will not accept connection from outside the container. Could you check if modifying the rails server to listening on 0.0.0.0 the issue solves?
You can do that using the -b flag of rails s:
FROM ruby:2.3.1
RUN gem install rails && \
apt-get update -y && \
apt-get install -y nodejs
VOLUME ["/web_app"]
ADD . /web_app
WORKDIR /web_app
RUN bundle install
CMD rails s -b 0.0.0.0 -p 80
EXPOSE 80
The port is only exposed to the vm running the docker inside. You have to still expose port 80 of your vm to your local machine so it can connect to it. I think the best approach is making your container to be listened o an optional port like 7070 and then using a simple nginx proxy pass to feed the content to the outside (listening on port 80)

Can't reach ActiveMQ from inside Docker container

I'm trying to access an ActiveMQ instance on my local machine from inside a docker container also running on my machine. AMQ is listening on 0.0.0.0:61616. I tried to configure my program running in the container to use the ip address of docker0 as well as enp6s0, both didn't work.
If I however use the --net=host option it suddenly works, no matter which ip address I use. The problem is that I can't use the option in production as the code that starts the container doesn't support this. So if it's not possible to change the default network in the Dockerfile, I have to fix this in a different way.
EDIT: My Dockerfile
FROM java:8-jre
RUN mkdir -p /JCloudService
COPY ./0.4.6-SNAPSHOT-SHADED/ /JCloudService
RUN apt-get update && apt-get install netcat -y && apt-get install nano
WORKDIR /JCloudService
CMD set -x; /bin/sh -c '/JCloudService/bin/JCloudScaleService'
And the run command: docker run -it jcs:latest. With this command it doesn't work. Only if I add --net=host
--net=host works because it tells Docker to put your container in the same networking stack as your host machine.
To connect to a service running on your machine you need the ip of your host machine on the docker0 network. So ip addr show docker0 on your host, then you should be able to use that IP and 61616 to connect to the host from within the container.

Resources