Learn in Public | Docker

Pull an Image

docker pull <image_name>

> docker pull python:3.8-slim

Delete an image

docker image rm <image_name>
docker image rm <image_id>

List available images

docker images

Run a container

docker run -v <mounting_path_on_our_machine:work_directory_in_container> <image_name> <command>

List running containers

docker ps

List all containers

docker ps -a

Delete a Container

docker rm <container_name>
docker rm <container_id>

Build a docker image

docker build -t <image_name>:<image_version> <path_to_dockerfile>

> docker build -t my-first-image:0.0.1 .

Run a docker container

docker run -p <local_machine_port>:<docker_runtime_port> <image_name> 

> docker run -p 3000:5000 my-first-image:0.0.1

Stop a docker container

docker stop <container_id/container_name>

Example Dockerfile 1

## 1. Which base image do you want to use?

FROM python:3.8-slim

## 2. Set the working directory in the container.

WORKDIR /src/app

## 3. Copy the project files into the working directory.

COPY . .

## 4. Install the dependencies

RUN pip install -r flask-demo/requirements.txt

## 5. Document and inform the developer that the application will use PORT 5000 of the container.

EXPOSE 5000

## 6. Define the command to run when the container starts.
CMD ["python", "/src/app/flask-demo/app.py"]

Untag an image

docker rmi <repository/image-name:tag>
> docker rmi mohamedsobhi777/grade-submission:flask-0.0.1

Push an image to docker hub

docker push <repository/image-name:tag>

> docker push mohamedsobhi777/grade-submission:flask-0.0.1

Pull an image

docker pull <repository/image-name:tag>

> docker pull mohamedsobhi777/grade-submissoin:flask-0.0.1

Prune docker engine (remove all stopped containers, networks without no containers, images with no containers, all build cache)

docker system prune -a

Inspect an image

docker inspect <image_name:tag>

Run a container with environment variables

docker run -p <local_machine_port>:<docker_runtime_port> -e <environment_variable_name>=<value> <image_name> 

>  docker run -p 4001:3000 -e DATABASE_HOST=host.docker.internal -e DATABASE_USER=user -e DATABASE_PASSWORD=password -e DATABASE_NAME=db flask-mysql:latest

Docker compose example

version: "3.3"
services:
  mysql:
    image: mysql/mysql-server:8.0
    environment: # Environment variables passed into the MySQL container
      MYSQL_DATABASE: "db"
      MYSQL_USER: "user"
      MYSQL_PASSWORD: "password"
    ports:
      - 3306:3306

  flaskapp:
    image: flask-mysql:latest
    depends_on:
      - mysql
    environment:
      DATABASE_HOST: "mysql"
      DATABASE_USER: "user"
      DATABASE_PASSWORD: "password"
      DATABASE_NAME: "db"
    ports:
      - 8000:3000

Start a docker compose file

docker compose up

Stop a docker compose

docker compose down

Docker compose with volumes

version: "3.3"
services:
  mysql:
    image: mysql/mysql-server:8.0
    environment:
      MYSQL_DATABASE: "db"
      MYSQL_USER: "user"
      MYSQL_PASSWORD: "password"
    ports:
      - "3306:3306"
    volumes:
      - new-db:/var/lib/mysql

  flaskapp:
    image: grade-submission:0.0.1
    depends_on:
      - mysql
    environment:
      DATABASE_HOST: "mysql"
      DATABASE_USER: "user"
      DATABASE_PASSWORD: "password"
      DATABASE_NAME: "db"
    ports:
      - "8080:8080"
volumes:
  new-db:
# Define named volumes here. These volumes can be used by services for persistent storage

Prune docker unnamed volumes

docker systme prune -a --volumes

List available volumes

docker volume ls

Delete a named docker volume

docker volume rm <volume_name>