How to Set Up Docker on Windows and Linux: A Step-by-Step Guide
Docker is a powerful tool that allows developers to create, deploy, and run applications in containers. Containers enable consistent environments, making it easier to develop and deploy applications across different systems. In this guide, we will walk through the steps to set up Docker on Windows and Linux, run it properly, and distribute Docker images.
Setting Up Docker on Windows
Step 1: Install Docker Desktop
- Download Docker Desktop
- Go to the Docker Desktop for Windows page.
- Download the Docker Desktop installer.
- Run the Installer
- Double-click the downloaded installer.
- Follow the installation instructions.
- Start Docker Desktop
- Once installed, launch Docker Desktop.
- Docker Desktop will require you to enable the WSL 2 feature if it’s not already enabled. Follow the prompts to enable it.
- Verify the Installation
- Open a command prompt and run:
docker --version
- You should see the Docker version information, indicating that Docker is installed correctly.
- Open a command prompt and run:
Step 2: Running a Docker Container
- Pull a Docker Image
docker pull hello-world
This command pulls the
hello-world
image from Docker Hub. - Run the Docker Container
docker run hello-world
You should see a message from the Docker container, indicating that Docker is working correctly.
Setting Up Docker on Linux
Step 1: Install Docker
- Update Your Package Index
sudo apt-get update
- Install Required Packages
sudo apt-get install apt-transport-https ca-certificates curl software-properties-common
- Add Docker’s GPG Key
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
- Set Up the Stable Repository
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
- Install Docker Engine
sudo apt-get update sudo apt-get install docker-ce
- Verify the Installation
sudo docker --version
You should see the Docker version information.
Step 2: Running a Docker Container
- Pull a Docker Image
sudo docker pull hello-world
- Run the Docker Container
sudo docker run hello-world
You should see a message from the Docker container, indicating that Docker is installed and running correctly.
Distributing Docker Images
Step 1: Create a Dockerfile
- Create a New Directory for Your Project
mkdir mydockerapp cd mydockerapp
- Create a DockerfileIn the
mydockerapp
directory, create a file namedDockerfile
and add the following content:FROM python:3.8-slim WORKDIR /app COPY . /app RUN pip install --no-cache-dir -r requirements.txt EXPOSE 80 ENV NAME World CMD ["python", "app.py"]
Step 2: Build and Run Your Docker Image
- Build the Docker Image
docker build -t mydockerapp .
- Run the Docker Container
docker run -p 4000:80 mydockerapp
Step 3: Push Your Image to Docker Hub
- Log In to Docker Hub
- If you don’t have an account, create one at Docker Hub.
- Log in from your terminal:
docker login
- Tag Your Image
docker tag mydockerapp yourdockerhubusername/mydockerapp
- Push Your Image
docker push yourdockerhubusername/mydockerapp
- Pull and Run Your Image on Another Machine
- On another machine, pull your image:
docker pull yourdockerhubusername/mydockerapp
- Run the Docker container:
docker run -p 4000:80 yourdockerhubusername/mydockerapp
- On another machine, pull your image:
Conclusion
By following this guide, you have successfully set up Docker on both Windows and Linux, run Docker containers, and distributed Docker images. Docker simplifies the development and deployment process, ensuring consistent environments across different systems. Happy containerizing!