# Docker for DevOps Engineers

## 📌 Docker Compose

Docker Compose is a tool that was developed to help define and share multi-container applications.

With Compose, we can create a YAML file to define the services and with a single command, can spin everything up or tear it all down.

## 📌 What is YAML?

YAML is a data serialization language that is often used for writing configuration files. Depending on whom you ask, YAML stands for yet another markup language or YAML ain’t markup language (a recursive acronym), which emphasizes that YAML is for data, not documents.

YAML is a popular programming language because it is human-readable and easy to understand.

YAML files use a .yml or .yaml extension.

## 📌 Task-1 Learn how to use the docker-compose.yml file, to set up the environment, configure the services and links between different containers, and also to use environment variables in the docker-compose.yml file.

```basic
==================== docker-compose.yml ==============================
version : "3.3"   
services:
  web:
    image: nginx:latest
    ports:
      - "80:80"
  db:
    image: mysql
    ports:
      - "3306:3306"
    environment:
      - "MYSQL_ROOT_PASSWORD=test@123"
==================================================================
version - docker-compose version specified
services - services that we want to create 
image - image for container
port - port number for service
environment - environment variables of container
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1691661715047/9dc9a794-9c2d-4b65-aa7c-32adbde85cad.png align="center")

```basic
## Run docker-compose 

docker-compose up -d 
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1691661795410/7bd4d231-5c7e-47fc-91b1-a151bb90a67b.png align="center")

```basic
## To check the containers

docker-compose ps -a
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1691661839312/37742642-0be3-4ddb-a002-c7b20b73aa46.png align="center")

```basic
## Verify the nginx service in browser   - http://13.126.211.173:80
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1691662007896/86a4ae7d-8d3f-43a5-93b1-e29ae46515fc.png align="center")

## 📌 Task -2 Pull a pre-existing Docker image from a public repository (e.g. Docker Hub) and run it on your local machine.

```basic
## Pulled jenkins docker image from dockerhub
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1691662453795/706baca4-2d8d-4150-9d78-64ab8aee0147.png align="center")

```basic
## Run the container and verify jenkins dashboard in browser
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1691662590657/97c14189-2d30-4c05-9e61-12f2af43e4f8.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1691662679607/3ea1526f-af5d-45ae-af8d-42fcba406aec.png align="center")

```basic
## Inspect the container's running processes and exposed ports using 
the docker inspect command.

docker inspect <container_name>
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1691662838468/d01ed6a5-930b-4b77-9dce-a5e2888561c4.png align="center")

```basic
## Use the docker logs command to view the container's log output.

docker logs <container_name>
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1691662905274/af45fdcb-f217-4302-93cf-4646ee0bd332.png align="center")

```basic
## Use the docker stop and docker start commands to stop and start the container.

docker stop <container_name>
docker start <container_name>
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1691663079067/021476d9-5cde-400f-a26f-2c6a0832290d.png align="center")

```basic
## Use the docker rm command to remove the container when you're done.

docker rm <container_name>
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1691663145724/34cda92c-3f47-4e09-af21-804e5ca33ec4.png align="center")
