Docker usefull commands

Docker Desktop is an easy-to-install application for your Mac or Windows environment that enables you to build and share containerized applications and microservices. Docker Desktop includes Docker Engine, Docker CLI client, Docker Compose, Docker Content Trust, Kubernetes, and Credential Helper.

Well, I’ve using docker for a while but not in my daily drive, so I have a problem. I always need to search for the same commands all the time. So that’s why this post was created for

How to install

https://docs.docker.com/desktop/mac/install/

Download, drag and drop, and run. Docker installation has changed over time, nowadays the process is now more guided.

Basic commands to get started.

Disclaimer: These commands were obtained from the docker Getting Started

Clone the repo and enter

> docker run --name repo alpine/git clone https://github.com/docker/getting-started.git
> docker cp repo:/git/getting-started/ .

Build the docker image and run it

docker build -t docker101tutorial . 
docker run -d -p 80:80 --name docker-tutorial docker101tutorial

Share that image with anybody else

docker tag docker101tutorial whistler092/docker101tutorial 
docker push whistler092/docker101tutorial

There are several commands here:

  • docker ps allows you to see the running containers.
  • docker ps -a allows you to see all the containers.
  • docker start allows you to start a container.
  • docker stop allows you to stop a container.
  • docker rm allows you to remove a container.
  • docker rmi allows you to remove an image.
  • docker images allows you to see all the images.
  • docker image rm allows you to remove an image. That image cannot be used anymore.

Another command to get started.

  • docker build -t myuser/myimage . Build an image based on the Dockerfile in the current directory. Tag is as myuser/myimage.
  • docker run [image id or image tag] Create and start a container based on the provided image id or tag.
  • docker run -it [image id or image tag] Create and start a container, but that flags also overrides the default command.
  • docker ps Print out information about all the running containers.
  • docker ps -a Print out information about all the containers.
  • docker exec -it [container id] [cmd command] Execute the given command in a running container
  • docker logs [container id] Print out the logs of a container.

Run postgresql

> docker run --name postgresql-container -p 5432:5432 -e POSTGRES_PASSWORD=your_secret_password -d postgres 

Unable to find image 'postgres:latest' locally
latest: Pulling from library/postgres
eff15d958d66: Pull complete
de2b4ab3ade5: Pull complete
108afa831d95: Pull complete
e5821d5963ce: Pull complete
5be06220aa99: Pull complete
c877668f09b8: Pull complete
7037ade1772d: Pull complete
d46bc4e17ddc: Pull complete
a60906bcf87f: Pull complete
a1c85f71c941: Pull complete
69f50e484cab: Pull complete
2f8a286a55a4: Pull complete
8d590b0d720c: Pull complete
Digest: sha256:1fe27b334443793af98d7eb320ad6f9f30fcc9bc068f545cb46ec01cefe9c8ee
Status: Downloaded newer image for postgres:latest
ad8e89cebc59ca22ac979568da17128e1cec3f5c8df6d3d2500e05a3b2daf990

>  docker ps
ad8e89cebc59   postgres            "docker-entrypoint.s…"   21 seconds ago   Up 21 seconds   0.0.0.0:5432->5432/tcp   postgresql-container

to stop it and run it:

> docker ps 
CONTAINER ID   IMAGE      ...
ad8e89cebc59   postgres   ...

> docker stop ad8e89cebc59

> docker ps
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES

>  ⇒  docker ps -a
CONTAINER ID   IMAGE               COMMAND                  CREATED       STATUS                          PORTS     NAMES
ad8e89cebc59   postgres            "docker-entrypoint.s…"   3 hours ago   Exited (0) About a minute ago             postgresql-container

> docker start postgresql-container

Now you can connect into your postgresql database

Run MSSQL Server

https://www.cloudiqtech.com/install-run-sql-server-docker-container-mac/

docker pull microsoft/mssql-server-linux:2017-latest 

docker run -d --name macsqlserver -e 'ACCEPT_EULA=Y' -e 'SA_PASSWORD=your_secret_password' -e 'MSSQL_PID=Developer' -p 1433:1433 microsoft/mssql-server-linux:2017-latest 

https://medium.com/@bossjones/how-i-setup-a-raspberry-pi-3-cluster-using-the-new-docker-swarm-mode-in-29-minutes-aa0e4f3b1768#.dg9u9rbll | How I setup a Raspberry Pi 3 Cluster Using The New Docker Swarm Mode In 29 Minutes | by Malcolm Jones | Medium
https://docs.microsoft.com/en-us/virtualization/windowscontainers/about/ | About Windows containers | Microsoft Docs
https://skillsmatter.com/skillscasts/9146-building-a-bank-with-kubernetes | Building a Bank with Kubernetes | SkillsCast | 19th October 2016
https://www.percona.com/blog/2016/11/16/is-docker-for-your-database/ | Are Docker Containers Good for Your Database? - Percona Database Performance Blog

Nginx with reverse proxy

https://blog.florianlopes.io/host-multiple-websites-on-single-host-docker/ | Host multiple websites on a single host with Docker
https://github.com/nginx-proxy/nginx-proxy | nginx-proxy/nginx-proxy: Automated nginx proxy for Docker containers using docker-gen
https://github.com/ehazlett/interlock | ehazlett/interlock: Docker Event Driven Plugin System
http://jasonwilder.com/blog/2014/03/25/automated-nginx-reverse-proxy-for-docker/ | Automated Nginx Reverse Proxy for Docker ·
https://www.yannmoisan.com/docker.html | Playing with Docker and nginx : from multi-domain to multi-container | Yann Moisan

Containerizing apps

https://somakdas.medium.com/what-are-containers-and-how-to-containerizing-an-asp-net-mvc-application-using-docker-30931579f248 | What are containers and how to containerizing an Asp.Net MVC application using Docker | by Somak Das | Medium
https://dotnetthoughts.net/dockerize-an-existing-aspnet-mvc5-application/ | Dockerize an existing ASP.NET MVC 5 application | dotnetthoughts
https://www.youtube.com/watch?v=gO6jJX4rxW4 | Back to the Future: Containerize Legacy Applications - YouTube