Arquivo da tag: Docker

Atalhos para lhe ajudar com o Docker

– docker images: lista toda as imagens, como seus nomes criados aleatoriamente ou não,
– docker run: cria os containers,
– docker exec: serve para executar alguma coisa no container,
– docker kill [nome da maquina] : matando um processo travado,
– docker start [nome do container] : revivendo containers já criados,
– docker inspect [nome do container] : quando a máquina estiver UP podemos ver o que tem na máquina, inclusive o IP,
– docker pull: faz o download de imagens,
– docker run -it -v $(pwd):/tmp [nome do container] bash: o -v é de volume e o $(pwd) pega o caminho local e cria a pastatmp,
– docker ps: acompanha os containers em execução,
– docker run —link mysql:db -it ubuntu bash: o uso do —link serve para não fixar o IP, assim criaríamos um ALIAS db,
– docker rm $(docker ps -qa): removendo todos os containers,
– docker rmi [nome da imagem]: removendo todas as imagens,
– docker run —rm -it ubuntu bash: instanciando um container que não será persistido,
– docker commit -m “Instalação do Apache” [nome ou id do container] [imagem]/apache: está salvando um FORK da máquina UBUNTU com o APACHE,
– docker run -it -p 8080:80 ubuntu/apache bash: a porta 8080 é dá máquina física e o 80 é do container,
– docker exec -it [nome do container] bash: nós deixa entrar na linha de comando do container.


Imagem x Máquina(Container)
Quando você está usando o Docker você precisa de uma imagem, isso quer dizer que a imagem poderá ser reutilizada várias vezes, já a máquina não sempre será criar uma nova caso você não chame a antiga pelo NAME.
– docker images: lista toda as imagens, como seus nomes criados aleatoriamente ou não.
– docker ps -a: lista toda as máquinas criadas já.


<!— inicio —>
FROM ubuntu//imagem base
RUN apt-getupdate&&apt-getinstall-y apache2//executa a instrução de instalação do Apache
ADD app/ /var/www/html
COPY app/ /var/www/html/app
<!— fim —>
– docker build-t ubuntu/apache: compilando e gerando a imagem,
– docker run -d -p 80:80 ubuntu/apache: rodando em primeiro plano com o FOREGROUND.


<!— inicio —>
<!— fim —>
– docker-compose up -d: executa uma série de comandos, o-d deixa rodando em BACKGROUND,
– docker-compose kill: força a parada dá máquina,
– docker-compose rm: remove máquina que foi criada via COMPOSE,

How To Install Microsoft SQL Server On Ubuntu Linux

In December 2016 Microsoft made their SQL Server database available in Linux. Here we’ll cover how to install and perform basic setup of MSSQL in the Ubuntu distribution of Linux.

Install MSSQL In Ubuntu 16.10

First we’ll set up the repository file, Microsoft provide a copy of this for Ubuntu here:

We’ll use the wget command to copy this file to the /etc/apt/sources.list.d directory so that we can use it using apt-get. After this we run ‘apt-get update’ in order to sync the package index files with the new source that we’ve just added.

root@ubuntu:~# wget -O /etc/apt/sources.list.d/mssql-server.list
root@ubuntu:~# apt-get update

At the time of writing, the 16.10 directory at was empty, so I have instead used the 16.04 files here which seems to have worked just fine.

Now that the repository file is in place, installation is as simple as running the following command. At the time of writing the total size of the package and all dependencies was a 747mb download.

root@ubuntu:~# apt-get install mssql-server -y
| Please run /opt/mssql/bin/sqlservr-setup to complete the setup of |
|                  Microsoft(R) SQL Server(R).                      |

Once the installation has completed, we are advised to run the /opt/mssql/bin/sqlservr-setup bash script to complete the setup process.

During my first installation attempt, I got the following error as my virtual machine was only running with 2GB of memory, so be sure that you have enough memory before proceeding.

sqlservr: This program requires a machine with at least 3250 megabytes of memory.
Microsoft(R) SQL Server(R) setup failed with error code 1.

You’ll be able to proceed once you have adequate memory available.

root@ubuntu:~# /opt/mssql/bin/sqlservr-setup
Microsoft(R) SQL Server(R) Setup

You can abort setup at anytime by pressing Ctrl-C. Start this program
with the --help option for information about running it in unattended

The license terms for this product can be downloaded from and found
in /usr/share/doc/mssql-server/LICENSE.TXT.

Do you accept the license terms? If so, please type "YES": YES

Please enter a password for the system administrator (SA) account:
Please confirm the password for the system administrator (SA) account:

Setting system administrator (SA) account password...

Do you wish to start the SQL Server service now? [y/n]: y
Do you wish to enable SQL Server to start on boot? [y/n]: y
Created symlink /etc/systemd/system/ â /lib/systemd/system/mssql-server.service.
Created symlink /etc/systemd/system/ â /lib/systemd/system/mssql-server-telemetry.service.

Setup completed successfully.

That’s it, Microsoft SQL Server is now running successfully and listening for traffic on TCP port 1434.

root@ubuntu:~# systemctl status mssql-server
â mssql-server.service - Microsoft(R) SQL Server(R) Database Engine
   Loaded: loaded (/lib/systemd/system/mssql-server.service; enabled; vendor preset: enabled)
   Active: active (running) since Fri 2016-12-30 22:55:23 AEDT; 33s ago
 Main PID: 4164 (sqlservr)
   CGroup: /system.slice/mssql-server.service
           ââ4164 /opt/mssql/bin/sqlservr
           ââ4201 /opt/mssql/bin/sqlservr

root@ubuntu:~# netstat -antp | grep 1434
tcp        0      0*               LISTEN      4201/sqlservr

Connecting To MSSQL

In order to actually connect to the server from Linux we need to install the mssql-tools package, which comes from a different repository than the one that we just set up. It can be found here:

First we’ll download a copy of the prod.list file and place it into the /etc/apt/sources.list.d directory.

root@ubuntu:~# wget -O /etc/apt/sources.list.d/pord.list
root@ubuntu:~# apt-get update

We can now proceed with installing the mssql-tools package, as shown below.

root@ubuntu:~# apt-get install mssql-tools -y

Once this is installed we can use the sqlcmd command to interact with the database. To see how to run sqlcmd, simply run it with the -? option for help.

Unfortunately it appears that when you specify the -P option for the password, the password must be provided in the command line with no option of being prompted for it later. Keep in mind that your password will be stored in your bash history running it this way.

root@ubuntu:~# sqlcmd -U SA -P password
1> create database test;
2> go
1> use test;
2> go
Changed database context to 'test'.
1> create table websites(domain varchar(255));
2> go
1> insert into websites (domain)
2> values ('');
3> go

(1 rows affected)
1> select domain
2> from websites;
3> go
(1 rows affected)

In this example we create a test database with a table named websites and a column for domain names. We then insert a domain name and pull it back out with select, confirming both that we are able to connect and that basic SQL queries appear to be working as expected.


Microsoft’s SQL Server is now available for installation on Linux. Personally I don’t think I’ll ever use this over other alternatives such as MariaDB or PostgreSQL, so hopefully someone somewhere actually finds this information useful!

SQL Server Running on a Mac?!

Microsoft’s recent Connect(); event included a lot of interesting announcements. The one that really got my attention was SQL Server for Linux. My first question, of course: Can I run this on my Mac?


The answer (and an increasingly common answer, I might add) is Docker. Here are the steps that worked for me.

Install and configure Docker

If you don’t already have Docker installed, you’ll need to download and install it.

Next step, you’ll need to increase Docker’s available memory to 4GB or more.

  1. Docker -> Preferences
  2. Increase Memory to at least 4GB
  3. Click Apply & Restart

Get the Docker image

Open a Terminal window, and download the latest SQL Server for Linux Docker image.

docker pull microsoft/mssql-server-linux

Now, launch an instance of the Docker image.

docker run -d --name name_your_container -e 'ACCEPT_EULA=Y' -e 'SA_PASSWORD=P@55w0rd' -p 1433:1433 microsoft/mssql-server-linux

You should now have SQL Server running on your Mac, ready for action!

A few notes on Docker parameters

  • -d: this launches the container in daemon mode, so it runs in the background
  • --name name_your_container: give your Docker container a friendly name, which is useful for stopping and starting containers from the Terminal.
  • -e 'ACCEPT_EULA=Y: this sets an environment variable in the container named ACCEPT_EULAto the value Y. This is required to run SQL Server for Linux.
  • -e 'SA_PASSWORD=P@55w0rd': this sets an environment variable for the sadatabase password. Set this to your own strong password. Also required.
  • -p 1433:1433: this maps the local port 1433 to the container’s port 1433. SQL Server, by default, listens for connections on TCP port 1433.
  • microsoft/mssql-server-linux: this final parameter tells Docker which image to use

Tip: Get Kitematic

Kitematic is a nice desktop application for managing Docker containers. The first time you click Open Kitematic, it will prompt you to download and install it. You can then use Kitematic to view the output of your containers, manage their settings, etc.

It’s running, now what?

sql-cli is a useful command-line tool for SQL Server. To use it, you’ll need Node.js. Download and install Node.js, if you don’t already have it.

From the Terminal, install sql-cli globally, so you can use it anywhere.

npm install -g sql-cli

Next, connect to your SQL Server instance running in Docker.

mssql -u sa -p P@55w0rd

You can now query and execute SQL Server commands from the mssql>prompt. Type .quit and press Enter to exit.

Getting an existing SQL database into Docker

I haven’t tried restoring a database from backup, yet. Instead, I chose to script out an existing database and use sql-cli to run the scripts to recreate my database running in Docker.

1. Generate scripts

First step is to use SQL Server Management Studio to generate scripts from an existing database.

Right-click on your database, and choose Tasks -> Generate Scripts…

I chose to separate my table and data scripts from my Views, Stored Procedures, User-Defined Functions, etc.

Next, click on Advanced

I chose to “Check for object existence,” “Script DROP and CREATE,” and set “Types of data to script” to “Schema and data.”

Checking for object existence and DROP/CREATE allows me to re-run the same script against an existing database, if I want to reset it back to its original state.

Choose a location to save your scripts. When finished, repeat the steps to script out your Views, Stored Procedures, and UDFs, if necessary.

2. Run the scripts against your SQL Server running in Docker

  • Copy the scripts you generated above to your Mac where you can easily get to them from the Terminal.
  • Open a Terminal, and change to the folder where you placed the scripts.
  • Now, connect to your SQL Server running in Docker using sql-cli, and make sure you are on master
mssql> use master
  • Create a new database
mssql> CREATE DATABASE devdb

Note: Change devdb to match the name of the database you scripted. The generated scripts expect a database with that same name to exist.

  • Change to the database we just created (change devdb to match your database name)
mssql> use devdb
  • Run the generated scripts to create all the tables and populate them with data.
mssql> .run script.sql
  • Repeat with the script that contains your other database objects, if necessary.

You now have a copy of your database, running on your Mac, without the need for entire Windows VM!