<blockquote>
  <p>What would be the best way to share ports among the containers? </p>
</blockquote>

<p>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 <strong>network alias</strong> (essentially a hostname) that you can use to hit the service. </p>

<pre><code>$ 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
</code></pre>

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

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

<pre><code>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
</code></pre>

<p>The other services would follow suit.</p>

<p>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.</p>

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

CC BY-SA 4.0
2020-02-12T15:29:32.703Z
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?
Cowboy_Patrick
CC BY-SA 4.0
2020-02-18T00:38:58.933Z
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
CC BY-SA 4.0
2020-02-12T12:47:56.393Z
2020-02-12T16:48:32.093Z
2020-02-12T16:48:32.093Z
Matthew
<p>I’m new to docker and I want to dockerize a aleardy existing application.</p>

<p>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).</p>

<p>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.</p>

<p>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.</p>

<p>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?</p>

<docker><docker-compose><node.js><orchestration><mysql>
Let docker containers work with each other
Cowboy_Patrick
# Let docker containers work with each other

(Asked by: username_0 on 2020-02-12)

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?

**Tags:** docker, docker-compose, node.js, orchestration, mysql


---

## 1 answers

### Answer by username_1 on 2020-02-12. Score: 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.

> **Comments:**
> **username_0**: 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?  
> **username_2**: 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.  

---

