The most useful Docker commands for DBA

In my latest blog post, I described How to start SQL Server Docker container in few simple steps. Since then I was playing a bit with Docker and now I want to share with you a list of the most useful Docker commands.

Check Docker version

You can check Docker version very easily using this command:

docker version

Example output:

Docker version

List downloaded images

To display a list of already downloaded (pulled) images use one of these commands:

docker image ls
docker images

Example output:

Docker images

Search for new image

You can use https://hub.docker.com/ to search for new images, but you can do this also from command line.

docker search [searchphrase]

Example output:

Docker search

Pull the image

To pull (download) the image you need to run this command:

docker pull <imagename>

Example output:

Docker pull

Run new container

To run new container use this command:

docker run <imagename>

Example output:

Docker run hello-world

Sometimes you need to provide additional parameters.

docker run --name <CONATINER_NAME> -d -p 1433:1433 -e sa_password=<SA_PASSWORD> -e ACCEPT_EULA=Y microsoft/mssql-server-windows-developer
Docker run mssql
List containers

To list running containers use:

docker ps

Docker ps

To list all created containers use:

docker ps -a

Docker ps -a

Stop container

To stop container:

docker stop <container ID>

To stop all running containers:

docker stop $(docker ps -a -q)
Start container

To start container:

docker start <container ID>

To start all containers

docker start $(docker ps -a -q)
Remove container

To remove container:

docker rm <conatiner ID>

To remove all containers:

docker rm $(docker ps -a -q)
Remove image

To remove image:

docker rmi <image ID>

Docker rmi

Copy files

To copy files between host and container use cp command:

docker cp <Container ID>:<Container path> <host path>
docker cp <host path> <Container ID>:<Container path>

 

I hope you find this list useful. Thanks for reading!

-Marek

Share it:
FacebooktwitterredditpinterestlinkedintumblrmailFacebooktwitterredditpinterestlinkedintumblrmail

How to start SQL Server Docker container in few simple steps

SQL Server 2017 is the first SQL Server version that is available on Windows, Linux, and in Docker containers. In this blog post, I will show you how to create your first SQL Server Docker container in a few very simple steps.

What is Docker?

Docker is an open-source container platform provider written in GO programming language. It’s available for Linux, Windows, and MacOS. In addition, it also works in Azure and AWS environments. Containers allow developers to package up an application with all of the parts it needs, such as libraries and other dependencies, and deliver it all out as one package. It’s something similar to Virtual Machines but without the guest operating system. They’re isolated, but they share Host OS. That makes them smaller and more lightweight.

container vs vm

Docker installation

Currently, for Windows users, Docker is available in two editions. Docker Community Edition for Windows (CE) is a free solution designed for Windows 10 users. Docker Enterprise Edition for Windows Server (EE) is dedicated for Windows Server 2016 users. There is also third (legacy) solution – Docker Toolbox with support for Windows 7 and 8. However, this edition has some additional requirements and limitations, for example, it supports only Linux containers.

Docker CE

To install Docker CE you need to follow two very simple steps:

  1. Download Docker CE installer from Docker Store.
  2. Double-click Docker for Windows Installer.exe and follow the install wizard to accept the license, authorize the installer, and proceed with the install.
Docker EE

Installation of Docker EE is more automated and can be performed using PowerShell:

  1. Open PowerShell command prompt and run following command to install Docker:
    Install-Module DockerProvider -Force
    Install-Package Docker -ProviderName DockerProvider -Force
  2. If prompted to install any prerequisites such as NuGet, type in “Y”.
  3. Run the following command to check if a reboot is required:
    (Install-WindowsFeature Containers).RestartNeeded
  4.  If the result is Yes, restart your computer with this command:
    Restart-Computer
    Docker Toolbox

Installation of Docker Toolbox is quite similar to the installation of Docker CE.

  1. Download Docker Toolbox installer.
  2. Install Docker Toolbox by double-clicking the installer.

The installer installs following software on your machine:

  • Docker Client for Windows
  • Docker Toolbox management tool and ISO
  • Oracle VM VirtualBox
  • Git MSYS-git UNIX tools

Installation validation

When Docker is already installed and running you may want to check if it works properly. For Docker CE and EE, start PowerShell command prompt. For Docker Toolbox, open Docker Toolbox Terminal (Docker QuickStart  Terminal). Then run the following command to check your installed version:

docker --version

docker version

Next step is to check if Docker can pull and run containers:

docker run hello-world

docker run hello-world

Creating SQL Server Docker container

Currently, Microsoft provides three SQL Server Docker containers:

You may look for them on Docker Hub page or you also may run the following command to list available images:

docker search microsoft

docker search microsoft

Pull needed image

The first thing to start your container is to pull the image from the repository. Depending on what image you want to use you need to execute one of the following commands:

docker pull microsoft/mssql-server-linux
docker pull microsoft/mssql-server-windows-developer
docker pull microsoft/mssql-server-windows-express

Important: Docker Toolbox supports only Linux containers. Docker CE and EE support Linux and windows containers, however, to pull and run docker image without any issues you need to ensure that your Docker installation is running in the proper mode. You may do it by right-clicking on the Docker whale icon in your tray docker icon.

I decided to download SQL Server Developer on Windows image.

docker pull

To check what images are downloaded run the following command:

docker images

docker images

Run new container

Finally, you’re ready to run your container. To do it you need to execute docker runcommand and provide few mandatory parameters.

  • --name – provide a name for your container
  • -d – run container in detached mode (in the background)
  • -p – publish a container᾿s port or a range of ports to the host. For example -p 14333:1433 maps port 14333 on the host to port 1433 in the container.
  • -e – create environment variables under the container execution runtime
    • sa_password (to assign the SQL Server SA password)
    • ACCEPT_EULA (to accept end-user license agreement)

Example:

docker run --name <CONATINER_NAME> -d -p 14333:1433 -e sa_password=<SA_PASSWORD> -e ACCEPT_EULA=Y microsoft/mssql-server-windows-developer

docker run mssql

Here you can find more details about docker run command.

How to connect to your new SQL Server?

The last thing is to connect to new SQL Server instance. To do this open SQL Server Management Studio and as Server name provide localhost with the port you specified for mapping. Then, choose SQL Server authentication and log in using sa account (with the password you specified during container setup).

SSMS - connect

Now you can run any queries you want.

SSMS - query

-Marek

Share it:
FacebooktwitterredditpinterestlinkedintumblrmailFacebooktwitterredditpinterestlinkedintumblrmail

QuickQuestion: How to start and stop SQL Server instance from a command line?

QuickQuestion series is a series of short posts in which I answer database related questions asked by my colleagues, friends, and co-workers, mainly application developers.

Today’s question:

How to start and stop SQL Server instance from a command line?

Windows

SQL Server instance as any other Windows network service can be managed using the NETcommand from elevated Command Prompt (Run as Administrator).

net options

To start default SQL Server instance use following command:

net start "SQL Server (MSSQLSERVER)"

or

net start MSSQLSERVER

To start named SQL Server instance use following command:

net start "SQL Server ( instancename )"

or

net start MSSQL$ instancename

SQLServer - net start

To stop default SQL Server instance use following command:

net stop "SQL Server (MSSQLSERVER)"

or

net stop MSSQLSERVER

To stop named SQL Server instance use following command:

net stop "SQL Server ( instancename )"

or

net stop MSSQL$ instancename

SQLServer - net stop

To restart instance you need to first stop it and then start it.  On Windows, there is no one single command that can be used for this purpose.

Linux (RHEL and Ubuntu)

On Linux, we don’t have yet named instances, so all commands are executed against default instance. SQL Server can be managed using thesystemctl command.

To check the current state of SQL Server instance you can run this command:

sudo systemctl status mssql-server

To start instance run following command:

sudo systemctl start mssql-server

SQLServer - systemctl start

To stop instance run following command:

sudo systemctl stop mssql-server

SQLServer - systemctl stop

To restart instance run following command:

sudo systemctl restart mssql-server
Docker

Docker provides us a very similar set of commands.

To start container run following command:

docker start <container ID>

SQLServer - docker start

To stop container run following command:

docker stop <container ID>

SQLServer - docker stop

To restart container run following command:

docker restart <container ID>

-Marek

Share it:
FacebooktwitterredditpinterestlinkedintumblrmailFacebooktwitterredditpinterestlinkedintumblrmail

By continuing to use the site, you agree to the use of cookies. more information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this.

Close