Docker Lab
Docker Lab
Here's a guide on installing Docker and using some common Docker commands. I'll
also outline the process of creating a Docker image and pushing it to Docker Hub.
Windows:
1. Download the Docker Desktop installer from the official Docker website:
https://www.docker.com/products/docker-desktop
2. Run the installer and follow the on-screen instructions. Ensure that the "Use WSL 2
instead of Hyper-V" option is selected if your system supports it.
2. Verify Installation
Bash
docker --version
o Example: docker run -it ubuntu /bin/bash (-it for interactive mode)
docker ps: Lists running containers.
To create a Docker image of your application, you'll need a Dockerfile. This file
contains instructions for building the image.
Here's an example Dockerfile for a simple Python application:
Dockerfile
# Use a base image
FROM python:3.9-slim-buster
# Install dependencies
RUN pip install -r requirements.txt
To build the image, navigate to the directory containing the Dockerfile in your terminal
and run:
Bash
docker build -t <your_dockerhub_username>/<image_name> .
1. Create a Docker Hub account: If you don't have one, sign up at https://hub.docker.com/
2. Log in to Docker Hub:
<!-- end list -->
Bash
docker login
Bash
docker push <your_dockerhub_username>/<image_name>
Note: This is a basic guide. For more advanced configurations and best practices, refer
to the official Docker documentation.
Example
Let's create a simple Python "Hello, world!" application and Dockerize it, then push it to
Docker Hub.
app = Flask(__name__)
@app.route("/")
def hello():
return "Hello, Dockerized World!"
if __name__ == "__main__":
app.run(debug=True, host='0.0.0.0', port=5000)
This uses Flask, a simple web framework, to create a web server that returns "Hello,
Dockerized World!".
username and the name hello-docker. This is crucial for pushing to Docker Hub.
.: Specifies the build context (the current directory).
Bash
docker run -p 5000:5000 your_dockerhub_username/hello-docker
-p 5000:5000: Maps port 5000 on your host machine to port 5000 in the container. This
Bash
docker login
This step-by-step example should make the process much clearer. You now have a
working Docker image of a simple web application hosted on Docker Hub. You can now
use this image to deploy your application on any system with Docker installed by simply
running docker pull your_dockerhub_username/hello-docker and docker run -p
5000:5000 your_dockerhub_username/hello-docker.