Changes between Initial Version and Version 1 of dockerdeployment2023


Ignore:
Timestamp:
Nov 13, 2023, 6:56:57 AM (12 months ago)
Author:
deepthi
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • dockerdeployment2023

    v1 v1  
     1Part 1:
     2Installing Docker on Ubuntu Server
     31. Update Your System: Ensure your system package database is up-to-date.
     4$ sudo apt update
     5$ sudo apt upgrade
     62. Install Docker: Install Docker using the convenience script provided by Docker.
     7$ curl -fsSL https://get.docker.com -o get-docker.sh
     8$ sudo sh get-docker.sh
     93. Add User to Docker Group (Optional): If you want to run Docker commands without sudo, add your user to the docker group.
     10$ sudo usermod -aG docker ${USER}
     11Log out and log back in for the group changes to take effect.
     124. Start and Enable Docker: Ensure Docker starts on boot.
     13$ sudo systemctl enable docker
     14$ sudo systemctl start docker
     155. Verify Docker Installation: Check the Docker version to ensure it's installed correctly.
     16$ docker --version
     176. Deploying a Sample Web Application using Docker
     186.1 Pull a Sample Web Application Image: For this guide, we'll use a simple HTTP server image from Docker Hub.
     19$ docker pull httpd
     206.2 Run the Web Application: Start a container using the httpd image. This will run the web server on port 8080.
     21$ docker run -d -p 8080:80 --name sample-webapp httpd
     226.3 Access the Web Application: If you're accessing the server locally, open a web browser and navigate to: (Since you are connected via SSH lets install a text-based web browser lynx.)
     23$ sudo apt-get install lynx
     24$ lynx http://localhost:8080
     256.4 Stop and Remove the Web Application (Optional):
     26When you're done testing the web application, you can stop and remove the container.
     27$ docker stop sample-webapp
     28$ docker rm sample-webapp
     29 
     30Extra Ref:
     31https://linuxhint.com/best_linux_text_based_browsers/
     32https://romanzolotarev.com/ssh.html
     33 
     34Basic Docker Commands and Their Usage
     35•           docker --version
     36        Usage: Displays the Docker version installed.
     37        Example: docker --version
     38•           docker info
     39        Usage: Provides detailed information about the Docker installation.
     40        Example: docker info
     41•           docker pull <image_name>
     42        Usage: Downloads a Docker image from Docker Hub.
     43        Example: docker pull nginx
     44•           docker build -t <image_name>:<tag> <path>
     45        Usage: Builds a Docker image from a Dockerfile located at <path>.
     46        Example: docker build -t myapp:latest .
     47•           docker images
     48        Usage: Lists all available Docker images on the system.
     49        Example: docker images
     50•           docker run <options> <image_name>
     51        Usage: Creates and starts a container from a Docker image.
     52        Example: docker run -d -p 80:80 nginx
     53•           docker ps
     54        Usage: Lists running containers.
     55        Example: docker ps
     56•           docker ps -a
     57        Usage: Lists all containers, including stopped ones.
     58        Example: docker ps -a
     59•           docker stop <container_id/container_name>
     60        Usage: Stops a running container.
     61        Example: docker stop my_container
     62•           docker rm <container_id/container_name>
     63        Usage: Removes a stopped container.
     64        Example: docker rm my_container
     65•           docker rmi <image_name>
     66        Usage: Removes a Docker image.
     67        Example: docker rmi nginx
     68•           docker logs <container_id/container_name>
     69        Usage: Displays logs from a running or stopped container.
     70        Example: docker logs my_container
     71 
     72Troubleshooting Common Docker Container Issues
     73•           Container Fails to Start
     74        Check Logs: Use docker logs <container_name> to check for any error messages.
     75        Inspect Configuration: Ensure that the Docker run command has the correct parameters, such as port mappings and volume mounts.
     76•           Networking Issues
     77        Check IP Address: Use docker inspect <container_name> | grep IPAddress to find the container's IP address.
     78        Check Port Bindings: Ensure that the ports inside the container are correctly mapped to the host using the -p option.
     79        You may use docker port <container_name> to further check the port mapping.
     80•           File or Directory Not Found in Container
     81        Check Volumes: Ensure that directories or files from the host are correctly mounted into the container using the -v option.
     82        You may use docker volume ls to list all volumes mapped and docker volume inspect  <volume_name> to inspect a selected volume.
     83        Inspect Image: Use docker image inspect <image_name> to see the image's layers and ensure the required files are present.
     84•           Container Performance Issues
     85        Check Resources: Containers might face performance issues if they're not allocated enough resources. Use docker stats to check the resource usage of running containers.
     86        Limit Resources: When running a container, you can use flags like --cpus and --memory to limit its resources.
     87        You can use docker top <container_name> to see some stats.
     88•           Image-Related Issues
     89        Pull Latest Image: Ensure you have the latest version of the image using docker pull <image_name>.
     90        Check Dockerfile: If you're building your own image, ensure that the Dockerfile has the correct instructions.
     91•           Permission Issues
     92        User Mappings: If a containerized application can't access certain files, it might be a user permission issue. Ensure that the user inside the container has the necessary permissions.
     93        Use --user Flag: When running a container, you can specify which user the container should run as using the --user flag.
     94 
     95 
     96Part 2:
     97What is a Dockerfile?
     98A Dockerfile is a script containing a set of instructions used by Docker to automate the process of building a new container image. It defines the environment inside the container, installs necessary software, sets up commands, and more.
     99Basic Structure of a Dockerfile
     100A Dockerfile consists of a series of instructions and arguments. Each instruction is an operation used to build the image, like installing a software package or copying files. The instruction is written in uppercase, followed by its arguments.
     101Key Dockerfile Instructions
     102    FROM: Specifies the base image to start from. It's usually an OS or another application.
     103        Example: FROM ubuntu:20.04
     104    LABEL: Adds metadata to the image, like maintainer information.
     105        Example: LABEL maintainer="name@example.com"
     106    RUN: Executes commands in a new layer on top of the current image and commits the result.
     107        Example: RUN apt-get update && apt-get install -y nginx
     108    CMD: Provides defaults for the executing container. There can only be one CMD instruction in a Dockerfile.
     109        Example: CMD ["nginx", "-g", "daemon off;"]
     110    ENTRYPOINT: Configures the container to run as an executable. It's often used in combination with CMD.
     111        Example: ENTRYPOINT ["nginx"]
     112    COPY: Copies files or directories from the host machine to the container.
     113        Example: COPY ./webapp /var/www/webapp
     114    ADD: Similar to COPY, but can also handle URLs and tarball extraction.
     115        Example: ADD https://example.com/app.tar.gz /app/
     116    WORKDIR: Sets the working directory for any subsequent RUN, CMD, ENTRYPOINT, COPY, and ADD instructions.
     117        Example: WORKDIR /app
     118    EXPOSE: Informs Docker that the container listens on the specified network port at runtime.
     119        Example: EXPOSE 80
     120    ENV: Sets environment variables.
     121        Example: ENV MY_VARIABLE=value
     122    VOLUME: Creates a mount point for external storage or other containers.
     123        Example: VOLUME /data
     124 
     125Let's create a Dockerfile for a basic web server using Nginx:
     126First, create a folder called my-webserver and go inside it cd my-webserver
     127Then create another folder inside that called website and a file called index.html within the folder website with any content of your choice.
     128 
     129Create a file dockerfile with the following content within the my-webserver folder.
     130 
     131# Use the official Nginx image as a base
     132FROM nginx:latest
     133
     134# Set the maintainer label
     135LABEL maintainer="name@example.com"
     136
     137# Copy static website files to the Nginx web directory
     138COPY ./website /usr/share/nginx/html
     139
     140# Expose port 80 for the web server
     141EXPOSE 80
     142
     143# Default command to run Nginx in the foreground
     144CMD ["nginx", "-g", "daemon off;"]
     145
     146
     147Building an Image from a Dockerfile
     148To build a Docker image from your Dockerfile, navigate to the directory containing the Dockerfile and run:
     149docker build -t my-webserver:latest .
     150This command tells Docker to build an image using the Dockerfile in the current directory (.) and tag it as my-webserver:latest.
     151 
     152Best Practices
     153•       Minimize Layers: Try to reduce the number of layers in your image to make it lightweight. For instance, chain commands using && in a single RUN instruction.
     154•       Use .dockerignore: Just like .gitignore, you can use .dockerignore to exclude files that aren't needed in the container.
     155•       Avoid Installing Unnecessary Packages: Only install the packages that are necessary to run your application.
     156•       Clean Up: Remove temporary files and caches to reduce image size.
     157 
     158Part 3:
     159What is Docker Compose?
     160Docker Compose is a tool for defining and running multi-container Docker applications. With Compose, you can define a multi-container application in a single file, then spin up your application with a single command (docker-compose up).
     161Key Concepts
     162    Services: Each container started by Docker Compose is a service. Services are defined in the docker-compose.yml file.
     163    Networks: By default, Docker Compose sets up a single network for your application. Each container for a service joins the default network and is discoverable via a hostname identical to the container name.
     164    Volumes: Volumes can be used to share files between the host and container or between containers.
     165 
     166Basic docker-compose Commands
     167•           docker-compose up: Starts up the services defined in the docker-compose.yml file.
     168•           docker-compose down: Stops and removes all the containers defined in the docker-compose.yml file.
     169•           docker-compose ps: Lists the services and their current state (running/stopped).
     170•          docker-compose logs: Shows the logs from the services.
     171 
     172Deploying WordPress with Docker Compose
     173Let's deploy a WordPress application using two containers: one for WordPress and another for the MySQL database.
     174Create a docker-compose.yml file:
     175version: '3'
     176
     177services:
     178  # Database Service
     179  db:
     180    image: mysql:5.7
     181    volumes:
     182      - db_data:/var/lib/mysql
     183    environment:
     184      MYSQL_ROOT_PASSWORD: somewordpress
     185      MYSQL_DATABASE: wordpress
     186      MYSQL_USER: wordpress
     187      MYSQL_PASSWORD: wordpress
     188
     189  # WordPress Service
     190  wordpress:
     191    depends_on:
     192      - db
     193    image: wordpress:latest
     194    ports:
     195      - "8080:80"
     196    environment:
     197      WORDPRESS_DB_HOST: db:3306
     198      WORDPRESS_DB_USER: wordpress
     199      WORDPRESS_DB_PASSWORD: wordpress
     200      WORDPRESS_DB_NAME: wordpress
     201    volumes:
     202      - wordpress_data:/var/www/html
     203
     204volumes:
     205    db_data: {}
     206    wordpress_data: {}
     207 
     208Start the WordPress and Database Containers: Navigate to the directory containing the docker-compose.yml file and run:
     209
     210
     211docker-compose up -d
     212This command will start the services in detached mode. Once the services are up, you can access the WordPress site by navigating to http://<Floating_IP>:8080 from your browser.
     213 
     214Stopping the Services: To stop the services, navigate to the same directory and run:
     215 
     216docker-compose down
     217 
     218Best Practices
     219•       Explicit Service Names: Give your services explicit names to make it clear what each service does.
     220•       Environment Variables: Use environment variables for sensitive information and configurations.
     221•       Service Dependencies: Use the depends_on option to ensure services start in the correct order.
     222 
     223 
     224Part 4:
     225Deploy any web app as per your wish and showcase its usage of it. You need to use more than one docker container eg: you can use three containers, one to run a web app and the others to run a database and other data storage respectively. You may use the docker hub to get any existing containers. What we evaluate is your ability to deploy the containers and bringing up a working web app.