Changes between Version 2 and Version 3 of dockerdeployment2023


Ignore:
Timestamp:
Nov 14, 2023, 10:03:16 AM (12 months ago)
Author:
deepthi
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • dockerdeployment2023

    v2 v3  
    1919
    20204. Start and Enable Docker: Ensure Docker starts on boot.
     21
    2122$ sudo systemctl enable docker
    2223$ sudo systemctl start docker
     24
    23255. Verify Docker Installation: Check the Docker version to ensure it's installed correctly.
     26
    2427$ docker --version
    2528
    2629=== 6. Deploying a Sample Web Application using Docker ===
     30
    27316.1 Pull a Sample Web Application Image: For this guide, we'll use a simple HTTP server image from Docker Hub.
     32
    2833$ docker pull httpd
     34
    29356.2 Run the Web Application: Start a container using the httpd image. This will run the web server on port 8080.
     36
    3037$ docker run -d -p 8080:80 --name sample-webapp httpd
     38
    31396.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.)
     40
    3241$ sudo apt-get install lynx
    3342$ lynx http://localhost:8080
     43
    34446.4 Stop and Remove the Web Application (Optional):
    3545When you're done testing the web application, you can stop and remove the container.
     46
    3647$ docker stop sample-webapp
    3748$ docker rm sample-webapp
    3849 
    3950Extra Ref:
     51
    4052https://linuxhint.com/best_linux_text_based_browsers/
    4153https://romanzolotarev.com/ssh.html
    4254 
    4355Basic Docker Commands and Their Usage
     56
    4457•           docker --version
    4558        Usage: Displays the Docker version installed.
    4659        Example: docker --version
     60
    4761•           docker info
    4862        Usage: Provides detailed information about the Docker installation.
    4963        Example: docker info
     64
    5065•           docker pull <image_name>
    5166        Usage: Downloads a Docker image from Docker Hub.
    5267        Example: docker pull nginx
     68
    5369•           docker build -t <image_name>:<tag> <path>
    5470        Usage: Builds a Docker image from a Dockerfile located at <path>.
    5571        Example: docker build -t myapp:latest .
     72
    5673•           docker images
    5774        Usage: Lists all available Docker images on the system.
    5875        Example: docker images
     76
    5977•           docker run <options> <image_name>
    6078        Usage: Creates and starts a container from a Docker image.
    6179        Example: docker run -d -p 80:80 nginx
     80
    6281•           docker ps
    6382        Usage: Lists running containers.
    6483        Example: docker ps
     84
    6585•           docker ps -a
    6686        Usage: Lists all containers, including stopped ones.
    6787        Example: docker ps -a
     88
    6889•           docker stop <container_id/container_name>
    6990        Usage: Stops a running container.
    7091        Example: docker stop my_container
     92
    7193•           docker rm <container_id/container_name>
    7294        Usage: Removes a stopped container.
    7395        Example: docker rm my_container
     96
    7497•           docker rmi <image_name>
    7598        Usage: Removes a Docker image.
    7699        Example: docker rmi nginx
     100
    77101•           docker logs <container_id/container_name>
    78102        Usage: Displays logs from a running or stopped container.
     
    80104 
    81105Troubleshooting Common Docker Container Issues
     106
    82107•           Container Fails to Start
     108
    83109        Check Logs: Use docker logs <container_name> to check for any error messages.
    84110        Inspect Configuration: Ensure that the Docker run command has the correct parameters, such as port mappings and volume mounts.
     111
    85112•           Networking Issues
     113
    86114        Check IP Address: Use docker inspect <container_name> | grep IPAddress to find the container's IP address.
    87115        Check Port Bindings: Ensure that the ports inside the container are correctly mapped to the host using the -p option.
    88116        You may use docker port <container_name> to further check the port mapping.
     117
    89118•           File or Directory Not Found in Container
     119
    90120        Check Volumes: Ensure that directories or files from the host are correctly mounted into the container using the -v option.
    91121        You may use docker volume ls to list all volumes mapped and docker volume inspect  <volume_name> to inspect a selected volume.
    92122        Inspect Image: Use docker image inspect <image_name> to see the image's layers and ensure the required files are present.
     123
    93124•           Container Performance Issues
     125
    94126        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.
    95127        Limit Resources: When running a container, you can use flags like --cpus and --memory to limit its resources.
    96128        You can use docker top <container_name> to see some stats.
     129
    97130•           Image-Related Issues
     131
    98132        Pull Latest Image: Ensure you have the latest version of the image using docker pull <image_name>.
    99133        Check Dockerfile: If you're building your own image, ensure that the Dockerfile has the correct instructions.
     134
    100135•           Permission Issues
     136
    101137        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.
    102138        Use --user Flag: When running a container, you can specify which user the container should run as using the --user flag.
    103139 
    104140 
    105 Part 2:
     141=== Part 2: ===
     142
    106143What is a Dockerfile?
     144
    107145A 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.
    108 Basic Structure of a Dockerfile
     146
     147==== Basic Structure of a Dockerfile ====
     148
    109149A 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.
     150
    110151Key Dockerfile Instructions
     152
    111153    FROM: Specifies the base image to start from. It's usually an OS or another application.
    112154        Example: FROM ubuntu:20.04
     155
    113156    LABEL: Adds metadata to the image, like maintainer information.
    114157        Example: LABEL maintainer="name@example.com"
     158
    115159    RUN: Executes commands in a new layer on top of the current image and commits the result.
    116160        Example: RUN apt-get update && apt-get install -y nginx
     161
    117162    CMD: Provides defaults for the executing container. There can only be one CMD instruction in a Dockerfile.
    118163        Example: CMD ["nginx", "-g", "daemon off;"]
     164
    119165    ENTRYPOINT: Configures the container to run as an executable. It's often used in combination with CMD.
    120166        Example: ENTRYPOINT ["nginx"]
     167
    121168    COPY: Copies files or directories from the host machine to the container.
    122169        Example: COPY ./webapp /var/www/webapp
     170
    123171    ADD: Similar to COPY, but can also handle URLs and tarball extraction.
    124172        Example: ADD https://example.com/app.tar.gz /app/
     173
    125174    WORKDIR: Sets the working directory for any subsequent RUN, CMD, ENTRYPOINT, COPY, and ADD instructions.
    126175        Example: WORKDIR /app
     176
    127177    EXPOSE: Informs Docker that the container listens on the specified network port at runtime.
    128178        Example: EXPOSE 80
     179
    129180    ENV: Sets environment variables.
    130181        Example: ENV MY_VARIABLE=value
     182
    131183    VOLUME: Creates a mount point for external storage or other containers.
    132184        Example: VOLUME /data
    133185 
    134186Let's create a Dockerfile for a basic web server using Nginx:
     187
    135188First, create a folder called my-webserver and go inside it cd my-webserver
    136189Then create another folder inside that called website and a file called index.html within the folder website with any content of your choice.
     
    155208
    156209Building an Image from a Dockerfile
     210
    157211To build a Docker image from your Dockerfile, navigate to the directory containing the Dockerfile and run:
    158212docker build -t my-webserver:latest .
     
    160214 
    161215Best Practices
     216
    162217•       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.
    163218•       Use .dockerignore: Just like .gitignore, you can use .dockerignore to exclude files that aren't needed in the container.
     
    165220•       Clean Up: Remove temporary files and caches to reduce image size.
    166221 
    167 Part 3:
     222=== Part 3: ===
     223
    168224What is Docker Compose?
     225
    169226Docker 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).
     227
    170228Key Concepts
     229
    171230    Services: Each container started by Docker Compose is a service. Services are defined in the docker-compose.yml file.
    172231    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.
     
    174233 
    175234Basic docker-compose Commands
     235
    176236•           docker-compose up: Starts up the services defined in the docker-compose.yml file.
    177237•           docker-compose down: Stops and removes all the containers defined in the docker-compose.yml file.
     
    180240 
    181241Deploying WordPress with Docker Compose
     242
    182243Let's deploy a WordPress application using two containers: one for WordPress and another for the MySQL database.
    183244Create a docker-compose.yml file:
     
    231292 
    232293 
    233 Part 4:
     294==== Part 4: ====
     295
    234296Deploy 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.