Frequently used Docker commands

  • Post category:Docker
  • Post comments:0 Comments
  • Reading time:8 mins read

In here I have listed few of the most frequently used Docker commands. Also scripts to install Docker or Docker Compose components through commandline. Useful while setting them up on remote virtual machine like AWS, Google Cloud services etc.

Most frequently used Docker commands
Created by me using Piskel webapp

To list the running containers:

docker ps
docker ps command

How to list all the running and stopped containers :

docker ps --all
docker ps -all command

How to run a new container:

docker run {options} <image name>

How to run a command in a running container:

docker exec <container_name> {command}

How to shell into a container:

docker exec -it <container name> sh(or bash)

How to build a custom docker image:

docker build -t nginx-rtmp .

How to Docker login into gitlab registry:

docker login registry.gitlab.com -u <username> -p <token>

How to install docker through commandline:

sudo apt-get update

sudo apt-get install \

    apt-transport-https \

    ca-certificates \

    curl \

    gnupg \

    lsb-release

curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg –dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg

echo \

  “deb [arch=amd64 signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu \

$(lsb_release -cs) stable” | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null

sudo apt-get update

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

How to install Docker Compose through commandline:

curl -L “https://github.com/docker/compose/releases/download/1.23.2/docker-compose-$(uname -s)-$(uname -m)” -o /usr/local/bin/docker-compose

chmod +x /usr/local/bin/docker-compose

hash -d docker-compose

ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose

I have created a script to install Docker and Docker Compose components. I use it to install them on remote host machines on the cloud, very often.

https://github.com/asingh33/setup_docker_ubuntu.git

How to create and push your own custom docker image based on running container:

  1. Create and run local container using base image.
  2. Make any changes required
  3. Docker commit
    • eg: docker commit -m “Some docker commit message” -a “Your Name” <container_name> registry.gitlab.com/<username>/<imagename:version_number>
  4. docker push the created image
    • eg: docker push registry.gitlab.com/<username>/<imagename:version_number>

Share the post, if you liked it

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.