1

I can create two linked virtual serial ports on a Linux system with socat, and pretend one end is a serial device, and the other one is some code that uses the device. The socat command would look like:

socat -d -d pty,raw,echo=1 pty,raw,echo=0

And it creates two ports on the system, e.g. /dev/pts/3 and /dev/pts/4.

Now, I want to go one step further, and have two docker containers representing the system: one container pretending to be the serial device, and the other container being the code using that device. The goal being that when in production, I can remove the virtual device container and just use the real sensor.

The problem is that I can't seem to find a way to share the devpts between two containers, and somehow it feels like it is not something good to do.

Would there be a way to achieve the exact same behavior (virtual serial ports) by using named pipes instead of pty? Then I could share the file (say "/home/user/folder/my_pipe") between the containers (actually I would mount "/home/user/folder" on both, and both would then access my_pipe).

Or is there another, better way to do all that with docker?

  • 1
    can socat work across the network? – ctrl-alt-delor Dec 17 '18 at 14:03
  • Good question. Ideally, I want the "code container" to think that it is connected to a serial device. If I changed the code to use a network source instead of a serial one, then that would not be a simulation of the sensor anymore, would it? :-) – JonasVautherin Dec 17 '18 at 15:07
  • 1
    I am thinking that you create two virtual serial ports, connected via the network. – ctrl-alt-delor Dec 17 '18 at 15:34
  • Right, that sounds interesting. Still, the "code container" has to know that and create it's own virtual serial port. Ideally I would not want it to know about that. Maybe that's not possible, in which case I would just put everything in the same container, and have a CMD argument to enable simulation :-/ – JonasVautherin Dec 17 '18 at 15:39
1

(BTW, "virtual serial port" is a Windows term, on Unix these things are called "(pseudo) tty".)

What ctrl-alt-delor means is that you can just do one socat in the "serial device container", and one socat in the "code container" using the device; they communicate via a network connection, so one container has to know the IP address and port of the other container, and that's it (and you can choose which container connects to the other container). Also, the code running in the "code container" just uses the tty; so it doesn't depend on anything else except the path of the tty (which should be a parameter/commandline argument/etc., anyway).

Details of the socat call depend on some other details of what you want to do; maybe using tcp-listen and tcp is the simplest variant. Plenty of socat examples are e.g. here.

Edit

To explain the naming: A tty (teletype) is an abstraction over certain serial port parameters, together with a translation and interpretation of of characters (like end-of-line), and other things like the line discipline. A pseudo-tty is a tty without real hardware. A tty differs from a file in that it allows ioctls to set all these parameters.

So if your program has a feature to set, say, baud rate, you need a tty. If it doesn't, you could also use a named pipe.

However, it makes no difference if you use a named pipe or a tty with respect to sharing between containers: You'd still have to set up a common filesystem somewhere, where you can put either of them. And that's not necessarily how (docker) containers typically work.

OTOH, (docker) containers are usually ready for networking. So it might just be simpler to connect the containers via networking, instead of getting them to have a shared filesystems. And it has the additional advantage that the containers need not run on the same host (which docker containers don't assume). So it's a more natural fit.

You can do it any way you like, of course.

  • Interesting point about the naming. Does that mean that you cannot simulate a serial link other than through a (pseudo) tty? I guess part of the question was that: if I could make that through an arbitrary file, I could share that file between both containers. – JonasVautherin Dec 18 '18 at 9:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.