How to access container's web application from host - docker

I am running a site inside Docker container which exports following
https://172.17.0.2:8443/admin/ &
http://172.17.0.2:8463/users/
$ docker run -it -d --expose=8000-9900 ubuntu-java8-webapp
bf193d011fd8....
Docker PS cmd
$ docker ps -a
CONTAINER ID IMAGE COMMAND PORTS NAMES
bf193d011fd8 ubuntu-.... "/bin/bash" 8000-9900/tcp desperate_mclean
Docker ls cmd
$ docker-machine ls
NAME ACTIVE DRIVER STATE URL DOCKER ERRORS
default * virtualbox Running tcp://192.168.99.100:2376 v1.10.3
Docker machine ip cmd
$ docker-machine ip default
192.168.99.100
How do I access the site? Just in case it matters, I am running docker on Mac here.

You can try and access it through the docker machine IP:
https://192.168.99.100:8443/admin
http://192.168.99.100:8463/users
But ideally, you would:
map those port to the host:
docker run -p 8443:8443 -p 8463:8463 ...
port-forward those port to your actual host through VirtualBox VM Network setting, and access the site with:
https://localhost:8443/admin
http://localhost:8463/users

Related

when i ssh into a docker, the things are different from I docker run from its host?

I have a docker image, mapped the host 8888 to docker 22.
when i use another computer to ssh to host 8888, it goes into docker directly, but it's weird.
if i use 'sudo docker exec -u 0 -it xxxx /bin/bash' goes into it, when i input 'pip list', i can get the results like below
but if i ssh to docker via host 8888 port by root, it says command not found!
and if i input python from docker exec
well by ssh directly into docker, it is like this
totally different, what should i do to ssh into docker like docker exec from host, much appreciate!

how to ssh to docker container created from one machine (centos) from another machine(centos or mac)

I want to create a docker container from one machine (suppose having centos) machine and then access that container from another machine(may be centos or mac). How can we do that? Is it possible with macvlan networking? If yes , what are steps? If not, what is the way?
Depends from what is your final goal. Following are some approaches (depending on what you want to achieve as final goal):
Manage container and execute bash in the container on a remote host:
Easiest way is to use the environment variable DOCKER_HOST
export DOCKER_HOST=ssh://vagrant#192.168.5.178
docker exec -ti centos_remote /bin/bash
You can find more information in this answer https://stackoverflow.com/a/51897942/2816703
Use the container as a form of virtual machine on which user can ssh:
First you will need a container that is running the sshd. You will expose the port 22 on another port on the host network. Finally you will use the ssh with -p to connect that port. Here is a working example:
$ sudo docker run -d -P --name test_sshd rastasheep/ubuntu-sshd:14.04
$ sudo docker port test_sshd 22
0.0.0.0:49154
$ ssh root#localhost -p 49154
# The password is `root`
root#test_sshd $
or if you are on a remote machine, use the host IP address xxx.xxx.xxx.xxx, to connect to the container use:
$ ssh root#xxx.xxx.xxx.xxx -p 49154
# The password is `root`
Also you can pre-select a port (in this case port 22000) and test from the host.
~# docker run -d -p 22000:22 --name test_sshd rastasheep/ubuntu-sshd:14.04
~# ssh root#<ipaddress> -p 22000
Setup a network layer (L2/L3) between the hosts:
Using macvlan is one approach. Another approach is the ipvlan. In both cases, you are converting the host network adapter to a virtual router, after which you need to setup the routes. You can find detailed explanation on this link http://networkstatic.net/configuring-macvlan-ipvlan-linux-networking/

SSH from host machine into docker container

We're learning about docker and for practice we have to SSH from the host machine into a container. I'm running Ubuntu server on VMWare Workstation. I have successfully installed SSH and the service is running. The container I've created is running on an Ubuntu image. When I try to SSH into the container by using #ssh root#ContainerIP, I get the error "Connection refused". How can I fix this?
Try the following commands.
docker ps
It will give you a list of all the working containers. Select the appropriate container in which you want to log in and pass to below command
docker exec -it container bash
It will log you in the container.
Firstly you need to install a SSH server in the images you wish to ssh-into. You can use a base image for all your container with the ssh server installed. Then you only have to run each container mapping the ssh port (default 22) to one to the host's ports (Remote Server in your image), using -p :. i.e:
docker run -p 52022:22 container1
docker run -p 53022:22 container2
Then, if ports 52022 and 53022 of hosts are accessible from outside, you can directly ssh to the containers using the IP of the host (Remote Server) specifying the port in ssh with -p . I.e.:
ssh -p 52022 myuser#RemoteServer --> SSH to container1
ssh -p 53022 myuser#RemoteServer --> SSH to container2
I think this post would help a lot: How to SSH into Docker?

Tomcat with Docker

I am testing out running a tomcat8 on my Mac. I have the following Dockerfile:
FROM tomcat:8-jre7
MAINTAINER "Sonam Lastname <sonam#mymail.com>"
When I run the Docker container with the following command:
docker run -d -P sonam/docker-webapp
I check for the docker process by
docker ps -l
and see the port mapped at:
0.0.0.0:32769->8080/tcp
I am not able to access the tomcat page with localhost:32769 (and even tried to 8080 port).
thanks
-Sonam
Docker runs in a virtual machine when on Mac. Tomcat will listen to that network interface.
You can run docker-machine ip <name of your docker machine> and access it via that IP instead of localhost.
If on boot2docker, it is similar: boot2docker ip.

docker: connect to database container via dockerhost

I am trying to connect from an application container to a database container in two situations, one succeeds, one doesn't.
There are two containers on my dockerhost:
mysql container with port 3306 connected to 3356 on dockerhost
application container
At work, dockerhost has IP-address 10.0.2.15, at home, dockerhost has IP-address 192.168.8.11 (hostname -I).
In both situations, I want to connect to the database container from the app container with host 10.0.2.15/192.168.8.11 and port 3356.
When I do this at work (Windows network, Vagrant/Virtualbox dockerhost), this is no problem. I can 'telnet 10.0.2.15 3356' from the app container and connect to the db container.
When I do this at home (Ubuntu), it is impossible to connect. The only way is to use the docker ip address of the db container (172.17.0.2) with port 3306. However, I can ping 192.168.8.11.
The scripts to start the containers are identical; I do not use --add-host, so the dockerhost IP-address is not in /etc/hosts.
Any suggestions?
Ok, use docker to run 3 database instances
docker run --name mysqldb1 -e MYSQL_ROOT_PASSWORD=changeme -d mysql
docker run --name mysqldb2 -e MYSQL_ROOT_PASSWORD=changeme -d mysql
docker run --name mysqldb3 -e MYSQL_ROOT_PASSWORD=changeme -d mysql
Each one will have a different IP address on my host machine:
$ for i in mysqldb1 mysqldb2 mysqldb3
> do
> docker inspect --format='{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' $i
> done
172.17.0.2
172.17.0.3
172.17.0.4
Repeat this on your machine and you'll very likely have different IP addresses.
So how is this problem fixed.
The older approach (deprecated in docker 1.9) is to use links. The following commands will shows how environment variables are set within your linked application container (the one using the database)
$ docker run -it --rm --link mysqldb1:mysql mysql env
..
MYSQL_PORT_3306_TCP_ADDR=172.17.0.2
$ docker run -it --rm --link mysqldb2:mysql mysql env
..
MYSQL_PORT_3306_TCP_ADDR=172.17.0.3
$ docker run -it --rm --link mysqldb3:mysql mysql env
..
MYSQL_PORT_3306_TCP_ADDR=172.17.0.4
And the following demonstrates how links are also created:
$ docker run -it --rm --link mysqldb1:mysql mysql grep mysql /etc/hosts
172.17.0.2 mysql 2a12644351a0 mysqldb1
$ docker run -it --rm --link mysqldb2:mysql mysql grep mysql /etc/hosts
172.17.0.3 mysql 89140cbf68c7 mysqldb2
$ docker run -it --rm --link mysqldb3:mysql mysql grep mysql /etc/hosts
172.17.0.4 mysql 27535e8848ef mysqldb3
So you can just refer to the other container using the "mysql" hostname or the "MYSQL_PORT_3306_TCP_ADDR" environment variable.
In Docker 1.9 there is a more powerful networking feature that enables containers to be linked across hosts.
http://docs.docker.com/engine/userguide/networking/dockernetworks/
you can use my container acting as a NAT gateway to dockerhost without any manually setup https://github.com/qoomon/docker-host

Resources