Skip to main content

Command Palette

Search for a command to run...

Day 17 Task: Docker for DevOps Engineers

Published
3 min read
Day 17 Task: Docker for DevOps Engineers

Docker

Docker is a tool that makes it easy to run applications in containers. Containers are like small packages that hold everything an application needs to run. To create these containers, developers use something called a Dockerfile.

Dockerfile

The Dockerfile uses a Domain Specific Language (DSL) and contains instructions for generating a Docker image. It defines the steps to quickly produce an image.

Docker Image

An artifact with several layers and a lightweight, compact stand-alone executable package that contains all of the components required to run a piece of software, including the code, a runtime, libraries, environment variables, and configuration files is called a Docker Image**.**

Docker Container

A container is a runtime instance of an image. Containers make development and deployment more efficient since they contain all the dependencies and parameters needed for the application it runs completely isolated from the host environment.

Lightbox

Dockerfile commands/Instructions

1. FROM

2. MAINTAINER

3.RUN

4. ADD

5.ENV

6.ENTRYPOINT

7.CMD

8. EXPOSE

9. VOLUME

10. WORKDIR

11. USER

12. ARG

Task 1:

  • Create a Dockerfile for a simple web application (e.g. a Node.js or Python app)

      FROM python:3.6.1-alpine
      RUN pip install --upgrade pip
      RUN pip install flask
      CMD ["python","app.py"]
      copy app.py  /app.py
      EXPOSE 5001
    
  • Build the image using the Dockerfile and run the container

      docker build -f Dockerfile -t python .
    

  •                   sudo docker run -d --name pyprog -p 5001:5000 python
    
  • Verify that the application is working as expected by accessing it in a web browser

  • Push the image to a public or private repository (e.g. Docker Hub )

    Log in to the Docker registry account by entering docker login on your terminal:

  • Image is pushed in to public repository(Docker hub).

  • Docker Compose

    Docker Compose is a tool that assists in defining and sharing multi-container applications. Each of the containers here run in isolation but can interact with each other when required.

    Docker Compose files are very easy to write in a scripting language called YAML, which is an XML-based language that stands for Yet Another Markup Language. Another great thing about Docker Compose is that users can activate all the services (containers) using a single command ,can spin everything up or tear it all down.

    Task 2:

    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.

    Docker Compose Example File

      version: '3.7'
      services:
        akki:
         image: nginx
         ports:
         - "8081:80"
        dhoni:
         image: httpd
         ports:
         - "8080:80"
    
    •         docker-compose build
      

  •       docker-compose up -d
    

  • To stop the services and to delete container use command docker-compose down.

    Task-3

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

  • Inspect the container's running processes and exposed ports using the docker inspect command.

  • Use the docker logs command to view the container's log output.

  • Use the docker stop and docker start commands to stop and start the container.

  • Use the docker rm command to remove the container when you're done.

Docker-Volume

Docker allows you to create something called volumes. Volumes are like separate storage areas that can be accessed by containers. They allow you to store data, like a database, outside the container, so it doesn't get deleted when the container is deleted. You can also mount from the same volume and create more containers having same data.