Tags | docker-compose postgres skill/dev_env |
Hard Prerequisites | |
IMPORTANT: Please review these prerequisites, they include important information that will help you with this content. | |
|
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. It illustrates how docker revolutionised our industry. Here you go
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:
https://docs.docker.com/install/linux/docker-ce/ubuntu/
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"
For the above linux based systems, You will also need to follow these instructions:
https://docs.docker.com/engine/install/linux-postinstall/
https://docs.docker.com/compose/install/
https://docs.docker.com/docker-for-mac/install/
You wont need to install docker-compose as this is already included.
https://docs.docker.com/desktop/windows/install/
An extension for VSCode can be installed as well https://marketplace.visualstudio.com/items?itemName=ms-azuretools.vscode-docker
You wont need to install docker-compose as this is already included.
If you are having issues with Docker Desktop, please see this topic for troubleshooting TOPIC: Troubleshooting Docker Desktop for Windows
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
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.
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 to use them.
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 volumes. A volume is like a Linux “link” or windows “shortcut”. It maps a 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 behaviour 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:
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 moving your database to a new computer. Can you figure out how?
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:
Prerequisites: It would be really useful if you were comfortable with Bash.