In the past docker posts we used docker run command to run containers using docker image and also the Dockerfile to create project image.
Sometimes we need to use/run a database in container during development, in such cases docker-compose came into light.
The Docker-Compose file
Docker-compose is a file at the root of your project, which has no extension and carry some special script. In our example we are configuring MySQL database.
version: '3.1'
services:
db:
image: mysql
command: --default-authentication-plugin=mysql_native_password
restart: always
environment:
MYSQL_ROOT_PASSWORD: example
MYSQL_USER: development
MYSQL_PASSWORD: development
MYSQL_DATABASE: blog
ports:
- 127.0.0.1:3306:3306
Up and Down
To start the docker-compose use the up command from the project root
docker-compose up -d
The down command used to remove and stop for stopping the container .
Checking the docker-compose containers
For listing the containers we can use
docker-compose ps
Accessing MySQL CLI
Login into the database and create some database ( as root)
docker exec -i some-id sh -c "exec -uroot -p password"
You can also use -u username for login as normal user and can execute basic sql operations such create database, tables and CURD operations on objects etc. Note that the database uses the default port of MySQL
We can stop running a container at any time using docker stop <id/name> and resume with docker start <id/name>
Following Docker Post may deserve a good read
- How to Containerize Golang app with Docker
- How to create a strapi project with Docker on Windows
- Docker-compose for projects
- Create a docker image of Node-Express API – Part II
- Create a docker image of Node-Express API – Part I
- Create portfolio app with nginx in 5 minute
- Start a nginx app container in Docker
- Howto download images in Docker