In this article, we’ll explore the steps needed to add a SQL user in a Docker container. Working with databases inside Docker can significantly enhance your development workflow by encapsulating your database environment, making it reproducible and easy to manage. We will cover different SQL database types, namely MySQL, PostgreSQL, and Microsoft SQL Server (MSSQL), allowing you to choose based on your suite of tools.
In This Tutorial, You Will Learn:
- How to create a database in a Dockerized SQL environment.
- How to add a user to the specified SQL database.
- How to assign permissions for the created user.

Software Requirements and Linux Command Line Conventions
Category | Requirements, Conventions, or Software Version Used |
---|---|
System | Docker installed on your Linux machine. |
Software | MySQL, PostgreSQL, or MSSQL server images pulled from Docker Hub. |
Other | A basic understanding of SQL commands and Docker usage. |
Conventions | # – Requires commands to be executed with root privileges, either directly as root or using sudo .$ – Requires commands to be executed as a regular non-privileged user. |
How to Add a SQL User in a Docker Container
DOCKER AND SQL DATABASES
Did you know? Running SQL databases in Docker can improve development efficiency by allowing instant environment replication, but for production, tuning storage and networking is crucial for performance and reliability.
This section will provide step-by-step instructions to create a database, add users, and assign permissions in different SQL environments running in Docker containers.
Example-by-Example Instructions
- MySQL: Adding a user in a MySQL Docker container.
# Start a MySQL container docker run --name mysql-container -e MYSQL_ROOT_PASSWORD=yourpassword -d mysql # Access the MySQL shell docker exec -it mysql-container mysql -u root -p # Create a new database CREATE DATABASE mydatabase; # Create a new user CREATE USER 'myuser'@'%' IDENTIFIED BY 'mypassword'; # Grant permissions to the user GRANT ALL PRIVILEGES ON mydatabase.* TO 'myuser'@'%';
In this example, we start by running a MySQL Docker container, then we create a database named
mydatabase
. Following that, we create a usermyuser
with a password and grant the user all privileges on the previously created database. - PostgreSQL: Adding a user in a PostgreSQL Docker container.
# Start a PostgreSQL container docker run --name postgres-container -e POSTGRES_PASSWORD=yourpassword -d postgres # Access the PostgreSQL shell docker exec -it postgres-container psql -U postgres # Create a new database CREATE DATABASE mydatabase; # Create a new user CREATE USER myuser WITH PASSWORD 'mypassword'; # Grant permissions to the user GRANT ALL PRIVILEGES ON DATABASE mydatabase TO myuser;
For PostgreSQL, we follow a similar process as in MySQL. We start a PostgreSQL container, create a database, and then a user
myuser
with a password, followed by granting all privileges to that user on the designated database. - MSSQL: Adding a user in a Microsoft SQL Server Docker container.
# Step 1: Start an MSSQL container as root docker run -e 'ACCEPT_EULA=Y' -e 'SA_PASSWORD=Str0ngP@ssw0rd!' -p 1433:1433 \ --name mssql-container --user root -d mcr.microsoft.com/mssql/server:latest # Step 2: Enter the MSSQL container docker exec -it mssql-container bash # Step 3: Update package lists and install sqlcmd apt update && apt install -y sqlcmd # Step 4: Access the MSSQL shell using sqlcmd sqlcmd -S localhost -U SA -P 'Str0ngP@ssw0rd!' # Step 5: Create a new database CREATE DATABASE mydatabase; GO # Step 6: Create a new user with a strong password CREATE LOGIN myuser WITH PASSWORD = 'S3cur3P@ssword!'; GO USE mydatabase; GO CREATE USER myuser FOR LOGIN myuser; GO # Step 7: Grant db_owner permissions to the user ALTER ROLE db_owner ADD MEMBER myuser; GO
In this example, we start the MSSQL container as root, install `sqlcmd`, and then create a database, a user, and grant them
db_owner
role, giving full control over the database.
Conclusion
In this tutorial, we covered how to add a SQL user in a Docker container for different database types including MySQL, PostgreSQL, and MSSQL. These steps will help you manage users effectively within your Dockerized database environments. Keeping user permissions minimal enhances security while providing necessary access for your applications.
Frequently Asked Questions (FAQ)
-
Can I run multiple SQL database containers on the same machine?
Yes, you can run multiple SQL database containers as long as each container is mapped to a unique port on the host machine.
-
Is it safe to expose my database container to the internet?
Exposing your database container to the internet can pose security risks. It’s strongly recommended to use a firewall