Docker Community Forums

Share and learn in the Docker community.

Running client code and server code on a single docker container

docker

(Anjjan Narayan) #1

I have a docker container that runs Ubuntu 14.04. It has Java installed in it. I have a client program called client.java and I have a server code server.java. I want the server code to run along with the client code. How can I do it?

When I run the container I only get a single shell prompt.


(David Maze) #2

You should have an image, right? First step, if you haven’t yet, is to build a Dockerfile that starts FROM ubuntu:14.04 (or another image that has a JVM preinstalled) and installs your software in it.

Once you’ve done that, you can run two copies of the image, which would look something like

docker run -d --name server myimage java com.example.Server -port 5555
docker run -it --link server:server myimage java com.example.Client -connect server:5555

The first line runs, in the background (-d), the server; everything from java onwards is the command line to run in that container. The second runs a second copy of the same image, telling it (--link) to make the server available as the host name “server”.

You can use tools like Docker Compose to automate this a little more. It would make a little more sense for a case where you wanted to start the server, run the client once, then immediately stop the server; or a case where you had two services where one was a client of the other, but you needed to keep both running.