Code
# Creation of a network
-app docker network create todo
Mar 4, 2023
Once we have worked with the single container apps, being a data scientist, we need to deal with various processes, at the same time to interact with our data. For example we can have redis to cache the process, we can have MySql, MongoDb and much more. So where to run these other processes.
In general the philosophy of docker is that each container should do one thing and do it well. So the few reasons because of which we will run any different process in a seperate container are:
So with these reasons, it’s better to go with seperate containers for different processes for the application.
Here the volume named as todo-mysql-data is mounted to /var/lib/mysql, and here it runs without creating a new volume via docker volume create command.
secret
. In the MySQL shell, list the databases and verify you see the todos databse. +--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
| todos |
+--------------------+
5 rows in set (0.00 sec)
With all this setup we have a todos database ready for use on the network we created earlier.
dig mysql
; <<>> DiG 9.18.8 <<>> mysql
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 32162
;; flags: qr rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 0
;; QUESTION SECTION:
;mysql. IN A
;; ANSWER SECTION:
mysql. 600 IN A 172.23.0.2
;; Query time: 0 msec
;; SERVER: 127.0.0.11#53(127.0.0.11)
;; WHEN: Tue Oct 01 23:47:24 UTC 2019
;; MSG SIZE rcvd: 44
--network-alias
used earlier with the creation of SQL container.
mysql
and it’ll talk to the database.Now once we know the ip address of MySQL container, we will simply connect our todo app’s container to it.
The todo app supports a few MySQL env variables setup.
MYSQL_HOST
- the hostname for the running MySQL serverMYSQL_USER
- the username to use for the connectionMYSQL_PASSWORD
- the password to use for the connectionMYSQL_DB
- the database to use once connectedWhile using env vars to set connection settings is generally accepted for development, it’s highly discouraged when running applications in production. Diogo Monica, a former lead of security at Docker, wrote a fantastic blog post explaining why.
We can now start the dev-ready container.
getting-started/app directory
.Open the app in your browser and add a few items to your todo list.
Connect to the mysql database and prove that the items are being written to the database. Remember, the password is secret.
+--------------------------------------+--------------------+-----------+
| id | name | completed |
+--------------------------------------+--------------------+-----------+
| c906ff08-60e6-44e6-8f49-ed56a0853e85 | Do amazing things! | 0 |
| 2912a79e-8486-4bc3-a4c5-460793a575ab | Be awesome! | 0 |
+--------------------------------------+--------------------+-----------+
/getting-started/app
folder, create a file named docker-compose.yml
docker-compose.yml
file we add a new service and name it mysql
so it automatically gets the network alias. We’ll go ahead and specify the image to use as well.docker run
, the named volume was created automatically. However, that doesn’t happen when running with Compose. We need to define the volume in the top-level volumes: section and then specify the mountpoint in the service config. By simply providing only the volume name, the default options are used. there are many options available though.docker-compose.yml
should look like this:services:
app:
image: node:18-alpine
command: sh -c "yarn install && yarn run dev"
ports:
- 3000:3000
working_dir: /app
volumes:
- ./:/app
environment:
MYSQL_HOST: mysql
MYSQL_USER: root
MYSQL_PASSWORD: secret
MYSQL_DB: todos
mysql:
image: mysql:8.0
volumes:
- todo-mysql-data:/var/lib/mysql
environment:
MYSQL_ROOT_PASSWORD: secret
MYSQL_DATABASE: todos
volumes:
todo-mysql-data:
docker-compose.yml
file, we can start it up!
docker ps
and docker rm -f <container-ids>
)docker compose logs -f
command. You’ll see the logs from each of the services interleaved into a single stream. This is incredibly useful when you want to watch for timing-related issues. The -f flag “follows” the log, so will give you live output as it’s generated.If you have run the command already, you’ll see output that looks like this:
mysql_1 | 2019-10-03T03:07:16.083639Z 0 [Note] mysqld: ready for connections.
mysql_1 | Version: '8.0.31' socket: '/var/run/mysqld/mysqld.sock' port: 3306 MySQL Community Server (GPL)
app_1 | Connected to mysql db at host mysql
app_1 | Listening on port 3000
docker compose down
or hit the trash can on the Docker Dashboard for the entire app. The containers will stop and the network will be removed.In this section, you learned about Docker Compose and how it helps you dramatically simplify the defining and sharing of multi-service applications. You created a Compose file by translating the commands you were using into the appropriate compose format. With this we can end the tutorial, but yup don’t forget to keep up with the dockers and docking from the official guides.