I am following this lecture to build from here: https://amigoscode.com/courses/631065/lectures/11260263
Docker Image:
Image is a template for creating an environment of your choice, snapshot, has everything to run your apps, OS, software, app code.
Below code we are pulling Apache httpd in docker desktop:
docker pull httpd
Check the images:
docker images
Run container from this image:
docker run httpd:latest
It will hang up so we write ctrl+c to delete it
we check with this code:
docker container ls
docker run
docker run -d httpd:latest
we check:
docker ps
or
docker container ls
Docker port exposing: before that we are stopping the running container
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 5c14bf083cfe httpd:latest "httpd-foreground" About a minute ago Up About a minute 80/tcp objective_roentgen 7c60047c16a0 httpd:latest "httpd-foreground" 3 minutes ago Up 3 minutes 80/tcp intelligent_wiles
docker stop objective_roentgen
docker stop intelligent_wiles
Now writing command for exposing the port and running the container:
docker run -d -p 8080:80 httpd:latest
d for dettach and p for port exposing t
Now the container is running perfectly and exposing the port which we can visit and check with command:
docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 46077824c7b4 httpd:latest "httpd-foreground" 3 seconds ago Up 2 seconds 0.0.0.0:8080->80/tcp boring_chaplygin
Now going to
http://localhost:8080/
will work perfectly.
It means the container port 80 is exposing to user by port 8080
Now I stopped the container:
docker stop boring_chaplygin
So cannot reach to localhost:8080
Now we want to run in different port we can also follow the step:
docker run -d -p 8095:80 httpd:latest
Now we can also run multiple containers from a single command:
docker run -d -p 3050:80 -p 8060:80 httpd:latest
We stop and start again the docker container:
docker stop lucid_hodgkin
docker start lucid_hodgkin
now docker help finding:
docker ps --help
it will show like this:
Usage: docker ps [OPTIONS] List containers Options: -a, --all Show all containers (default shows just running) -f, --filter filter Filter output based on conditions provided --format string Pretty-print containers using a Go template -n, --last int Show n last created containers (includes all states) (default -1) -l, --latest Show the latest created container (includes all states) --no-trunc Don't truncate output -q, --quiet Only display container IDs -s, --size Display total file sizes
removing a container from here:
docker rm objective_brown
we can show id by
docker ps -aq
it will show like this:
6e76acc8c731 9686c31de12b 46077824c7b4 5c14bf083cfe 7c60047c16a0 0e6293219d47 033ea6143731 aeaaa846803a f616f2b69cd9 b4d329afdf8d d9b3aa679977 6d18113160f1 e916576838d6 d7cd7a179f9a e6a5f1eb2ee7 ed46b147cacb 25aea2cb6569 4f3ef15a6883 c4db01275809 eab7966ed2de ce74f7d9e722 0568539c9146
when we need to remove running container forcefully
docker rm -f container_name
-f for force
Custom name for docker container:
docker run --name website -d -p 3000:80 -p 8080:80 httpd:latest
docker ps
Now stopping docker container named website
docker stop website
Now running again the docker container:
docker start website
we check containers with command
docker ps
Docker ps formatting:
docker ps --format="ID\t{{.ID}}\nNAME\t{{.Names}}\nIMAGE\t{{.Image}}\nPORTS\t{{.Ports}}\nCOMMAND\t{{.Command}}\t{{.CreatedAt}}\nSTATUS\t{{.Status}}\n"
export FORMAT="ID\t{{.ID}}\nNAME\t{{.Names}}\nIMAGE\t{{.Image}}\nPORTS\t{{.Ports}}\nCOMMAND\t{{.Command}}\t{{.CreatedAt}}\nSTATUS\t{{.Status}}\n"
docker ps --format=$FORMAT
Docker volumes: between host and controllers
cd /mnt/c/Users/Zaki/onedrive/desktop/winter2021/cloud/docker-me
docker run --name docker-me-new -v $(pwd) -d -p 8090:80 httpd:latest
pwd=present working directory
Command for without dockerfile inside:
docker run --name docker-me-new -v $(pwd):/usr/local/apache2/htdocs/ -d -p 8090:80 httpd:latest
Command for with dockerfile inside: