Docker introduces docker-toolbox for installing docker, docker-compose, docker-machine. In my understanding, docker-machine can be considered as a replacement and a upgraded version of boot2docker. It will allow you to create separated virtualboxes for different projects. Docker-compose is a script which consist of several docker containers. Each docker container can be built with an independent Dockerfile.
Docker file structure
As the followings, we need to create a file called docker-compose.yml first. It's a config file for all docker containers. Each container can be created by a Dockerfile which is stored in .docker folder. We usually put the code base in docroot directory.
./ .docker/ docroot/ docker-compose.yml
Create a machine. Requires the --driver flag to indicate which provider (VirtualBox, DigitalOcean, AWS, etc.) the machine should be created on, and an argument to indicate the name of the created machine. The following command can be used as a sample.
docker-machine create --driver virtualbox os
You can also check the docker-machine list by the command:
The result of the command above will be like the following. We can see the ip address of each docker-machine. The ACTIVE column shows only one docker-machine drupalchilli is active by the sign *.
NAME ACTIVE DRIVER STATE URL SWARM DOCKER ERRORS default - virtualbox Running tcp://192.168.99.100:2376 v17.03.0-ce drupalchilli * virtualbox Running tcp://192.168.99.101:2376 v17.03.0-ce os - virtualbox Running tcp://192.168.99.102:2376 v17.04.0-ce
To switch a docker-machine to be active, you can run the command below. All docker-machines are independent projects. If you are working on multiple projects, it is important to make sure you are using the right docker-machine env.
eval $(docker-machine env os)
Get started with docker-compose
Create a file called docker-compose.yml in your project directory and paste the following:
version: '2' services: web: build: .docker/web ports: - "80:80" volumes: - .:/docroot redis: image: "redis:alpine"
This Compose file defines two services,
redis. The web service:
- Uses an custom image that’s built from the
Dockerfilein the .docker/web directory.
- Forwards the exposed port 80 on the container to port 80 on the host machine.
- "volumes" mounts the project directory on the host to
/docrootinside the container, allowing you to modify the code without having to rebuild the image.
The redis service uses a public Redis image pulled from the Docker Hub registry.
Create a Dockerfile
You write a Dockerfile that builds a Docker image. The image contains all the dependencies the php application requires, including php itself.
This tells Docker to:
- Build an image starting with the php:7.0-apache image.
- Add the current directory
.into the path
/codein the image.
- Set the working directory to
- Install the Python dependencies.
- Run bash command a2enmod rewrite
FROM php:7.0-apache ADD . /code WORKDIR /code RUN a2enmod rewrite
Docker-compose setup custom database user account
The "environment" command setups custom database configuration. "MYSQL_DATABASE" setup database name; "MYSQL_USER" and "MYSQL_PASSWORD" create a new mysql user and its password. "MYSQL_ROOT_PASSWORD" setup the root user's password.
db: build: .docker/db ports: - 3306:3306 environment: MYSQL_ROOT_PASSWORD: rootpass MYSQL_USER: dbuser MYSQL_PASSWORD: userpass MYSQL_DATABASE: myimage_db
How to import existed MYSQL database
The official postgres docker image will run .sql scripts found in the /docker-entrypoint-initdb.d/ folder. So all you need is to create db Dockerfile like below:
FROM mysql:5.6 ADD drupalchilli.sql /docker-entrypoint-initdb.d
If you are working on Drupal project, Drupal settings.php file needs to be configed according to the custom database environment you just setup. Also the database host should be the docker-machine ip address.