docker-compose & Exposed Ports

Video not working?

It looks like your browser may not support the H264 codec. If you're using Linux, try a different browser or try installing the gstreamer0.10-ffmpeg gstreamer0.10-plugins-good packages.

Thanks! This saves us from needing to use Flash or encode videos in multiple formats. And that let's us get back to making more videos :). But as always, please feel free to message us.

We just started our MySQL docker container thanks to docker-compose. So... ah... now what? How can we talk to that database? Great question!

Start by running just:

docker-compose

This lists all the commands that you can use with docker-compose. Most of these we won't need to worry about. But one good one is ps, which stands for "process status". Try it:

docker-compose ps

This shows all the containers that docker-compose is running for this project... which is just one right now. Ah, and check this out! Port 3306 of the container is being shared to our local machine on port 32773. This is a random port number that will be different each time we restart the container.

Connecting to the MySQL Docker Container

This means that we can talk to the MySQL server in the container via port 32773! Let me show you. I actually do have mysql installed on my local machine, so I can say mysql -u root --password=password because, in our docker-compose.yaml file, that's what we set the root user password to. Then --host=127.0.0.1 - to talk to my local computer - and --port= set to this one right here: 32773. Try it!

mysql -u root --password=password --host=127.0.0.1 --port=32773

Boom! We are inside of the container talking to MySQL! By the way, if you don't have MySQL installed locally, you can also do this by running:

docker-compose exec database mysql -u root --password=password

That will "execute" the mysql command inside the container that's called database.

Anyways, now that we're here, we can do normal stuff like:

SHOW DATABASES

... or even create a new database called docker_coolness:

CREATE DATABASE docker_coolness

There it is! I'll type exit to get out.

Stopping and Destroying the Containers

When you're done with the containers and want to turn them off, you can do that with:

docker-compose stop

Or the more common:

docker-compose down

This loops through all of the services in docker-compose.yaml and, not only stops each container, but also removes its image. It's like completely deleting that "mini server" - including its data.

Thanks to that, the next time we run:

docker-compose up -d

It will create the whole container from scratch: any data from before will be gone.

Booting isn't Instant

Let's see the whole process from the start. First, run:

docker-compose down

to stop and destroy the container. If we try to connect to MySQL now it, of course, fails. Now run:

docker-compose up -d

To start the container. Let's check on the process:

docker-compose ps

Ah! Look at that port! It was 32773 the first time we ran it. Now the container is exposed on port 32775. Let's try connecting:

mysql -u root --password=password --host=127.0.0.1 --port=32775

And... oh! It didn't work!

Lost connection to MySQL server

Ah. The truth is that, even though it looks like docker-compose up is instant, in reality, it takes a few seconds for MySQL to truly start. Eventually if we try again...

mysql -u root --password=password --host=127.0.0.1 --port=32775

Yes! We are in! But you won't see the docker_coolness database that we created earlier because docker-compose down destroyed our data.

At this point, we've created a docker-compose.yaml file and used docker-compose to launch a MySQL container that we can talk to. Awesome!

To connect to this from our Symfony app, all we need to do is update the DATABASE_URL environment variable to use the right password and port.

But... we're not going to do that. It would work... but it turns out that our app is already aware of the correct DATABASE_URL value... even though we haven't configured anything. Let's talk about how next.

Leave a comment!

What PHP libraries does this tutorial use?

// composer.json
{
    "require": {
        "php": "^7.2.5",
        "ext-ctype": "*",
        "ext-iconv": "*",
        "composer/package-versions-deprecated": "^1.8", // 1.8.2
        "doctrine/doctrine-bundle": "^2.1", // 2.1.0
        "doctrine/doctrine-migrations-bundle": "^3.0", // 3.0.1
        "doctrine/orm": "^2.7", // v2.7.3
        "knplabs/knp-markdown-bundle": "^1.8", // 1.8.1
        "knplabs/knp-time-bundle": "^1.11", // v1.12.0
        "sensio/framework-extra-bundle": "^5.5", // v5.6.1
        "sentry/sentry-symfony": "^3.4", // 3.5.2
        "stof/doctrine-extensions-bundle": "^1.4", // v1.4.0
        "symfony/asset": "5.1.*", // v5.1.2
        "symfony/console": "5.1.*", // v5.1.2
        "symfony/dotenv": "5.1.*", // v5.1.2
        "symfony/flex": "^1.3.1", // v1.9.0
        "symfony/framework-bundle": "5.1.*", // v5.1.2
        "symfony/monolog-bundle": "^3.0", // v3.5.0
        "symfony/stopwatch": "5.1.*", // v5.1.2
        "symfony/twig-bundle": "5.1.*", // v5.1.2
        "symfony/webpack-encore-bundle": "^1.7", // v1.7.3
        "symfony/yaml": "5.1.*", // v5.1.2
        "twig/extra-bundle": "^2.12|^3.0", // v3.0.4
        "twig/twig": "^2.12|^3.0" // v3.0.4
    },
    "require-dev": {
        "doctrine/doctrine-fixtures-bundle": "^3.3", // 3.3.1
        "symfony/debug-bundle": "5.1.*", // v5.1.2
        "symfony/maker-bundle": "^1.15", // v1.20.0
        "symfony/var-dumper": "5.1.*", // v5.1.2
        "symfony/web-profiler-bundle": "5.1.*", // v5.1.2
        "zenstruck/foundry": "^1.1" // v1.1.0
    }
}