So I'm trying to setup a new container that needs to communicate with mysql. I setup the mysql container. I did a
docker network ls
to see the name of the network it uses. When I start the snipe-it container using
docker run -d -p 8082:80 -p 443:443 --name="snipeit" --network=mysql_default --mount source=snipe-vol,dst=/var/lib/snipeit --env-file=./snipe-it-env snipe/snipe-it
When going to the web portal of the docker container I get a message from the setup script saying it can't connect to the db. To update the settings in the .env file.
As far as I can tell, the environment variables were all correct.
Use busybox image to diagnose it first:
docker run --rm -it --network=mysql_default busybox
Then in console try to ping your mysql instance: ping mysql or whatever your container name with mysql is defined as a service.
You may check what services are actually running on given network with:
docker network inspect mysql_default
There should be section named "Containers" with proper name of container with mysql - providing it is running right now.
I have an application that extracts the client IP making the request. When I run the application directly on a server, it works and I could get the IP.
But when I run it within a docker containing by executing this command:
docker run --rm -d -p 4300:4300 image
All of a sudden the client IP being reported is now 172.17.0.1.
Googling around I see suggestion to pass in --net=host but doing this:
docker run --rm --net=host -p 4300:4300 image
now leads to the application not being reachable. For some strange reason it looks like the application is no longer available at the specified port.
This also does not work even when I drop the -p 4300:4300 as I got a message in the console that it is not needed when --net=host is used. That is:
docker run --rm --net=host -p image
Any suggestions on how to get this done? That is how to get the client IP from within a web service running within a docker container? I am running docker on a mac. Don't know if this has anything to do with the problem.
If you use the net=host configuration, you don't need the -p <host>:<container> setting, since this is only used to port forward when you use the bridge network configuration.
Just drop this flag and browse to whatever port your application listens on. It should work.
I have two containers, wds and apache. Both of them are running and have clear logs. I also checked if apache is running inside apache container and It is. My problem is that if I try to connect at localhost:80 which is the port that apache container listents to, I get only ERR_TIMED_OUT. Can you point me in which direction to look ? Containers were builded succesfully, no errors in logs, apache is running. I don't know where to look.
did you expose the port in Dockerfile and used -p 80:80 while using docker run command?
There is a specific logic to be followed while running or interacting with containers.
I do not know what commands or arguments you want to use so I will put an example here with basic explanation assuming you want to run a container with exposed port 80 in terminal interactive
docker run [container ID] -ti -p 80:80 /bin/bash
used commands:
-t tty - allocate a terminal so you can directly interact with the docker command
-i - interactive - connects STDIN to the allocated terminal. Any command you enter after this will go to the terminal.
-p - binds port
https://docs.docker.com/network/host/
https://docs.docker.com/engine/reference/run/
I have mysql server running in centos host and I want to deploy my war in tomcat inside docker container in same host. Any idea how can I connect mysql from inside the container.
Here is what you can do to connect to the DB (mysql) from the App (tomcat).
There are two ways you can do this
By using links
Manual way by providing the IP address of db to the tomcat app
Example-1 (Links)
Get your mysql container up:
"docker run --name db -e MYSQL_ROOT_PASSWORD="xxxx" -e
MYSQL_DATABASE="xxxx" -d mysql"
You now need to build and run the application container which must
know where is the db instance. You will have to figure out where you
provide the link to the db in your application. It can be in the code
or in a property file. You can use volume to mount this file into the
application container. You will also be able to modify them on the
fly because they will be accessible to you on the host system. This
is one example of doing this: docker
docker run -d -p 8080:8080 --name app --link db:db -v
$PWD/webapp:/var/lib/tomcat7/webapps/ app
Here we are using the image app where i am mounting webapp folder
from my docker base folder. This folder is having my code which i am
pushing to the tomcat's webapp folder. I am also pushing the
properties file to the appropiate location on the container where the
mysql connection is mentioned like this :
"jdbc.url=jdbc:mysql://db:3306/dbname" . We are mapping port 8080 of
the container to the port 8080 of the host. We are mapping container
db with container app. This creates an entry on /etc/hosts file - "db
ipname". So both containers can communicate with each other even if
we restart the db container (which changes the IP). Links will update
the host entry with the new IP.
Example 2:
Up the db container
Get the IP of the db container using this "docker inspect -f '{{ .NetworkSettings.IPAddress }}' container-name/ID"
Up the app containers with volumes where you can edit the jdbc url on your host.
Change the IP to db container's IP and this should be able to establish the connection
If you want to use Host's mysql
docker run -d --net=host --name app image command
Do let me know if you need any further explanation.
You can go through the topics on volumes, links from docker documentation: https://docs.docker.com/
I have a ubuntu machine which is a VM where I have installed docker in it. I am using this machine from my local windows machine and doing ssh , opening the terminal to the ubuntu machine.
Now , I am going to take a docker image which contains all the necessary softwares for eg: apache installed in it. Later I am going to deploy a sample appication(which is a web applicationP on to it and save it .
Now , I am in a confused mode as in how to check the deployed application if its running properly. i.e., what would be the address of the container which containds the deployed application.
for eg:- If I type http://127.x.x.x which is the address of the ubuntu machine , I am just getting time out .
Can anyone tell me how to verify the deployed application . Also, the printing the output of the program on the console works seemlessly fine , as the output gets printed , only thing I have some doubts is regarding the web application.
There are some possibilities to check whether your app is running.
Remote API
As JimiDini said, one possibility is the Docker remote API. You can use it to see all running containers (which would be your use case, right?), inspect a certain container or start and stop containers. The API is a REST-API with several binding for programming languages (at https://docs.docker.io/reference/api/remote_api_client_libraries/). Some of them are very outdated. To use the Docker remote API from another machine, I needed to open it explicitly:
docker -H tcp://0.0.0.0:4243 -H unix:///var/run/docker.sock -d &
Note that the API is open to the world now! In a real scenario you would need to secure it in some way (e.g. see the example at http://java.dzone.com/articles/securing-docker%E2%80%99s-remote-api).
Docker PS
To see all running containers run docker ps on your host. This will list all running containers. If you do not see your app, it is not running. It also shows you the ports your app is exposing. You can also do this via the remote API.
Logs
You can also check the logs. You can run docker attach <container id> to attach to a certain container an see its stdout. You can run also run docker logs <container id> to receive the Docker logs. What I prefer is to write the logs to a certain directory, e.g. all logs to /var/log and mount this folder to my host machine. Then all your logs will end up in /home/ubuntu/docker-logs on your host.
docker run -p 80:8080 -v /home/ubuntu/docker-logs:/var/log:rw my/application
One word to ports and IP
Every container will get its own IP address. You can check this IP address via the remote API or via Docker on the host machine directly. You can also specify a certain host name for the container (by passing the --hostname="test42" to the run command). However, you mostly did not need that.
To access the application in the container, you need to open the port in the container and bind to a port on the host.
In your Dockerfile you need to EXPOSE the port your app runs on:
FROM ubuntu
...
EXPOSE 8080
CMD run-my-app.sh
When you start your container, you need to bind this port to a port of the host:
docker run -p 80:8080 my/application
Now you can access your app on http://localhost:80 or http://127.0.0.1:80.
If you app does not response, check if the container is running by typing docker ps or the remote API. If it is not running, check the logs for the reason.
(Note: If you run your Ubuntu VM in something like VirtualBox and you try to access it from your Windows machine, make sure you opened the ports in VirtualBox too!).
Docker container has a separate IP address. By default it is private (accessible only from the host-machine).
Docker provides all metadata (including IP address) via its API:
https://docs.docker.io/reference/api/docker_remote_api_v1.10/#inspect-a-container
https://docs.docker.io/reference/api/docker_remote_api_v1.10/#monitor-docker-s-events
You can also take a look at a little tool called docker-gen for inspiration. It monitors docker-events and created configuration-files on host machine using templates.
To obtain the ip address of a docker container, if you know its id (a long hex string) or if you named it:
docker inspect --format '{{ .NetworkSettings.IPAddress }}' <container-id-or-name>
Docker is running its own network and to get information about it you can run the following commands:
docker network ls
docker network inspect <network name>
docker inspect <container id>
In the output, you should be able to find the IP.
But there is also a couple of things you need to be aware of, regarding Dockerfile and docker run command:
when you EXPOSE a port in Dockerfile, the service in the container is not accessible from outside Docker, but from inside other Docker containers
and when you EXPOSE and use docker run -p ... flag, the service in the container is accessible from anywhere, even outside Docker
So for example, if your apache is running on port 8080 you should expose it in Dockerfile and then you can run it as:
docker run -d -p 8080:8080 <image name> and you should be able to access it from your host on HTTP://localhost:8080.
It is an old question/answer but it might help somebody else ;)
working as of 2020
docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' container_name_or_id