Use containers for development


Work through the steps to build an image and run it as a containerized application in Run your image as a container.


In this module, we’ll walk through setting up a local development environment for the application we built in the previous modules. We’ll use Docker to build our images and Docker Compose to make everything a whole lot easier.

Run a database in a container

First, we’ll take a look at running a database in a container and how we use volumes and networking to persist our data and allow our application to talk with the database. Then we’ll pull everything together into a Compose file which allows us to setup and run a local development environment with one command.

Instead of downloading MySQL, installing, configuring, and then running the MySQL database as a service, we can use the Docker Official Image for MySQL and run it in a container.

Before we run MySQL in a container, we’ll create a couple of volumes that Docker can manage to store our persistent data and configuration. Let’s use the managed volumes feature that Docker provides instead of using bind mounts. You can read all about Using volumes in our documentation.

Let’s create our volumes now. We’ll create one for the data and one for configuration of MySQL.

$ docker volume create mysql
$ docker volume create mysql_config

Now we’ll create a network that our application and database will use to talk to each other. The network is called a user-defined bridge network and gives us a nice DNS lookup service which we can use when creating our connection string.

$ docker network create mysqlnet

Now we can run MySQL in a container and attach to the volumes and network we created above. Docker pulls the image from Hub and runs it for you locally. In the following command, option -v is for starting the container with volumes. For more information, see Docker volumes.

$ docker run --rm -d -v mysql:/var/lib/mysql \
  -v mysql_config:/etc/mysql -p 3306:3306 \
  --network mysqlnet \
  --name mysqldb \
  -e MYSQL_ROOT_PASSWORD=p@ssw0rd1 \

Now, let’s make sure that our MySQL database is running and that we can connect to it. Connect to the running MySQL database inside the container using the following command and enter “p@ssw0rd1” when prompted for the password:

$ docker exec -ti mysqldb mysql -u root -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.23 MySQL Community Server - GPL

Copyright (c) 2000, 2021, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.


Connect the application to the database

In the above command, we logged in to the MySQL database by passing the ‘mysql’ command to the mysqldb container. Press CTRL-D to exit the MySQL interactive terminal.

Next, we’ll update the sample application we created in the Build images module. To see the directory structure of the Python app, see Python application directory structure.

Okay, now that we have a running MySQL, let’s update the to use MySQL as a datastore. Let’s also add some routes to our server. One for fetching records and one for creating our database and table.

import mysql.connector
import json
from flask import Flask

app = Flask(__name__)

def hello_world():
    return 'Hello, Docker!'

def get_widgets():
    mydb = mysql.connector.connect(
    cursor = mydb.cursor()

    cursor.execute("SELECT * FROM widgets")

    row_headers=[x[0] for x in cursor.description] #this will extract row headers

    results = cursor.fetchall()
    for result in results:


    return json.dumps(json_data)

def db_init():
    mydb = mysql.connector.connect(
    cursor = mydb.cursor()

    cursor.execute("DROP DATABASE IF EXISTS inventory")
    cursor.execute("CREATE DATABASE inventory")

    mydb = mysql.connector.connect(
    cursor = mydb.cursor()

    cursor.execute("DROP TABLE IF EXISTS widgets")
    cursor.execute("CREATE TABLE widgets (name VARCHAR(255), description VARCHAR(255))")

    return 'init database'

if __name__ == "__main__": ='')

We’ve added the MySQL module and updated the code to connect to the database server, created a database and table. We also created a route to fetch widgets. We now need to rebuild our image so it contains our changes.

First, let’s add the mysql-connector-python module to our application using pip.

$ pip3 install mysql-connector-python
$ pip3 freeze | grep mysql-connector-python >> requirements.txt

Now we can build our image.

$ docker build --tag python-docker-dev .

If you have any containers running from the previous sections using the name rest-server or port 8000, stop them now.

Now, let’s add the container to the database network and then run our container. This allows us to access the database by its container name.

$ docker run \
  --rm -d \
  --network mysqlnet \
  --name rest-server \
  -p 8000:5000 \

Let’s test that our application is connected to the database and is able to add a note.

$ curl http://localhost:8000/initdb
$ curl http://localhost:8000/widgets

You should receive the following JSON back from our service.


Use Compose to develop locally

In this section, we’ll create a Compose file to start our python-docker and the MySQL database using a single command.

Open the python-docker directory in your IDE or a text editor and create a new file named Copy and paste the following commands into the file.

version: '3.8'

   context: .
  - 8000:5000
  - ./:/app

  image: mysql
  - 3306:3306
  - mysql:/var/lib/mysql
  - mysql_config:/etc/mysql


This Compose file is super convenient as we do not have to type all the parameters to pass to the docker run command. We can declaratively do that using a Compose file.

We expose port 8000 so that we can reach the dev web server inside the container. We also map our local source code into the running container to make changes in our text editor and have those changes picked up in the container.

Another really cool feature of using a Compose file is that we have service resolution set up to use the service names. Therefore, we are now able to use “mysqldb” in our connection string. The reason we use “mysqldb” is because that is what we’ve named our MySQL service as in the Compose file.

Note that we did not specify a network for those 2 services. When we use docker-compose it automatically creates a network and connect the services to it. For more information see Networking in Compose

If you have any containers running from the previous sections, stop them now.

Now, to start our application and to confirm that it is running properly, run the following command:

$ docker compose -f up --build

We pass the --build flag so Docker will compile our image and then start the containers.

Now let’s test our API endpoint. Open a new terminal then make a GET request to the server using the curl commands:

$ curl http://localhost:8000/initdb
$ curl http://localhost:8000/widgets

You should receive the following response:


This is because our database is empty.

Next steps

In this module, we took a look at creating a general development image that we can use pretty much like our normal command line. We also set up our Compose file to map our source code into the running container.

In the next module, we’ll take a look at how to set up a CI/CD pipeline using GitHub Actions. See:

Configure CI/CD


Help us improve this topic by providing your feedback. Let us know what you think by creating an issue in the Docker Docs GitHub repository. Alternatively, create a PR to suggest updates.