Getting Up and Running - Docker

By the end of this tutorial, your device will be serving a Symfony project using Nginx. Along the way, you’ll be:

  • Installing and setting up editor to manage your code with
  • Setting up Docker to run your project
  • Downloading the code you will use to follow the tutorial
  • Running the project

IMPORTANT NOTE

This tutorial assumes you are running Ubuntu 16.04 as your operating system, the package names and file locations will be different if you are running in a different environment.

Editing our Code

You will want to have a powerful way to manage code because the default environment (Ubuntu) lacks one. For these tutorials, I have chosen Atom because it’s lightweight and incredibly extendable thanks to the plugin interface. More information about Atom can be found on the official site

There is a Personal Package Archive (repositories used to install programs that cannot be found through official channels) available which allows you to install and manage Atom like any other Linux package so, you'll be adding that first.

  1. sudo add-apt-repository ppa:webupd8team/atom
  2. sudo apt-get update
  3. sudo apt-get install atom

Once you've installed Atom, we're going to install an extension to improve usability of the editor. Make sure you've got Atom Editor open, click edit -> preferences and then when the options tab opens choose the install option. Using the search option find and install php-twig. Twig is the templating language that Symfony by default uses and this add-on adds further syntax support. The Twig extension is a recommendation but, in installing it I more wanted to highlight the process of installing extensions. Atom Editor has an excellent array of extensions to choose from, here is a quick list as food for thought that the author recommends investigating

  • docker - Control Docker straight from Atom
  • docblockr - A helper package for commenting your code
  • pigments - Detects colour values within your code and previews them
  • color-picker - With an ctrl-click over colour values in your code, change the colour with ease
  • minimap - A navigation aid for your code which allows you to have a more visual oriented scrollbar
  • minimap-git-diff - When using version control, highlight the changes in your minimap

The Application Stack

We'll start by installing the packages that will run the web environment. Docker is a container technology which allows you (as a system administrator) to quickly setup entire isolated environments in which to run applications. For this tutorial, I have provided a complete environment for you which is similar to that found in the Getting up and running - Native version of this tutorial.

In order to setup Docker, use the following commands from a terminal:

  1. # Install some prerequisite packages
  2. sudo apt-get update
  3. sudo apt-get install apt-transport-https ca-certificates curl software-properties-common
  4. # Add the Docker software repository to your system
  5. curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
  6. sudo add-apt-repository \
  7.   "deb [arch=amd64] https://download.docker.com/linux/ubuntu \
  8.   $(lsb_release -cs) \
  9.   stable"
  10. # Run the install
  11. sudo apt-get update
  12. sudo apt-get install docker-ce
  13. # Install Docker Compose
  14. sudo curl -L "https://github.com/docker/compose/releases/download/1.11.2/docker-compose-$(uname -s)-$(uname -m)" \
  15.    -o /usr/local/bin/docker-compose
  16. # Make Docker Compose executable
  17. sudo chmod +x /usr/local/bin/docker-compose

In order to allow your user to run Docker without admin rights, there is the need to add them to the docker group. Don't forget to logout and back in after the next command in order to refresh your user settings

  1. sudo usermod -aG docker YOUR_USER_NAME

Before proceeding, make sure that both the tools installed properly. The following commands should print out version numbers for each

  1. docker -v
  2. docker-compose -v

If you are having any problems with the above commands, I highly recommend reading the official documentation as it will be much more thorough than in this tutorial.
Installing Docker on Ubuntu
Docker Compose documentation

Grabbing the code

As mentioned earlier, I have setup a GIT repository with all the code needed throughout this tutorial. In order to access it, you will need GIT installed first

  1. sudo apt-get install git

With GIT now active, it is only a matter of cloning the repository to your device.

  1. git clone https://github.com/symball/symfony_tutorial.git

There will now be a folder called symfony_tutorial in the directory from which you cloned the repository.

Running the project

Because we will be both running the environment and wanting to develop it locally, there are some slight differences to the way setting up Docker to run compared to a production system. Don't worry about it though, I have you covered!

For starting the project the first time, I created a bootstrap script for you, all you have to do is

  1. # go to the project directory
  2. cd symfony_tutorial
  3. # running the startup script
  4. sh initial.sh

This may take a while as you are:

  • Creating an environment file which gives Docker details of your user
  • Downloading micro-operating systems from the internet (even though I've tried to make them small using Alpine Linux)
  • Running some build actions on the aforementioned operating systems to create users which are in line with your own for permission purposes
  • Running the initial Symfony installation procedures

It is important to note that when wishing to rerun the project, you DO NOT need to run this bootstrap script again, instead run

  1. docker-compose up

You should now be able to access the Symfony project in your browser by visiting 127.0.0.1:8086/app_dev.php. Pay attention to the port number, this is important because port 80 is forwarded from Docker to here. Throughout this tutorial, I may make reference to links on your own web server, DO NOT FORGET to substitute the port number in the URL.

Communication, our greatest tool

Whether you want to connect, have a problem I can help with or simply want to drop a line, I welcome your words!