DEV Community

trycatch
trycatch

Posted on • Updated on

5 docker commands to install PostgreSQL (Postgis), MongoDB, MySQL, and Redis

PostgreSQL

docker run --name "postgresql" -p 25432:5432 -e POSTGRES_USER=your_user -e POSTGRES_PASSWORD=your_password -e POSTGRES_DB=your_dbname -t -d postgres
Enter fullscreen mode Exit fullscreen mode

where (you can change them on your choice):

  • your_username is required PostgreSQL user
  • your_password is the user's password
  • your_dbname is db's name (notice that the missing db_name will be substituted by your_username)
  • 25432 is a PORT available outside docker's container
  • postgresql is a key you can use for accessing the container

Hint: using 25432 (or any other rarely used) port prevents conflicts

You also can install it directly to your machine using this guide:

PostgreSQL + Postgis

docker run --name "postgis" -e POSTGRES_USER=your_user -e POSTGRES_DBNAME=your_dbname -e POSTGRES_PASSWORD=your_password -p 25432:5432 -d -t kartoza/postgis
Enter fullscreen mode Exit fullscreen mode

Hint: -d flag runs the container as a daemon: it starts in the background and you can continue working with the same terminal tab

where (you can change them on your choice):

  • your_username is required PostgreSQL user
  • your_password is the user's password
  • your_dbname is database's name
  • 25432 is a PORT available outside docker's container
  • postgis is a key you can use for accessing the container

MongoDB

docker run --name "mongodb"  -p 28777:27017 -e MONGO_INITDB_ROOT_USERNAME=your_username -e MONGO_INITDB_ROOT_PASSWORD=your_password -d mongo
Enter fullscreen mode Exit fullscreen mode

where (you can change them on your choice):

  • your_username is required MongoDB admin user
  • your_password is the user's password
  • 28777 is a PORT available outside docker's container
  • mongodb is a key you can use for accessing the container

Hint: -t flag runs a pseudo terminal, therefore you can send different commands to the container

MySQL

docker run --name "mysql" -p 23306:3306 -e MYSQL_ROOT_PASSWORD=your_password -t -d mysql/mysql-server
Enter fullscreen mode Exit fullscreen mode

where (you can change them on your choice):

  • your_password is the root password
  • 23306 is a PORT available outside docker's container
  • mysql is a key you can use for accessing the container

Hint: you can add a volume for any container in this guide: it will store data on your machine nevertheless the container is removed or stoped, e.g. for mysql -v $HOME/docker/volumes/mysql:/var/lib/mysql, for postgres -v $HOME/docker/volumes/postgres:/var/lib/postgresql/data

Redis

docker run --name "redis" -p 26379:6379 -t -d redis
Enter fullscreen mode Exit fullscreen mode

where (you can change them on your choice):

  • 26379 is a PORT available outside docker's container
  • redis is a key you can use for accessing the container

Check out more DevOps stuff on my blog

Top comments (1)

Collapse
 
kevinsheeranxyj profile image
Kevin Sheeran

It seems like MongoDB can't pull from docker directly, it prompted with 'pull access'