Intro to Docker and Docker-compose

Tags docker-compose postgres
Hard Prerequisites
  • TOPICS: Environmental Variables
  • What is Docker

    Why is docker cool? Here’s the first part of a three part tutorial on microservices. You don’t need to read all three parts. Basically it illustrates how docker revolutionised our industry. Here you go

    Set up

    In this section we’ll get docker set up on your computer. Then we’ll use it to run a mysql server. This is cool because:

    • you can use mysql without having to actually install mysql
    • the same technique will work for running any other database (or many other applications) once docker is installed. This means you can play and experiement with different tools without much of a fuss

    Install docker

    For Ubuntu:

    1. https://docs.docker.com/install/linux/docker-ce/ubuntu/
    2. https://docs.docker.com/install/linux/linux-postinstall/

    For Mint:

    1. follow these instructions https://docs.docker.com/install/linux/docker-ce/ubuntu/ then when it is time to call add-apt-repository rather do this: sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(. /etc/os-release; echo "$UBUNTU_CODENAME") stable"
    2. https://docs.docker.com/install/linux/linux-postinstall/

    For Mac:

    https://docs.docker.com/docker-for-mac/install/

    Super important

    For some reason most people don’t follow ALL the installation instructions.

    Please do this:

    https://docs.docker.com/engine/install/linux-postinstall/

    Now install docker-compose

    https://docs.docker.com/compose/install/

    Create a docker-composition file

    Make a file called docker-compose.yaml. This is where you specify what containers you want to run and how you want them “constructed”. Paste the following into the file:

    
    version: "3.3"
    services:
      postgres:
        image: postgres:9.6
        environment:
          - POSTGRES_USER=user
          - POSTGRES_PASSWORD=pass
          - POSTGRES_DB=db
        volumes:
          - ./gitignore/postgresql:/var/lib/postgresql/data
        ports:
          - 5432:5432
    
      adminer:
        image: adminer
        restart: always
        ports:
          - 8080:8080
    
    

    Now open up a terminal and cd into the directory containing the docker compose file then say docker-compose up

    This launches two containers. One for postgresql, and one for adminer. Adminer is a simple web based gui that you can use to interact with different databases. You’ll be able to see this UI at http://localhost:8080

    Alternatively: Mysql composition

    You can run a mysql composition like this one

    
    version: "3.1"
    
    services:
      db:
        image: mysql
        command: --default-authentication-plugin=mysql_native_password
        restart: always
        environment:
          MYSQL_ROOT_PASSWORD: rootpass
          MYSQL_USER: user
          MYSQL_PASSWORD: pass
          MYSQL_DATABASE: db
      adminer:
        image: adminer
        restart: always
        ports:
          - 8080:8080
    
    

    What’s the difference? Postgresql is more of an industry standard than Mysql. But they are both great tools.

    Advanced topics

    If you want to use Docker containers in production then there are a bunch of extra things you need to know about. Some of these concepts are pretty deep but you don’t need to be an expert in order to use them.

    Volumes

    If you are running a container that needs to store data (like a database) and you want to make sure that you don’t lose that data if the container dies (or gets explicitly killed) then you need to use volumnes. Basically a volume is like a linux “link” or windows “shortcut”. It maps a totally normal directory/folder on your computer (your computer is the host) to a directory within the container. When the container tries to store something in the directory then that data appears in the host directory.

    Now if the container completely disappears the data still exists.

    One use case for this behavior is upgrading. Let’s say you are running mysql:8.0. Your compose file will initially contain something like this

     db:
        image: mysql:8.0
        volumes: /my/own/datadir:/var/lib/mysql
    

    To upgrade, you can simply do the following:

    1. kill the docker composition
    2. Edit the compose file to say something like this:
     db:
        image: mysql:8.0.17
        volumes: /my/own/datadir:/var/lib/mysql
    

    Now just docker-compose up and you have upgraded your mysql version. Easy peasy. And the same thing can be done for many other applications.

    Another use case of volumes is of course backing up your data or movving your database to a new computer. Can you figure out how?

    ports

    I’m not going to get into the definition of a port here. We’ll just talk about how to configure them. Remember how we accessed the adminer gui on port 8080 a second ago? Adminer exposes port 8080 by default.

    Try editing your docker compose file to contain this:

      adminer:
        image: adminer
        restart: always
        ports:
          - 9090:8080   ###### see how this line changed?
    

    Now restart your composition.

    Try out these links:

    Creating your own docker images

    Prerequisites: It would be really useful if you were comfortable with Bash.

    https://docs.docker.com/get-started/part2/


    RAW CONTENT URL