---
# System prepended metadata

title: Docker Installation

---

# Docker Installation
Mac: [Docker Desktop for Mac](https://hub.docker.com/editions/community/docker-ce-desktop-mac)
Windows: [Docker Desktop for Windows](https://hub.docker.com/editions/community/docker-ce-desktop-windows)
Linux: 
```
sudo apt-get update
sudo apt-get upgrade
sudo apt install docker.io
sudo systemctl start docker
sudo systemctl enable docker


sudo systemctl status docker
```
:::spoiler 
**Installation Steps for Mac/Windows:**
1. Download Windows/Mac docker installable from above links.
2. Install dmg/exe files by following on-screen instructions and provide credentials If needed.

**Installation Steps for Linux Ubuntu:**
1. Execute above commands in Linux Terminal.
:::

# Docker Lab Setup
> [color=#00bfff] **Linux Ubuntu VM Hostnames**

```
docker001.westeurope.cloudapp.azure.com
docker002.westeurope.cloudapp.azure.com
docker003.westeurope.cloudapp.azure.com
..
..
docker009.westeurope.cloudapp.azure.com

docker001.eastus.cloudapp.azure.com
docker001.eastus2.cloudapp.azure.com
docker001.westus.cloudapp.azure.com
docker001.westus2.cloudapp.azure.com
```

> [color=#00bfff] **Connect From Windows/Mac**
HostName: docker00X.westeurope.cloudapp.azure.com
Username: ubuntu
Password: DockerUser@123

==Syntax:== ssh <username>@<hostname>

> Example

```bash=
ssh ubuntu@docker000.westeurope.cloudapp.azure.com
```

:::info
:bulb: Note: Type 'yes' for 1st time login with ssh and enter password
:::

:::spoiler 
**Mac:**
1. Open ==Terminal== and execute above commands

**Windows:**
1. Connect from [Putty](https://www.putty.org/) or [Chrome Secure shell](https://chrome.google.com/webstore/detail/secure-shell-app/pnhechapfaindjhompbnflcldabbghjo/related?hl=en)


User1.
docker000.eastus.cloudapp.azure.com
docker001654442.eastus.cloudapp.azure.com

User2.
docker002.eastus.cloudapp.azure.com
docker003.eastus.cloudapp.azure.com

User3.
docker004.eastus.cloudapp.azure.com
docker005.eastus.cloudapp.azure.com

User4.
docker006.eastus.cloudapp.azure.com
docker007.eastus.cloudapp.azure.com

User5.
docker008.eastus.cloudapp.azure.com
docker009.eastus.cloudapp.azure.com

User6.
docker000.eastus2.cloudapp.azure.com
docker001.eastus2.cloudapp.azure.com

User7.
docker002.eastus2.cloudapp.azure.com
docker003.eastus2.cloudapp.azure.com

User8.
docker004.eastus2.cloudapp.azure.com
docker005.eastus2.cloudapp.azure.com

User9.
docker006.eastus2.cloudapp.azure.com
docker007.eastus2.cloudapp.azure.com

User10.
docker008.eastus2.cloudapp.azure.com
docker009.eastus2.cloudapp.azure.com

User11.
docker000.westus.cloudapp.azure.com
docker001.westus.cloudapp.azure.com

User12.
docker002.westus.cloudapp.azure.com
docker003.westus.cloudapp.azure.com

User13.
docker004.westus.cloudapp.azure.com
docker005.westus.cloudapp.azure.com

User14.
docker006.westus.cloudapp.azure.com
docker007.westus.cloudapp.azure.com

User15.
docker008.westus.cloudapp.azure.com
docker009.westus.cloudapp.azure.com


User16.
docker000583293.westus2.cloudapp.azure.com
docker001.westus2.cloudapp.azure.com

User17.
docker002.westus2.cloudapp.azure.com
docker003.westus2.cloudapp.azure.com

User18.
docker004.westus2.cloudapp.azure.com
docker005.westus2.cloudapp.azure.com

User19.
docker006.westus2.cloudapp.azure.com
docker007.westus2.cloudapp.azure.com

User20.
docker008.westus2.cloudapp.azure.com
docker009.westus2.cloudapp.azure.com


User21.
docker000.westeurope.cloudapp.azure.com
docker001.westeurope.cloudapp.azure.com

User22.
docker002.westeurope.cloudapp.azure.com
docker003.westeurope.cloudapp.azure.com

User23.
docker004.westeurope.cloudapp.azure.com
docker005.westeurope.cloudapp.azure.com

User24.
docker006.westeurope.cloudapp.azure.com
docker007.westeurope.cloudapp.azure.com

User25.
docker008.westeurope.cloudapp.azure.com
docker009.westeurope.cloudapp.azure.com


User26.
docker000.northeurope.cloudapp.azure.com
docker001.northeurope.cloudapp.azure.com

User27.
docker002.northeurope.cloudapp.azure.com
docker003.northeurope.cloudapp.azure.com

User28.
docker004.northeurope.cloudapp.azure.com
docker005.northeurope.cloudapp.azure.com

User29.
docker006.northeurope.cloudapp.azure.com
docker007.northeurope.cloudapp.azure.com

User30.
docker008.northeurope.cloudapp.azure.com
docker009.northeurope.cloudapp.azure.com    


:::


# Docker Validation
```bash=
docker -v
```
### Docker Hub URL:
[Docker Hub URL](https://hub.docker.com/)

gaptech


# Run sample Docker Container

**Docker Image:** gaptech/dockerdemo
```bash=
docker pull gaptech/dockerdemo
docker images
docker container run -d -p 8082:80 --name my_first_container gaptech/dockerdemo
docker ps -a
```
> Validation
http://docker001.centralus.cloudapp.azure.com:8082
http://docker001.centralus.cloudapp.azure.com:8082/welcome.html

# Play with Docker Commands
> [color=#757575] Stop
```bash=
docker container stop my_first_container
```

> [color=#66b032] Start
```bash=
docker container start my_first_container
```

> [color=#ff0000] Remove
```bash=
docker container rm -f my_first_container
```


# Container Lifecycle Commands
==Syntax:== docker container <container-state> <container-id> (or) <container-name>
:::info
Note: we can find container id using “docker ps –a ” command
:::

:::spoiler
> [color=#757575] Stop
```bash=
docker container stop my_first_container
```

> [color=#66b032] Start
```bash=
docker container start my_first_container
```
> [color=#3cd070] Restart
```bash=
docker container restart my_first_container
```

> [color=#8b0000] Kill
```bash=
docker container kill my_first_container
```
> [color=#ffb300] Pause
```bash=
docker container pause my_first_container
```

> [color=#48d1cc] Unpause
```bash=
docker container unpause my_first_container
```

> [color=#ff0000] Remove
```bash=
docker container rm -f my_first_container
```

> [color=#ff0000] Remove All Containers
```bash=
docker container rm -f  $(docker ps -aq)
```
:::



# Portainer Docker Container
> Installation
```bash=
docker volume create portainer_data
docker run -d -p 8000:8000 -p 9000:9000 -v /var/run/docker.sock:/var/run/docker.sock -v portainer_data:/data --name portainer portainer/portainer
```
> Validation: 
http://docker001.centralus.cloudapp.azure.com:9000

# Other Docker Commands:
:::spoiler
>Login
```bash=
docker login
```
Note: Enter docker hub credentials.
Creds: gaptech/GapTech@123

>Logout
```bash=
docker logout
```

>Logs
```bash=
docker container logs –f <container-id>
```
Note: Running a container without detach flag show logs.

>Info
```bash=
docker info
```
Note: Display all docker information like containers running

>Exec
```bash=
docker container exec -it <container-id> /bin/bash
```
Note: To log into container bash
Note: Enter “exit” command to get out from the container.

:::


# Docker Environmental Variables
:::info
==Syntax:== docker container run -d -p 8080:8080 -e key=value   -e key=value --name image-name gaptech/dockerdemo
:::

## Run RabbitMQ Container with ENV’s
> without ENV variables:
```bash=
docker container run -d -p 8091:15672 --name rabbitmq rabbitmq:3-management
```
:::info
RabbitMQ docker container will be launched with default creds i.e., guest/guest
:::

> Validation:
http://docker001.centralus.cloudapp.azure.com:8091

> with ENV variables:
```bash=
docker container run -d -p 8092:15672 --name rabbitmq-with-env -e RABBITMQ\DEFAULT_USER=gaptech -e RABBITMQ_DEFAULT_PASS=gaptech rabbitmq:3-management
```
:::info
Note: Note: RabbitMQ docker container will be launched with custom creds i.e., gaptech/gaptech
:::

> Validation:
http://docker001.centralus.cloudapp.azure.com:8092

# Docker Volumes
==Syntax:== docker volume create <docker-volume-name> 
:::info
Example: docker volume create volume1
:::

## Jenkins Docker Container
```bash=
docker volume create jenkins_home
docker run -p 8085:8080 -v jenkins_home:/var/jenkins_home --name jenkins jenkins/jenkins:lts
```

**Validation:** Jenkins should be accessible
http://docker001.centralus.cloudapp.azure.com:8085/
> Case1: Remove the Container and Rerun the container
```bash=
docker container rm -f jenkins
docker run -p 8085:8080 -v jenkins_home:/var/jenkins_home --name jenkins jenkins/jenkins:lts
```
**Validation:** Previous Jobs should Exist
http://docker001.centralus.cloudapp.azure.com:8085/
> Case2: Run another container without volume, different port & different container name
```bash=
docker run -p 8086:8080 --name jenkins-without-volume jenkins/jenkins:lts
```
**Validation:** Fresh Jenkins setup in 8086 port
http://docker001.centralus.cloudapp.azure.com:8085/
http://docker001.centralus.cloudapp.azure.com:8086/




# Build Spring Boot App as Docker Image
GitHub - [Spring Boot App Source Code](https://github.com/gaptech/springbootapp.git)
Step1: Open IntelliJ and Import the above project
Step2: Build Jar by executing below command
```bash=
./gradlew clean build
```
Step3: create a file named “Dockerfile” in project root directory
Step4: Write Dockerfile as below

```dockerfile=
FROM openjdk:8  
EXPOSE 8090  
ADD build/libs/springbootapp-0.0.1-SNAPSHOT.jar   springbootapp-0.0.1-SNAPSHOT.jar  
ENTRYPOINT ["java","-jar","/springbootapp-0.0.1-SNAPSHOT.jar"]
```
Step5: Modify parameters like port, jar name as per your configuration. (If needed)
Step6:
> **Way1:** If Docker Installed in Mac/Windows
Open a terminal where Dockerfile folder resides.
Example: ../../<project-name>/
==Syntax:== docker build -t <docker-image-name> <docker-file-path>
> **Example**
```bash=
docker build -t gaptech/springbootapp .
```

## Run Spring Boot Docker Container
``` bash=
docker images
docker container run -d -p 8090:8090 --name myspringbootapp   gaptech/springbootapp
docker ps -a
```
Validation:
```bash=
URL: [http://docker001.centralus.cloudapp.azure.com:8090](http://localhost:8090/)
URL: http://docker001.centralus.cloudapp.azure.com:8090/hellodocker
```

## Pushing Docker Images to Docker Hub
```bash=
Syntax: docker push <image-name>

Example: docker push gaptech/springbootapp
```
Validate Docker Image from Docker Hub
Docker Hub: https://hub.docker.com/
:::info
Docker Hub Credentials:  
username: gaptech  
password: GapTech@123
:::

# Docker Compose
:::spoiler
[Docker Compose File GitLink](https://github.com/gaptech/DockerFiles/blob/master/docker-compose.yaml)
```bash=
docker compose up –d
```
Note:
File name should be docker-compose.yaml (or) docker-compose.yml

Docker Compose - Example
Example DockerCompose: [WordPress Docker Compose File](https://docs.docker.com/compose/wordpress/)
Step1: create a file with docker-compose.yml and paste the above sample compose file
Step2: Execute the below command
```bash=
Command: docker-compose up -d
```
Step3: Validate Docker containers from UI
:::

# Creating a Docker Swarm Cluster
Step1: Get the IP address of VM using below command
```bash=
curl ifconfig.co
```
	   
Step2: 
```bash=
Syntax: docker swarm init --advertise-addr=<leader-node-ip-address>

Example: docker swarm init --advertise-addr=192.168.0.23
```

Step3:
```bash=
docker swarm join --token <swarm-token> <leader-node-ip>:<generated-port>
```
Step4:
```bash=
docker node ls
```

## Deploy Docker Container in Swarm Cluster
```bash=
Syntax: docker service create -d -p <port> --name <name-of-service> --replicas=<count>   <image-name>

Example1: docker service create -d -p 8090:8090 --name swarmtest --replicas=5 gaptech/dockerdemo

Example2: docker service create -d -p 8091:80 --name springbootservice --replicas=5  gaptech/springbootapp
```

```bash=
Command: docker service ls
```

```bash=
Command: docker ps -a
```

```bash=
Syntax: docker service ps <service-name>

Command: docker service ps swarmtest
```

## Scaling Docker Services in Swarm
Scaling UP
```bash=
Syntax: docker service scale <service-name>=<new-count>

Example: docker service scale swarmtest=10
```
Scaling Down
```bash=
Example: docker service scale swarmtest=2
```
Removing
```bash=
Example: docker service rm swarmtest

## Docker Stack File
Docker Stack YAML File
```yaml=
version: '3.3'
services:
  myspringbootapp:
    image: gaptech/springbootapp:latest
    ports:
      \- 8090:8090/tcp
    deploy:
      replicas: 5
      placement:
        constraints:
          \- node.hostname == master
  dockerdemo:
    image: gaptech/dockerdemo:latest
    ports:
      \- 8091:80/tcp
    deploy:
      replicas: 5

```
==Syntax:== docker stack deploy --compose-file <docker-yaml.yml> <stack-name> 
```bash=
docker stack deploy --compose-file docker-compose.yml centric
```
