Changes between Version 2 and Version 3 of dockerdeployment2023
- Timestamp:
- Nov 14, 2023, 10:03:16 AM (12 months ago)
Legend:
- Unmodified
- Added
- Removed
- Modified
-
dockerdeployment2023
v2 v3 19 19 20 20 4. Start and Enable Docker: Ensure Docker starts on boot. 21 21 22 $ sudo systemctl enable docker 22 23 $ sudo systemctl start docker 24 23 25 5. Verify Docker Installation: Check the Docker version to ensure it's installed correctly. 26 24 27 $ docker --version 25 28 26 29 === 6. Deploying a Sample Web Application using Docker === 30 27 31 6.1 Pull a Sample Web Application Image: For this guide, we'll use a simple HTTP server image from Docker Hub. 32 28 33 $ docker pull httpd 34 29 35 6.2 Run the Web Application: Start a container using the httpd image. This will run the web server on port 8080. 36 30 37 $ docker run -d -p 8080:80 --name sample-webapp httpd 38 31 39 6.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 32 41 $ sudo apt-get install lynx 33 42 $ lynx http://localhost:8080 43 34 44 6.4 Stop and Remove the Web Application (Optional): 35 45 When you're done testing the web application, you can stop and remove the container. 46 36 47 $ docker stop sample-webapp 37 48 $ docker rm sample-webapp 38 49 39 50 Extra Ref: 51 40 52 https://linuxhint.com/best_linux_text_based_browsers/ 41 53 https://romanzolotarev.com/ssh.html 42 54 43 55 Basic Docker Commands and Their Usage 56 44 57 • docker --version 45 58 Usage: Displays the Docker version installed. 46 59 Example: docker --version 60 47 61 • docker info 48 62 Usage: Provides detailed information about the Docker installation. 49 63 Example: docker info 64 50 65 • docker pull <image_name> 51 66 Usage: Downloads a Docker image from Docker Hub. 52 67 Example: docker pull nginx 68 53 69 • docker build -t <image_name>:<tag> <path> 54 70 Usage: Builds a Docker image from a Dockerfile located at <path>. 55 71 Example: docker build -t myapp:latest . 72 56 73 • docker images 57 74 Usage: Lists all available Docker images on the system. 58 75 Example: docker images 76 59 77 • docker run <options> <image_name> 60 78 Usage: Creates and starts a container from a Docker image. 61 79 Example: docker run -d -p 80:80 nginx 80 62 81 • docker ps 63 82 Usage: Lists running containers. 64 83 Example: docker ps 84 65 85 • docker ps -a 66 86 Usage: Lists all containers, including stopped ones. 67 87 Example: docker ps -a 88 68 89 • docker stop <container_id/container_name> 69 90 Usage: Stops a running container. 70 91 Example: docker stop my_container 92 71 93 • docker rm <container_id/container_name> 72 94 Usage: Removes a stopped container. 73 95 Example: docker rm my_container 96 74 97 • docker rmi <image_name> 75 98 Usage: Removes a Docker image. 76 99 Example: docker rmi nginx 100 77 101 • docker logs <container_id/container_name> 78 102 Usage: Displays logs from a running or stopped container. … … 80 104 81 105 Troubleshooting Common Docker Container Issues 106 82 107 • Container Fails to Start 108 83 109 Check Logs: Use docker logs <container_name> to check for any error messages. 84 110 Inspect Configuration: Ensure that the Docker run command has the correct parameters, such as port mappings and volume mounts. 111 85 112 • Networking Issues 113 86 114 Check IP Address: Use docker inspect <container_name> | grep IPAddress to find the container's IP address. 87 115 Check Port Bindings: Ensure that the ports inside the container are correctly mapped to the host using the -p option. 88 116 You may use docker port <container_name> to further check the port mapping. 117 89 118 • File or Directory Not Found in Container 119 90 120 Check Volumes: Ensure that directories or files from the host are correctly mounted into the container using the -v option. 91 121 You may use docker volume ls to list all volumes mapped and docker volume inspect <volume_name> to inspect a selected volume. 92 122 Inspect Image: Use docker image inspect <image_name> to see the image's layers and ensure the required files are present. 123 93 124 • Container Performance Issues 125 94 126 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. 95 127 Limit Resources: When running a container, you can use flags like --cpus and --memory to limit its resources. 96 128 You can use docker top <container_name> to see some stats. 129 97 130 • Image-Related Issues 131 98 132 Pull Latest Image: Ensure you have the latest version of the image using docker pull <image_name>. 99 133 Check Dockerfile: If you're building your own image, ensure that the Dockerfile has the correct instructions. 134 100 135 • Permission Issues 136 101 137 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. 102 138 Use --user Flag: When running a container, you can specify which user the container should run as using the --user flag. 103 139 104 140 105 Part 2: 141 === Part 2: === 142 106 143 What is a Dockerfile? 144 107 145 A 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 109 149 A 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 110 151 Key Dockerfile Instructions 152 111 153 FROM: Specifies the base image to start from. It's usually an OS or another application. 112 154 Example: FROM ubuntu:20.04 155 113 156 LABEL: Adds metadata to the image, like maintainer information. 114 157 Example: LABEL maintainer="name@example.com" 158 115 159 RUN: Executes commands in a new layer on top of the current image and commits the result. 116 160 Example: RUN apt-get update && apt-get install -y nginx 161 117 162 CMD: Provides defaults for the executing container. There can only be one CMD instruction in a Dockerfile. 118 163 Example: CMD ["nginx", "-g", "daemon off;"] 164 119 165 ENTRYPOINT: Configures the container to run as an executable. It's often used in combination with CMD. 120 166 Example: ENTRYPOINT ["nginx"] 167 121 168 COPY: Copies files or directories from the host machine to the container. 122 169 Example: COPY ./webapp /var/www/webapp 170 123 171 ADD: Similar to COPY, but can also handle URLs and tarball extraction. 124 172 Example: ADD https://example.com/app.tar.gz /app/ 173 125 174 WORKDIR: Sets the working directory for any subsequent RUN, CMD, ENTRYPOINT, COPY, and ADD instructions. 126 175 Example: WORKDIR /app 176 127 177 EXPOSE: Informs Docker that the container listens on the specified network port at runtime. 128 178 Example: EXPOSE 80 179 129 180 ENV: Sets environment variables. 130 181 Example: ENV MY_VARIABLE=value 182 131 183 VOLUME: Creates a mount point for external storage or other containers. 132 184 Example: VOLUME /data 133 185 134 186 Let's create a Dockerfile for a basic web server using Nginx: 187 135 188 First, create a folder called my-webserver and go inside it cd my-webserver 136 189 Then create another folder inside that called website and a file called index.html within the folder website with any content of your choice. … … 155 208 156 209 Building an Image from a Dockerfile 210 157 211 To build a Docker image from your Dockerfile, navigate to the directory containing the Dockerfile and run: 158 212 docker build -t my-webserver:latest . … … 160 214 161 215 Best Practices 216 162 217 • 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. 163 218 • Use .dockerignore: Just like .gitignore, you can use .dockerignore to exclude files that aren't needed in the container. … … 165 220 • Clean Up: Remove temporary files and caches to reduce image size. 166 221 167 Part 3: 222 === Part 3: === 223 168 224 What is Docker Compose? 225 169 226 Docker 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 170 228 Key Concepts 229 171 230 Services: Each container started by Docker Compose is a service. Services are defined in the docker-compose.yml file. 172 231 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. … … 174 233 175 234 Basic docker-compose Commands 235 176 236 • docker-compose up: Starts up the services defined in the docker-compose.yml file. 177 237 • docker-compose down: Stops and removes all the containers defined in the docker-compose.yml file. … … 180 240 181 241 Deploying WordPress with Docker Compose 242 182 243 Let's deploy a WordPress application using two containers: one for WordPress and another for the MySQL database. 183 244 Create a docker-compose.yml file: … … 231 292 232 293 233 Part 4: 294 ==== Part 4: ==== 295 234 296 Deploy 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.