<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Brice Fotzo</title>
    <description>The latest articles on DEV Community by Brice Fotzo (@bricefotzo).</description>
    <link>https://dev.to/bricefotzo</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1156241%2Faa54f95b-ef4f-44b4-95d6-969f5e83e425.png</url>
      <title>DEV Community: Brice Fotzo</title>
      <link>https://dev.to/bricefotzo</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/bricefotzo"/>
    <language>en</language>
    <item>
      <title>Your Free and Offline SQL console in a few steps</title>
      <dc:creator>Brice Fotzo</dc:creator>
      <pubDate>Tue, 10 Oct 2023 07:40:25 +0000</pubDate>
      <link>https://dev.to/docker/your-free-and-offline-sql-console-in-a-few-steps-4iic</link>
      <guid>https://dev.to/docker/your-free-and-offline-sql-console-in-a-few-steps-4iic</guid>
      <description>&lt;p&gt;Continuing from the &lt;a href="https://dev.to/docker/sql-docker-the-combo-for-quick-and-safe-query-testing-3ib"&gt;previous article&lt;/a&gt;, on running SQL queries with &lt;strong&gt;Docker&lt;/strong&gt;, we’ll explore how to achieve the same goal with a more straightforward and efficient approach.&lt;/p&gt;

&lt;p&gt;One of the key benefits of this tutorial is the ability to establish your &lt;strong&gt;own SQL console&lt;/strong&gt; that’s not only free, but also flexible. You’re not limited to PostgreSQL(&lt;em&gt;that we’ll use in the following&lt;/em&gt;); you can opt for any RDBMS of your choice. Plus your independant from an internet connection. For this we’ll leverage Docker Compose.&lt;/p&gt;

&lt;p&gt;This tool allows you to manage multiple container configurations through a single YAML file, making your workflow more efficient. In this tutorial, we’ll focus on setting up two containers: one for the SQL server (&lt;code&gt;sql-server&lt;/code&gt;) and another for the SQL client (&lt;code&gt;sql-client&lt;/code&gt;).&lt;/p&gt;

&lt;h2&gt;
  
  
  Prerequisites
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Basic understanding of SQL, Docker, and &lt;a href="https://docs.docker.com/compose/"&gt;Docker Compose&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Docker and &lt;a href="https://docs.docker.com/compose/install/"&gt;Docker Compose&lt;/a&gt; installed on your system&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In this guide, I’ll walk you through two simple steps to execute your SQL script effortlessly.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--M7RYswBs--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nqeg820bgj8p7pvy5ugc.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--M7RYswBs--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nqeg820bgj8p7pvy5ugc.png" alt="2 steps to run SQL queries with docker" width="700" height="700"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 1: Specify Docker Compose Configurations
&lt;/h2&gt;

&lt;p&gt;Here’s a sample Docker Compose file to get you started:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;version: '3'
services:
  sql-server:
    image: postgres:13.12-bullseye
    container_name: sql_server
    environment:
      POSTGRES_PASSWORD=root
    networks:
      - sql_network
    ports:
      - "5432:5432"
    healthcheck:
      test: ["CMD", "pg_isready", "-U", "postgres"]
      interval: 5s
      timeout: 5s
      retries: 2
      start_period: 1s

  sql-client:
    image: postgres:13.12-bullseye
    container_name: sql_client
    environment:
      PGPASSWORD=root
    networks:
      - sql_network
    depends_on:
      sql-server:
        condition: service_healthy
    volumes:
      - ./local_scripts:/container_scripts
    command: ["/bin/bash", "-c", "psql -h sql-server -U postgres -f /container_scripts/query.sql"]

networks:
  sql_network:
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Decoding the Docker Compose File
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;1. Version&lt;/strong&gt;: The &lt;code&gt;version: '3'&lt;/code&gt; line indicates that we're using the third version of the Docker Compose file format.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. Services:&lt;/strong&gt; The &lt;code&gt;services:&lt;/code&gt; section is where you list all the containers you wish to run. In this example, we have two: &lt;code&gt;sql-server&lt;/code&gt; and &lt;code&gt;sql-client&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;SQL Server Service:&lt;/strong&gt; Here’s a breakdown of the &lt;code&gt;sql-server&lt;/code&gt; service configuration:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;image&lt;/code&gt;: Specifies the Docker image, in this case, &lt;a href="https://www.postgresql.org/"&gt;PostgreSQL&lt;/a&gt; version 13.12. I chose the specific tag &lt;strong&gt;13.12-bullseye&lt;/strong&gt; from the &lt;a href="https://hub.docker.com/_/postgres/tags?page=1&amp;amp;name=13"&gt;Docker Hub&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;container_name&lt;/code&gt;: Names the container as &lt;code&gt;sql_server&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;environment&lt;/code&gt;: Sets the PostgreSQL password through an environment variable.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;networks&lt;/code&gt;: Links the container to a custom network named &lt;code&gt;sql_network&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;ports&lt;/code&gt;: Maps the container's port 5432 to the host machine's corresponding port.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;healthcheck&lt;/code&gt;: Sets up a health check to ensure the service is operational.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;SQL Client Service:&lt;/strong&gt; This service has additional parameters:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;depends_on&lt;/code&gt;: Indicates a dependency on the &lt;code&gt;sql_server&lt;/code&gt; being healthy.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;volumes&lt;/code&gt;: Maps a local directory to a container directory. &lt;em&gt;Specify the local directory containing your .sql script there.&lt;/em&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;command&lt;/code&gt;: Specifies the command to run at startup, which in this case is our SQL script.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;3. Networks:&lt;/strong&gt; This section defines a custom network (&lt;code&gt;sql_network&lt;/code&gt;) that both services will use for communication.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 2: Edit and execute your SQL Queries
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;Ensure your SQL script is in the local directory specified in the volumes section of your Docker Compose file.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;To execute the script, run the following command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;docker-compose up sql-client
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This command only starts the &lt;code&gt;sql-client&lt;/code&gt; container but also initiates the &lt;code&gt;sql-server&lt;/code&gt; in the background due to the dependency. The advantage is that you'll only see the output logs for &lt;code&gt;sql-client&lt;/code&gt;, keeping your console clean.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--HI8Rzij9--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7rc2ykt8apz874kiiim4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--HI8Rzij9--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7rc2ykt8apz874kiiim4.png" alt="docker compose up sql-client outputs" width="585" height="209"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If we’ve used docker comose up without specifying, we would have the sql_server outputs too.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--lvXiTCAL--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/39b7nt7g33lx9lkkquug.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--lvXiTCAL--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/39b7nt7g33lx9lkkquug.png" alt="docker compose up output&amp;lt;br&amp;gt;
" width="700" height="211"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You can identify in the left part, the container for which the logs are.&lt;/p&gt;
&lt;h2&gt;
  
  
  Best Practices Digest
&lt;/h2&gt;
&lt;h3&gt;
  
  
  Create a Free and Offline SQL Console
&lt;/h3&gt;

&lt;p&gt;After following this tutorial, all you need to create your own console is your preferred IDE, a file to edit your queries, and this Docker Compose setup.&lt;/p&gt;

&lt;p&gt;Here’s how it works:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Your favorite IDE: Open your IDE to navigate to the folder where your Docker Compose file and SQL script are located.&lt;/li&gt;
&lt;li&gt;Edit your aueries: Open the SQL file in your IDE and make your changes or add new queries as needed.&lt;/li&gt;
&lt;li&gt;Run the command: Once you’ve edited the SQL file, simply run the following command to execute your queries:
&lt;/li&gt;
&lt;/ol&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;docker-compose up sql-client
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;This approach not only streamlines your SQL testing process but also makes it incredibly accessible. Each time you want to test a query, you just have to navigate to the folder in your IDE, edit the file, and run the command. It’s that simple!&lt;/p&gt;
&lt;h3&gt;
  
  
  Using .env Files
&lt;/h3&gt;

&lt;p&gt;It’s crucial to manage environment variables using &lt;code&gt;.env&lt;/code&gt; files for better security and maintainability. It prevent you from pushing sensitive information in your VCS(github, gitlab, etc…).&lt;/p&gt;

&lt;p&gt;You can replace the &lt;code&gt;environment&lt;/code&gt; section with &lt;code&gt;env_file&lt;/code&gt;, like so:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# Replace the section
environment:
      POSTGRES_PASSWORD=root
# By the section
env_file:
  - server.env
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;With &lt;code&gt;server.env&lt;/code&gt; containing :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# Variables for the database server
POSTGRES_PASSWORD=root
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  The Power of CTEs
&lt;/h3&gt;

&lt;p&gt;CTEs are invaluable for simulating table creation and executing complex queries, making your SQL scripts more flexible and readable.&lt;/p&gt;

&lt;p&gt;To illustrate the utility of CTEs, let’s look at a SQL sample where we use a CTE to create a temporary table for inline queries:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;WITH users AS (
  SELECT 'John Doe' as name, 'john.doe@example.com' as email
  UNION
  SELECT 'Jane Smith' as name, 'jane.smith@example.com' as email
  UNION
  SELECT 'Samuel Jackson' as name, 'samuel.jackson@example.com' as email)
-- Retrieve some records from the users table
SELECT *
FROM users
WHERE name LIKE 'J%';

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In summary, Docker Compose simplifies SQL testing, making it more accessible and efficient. Employing &lt;code&gt;.env&lt;/code&gt; files and CTEs(Common Table Expressions) in your SQL scripts are not just good practices; they're best practices that enhance security, maintainability, and flexibility.&lt;/p&gt;

&lt;p&gt;I hope you find this guide useful. Stay tuned for more articles on maximizing Docker for a variety of tasks. Feel free to share your thoughts or alternative methods in the comments below.&lt;/p&gt;

&lt;p&gt;Don’t forget to follow me for the latest updates and insights.&lt;/p&gt;

&lt;p&gt;Happy Querying!&lt;/p&gt;

&lt;p&gt;I would like to thank &lt;a href="https://www.linkedin.com/in/ajeetsraina"&gt;Ajeet Singh RainaVoir&lt;/a&gt; for his contribution in the Docker Compose file spcification. &lt;/p&gt;

&lt;p&gt;To keep the conversation going, connect with me on &lt;a href="https://www.linkedin.com/in/bricefotzo/"&gt;LinkedIn&lt;/a&gt;!&lt;/p&gt;

</description>
      <category>devops</category>
      <category>sql</category>
      <category>database</category>
      <category>dataengineering</category>
    </item>
    <item>
      <title>SQL + Docker: The combo for Quick and Safe Query Testing</title>
      <dc:creator>Brice Fotzo</dc:creator>
      <pubDate>Fri, 08 Sep 2023 14:20:35 +0000</pubDate>
      <link>https://dev.to/docker/sql-docker-the-combo-for-quick-and-safe-query-testing-3ib</link>
      <guid>https://dev.to/docker/sql-docker-the-combo-for-quick-and-safe-query-testing-3ib</guid>
      <description>&lt;p&gt;Testing SQL queries is a common need for developers, data engineers, analysts and so on.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.docker.com/" rel="noopener noreferrer"&gt;Docker&lt;/a&gt; provides a remarkable solution to run SQL queries, especially for concerns such as offline access, data safety, reliability, or flexibility to switch RDBMS and their versions.&lt;/p&gt;

&lt;p&gt;In this article, I will walk you through executing your SQL queries using Docker.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;This guide is ideal for SQL enthusiasts, data guys, and developers.&lt;/strong&gt;&lt;br&gt;
 Want to learn SQL, write and dry run complex transformations or even test queries before adding them to your app code? Stay tuned for this and upcoming articles around this topic.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Let’s dive in!&lt;/p&gt;

&lt;h3&gt;
  
  
  Prequisites
&lt;/h3&gt;

&lt;p&gt;Before starting, make sure you have:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Basic understanding of SQL and Docker(this &lt;a href="https://docker-curriculum.com/" rel="noopener noreferrer"&gt;tutorial&lt;/a&gt; can help)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://docs.docker.com/get-docker/" rel="noopener noreferrer"&gt;Docker installed&lt;/a&gt; on your machine&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I will guide you through the steps you need to run SQL queries with docker.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2400%2F1%2AfDoFTtedEMJ_wPpfEXJI_w.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2400%2F1%2AfDoFTtedEMJ_wPpfEXJI_w.png" alt="Steps to run SQL in Docker container"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Getting Started:
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Step 1: Identify and Pull a SQL Database
&lt;/h3&gt;

&lt;p&gt;Choose the desired SQL database version and fetch its image from &lt;a href="https://hub.docker.com/" rel="noopener noreferrer"&gt;Docker Hub&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2534%2F1%2A9esXtGaRv7PtbBSFY07SVg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2534%2F1%2A9esXtGaRv7PtbBSFY07SVg.png" alt="Docker Hub registry"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;For this demonstration, we’ll use version 13 of PostgreSQL.&lt;/p&gt;

&lt;p&gt;Let’s pull our desired image version using the &lt;code&gt;docker pull&lt;/code&gt; command:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;code&gt;docker pull&lt;/code&gt;: This command fetches a Docker image from a repository, usually from Docker Hub. Once pulled, the image is stored locally on your machine, allowing you to create containers from it.&lt;/p&gt;

&lt;p&gt;docker pull postgres:13.12-bullseye&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;code&gt;postgres:13.12-bullseye&lt;/code&gt;: This is the name and tag of the Docker image we want to pull. Here, we're pulling version 13.12 of the PostgreSQL image tagged with "bullseye".&lt;/p&gt;
&lt;/blockquote&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Step 2: Run a DB container
&lt;/h3&gt;

&lt;p&gt;Initiate the selected database server within a Docker container for testing.&lt;/p&gt;

&lt;p&gt;Having the image on our local Docker registry, let’s run the PostgresSQL server in a container with the command &lt;code&gt;docker run&lt;/code&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;code&gt;docker run&lt;/code&gt;: This command is used to start a new container from a Docker image.&lt;/p&gt;

&lt;p&gt;docker run --name sql_container -e POSTGRES_PASSWORD=root -d postgres:13.12-bullseye&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;code&gt;--name sql_container&lt;/code&gt;: This flag allows us to name our container. In this case, we're naming it "sql_container".&lt;br&gt;
 &lt;code&gt;-e POSTGRES_PASSWORD=root&lt;/code&gt;: The -e flag lets us set environment variables inside the container. Here, we're setting the PostgreSQL password to "root".&lt;br&gt;
 &lt;code&gt;-d&lt;/code&gt;: This flag means "detached mode", which runs the container in the background.&lt;br&gt;
 &lt;code&gt;postgres:13.12-bullseye&lt;/code&gt;: This specifies the image (and its tag) from which the container should be created.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Step 3: Import your SQL queries into the container
&lt;/h3&gt;

&lt;p&gt;Prepare and transfer your SQL script into the Docker container for execution.&lt;/p&gt;

&lt;p&gt;Let’s say we want to test a simple query that filters a user table for names starting by &lt;strong&gt;J&lt;/strong&gt;. The query would be:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;-- Retrieve records from the users table
SELECT * 
FROM users
WHERE name LIKE 'J%';
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;To test this, you need some sample data. Here is a sample:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;-- Drop the users table if it already exists
DROP TABLE IF EXISTS users;

-- Create a new table named users
CREATE TABLE users (
    id SERIAL PRIMARY KEY,
    name VARCHAR(100) NOT NULL,
    email VARCHAR(100) UNIQUE NOT NULL
);

-- Insert a few records into the users table
INSERT INTO users (name, email) VALUES ('John Doe', 'john.doe@example.com');
INSERT INTO users (name, email) VALUES ('Jane Smith', 'jane.smith@example.com');
INSERT INTO users (name, email) VALUES ('Samuel Jackson', 'samuel.jackson@example.com');
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Let’s encapsulate all of that into a singular &lt;strong&gt;.sql&lt;/strong&gt; script and copy it into the container using &lt;code&gt;docker cp&lt;/code&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;code&gt;docker cp&lt;/code&gt;: This command is used to copy files or directories from the host system (your machine) to a container or vice versa.&lt;/p&gt;

&lt;p&gt;docker cp /path/to/the/script.sql sql_container:/query.sql&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;code&gt;/path/to/the/script.sql&lt;/code&gt;: This is the path on your machine where your SQL script is located. You should replace this with the actual path to your file.&lt;br&gt;
 &lt;code&gt;sql_container&lt;/code&gt;: This is the name of the container where you want to copy the file. As mentioned before, we named our container "sql_container".&lt;br&gt;
 &lt;code&gt;:/query.sql&lt;/code&gt;: This specifies the destination path inside the container. After the command is executed, your SQL script will be accessible inside the container at the path /query.sql.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Step 4: Execute the query
&lt;/h3&gt;

&lt;p&gt;Run your SQL script within the containerized database to see the results.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Default execution mode&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Once the script is available in the container, run it using the command &lt;code&gt;docker exec&lt;/code&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;code&gt;docker exec&lt;/code&gt;: This command allows you to run commands inside a running Docker container.&lt;/p&gt;

&lt;p&gt;docker exec sql_container sh -c "psql -U postgres -a -f /query.sql"&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;code&gt;sql_container&lt;/code&gt;: This is the name we gave our running PostgreSQL container. We're telling Docker to execute our command inside this container.&lt;br&gt;
 &lt;code&gt;sh -c&lt;/code&gt;: We're using the shell (sh) to execute a command. The -c flag allows us to pass in the command we want the shell to execute.&lt;br&gt;
 "&lt;code&gt;psql -U postgres -a -f /query.sql&lt;/code&gt;": This is the command we're asking the shell to run. Let's break it down further:&lt;/p&gt;
&lt;/blockquote&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;By running this command, you’re effectively telling Docker to run your SQL script inside the PostgreSQL server that’s running in the sql_container. You end up with the result:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F1%2A-MQBkc4vDEmu2x6KfiTHxw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F1%2A-MQBkc4vDEmu2x6KfiTHxw.png" alt="SQL Query results"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Optionally, if you need to stop the container after usage use:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;docker stop sql_container
docker rm sql_container
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Console execution mode&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;It’s also possible to run your queries in SQL console mode, enabling quick edits and reruns. I will cover in the next article.&lt;/p&gt;

&lt;p&gt;While traditional sandboxing methods work, Docker provides added advantages:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;No Interference:&lt;/strong&gt; With Docker, you can test SQL without touching your main databases. It’s like having a separate room for testing.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Different SQL Versions:&lt;/strong&gt; With Docker, you can use many SQL versions without installing them all. Just choose and use.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Consistent Everywhere:&lt;/strong&gt; If you use Docker, your SQL tests will work the same on any computer. This is good for teams that wants to share tests in the same environment.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Clean After Use:&lt;/strong&gt; When you finish testing, Docker lets you remove everything easily. Your computer stays clean.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Works with DevOps:&lt;/strong&gt; If you use DevOps tools, Docker can fit in. This makes testing SQL automatic and easy.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;In summary, in just a few steps, you can setup a local sandbox suitable for any RDBMS database for your querying needs.&lt;/p&gt;

&lt;p&gt;Docker makes SQL testing easy and safe. If you want to test SQL, Docker helps a lot. It’s simple, clean, and useful for everyone, even if you’re not an expert. When you need to test SQL, think of Docker.&lt;/p&gt;

&lt;p&gt;I hope you find this helpful. I plan to share more content on how to leverage Docker not only for SQL but other tasks. Stay tuned for more insights!&lt;/p&gt;

&lt;p&gt;Feel free to share your thoughts or alternative methods in the comments.&lt;/p&gt;

&lt;p&gt;Happy Querying!&lt;/p&gt;

&lt;p&gt;Connect with me &lt;a href="https://www.linkedin.com/in/bricefotzo/" rel="noopener noreferrer"&gt;LinkedIn&lt;/a&gt; to continue the discussion!&lt;/p&gt;

</description>
      <category>devops</category>
      <category>sql</category>
      <category>dataengineering</category>
      <category>docker</category>
    </item>
  </channel>
</rss>
