How To Rebuild Docker Container In Docker-Compose.Yml?

2.1K    Asked by AlisonKelly in Devops , Asked on Nov 17, 2022

So I learned that there are a lot of services that are defined  docker-compose.yml. I started a few of these services. I want to create only one of these and then start it up without using the other services 

These are the commands I have run :

docker-compose up -d # run all services
docker-compose stop nginx # stop only one. but it still running !!!
docker-compose build --no-cache nginx 
docker-compose up -d --no-deps # link nginx to other services

I got a nginx container by the end and I also learned that docker-compose will not kill all running containers! How does docker rebuild containers?

Answered by Alastair McDade
With docker-compose 1.19 up

docker-compose up --build --force-recreate --no-deps [-d] [..]
Without one or more service_name arguments all images will be built if missing and all containers will be recreated.
From the help menu
Options:
    -d, --detach Detached mode: Run containers in the background,
                        print new container names. Incompatible with
                        --abort-on-container-exit.
    --no-deps Don't start linked services.
    --force-recreate Recreate containers even if their configuration
                        and image haven't changed.
    --build Build images before starting containers.
Without cache
To force a rebuild to ignore cached layers, we have to first build a new image
docker compose rebuild --no-cache [..]
From the help menu
Options:
    --force-rm Always remove intermediate containers.
    -m, --memory MEM Set memory limit for the build container.
    --no-cache Do not use cache when building the image.
    --no-rm Do not remove intermediate containers after a successful build.
Then recreate the containerdocker-compose up --force-recreate --no-deps [-d] [..]


Your Answer

Answer (1)

To check if a table exists on a linked server in SQL Server, you can use a combination of system stored procedures and dynamic SQL. Here’s a step-by-step guide on how to do this:1. Using OPENQUERY

You can use the OPENQUERY function to execute a query on the linked server. However, this method requires you to know the database name and table name in advance.

  IF EXISTS (    SELECT 1     FROM OPENQUERY([LinkedServerName], 'SELECT * FROM [DatabaseName].[SchemaName].[TableName]'))BEGIN    PRINT 'Table exists'ENDELSEBEGIN    PRINT 'Table does not exist'END

2. Using EXECUTE AT

You can also use the EXECUTE AT command to run a query on the linked server and check for the existence of the table.

  DECLARE @sql NVARCHAR(MAX)SET @sql = N'SELECT 1              FROM sys.tables              WHERE name = ''TableName''              AND SCHEMA_NAME(schema_id) = ''SchemaName'''IF EXISTS (EXEC('EXECUTE AT [LinkedServerName] ' + @sql))BEGIN    PRINT 'Table exists'ENDELSEBEGIN    PRINT 'Table does not exist'END

3. Using sp_tables_ex

sp_tables_ex is a system stored procedure that returns a list of objects that can be queried from a linked server.

  EXEC sp_tables_ex @table_server = 'LinkedServerName', @table_name = 'TableName', @table_schema = 'SchemaName'You can capture the output of this stored procedure to check if the table exists.

4. Using Dynamic SQL

You can use dynamic SQL to build and execute a query that checks for the existence of the table.

  DECLARE @sql NVARCHAR(MAX)DECLARE @result INTSET @sql = N'SELECT @result = COUNT(*)              FROM [' + QUOTENAME('LinkedServerName') + '].[' + QUOTENAME('DatabaseName') + '].sys.tables              WHERE name = ''TableName''              AND SCHEMA_NAME(schema_id) = ''SchemaName'''EXEC sp_executesql @sql, N'@result INT OUTPUT', @result OUTPUTIF @result > 0BEGIN    PRINT 'Table exists'ENDELSEBEGIN    PRINT 'Table does not exist'END

Example with Detailed Steps

Assume you have a linked server named LinkedServer1 and you want to check if a table named Employees exists in the HR schema of the CompanyDB database.

Step 1: Define the Query

  DECLARE @sql NVARCHAR(MAX)DECLARE @result INTSET @sql = N'SELECT @result = COUNT(*)              FROM [' + QUOTENAME('LinkedServer1') + '].[' + QUOTENAME('CompanyDB') + '].sys.tables              WHERE name = ''Employees''              AND SCHEMA_NAME(schema_id) = ''HR'''EXEC sp_executesql @sql, N'@result INT OUTPUT', @result OUTPUTIF @result > 0BEGIN    PRINT 'Table exists'ENDELSEBEGIN    PRINT 'Table does not exist'END

This script will dynamically check if the Employees table exists in the HR schema of the CompanyDB database on the LinkedServer1 linked server.

Notes:

Permissions: Ensure you have the necessary permissions to query the linked server and the relevant databases.

Linked Server Configuration: Ensure the linked server is correctly configured and accessible.

SQL Injection: Be cautious of SQL injection when using dynamic SQL. Always sanitize inputs if they are coming from user input or external sources.

These methods should help you determine the existence of a table on a linked server in SQL Server.


How To Rebuild Docker Container In Docker-Compose.Yml?

ChatGPT

Rebuilding a Docker container using docker-compose.yml involves several steps. You can use the docker-compose command to stop, remove, and rebuild the container. Here’s a detailed guide on how to do this:

  Step-by-Step Guide

Navigate to the Project Directory

Ensure you are in the directory containing your docker-compose.yml file.

  cd path/to/your/project

Stop and Remove Containers

Stop and remove the containers defined in the docker-compose.yml file. This ensures that old containers are not running and the new build will start fresh.

docker-compose down

Rebuild the Containers

Use the --build flag with docker-compose up to force a rebuild of the images. This command will rebuild the images and start the containers.

  docker-compose up --build

This command rebuilds the images specified in the docker-compose.yml file and then starts the containers.

Optional: Remove Unused Images

After rebuilding, you may want to remove unused images to free up space.

  docker image pruneExample docker-compose.yml

Here’s a sample docker-compose.yml file for reference:

  version: '3'services:  web:    build: .    ports:      - "5000:5000"    volumes:      - .:/code    depends_on:      - redis  redis:    image: "redis:alpine"

Additional Tips

Cache Busting: If you want to ensure that Docker does not use any cached layers, you can use the --no-cache option when building.

  docker-compose build --no-cachedocker-compose up

Rebuilding a Specific Service: If you only need to rebuild a specific service defined in your docker-compose.yml, you can specify the service name.

  docker-compose up --build For example, to rebuild only the web service:docker-compose up --build web

Force Recreate Containers: If you want to ensure that the containers are recreated even if the images haven't changed, use the --force-recreate option.

docker-compose up --force-recreate --build

Automating the Process with a Script

You can create a shell script to automate the process:

  #!/bin/bash# Navigate to project directorycd path/to/your/project# Stop and remove containersdocker-compose down# Rebuild and start containersdocker-compose up --build# Optional: Clean up unused imagesdocker image prune -fSave this script as rebuild.sh, make it executable, and run it:bashCopy codechmod +x rebuild.sh./rebuild.sh

By following these steps, you can effectively rebuild your Docker containers using docker-compose.yml.








4 Months

Interviews

Parent Categories