0

I’m new to docker and I want to dockerize a aleardy existing application.

It consists of a react application (I already created a container for that one successfully), a REST-api based on node-express (that one kinda works) and a mysql-database (not workin so far).

While developing I put a proxy into the package.json file of my React-Application. This enabled me to forward all the api-requests to a different port. And I want to also have this feature between my docker containers. So that my react application on port 3000 for example forwards all the unknown requests to the rest api on port 5000 via a proxy. Port 3000 and 5000 are in different containers.

Furhtermore I need the REST-container to have access to port 3306 of another mysql-container. This other mysql container still needs to be created though. It should use hypriot/rpi-mysql or something similar and within the dockerfile it should import a given .sql file into the database and then run in the background.

So my questions are: What would be the best way to share ports among the containers? How can I set up a Dockerfile for a MySql container with a .sql file as setup?

1 Answer 1

2

What would be the best way to share ports among the containers?

You don't really "share" ports between containers. Instead, you want to create network and attach each container to that network. Each container gets a network alias (essentially a hostname) that you can use to hit the service.

$ docker network create foo
$ docker run --network=foo --network-alias=mysql -p 3306:3306 my-mysql:latest
$ docker run --network=foo --network-alias=restapi -p 5000:5000 --link mysql my-restapi:latest
$ docker run --network=foo --network-alias=react -p 3000:3000 --link restapi my-reactapp:latest

So the myrestapi container can access the mysql container using the hostname mysql and port 3306 and your react app can hit the myrestapi container using the hostname restapi. The containers will also be accessible from your localhost so you can hit your react app with a browser without additional setup

In docker-compose.yml (which I don't use a ton so someone may have a better solution), it would look something like this:

networks: 
  foo:
services:
  mariadb:
    container_name: mysql # this works for your alias so long as you don't have another container called mysql
    image: mariadb:latest
    networks: ["foo"]
    ports:
    - 3306:3306

The other services would follow suit.

As for "priming" the database, that's a little trickier. If the DB has to be running, you would need to replace the docker container's entrypoint to start the server, run the mysql command to import the data, then your script would just spin forever.

Alternately, you could script your container start-up and after it starts, import the data that way.

2
  • Thank you, do you may also know how I would put these instructions in a docker-compose or dockerfile? Would help me a lot. And do you may also know how I can import a database in a bitnami/mariadb container? Commented Feb 12, 2020 at 15:29
  • Firstly I'd suggest you look into using something like amazon cloudfront and lambda for this kind of application. Secondly you can certainly do this with docker-compose, but the easiest way to test this out it to just write a shell script (bash or powershell) which runs each container up and then joins the to an existing 'overlay network'. Once on the overlay network each container can access any other container by it's container name. It sounds like you're going in the right direction but you're kind of asking for an implementation here, which nobody is going to do for you.
    – Richard
    Commented Feb 18, 2020 at 0:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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