Tomcat with Docker - 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.

Related

Docker Desktop created container do not have access to macos [duplicate]

Running docker for Mac 17.06.0 I have created a docker file that creates an image of Apache server. Notice it exposes port 80.
FROM ubuntu:16.04
RUN apt-get update
RUN apt-get install -y apache2
ADD index.html /var/www/html/
CMD /usr/sbin/apache2ctl -D FOREGROUND
EXPOSE 80
In the same folder of the Dockerfile I have created a simple index.httml file.
Then I built and ran it using
docker build -t webserver .
docker run -d webserver
I took the IP address of the running container using
docker inspect [container_name] | grep -i IPAddress
and when I curl
curl 172.17.0.2
I get no answer.
I do get an answer when running -p 80:80 and using localhost in the curl command.
curl localhost
But I want to understand why can't I curl the container IP.
Questions:
How can I get an answer for my curl?
I understand I can't ping my container when using docker for Mac (link).
Can I telnet it just to verify that the port is exposed?
Can I SSH it?
On Docker for Mac the Docker engine is running inside a small VM using Hyper-V. As consequence, the ip 172.17.0.2 is valid only inside that VM and not on your host system. See https://docs.docker.com/docker-for-mac/docker-toolbox/#the-docker-for-mac-environment for more details and comparison to other VM concepts like Docker Machine.
When you run your Docker container, you need to bind a local port to the container like so:
docker run -d -p 80:80 webserver
where the first 80 is the port on the localhost and the second is the port on the container that is exposed. Just having the port exposed in the dockerfile is not enough to access it from the localhost.

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?

How to access Docker Selenium hub console in browser?

I am running docker selenium hub on windows. The container is up as docker log shows. However I am confused at what is the actual hostname that the hub uses. When i use conventional Selenium hub, I can use http://localhost:4444/grid/console to check that it's running correctly. But in this case of docker, I am confused at the printing of Docker log.
I tried 0.0.0.0, localhost, 172.17.0.2 as hostname to open /grid/console in browser. None of them works.
Also when I tried to use 'netstat -a' to list all tcp connections, I do not see any of them has port 4444.
Could anyone let me know what went wrong?
You are running docker on windows, there are two things you can do.
Map your port 4444 to host when starting the container.
On windows, your container is ruining in Boot2Docker virtual machine.
So instead of http://localhost:4444/grid/console use
http://{ip of boot2docker vm}:4444/grid/console
To get IP on boot2docker machine try the following command:
docker-machine ip default
Port expose docker example:
docker run -d -p 4444:4444 --name <container-name> <image-name> 

docker visit container from LAN

I have a program running inside a docker container and I expose the port 8888. However, when I try to connect the program from a device (in the same LAN as host machine but not the host machine) it failed.
Here is my docker file
FROM golang:1.10.1
......
RUN go build -buildmode=plugin -o plugin.so plugin.go
EXPOSE 8666:8888
And I start the container with
docker run -it -P --network host plugin:v0.3 bash
and run
go run program.go
in bash.
It says
2018/07/30 01:51:43 listening port 8888
But I cannot connect to the port from other device(which is in the same LAN as host machine but not the host machine)
then I tried
docker ps -a
It looks different in that the ports column are empty(Usually there should be a mapping?)
Expose won’t create port mappings for you, they’re just a “note from the designer to the user”. Add -p 8666:8888 to your Docker run command line.
I've solved the problem by replacing
go run program.go
afer
docker run -it -P --network host plugin:v0.3 bash
with
docker run -p 8666:8888 plugin:v0.5 go run program.go
It's like magic I still don't know why but it works.(seems like docker will only do the port forwarding when the container starts)
I'll dig into it later.

Docker container doesn't expose ports when --net=host is mentioned in the docker run command

I have a CentOS docker container on a CentOS docker host. When I use this command to run the docker image docker run -d --net=host -p 8777:8777 ceilometer:1.x the docker container get host's IP but doesn't have ports assigned to it.
If I run the same command without "--net=host" docker run -d -p 8777:8777 ceilometer:1.x docker exposes the ports but with a different IP. The docker version is 1.10.1. I want the docker container to have the same IP as the host with ports exposed. I also have mentioned in the Dockerfile the instruction EXPOSE 8777 but with no use when "--net=host" is mentioned in the docker run command.
I was confused by this answer. Apparently my docker image should be reachable on port 8080. But it wasn't. Then I read
https://docs.docker.com/network/host/
To quote
The host networking driver only works on Linux hosts, and is not supported on Docker for Mac, Docker for Windows, or Docker EE for Windows Server.
That's rather annoying as I'm on a Mac. The docker command should report an error rather than let me think it was meant to work.
Discussion on why it does not report an error
https://github.com/docker/for-mac/issues/2716
Not sure I'm convinced.
The docker version is 1.10.1. I want the docker container to have same ip as the host with ports exposed.
When you use --net=host it tells the container to use the hosts networking stack. So you can't expose ports to the host, because it is the host (as far as the network stack is concerned).
docker inspect might not show the expose ports, but if you have an application listening on a port, it will be available as if it were running on the host.
On Linux, I have always used --net=host when myapp needed to connect to an another docker container hosting PostgreSQL.
myapp reads an environment variable DATABASE in this example
Like Shane mentions this does not work on MacOS or Windows...
docker run -d -p 127.0.0.1:5432:5432 postgres:latest
So my app can't connect to my other other docker container:
docker run -e DATABASE=127.0.0.1:5432 --net=host myapp
To work around this, you can use host.docker.internal instead of 127.0.0.1 to resolve your hosts IP address.
Therefore, this works
docker run -e DATABASE=host.docker.internal:5432 -d myapp
Hope this saves someone time!

Resources