Search code examples
dockerlocalhostdevelopment-environment

Setting local development with Docker and dockerizing repositories?


I have quite recently started learning Docker and what I'd like to do is to dockerize all my existing projects and all new ones. So basically set my local dev environment on Docker, but keep each project/repository isolated if that makes sence, as one php-app might be on php5 and another one on php7 etc.

What I usually did before was to place all my projects/repositories under home/Repositories folder, so I want to follow the same pattern, although each project folder will run on each own environment.

I have already installed Docker on my OS (I'm on Ubuntu Linux completely fresh installation, so no PHP or anything else is installed), however I'd like to ask a few questions as I don't have any previous experience with Docker

As far as I understand each project/repository should contain a docker-compose.yml file on the root directory and a docker folder where I put all the Dockerfiles, is that right?

- home
-- Repositories
--- a-laravel-project
---- docker // folder that has all required containers, like PHP, Mysql, Nginx conf etc etc
---- docker-compose.yml
---- index.php

--- another-oop-php-project
---- docker // folder that has all required containers, like PHP, Mysql, etc etc
---- docker-compose.yml
---- index.php

Do I also need to have install natively Git? I guess in order to dockerize all my existing repositories I need to clone them first so in this case git is (pre)required, correct?

Thanks in advance, any feedback would be appreciated.


Solution

  • You can keep your repositories directory for sure. You don't need any special layout for Docker at all.

    Lets say that in your pre-docker view of the world your environment was:

    • repositories
      • project1
        • index.php
      • project2
        • index.php

    You then add a Dockerfile (and if you want docker-compose.yml) to each project:

    • repositories
      • project1
        • index.php
        • Dockerfile
      • project2
        • index.php
        • Dockerfile

    You mention a "folder that has all required containers". That's not quite right. All your docker containers will be in Docker's own directory structure (that you do not want to mess with manually). What you do need to be concerned with is how to build your dependencies into your docker container for each project.

    Lets say your project1 will use just php, so you could make use of the official php docker container (https://hub.docker.com/_/php/).

    Then your Dockerfile could be something like

    FROM php:7.0-cli
    COPY index.php /usr/src/myapp
    WORKDIR /usr/src/myapp
    CMD [ "php", "./index.php" ]
    

    Or if you wanted to build your own from ubuntu or something. You start with the base ubuntu image and then install the dependencies you need on that ubuntu.

    FROM ubuntu
    RUN apt-get install php # Or whatever is appropriate, I don't use php
    WORKDIR /usr/src/myapp
    CMD [ "php", "./index.php" ]
    

    Then when you're in project1 directory and want to build your docker container. Then run it.

    docker build -t project1:latest .
    docker run project1:latest
    

    Note on source files:

    It's pretty common to want to keep your source files outside of your docker container, but run your test server inside the docker container. In that case you'll want to do something like:

    docker run \
      -v /home/repositories/project1:/project1 \
      php:7.0-cli \
      php /project1/index.php
    

    That bind mounts your source code directory from your machine into a container in which you run the php server. Also don't forget if you're running a development server inside the docker container you'll need to bind ports so you can connect to a port on your localhost and it be forwarded to the container.

    There is lots of good information you might be interested to read on https://hub.docker.com/_/php/ if you're using php.

    Git, I would still have installed on your host especially if mounting your source code into the container as I described