Swiftorial Logo
Home
Swift Lessons
Matchups
CodeSnaps
Tutorials
Career
Resources

Docker Tutorial

Introduction to Docker

Docker is an open platform for developing, shipping, and running applications. Docker enables you to separate your applications from your infrastructure so you can deliver software quickly. With Docker, you can manage your infrastructure in the same ways you manage your applications.

Installing Docker

To install Docker, follow the steps for your operating system:

For Windows:

Download Docker Desktop from the official Docker website and follow the installation instructions.

For macOS:

Download Docker Desktop from the official Docker website and follow the installation instructions.

For Linux:

sudo apt-get update
sudo apt-get install docker-ce docker-ce-cli containerd.io

Getting Started with Docker

Once Docker is installed, you can start using it. First, let's run a simple Docker container:

docker run hello-world
Hello from Docker!
This message shows that your installation appears to be working correctly.
...
                

Working with Docker Images

Docker images are the basis of containers. You can think of an image as a snapshot of a filesystem. To list all Docker images on your system, use:

docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
hello-world         latest              4ab4c602aa5e        2 weeks ago         1.84kB
                

To pull a Docker image from the Docker Hub, use:

docker pull ubuntu

Running Docker Containers

To run a Docker container, use the following command:

docker run -it ubuntu

This command will start an Ubuntu container and give you an interactive terminal inside it.

Managing Docker Containers

To list all running containers, use:

docker ps

To list all containers, including stopped ones, use:

docker ps -a

To stop a running container, use:

docker stop [container_id]

To remove a container, use:

docker rm [container_id]

Creating Docker Images

You can create your own Docker images using 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. Here's an example:

# 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"]
                

To build the Docker image, navigate to the directory containing your Dockerfile and run:

docker build -t my-python-app .

Docker Compose

Docker Compose is a tool for defining and running multi-container Docker applications. With Compose, you use a YAML file to configure your application's services. Then, with a single command, you create and start all the services from your configuration.

Here is an example of a docker-compose.yml file:

version: '3'
services:
  web:
    image: my-python-app
    build: .
    ports:
      - "5000:5000"
    volumes:
      - .:/code
  redis:
    image: "redis:alpine"
                

To start your application, use:

docker-compose up

Conclusion

Docker is a powerful tool that can help you manage and deploy applications more efficiently. By using Docker, you can ensure that your applications run smoothly on any environment, whether it's your local machine, a test server, or a production server.

We hope this tutorial has given you a good understanding of Docker and how to use it. Happy Dockering!