Revanth Vermareddy

Docker & Docker-Compose Commands to Remember

3 minutes (870 words)
docker-icon

🔗Docker & Docker-Compose

  1. Basic Docker Commands

    # basic operations
    docker start container...
    docker stop container...
    docker kill container...
    docker restart container...
    
    # to delete unused resources from docker
    docker system prune
    
    # To delete all the unused images from server without a single conatiner
    docker image prune -a
    
    # To delete all the unused objects from server without a single conatiner
    docker system prune --volume
    
    # docker-comose commands:
    docker-compose --env-file ./envs/dev.env up -d --build
    docker-compose --env-file ./envs/dev.env down
    
  2. To restart docker daemon, run the below command

    sudo service docker restart
    
  3. Installing docker compose cli v2 plugin

    # link: https://docs.docker.com/compose/install/linux/#install-the-plugin-manually
    
    # Commands:
    
    # 1.a To download and install the Compose CLI plugin, for just the current user on your system, run:
    DOCKER_CONFIG=${DOCKER_CONFIG:-$HOME/.docker}
    mkdir -p $DOCKER_CONFIG/cli-plugins
    curl -SL https://github.com/docker/compose/releases/download/v2.24.7/docker-compose-linux-x86_64 -o $DOCKER_CONFIG/cli-plugins/docker-compose
    
    # 1.b Install for All Users:
    # To make Docker Compose available for all users on your system, replace the directory ~/.docker/cli-plugins with /usr/local/lib/docker/cli-plugins.
    # You can do this by moving the downloaded docker-compose binary to the appropriate location:
    sudo mkdir -p /usr/local/lib/docker/cli-plugins/
    sudo mv $DOCKER_CONFIG/cli-plugins/docker-compose /usr/local/lib/docker/cli-plugins/
    
    # 2. Apply executable permissions to the binary:
    chmod +x $DOCKER_CONFIG/cli-plugins/docker-compose
    
    # or, if you chose to install Compose for all users:
    sudo chmod +x /usr/local/lib/docker/cli-plugins/docker-compose
    
  4. Installing Docker on AMZN linux 2 machine

    # Remove previous binaries
    sudo yum remove docker \
       docker-client \
       docker-client-latest \
       docker-common \
       docker-latest \
       docker-latest-logrotate \
       docker-logrotate \
       docker-engine
    
    # Update the packages on your instance
    sudo yum update -y
    
    # Install Docker
    sudo yum install docker -y
    
    # Print the version of docker daemon
    sudo docker --version
    
    # Start the Docker Service
    sudo service docker start
    
    # Create the Docker group
    sudo groupadd docker
    
    # Add your user to the Docker group
    sudo usermod -aG docker $USER
    
    # Note: after this logout and log back in
    
    #bash-4.2$ docker-compose --version
    #Docker Compose version v2.6.1
    #bash-4.2$ docker --version
    #Docker version 20.10.13, build a224086
    
  5. Installing Docker on Ubuntu machine

    # To install docker on ubuntu: https://docs.docker.com/engine/install/ubuntu/
    
    # Update the packages on your instance
    sudo apt update -y
    
    # Install awscli
    sudo apt install awscli -y
    
    # Install Docker
    sudo apt install docker.io -y
    
    # Print the version of docker daemon
    sudo docker --version
    
    # Install docker-compose
    sudo apt install docker-compose -y
    
    # Print the version of docker-compose
    sudo docker-compose --version
    
    # Start the Docker Service
    sudo service docker start
    
    # Add the curren user to the docker group so you can execute Docker commands without using sudo
    sudo usermod -aG docker $USER
    
    # check your groups by using the cmd
    id -nG
    
    # if docker doesn't show up then log back in for changes to take effect and then re-check
    id -nG
    
    #ubuntu@ip-10-0-0-218:/mnt/revanth/ds-underwriting-model-api$ docker-compose --version
    #docker-compose version 1.25.0, build unknown
    #ubuntu@ip-10-0-0-218:/mnt/revanth/ds-underwriting-model-api$ docker --version
    #Docker version 20.10.12, build 20.10.12-0ubuntu2~20.04.1
    
  6. Installing Docker-Compose on AMZN linux 2 machine

    # download docker-compose
    sudo curl -L https://github.com/docker/compose/releases/latest/download/docker-compose-$(uname -s)-$(uname -m) -o /usr/local/bin/docker-compose
    
    # give permissions to docker-compose
    sudo chmod +x /usr/local/bin/docker-compose
    
    # create sym-link
    sudo ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose
    
    # check version
    docker-compose version
    
  7. Un-Installing and Re-Installing docker-compose

    # At the moment the safest way to upgrade docker-compose is by deleting it and reinstalling it.
    sudo rm /usr/bin/docker-compose
    
    # Reinstall:
    sudo su
    sudo curl -L https://github.com/docker/compose/releases/download/v2.24.6/docker-compose-`uname -s | awk '{print tolower($0)}'`-`uname -m | awk '{print tolower($0)}'` > /usr/bin/docker-compose
    sudo su -l
    sudo chmod +x /usr/bin/docker-compose
    sudo usermod -aG docker ${USER}                 # to add myself to docker group
    docker-compose --version
    
    # Output: Docker Compose version v2.6.1
    
  8. Remove unused containers, images and volumes

    docker rm -vf $(docker ps -aq)
    docker rmi -f $(docker images -aq)
    docker volume prune -f
    docker volume rm $(docker volume ls -qf dangling=true)
    docker builder prune
    

Tags: #docker #docker-compose