Using Docker
Introduction to Docker
Docker is a platform designed to make it easier to create, deploy, and run applications by using containers. Containers allow a developer to package up an application with all parts it needs, such as libraries and other dependencies, and ship it all out as one package.
Installing Docker
Before you can use Docker, you need to install it on your system. Here’s a step-by-step guide for installing Docker on a Linux system.
sudo apt-get update
sudo apt-get install -y apt-transport-https ca-certificates curl software-properties-common
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
sudo apt-get update
sudo apt-get install -y docker-ce
After the installation, you can verify that Docker is installed correctly by running the following command:
sudo systemctl status docker
● docker.service - Docker Application Container Engine
Loaded: loaded (/lib/systemd/system/docker.service; enabled; vendor preset: enabled)
Active: active (running) since Wed 2021-09-01 12:34:56 UTC; 2min 5s ago
Running Your First Docker Container
Once Docker is installed and running, you can start your first container. We'll use the official Docker image for "hello-world" to verify that Docker is working correctly.
sudo docker run hello-world
Hello from Docker!
This message shows that your installation appears to be working correctly.
Creating a Dockerfile
A Dockerfile is a text document that contains all the commands a user could call on the command line to assemble an image. Using docker build, users can create an automated build that executes several command-line instructions in succession.
Here is an example of a simple Dockerfile:
# Use an official Python runtime as a parent image FROM python:3.8-slim # Set the working directory in the container WORKDIR /app # Copy the current directory contents into the container at /app COPY . /app # Install any needed packages specified in requirements.txt RUN pip install --no-cache-dir -r requirements.txt # Make port 80 available to the world outside this container EXPOSE 80 # Define environment variable ENV NAME World # Run app.py when the container launches CMD ["python", "app.py"]
Building and Running Your Docker Image
Once you have created a Dockerfile, you can build an image from it:
sudo docker build -t my-python-app .
After the image is built, you can run it as a container:
sudo docker run -p 4000:80 my-python-app
This command maps port 4000 on your host to port 80 in the container. You should now be able to access the application by navigating to http://localhost:4000
in your web browser.
Managing Docker Containers
You can list all running containers using:
sudo docker ps
To list all containers (running and stopped):
sudo docker ps -a
To stop a running container:
sudo docker stop CONTAINER_ID
To remove a stopped container:
sudo docker rm CONTAINER_ID
To remove an image:
sudo docker rmi IMAGE_ID
Conclusion
Docker is a powerful tool for developing, shipping, and running applications inside containers. This tutorial covered the basics of installing Docker, running your first container, creating Dockerfiles, building and running images, and managing containers. With these basics, you can start exploring more advanced Docker features and workflows to streamline your development and deployment processes.