Docker Compose and Docker (DDE Part 2)

Creating Docker Containers with the docker command

First, we are going to look at how to create a PHP/Apache web server and MySQL containers using the docker command. This is just a simple example of how to create Docker containers via command line. Later, we will use Docker Compose to simplify the creation of multiple containers.

PHP/Apache Container

Create a Dockerfile.

In a folder, create a file called Dockerfile that contains the following:

FROM php:7-0-apache
COPY src/ /var/www/html

Where the src/ directory is the directory on your computer that contains all of your PHP code.

Then, run the following commands to build and run the Docker image.

$ docker build -t my-php-app .
$ docker run -d --name my-app-name my-php-app

You should see something like the following when you run docker ps:

docker compose

Let’s stop the container:

$ docker stop my-app-name

MySQL Container

Now, let’s build a mysql 5.7 container and run it:

$ docker run --name some-mysql -e MYSQL_ROOT_PASSWORD=my-secret-pw -d mysql:5.7

some-mysql is the name that you want to assign to your container, my-secret-pw is the password to ser for the MySQL root user. This image exposes the standard MySQL port (3306), so container linking makes the MySQL instance available to other application containers.

$ docker run --name some-app --link some-mysql:mysql -d my-php-app

Running docker ps, you should see something like:

docker compose

You can stop these by running:

$ docker stop some-app
$ docker stop some-mysql

Using Docker Compose

In the above example, every time you want to run the containers you would have to run:

$ docker run --name some-mysql -e MYSQL_ROOT_PASSWORD=my-secret-pw -d mysql:5.7
$ docker run --name some-app --link some-mysql:mysql -d my-php-app

This may not be a problem with only a couple containers. But, as you add more containers, your list of commands grows. Luckily, there is Docker Compose.

Docker Compose is a tool for defining and running multi-container Docker applications. You use a docker-compose.yml file to configure your application’s services. Then, you can create and start all of your services using a single command.

Next time we will build our development environment using Docker Compose.

Leave a Reply

Your email address will not be published. Required fields are marked *