Part 1:
'Installing Docker on Ubuntu Server
'
1. Update Your System: Ensure your system package database is up-to-date.
`sudo apt update`
`sudo apt upgrade`
2. Install Docker: Install Docker using the convenience script provided by Docker.
`curl -fsSL https://get.docker.com -o get-docker.sh`
`sudo sh get-docker.sh`
3. Add User to Docker Group: to run Docker commands without sudo, add your user to the docker group.
`sudo usermod -aG docker ${USER}`
Log out and log back in for the group changes to take effect.
4. Start and Enable Docker: Ensure Docker starts on boot.
` sudo systemctl enable docker`
` sudo systemctl start docker`
5. Verify Docker Installation: Check the Docker version to ensure it's installed correctly.
` docker –version`
6. Deploying a Sample Web Application using Docker
6.1 Pull a Sample Web Application Image: For this guide, we'll use a simple HTTP server image from Docker Hub.
` docker pull httpd`
6.2 Run the Web Application: Start a container using the httpd image. This will run the web server on port 8080.
` docker run -d -p 8080:80 –name sample-webapp httpd`
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.)
`sudo apt-get install lynx`
`lynx http://localhost:8080`
6.4 Stop and Remove the Web Application (Optional): When you're done testing the web application, you can stop and remove the container.
` docker stop sample-webapp`
` docker rm sample-webapp`
Extra Ref:
Basic Docker Commands and Their Usage
• ` docker –version`
Usage: Displays the Docker version installed. Example: `docker --version`
• `docker info`
Usage: Provides detailed information about the Docker installation. Example:` docker info`
• `docker pull <image_name>`
Usage: Downloads a Docker image from Docker Hub. Example: `docker pull nginx`
• `docker build -t <image_name>:<tag> <path>`
Usage: Builds a Docker image from a Dockerfile located at <path>. Example: docker build -t myapp:latest .
• ` docker images`
Usage: Lists all available Docker images on the system. Example:` docker images`
• `docker run <options> <image_name>`
Usage: Creates and starts a container from a Docker image. Example: `docker run -d -p 80:80 nginx`
• `docker ps`
Usage: Lists running containers. Example:` docker ps`
• ` docker ps -a`
Usage: Lists all containers, including stopped ones. Example: `docker ps -a`
• `docker stop <container_id/container_name>`
Usage: Stops a running container. Example: `docker stop my_container`
• `docker rm <container_id/container_name>`
Usage: Removes a stopped container. Example: `docker rm my_container`
• ` docker rmi <image_name>`
Usage: Removes a Docker image. Example: `docker rmi nginx`
• `docker logs <container_id/container_name>`
Usage: Displays logs from a running or stopped container. Example: `docker logs my_container`
Troubleshooting Common Docker Container Issues
• Container Fails to Start
Check Logs: Use `docker logs <container_name>` to check for any error messages. Inspect Configuration: Ensure that the Docker run command has the correct parameters, such as port mappings and volume mounts.
• Networking Issues
Check IP Address: Use `docker inspect <container_name> | grep IPAddress` to find the container's IP address. Check Port Bindings: Ensure that the ports inside the container are correctly mapped to the host using the -p option. You may use `docker port <container_name>` to further check the port mapping.
• File or Directory Not Found in Container
Check Volumes: Ensure that directories or files from the host are correctly mounted into the container using the -v option. You may use `docker volume ls` to list all volumes mapped and `docker volume inspect <volume_name>` to inspect a selected volume. Inspect Image: Use `docker image inspect <image_name>` to see the image's layers and ensure the required files are present.
• Container Performance Issues
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. Limit Resources: When running a container, you can use flags like `--cpus` and `--memory` to limit its resources. You can use `docker top <container_name>` to see some stats.
• Image-Related Issues
Pull Latest Image: Ensure you have the latest version of the image using `docker pull <image_name>`. Check Dockerfile: If you're building your own image, ensure that the Dockerfile has the correct instructions.
• Permission Issues
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. Use `--user` Flag: When running a container, you can specify which user the container should run as using the --user flag.