Docker
Docker is open source software, provided by Docker Inc., which is designed to make it easier to create, deploy and run applications using containers. Containers allow a developer to package up an application with all of the parts it needs, such as libraries and other dependencies, and deploy it as one package.
Unlike virtual machines, a container does not need to incorporate a virtual operating system, but instead relies on aspects of the operating system that they are running on, therefore making containers much more lightweight when compared with virtual machines.
Example Usage of Docker
Below are some examples of how Docker can be used.
MySQL is a relational database that can be used in many different situations, whilst phpMyAdmin is a web based graphical front end for MySQL that allows you to browse the database, execute SQL commands against it and much more. The below commands create a basic setup of MySQL and phpMyAdmin in separate containers, which allows them to interact with one another and provides a means for the running of scripts against the database from the host machine.
Pull the required Docker images.
docker pull mysql docker pull phpmyadmin
Create a Docker network so that MySQL and phpMyAdmin can connect.
docker network create my-network-mysql
Create a volume called 'my-volume-mysql' to persist the MySQL data.
docker volume create my-volume-mysql
Create a MySQL container called 'my-mysql' that is assigned to the above network and volume. Root access is provided with the specified password and on the specified port. The container is also run in detached mode so the terminal window can be closed without stopping the container. The back ticks at the end of each line allow the command to be broken down over more than one line when executed from a PowerShell prompt. This may be different for other environments.
docker run -d `
--name my-mysql `
--network my-network-mysql `
-e MYSQL_ROOT_HOST=% `
-e MYSQL_ROOT_PASSWORD=DemoPW `
-v my-volume-mysql:/var/lib/mysql `
-p 3306:3306 `
mysql
Create a phpMyAdmin container named 'my-phpmyadmin' on the same network as above, specifying the port number that it is accessible from.
docker run -d `
--name my-phpmyadmin `
--network my-network-mysql `
-e PMA_HOST=my-mysql `
-p 8080:80 `
phpmyadmin
The phpMyAdmin container can then be accessed in a web browser at 'http://localhost:8080/', with 'root' as the username and 'DemoPW' as the password.
PostgreSQL is a relational database that can be used in many different situations, whilst pgAdmin is a web based graphical front end for PostgreSQL that allows you to browse the database, execute SQL commands against it and much more. The below commands create a basic setup of PostgreSQL and pgAdmin in separate containers, which allows them to interact with one another and provides a means for the running of scripts against the database from the host machine.
Pull the required Docker images.
docker pull postgres docker pull dpage/pgadmin4
Create a Docker network so that PostgreSQL and pgAdmin can connect.
docker network create my-network-postgres
Create a volume to persist the PostgreSQL data.
docker volume create my-volume-postgres
Create a PostgreSQL container called 'my-postgres' that is assigned to the above network and volume. Access is provided with the specified password and on the specified port for the user 'postgres'. The container is also run in detached mode so the terminal window can be closed without stopping the container. The back ticks at the end of each line allow the command to be broken down over more than one line when executed from a PowerShell prompt. This may be different for other environments.
docker run -d `
--name my-postgres `
--network my-network-postgres `
-e POSTGRES_PASSWORD=DemoPW `
-v my-volume-postgres:/var/lib/postgresql/data `
-p 5432:5432 `
postgres
Create a pgAdmin container called 'my-pgadmin' on the same network as above, specifying the email address and password to log in to pgAdmin.
docker run -d `
--name my-pgadmin `
--network my-network-postgres `
-e PGADMIN_DEFAULT_EMAIL=demo@example.com `
-e PGADMIN_DEFAULT_PASSWORD=DemoPW `
-p 80:80 `
dpage/pgadmin4
The database container can now be accessed from the command line using the 'progres' user to create a database called 'demo'.
docker exec -it my-postgres bash psql -U postgres CREATE DATABASE demo;
Once the database has been created, pgAdmin can be accessed via a web browser at 'http://localhost/', using the email address and password specified when creating the container.
Within pgAdmin, a connection to the database can be established with the following details.
Server Name: localhost Host name/address: my-postgres Port: 5432 Database: demo User: postgres Password: DemoPW
SQL Server is a relational database that can be used in many different situations. The below commands create a basic setup of SQL Server in a container. Once running, connections to the database can be established from, for example, SQL Server Management Studio, a graphical frontend for SQL server.
Pull the Docker image for SQL Server 2022.
docker pull mcr.microsoft.com/mssql/server:2022-latest
Create a volume to persist the SQL Server data.
docker volume create my-volume-sql-server
Create an SQL Server container with a name of 'my-sql-server' and hostname of 'my-sql-server-host', whilst assigning it to the above volume. A password for the 'sa' account is also set and the end user license agreement is accepted. Finally, the port number is specified, along with the previously pulled image. The back ticks at the end of each line allow the command to be broken down over more than one line when executed from a PowerShell prompt. This may be different for other environments.
docker run -d `
--name my-sql-server `
--hostname my-sql-server-host `
-e "MSSQL_SA_PASSWORD=DemoPW" `
-e "ACCEPT_EULA=Y" `
-v my-volume-sql-server:/var/opt/mssql `
-p 1433:1433 `
mcr.microsoft.com/mssql/server:2022-latest
In order to connect to the database from SQL Server Management Studio, the following details need to be used. This assumes that SQL Server Management Studio and the container are running on the same machine.
Server name: localhost Authentication: SQL Server Authentication Login: sa Password: DemoPW Encryption: Mandatory Trust server certificate: Checked
Useful Docker Links
- Docker Desktop for Mac and Windows.
- Docker Documentation.
- Docker Hub.
- Swarm mode.
- Dockerr for Windows frequently asked questions.
- Logs and troubleshooting Docker for Mac.