Docker is a containerized way of running different application with the same or different packages, tooks, source code without affecting one another in a same pc, instance or hypervisor. The docker run time will be the same for all of these docker images provided they are running on the same pc.
This is the first way to run a docker image
docker run -p <pc_port>:<docker_port> <docker_image_name>
docker run --publish <pc_port>:<docker_port> <docker_image_name>
The image above will stop if you click ctrl + c
on your pc
if you want to run a docker image in the background without interuption use the following command
docker run -p -d <pc_port>:<docker_port> <docker_image_name>
docker run --publish --detach <pc_port>:<docker_port> <docker_image_name>
Note that -p = --publish
and -d = --detach
Examples are given below
- Running a docker-nginx image
docker run -p -d 8000:80 nginx
- Running an apache web server
docker run -p -d 8000:80 apache
# could be wrong
- Running custom images
# will add more notes on this one
If you want to see all containers running in your pc use the following commands
dcoker containers ls # returns running containers
dcoker ls
dcoker ls -a
docker ps # this is an old way of doing it but it still works
-a means all or irrespective of their status
if you a container is running in the image and you want to stop it use
docker container stop <container-id>
docker stop <container-id>
docker container stop <container-name>
docker stop <container-name>
docker container stop <container-first-four-charectors-on-id>
docker stop <container-first-four-charectors-on-id>
Instances when a container is hidden
when an instance has been stopped it will be hidden to see it use
docker ls -a
To search images on dockerhub
docker search <image-name>
This will start only existing stopped images
docker start <image-name or id>
This will restart only existing stopped or running images
docker restart <image-name or id>
When running a container using docker run
without providing a name for the container,dcker will create an assign a default name to that container of some famous scientists
however if you want to assign a name for a container use '--name '
e.g
docker run -p -d 8000:80 --name <name-you-want> nginx
docker run -p -d 8000:80 --name web_server nginx
To see the result of this use
docker ps
docker ps -a
docker logs <container name>
docker top <container-name> or < container-id>
also try this for all
ps -ef | grep <container-name>
ps -ef | grep nginx
rm to running containers will give you an error
docker rm <container-id>
docker rm <container-id> <...id2> <id3>
-
They both have resource allocation and allocation benefits but the memory allocation for VMs is static where for containers it's static
-
Containers virtualizes operating systems where as VMs virtualizes hardware
-
Containers are more portable (1 creation multiple execution on different machines)
-
Containers are just processes
-
Contianers are limited to resources they can access
-
Containers can be killed on exit
docker run --name custom-mysql -e MYSQL_ROOT_PASSWORD=#Mulalo96 -d mysql
docker stats <container_name or container_id>
Get a detailed information about docker container when running
docker inspect <container_name or container_id>
docker run -it <image_name> <commands>
- using -i: means it will keep the stdin open if not attached
- using -t: Allow a pseudo-TTY terminal open
examples
docker run -it nginx /bin/bash # to access the terminal of the container
docker run -it nginx ls
docker run -it nginx pwd
this works when launching only
docker exec -it <image-name or id> touch /tmp/carrie
docker exec -it <image-name or id> /bin/bash
# docker run -it --name mysql_name -e MYSQL_ROOT_PASSWORD=#Mulalo96 mysql -uroot -p
docker run -it --name mysql_client -e MYSQL_ROOT_PASSWORD=#Mulalo96 mysql
docker stop $(docker ps -a -q)
docker rm $(docker ps -a -q)
docker container prune # all stopped containers
docker image prune # not tagged latest
Before I leave you, I have prepared a list of commands that may be useful to you on Docker.
docker image ls
docker image rm [image name]
docker image rm $(docker images -a -q)
docker ps -a
docker stop [container name]
docker stop $(docker ps -a -q)
docker rm [container name]
docker rm $(docker ps -a -q)
docker logs [container name]
if you want to connect two instances/containers to run on the same network interface then this is the way.
Networks connects a container to another container and two containers to a host machine
By default all docker containers are connected to the bridge network which is th default network created by the runtime envrionment. All containers in the bridge network communicate with each other using ip-addresses.Containers on different host machine cannot communicate with each other without additional configuration
This model enables communication between containers from two or more different host machines. Technlogies like VXLAN or IPSec are often used to create virtual machines that span multiple hosts.
CNI is a specification that defines how a container runtime interacts with networking plugins. it allows different container runtimes to be combined with various networking solutions.
docker port <contianer_id>
docker port <contianer_name>
docker inspect <contianer_id or name>
filter one item
docker inspect <container_id> -f {{ .key.value }}
- to see all networks are available
docker network ls
- to create a network
dcoker network create my_bridge_net
To filter networks in the bridge
docker network -f drive=bridge
To finds all networks ids and drivers
docker network ls -f "{{ .ID }}: {{ .Driver }}"
Try
docker network --help
DNS is a system in which domains to thier respective ip addresses
Dontainers use DNS to communicate
can add network to container when run using --network <network_name>
check if containers in the same network are connected
docker run -it <container1> ping <container_2>