Install composer and php, mysql
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
docker run --rm -u $UID -v `pwd`:/app composer/composer install $ docker run \ -d -v `pwd`:/var/www --name testphp \ php:5.6-fpm php -S 0.0.0.0:80 -t /var/www/html docker run \ -d -v `pwd`:/var/www --name testphp \ php:5.6-fpm php -S 0.0.0.0:80 -t /var/www/html // Mysql $ docker run \ -d --name mysqlserver \ -v /var/lib/mysql \ -e MYSQL_DATABASE=dockerfordevs -e MYSQL_ROOT_PASSWORD=docker \ mysql $ docker run \ -ti --link mysqlserver:mysqlserver --rm \ mysql mysql -h mysqlserver -u root -p // PHP $ docker rm -f testphp $ docker run \ -d --link mysqlserver:mysqlserver -v `pwd`:/var/www \ --name testphp \ php:5.6-fpm php -S 0.0.0.0:80 -t /var/www/html //NGINX docker run \ -d --name nginx --link phpserver:phpserver \ -v `pwd`:/var/www -v `pwd`/docker/nginx/nginx.conf:/etc/nginx/nginx.conf:ro \ nginx //TEST $ docker inspect -f "{{.NetworkSettings.IPAddress}}" nginx |
Docker Compose
Docker Compose is based on an older orchastration tool called ‘Fig2.’ The idea of Fig was to make it easy to define full-stack applications in a single configuration file yet still help maintain the “one process per application” design of Docker. Docker Compose extends this idea into an official tool that will allow you to define how applications are structured into Docker containers while bundling everything up into an easy-to-deploy configuration.
Compose will read a file and build the necessary docker run
commands that we need, in the proper order, to deploy our application. Since you should understand how all of this works from a manual process, Compose will just make the work you have done thus far much, much easier.
Compose starts with a docker-compose.yml
file which contains all of the information on each container inside of an application, and how it should be configured.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
phpserver: build: ./docker/php volumes: - /home/ctankersley/Projects/dockerfordevs:/var/www/ links: - mysqlserver mysqlserver: image: mysql environment: MYSQL_DATABASE: dockerfordevs MYSQL_ROOT_PASSWORD: docker volumes: - /var/lib/mysql nginx: build: ./docker/nginx ports: - "80:80" - "443:443" links: - phpserver |
To boot up a Compose configuration, go into the directory containing the docker-compose.yml
file, which in our case is in the root directory of our application. We can boot the entire configuration using docker-compose -d up
, and we should see something similiar to the following:
1 2 3 4 5 6 7 8 9 10 11 |
$ docker-compose up -d Creating dockerfordevs_mysqlserver_1... Creating dockerfordevs_phpserver_1... Creating dockerfordevs_nginx_1... $ docker-compose ps Name Command State Ports ----------------------------------------------------------------------------- dockerfordevs_mysqlserver_1 /entrypoint.sh mysqld Up 3306/tcp dockerfordevs_nginx_1 nginx -g daemon off; Up 443/tcp, 80/tcp dockerfordevs_phpserver_1 php-fpm Up 9000/tcp |
7 Command Cheatsheets
7.1 Images
docker images
– List out all of the images on the host machinedocker rmi [image]
– Removes a Docker image if no containers are using itdocker pull [image][:tag]
– Downloads a tag from a registry
7.2 Containers
docker ps [-a]
– Lists containers currently on the systemdocker run [options] IMAGE [command] [arguments]
– Creates a container and starts itdocker start [name]
– Starts a running containerdocker attach [name]
– Re-connects to a container that is running in the backgrounddocker stop [name]
– Stops a running containerdocker rm [-f] [name]
– Removes a containerdocker create [options] IMAGE [command] [arguments]
– Creates a container
7.3 docker-machine
docker-machine create --driver [driver] [machinename]
– Creates a new Docker Machinedocker-machine ls
– Lists all of the available machinesdocker-machine env [machinename]
– Returns the information about a machinedocker-machine start [machinename]
– Starts a stopped machinedocker-machine stop [machinename
– Stops a running machinedocker-machine restart [machinename]
– Restarts a machinedocker-machine ip [machinename]
– Returns the IP of a machinedocker-machine rm [machinename]
– Destroys a machinedocker-machine ssh [machinename]
– SSH’s into a machine
7.4 docker-compose
docker-compose up -d
– Builds a projectdocker-compose ps
– Shows container information for projectdocker-compose rm
– Destroys a projectdocker-compose stop
– Stops a projectdocker-compose scale [container]=[size]
– Adds or removes containers for scaling