Docker #2 – Docker compose

Our next goal is to run new docker container using docker composer. I think it is the simplest way to define what we need and how container should look like.

After run of docker we need to check ip adress of docker machine:

Now when we know that we should add this ip and our host name to hosts file located in c:\windows\system32\drivers\etc\hosts

Our next step is to create project folder and folders for our app and database:
konra@DESKTOP-JHE54VH MINGW64 ~/dev/docker
$ mkdir local_project local_db

After that we need to create new docker-compose.yml file in our project folder. It should like for example like this:

Detail description of used functions you can find in documentation. Shortly I can say that we create two services, one responsible for php application and another responsible for database. In line 2 we define used Ubuntu image that contains PHP and Apache. Next we specify local and remote folders where our app will be stored. Additionally we can set domain in line 8, ports for local machine and docker machine and link to MySQL which will be used later. We can configure also value which caused restart server if failures happened.

Similar options are available for mysql service. We define image and local and remote folders for database. We can configure user, password and database name and other options.

Our machine we can run using following command:
docker-compose up -d

Now when we create index.php file in ./local_project/ folder it should be available from our browser by using address example.local.

And before ending this article I will give you some useful commands that for sure will help you

docker ps -a # shows all of machines
docker stop $(docker ps -a -q) # stops all machines
docker rm $(docker ps -a -q) # removes all containers
docker images -a # shows all downloaded images
docker rmi $(docker images -a -q) # removes all downloaded images

And one more hint. Sometimes it happens that we meet on error such this:

In order to fix it, it’s necessary to create file .env which contains:

Leave a Reply

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