Skip to content

Instantly share code, notes, and snippets.

@zaenk
Last active April 26, 2017 07:10
Show Gist options
  • Save zaenk/9c5adebd7db7df4f5079b3bd5a3d93ce to your computer and use it in GitHub Desktop.
Save zaenk/9c5adebd7db7df4f5079b3bd5a3d93ce to your computer and use it in GitHub Desktop.
Set up dev environment with docker-compose

Set up dev environment with docker-compose

Python container

It is mostly (like a 100%) stolen from here

FROM ubuntu:16.04

RUN apt-get update -y
RUN apt-get install -y python-pip python-dev build-essential

COPY src/* /app/
WORKDIR /app

RUN pip install -r requirements.txt

ENTRYPOINT ["python"]
CMD ["app.py"]

Oh, and make sure that your (in my case flask) app will listen on all ifaces:

# your beautiful code here
if __name__ == "__main__":
    app.run(debug=True,host='0.0.0.0')

MySQL custom init script

For dev envs, when the environment variables of the mysql base image are not enough:

FROM mysql:5.7

RUN mkdir -p /docker-entrypoint-initdb.d
COPY initdb/01_create_some_crud_user.sql /docker-entrypoint-initdb.d/01_create_some_crud_user.sql
COPY initdb/02_insert_some_data.sql /docker-entrypoint-initdb.d/02_insert_some_data.sql

Shell scripts, SQL files, archived (tar) SQL files in /docker-entrypoint-initdb.d will be executed at build time by the base mysql image (which is strangly not documented enywhere but extremely useful). I could have used a volume for that, but, you know... this is a Windows machine.

Anyway, after that you could compose it like:

version: '3'
services:
  mysql:
    build:
      context: .
      dockerfile: Dockerfile
    ports:
      - "3306:3306"
    environment:
      MYSQL_DATABASE: my_db
      MYSQL_USER: superuser
      MYSQL_PASSWORD: sa
      MYSQL_RANDOM_ROOT_PASSWORD: true

Some note: the stuff in compose file environment section will create a superuser for the DB. This is done by the mysql base image. The scripts from the Dockerfile will create a user with only INSERT,SELECT,UPDATE,DELETE privileges, just like in a production environment.

Watch out! Keep in mind that docker-compose will only rebuild your images if you are using an image and you changed the tag. Otherwise when you change either the docker-compose.yml or the referenced Dockerfile then you have to remove the old images either with docker image rm or docker-compose rm -vf.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment