-
PULL
- To pull a docker image use thepull
flag i.edocker pull {image name}:latest
- Note: Always use the alpine version of the docker image cause its light weight and faster to pull. So always check for the alpine version of images and pull them.
-
SHOW DOCKER IMAGES
- To show the list of all docker imagesdocker images ls
-
SHOW RUNNING CONTAINER
- To show all running docker containers usedocker ps
||docker container ls
.docker ps
shows a list of the running containers.- To nicely format the output of output of docker ps use the
-format="ID\t{{.ID}}\nNAME\t{{.Names}}\nImage\t{{.Image}}\nPORTS\t{{.Ports}}\nCOMMAND\t{{.Command}}\nCREATED\t{{.CreatedAt}}\nSTATUS\t{{.Status}}\n"
flag. - To shorten this store the value in a variable using this command
export FORMAT="ID\t{{.ID}}\nNAME\t{{.Names}}\nImage\t{{.Image}}\nPORTS\t{{.Ports}}\nCOMMAND\t{{.Command}}\nCREATED\t{{.CreatedAt}}\nSTATUS\t{{.Status}}\n"
. Now you can usedocker ps --format=$FORMAT
to show the nicely formatted output.
- To nicely format the output of output of docker ps use the
-
RUN CONTAINER
- To run a docker container usedocker run {name of the docker image}:{the version of the docker image}
||docker run -d {name of the docker image}:{version of the docker image}
- Most time we run the
latest
version of the docker image. -d
flag means you are specify that this container should be detached and run in the background.
- Most time we run the
-
PORT
- To specify a port to attach a container to use the-p
and then specifying the port flag when running the containerdocker run -d -p 8000:80 {name of the docker image}:{version of the docker image}
- Note: You can specify more than one port to attach to a container you want to run
docker run -d -p 8000:80 -p 3000:80 {name of the docker image}:{version of the docker image}
.
- Note: You can specify more than one port to attach to a container you want to run
-
Network
- To connect a container to a network use the--net
flag and then specify the network name. So if you want to connect the container to a network calledbridge
use this flag--net=bridge
.- Note: You can connect a container to more than one network by specifying the network name multiple times.
-
Enviroment Variable
- To specify an environment variable use the-e
flag and then specifying the environment variable flag when running the containerdocker run -d -e {name of the environment variable}={value of the environment variable} {name of the docker image}:{version of the docker image}
- Note: You can specify more than one environment variable to attach to a container you want to run
docker run -d -e {name of the environment variable}={value of the environment variable} -e {name of the environment variable}={value of the environment variable} {name of the docker image}:{version of the docker image}
.
- Note: You can specify more than one environment variable to attach to a container you want to run
-
STOP CONTAINER
- To stop a docker container usedocker stop {the container id}
||docker stop {container name}
-
DELETE CONTAINER
- To delete a container usedocker rm {the container id}
-
INSPECT
- To inspect a container usedocker inspect {the container id}
-
LOGS
- To show logs of a container usedocker logs {the container id}
. Logs are essential whe you want to debug.- To see all useful commands you can use alongside logs use the
--help
flag. - To continuously show logs of a container use
-f
flag, i.edocker logs -f {the container id}
.
- To see all useful commands you can use alongside logs use the
-
DELETE IMAGE
- To delete a docker imagedocker image rm {the image id}
||docker rmi {repository}:{tag}
. -
DELETE ALL CONTAINER REGISTRY
- To remove all containers from registry usedocker rm $(docker ps -aq)
- Note: To forcefully remove a running container use
-f
flag i.edocker rm -f {docker ps -aq}
.
- Note: To forcefully remove a running container use
-
NAME CONTAINER
- To name a container use the--name
flag. i.edocker run --name {what you want to name the container} -d -p 3000:80 {name of the docker image}:{version of the docker image}
-
SHARE VOLUMES
- To share a volume || folder || project with a docker container running onnginx
for instance you need to use the-v
flag or command which stringifies volume. first cd into the project directory and rundocker run --name {name you want to call the container} -v $(pwd):/usr/share/nginx/html:ro -d -p 8080:80 nginx:latest
- NB: the
$(pwd)
command gives you the current working directory. In this instance its the folder you've cd into. - To share volumes between two docker containers, use the
--volume-from
flag for instancesudo docker run --name divine-copy --volumes-from divine -d -p 3000:80 nginx:latest
- NB: the
Create a cutom docker image A image should contain all the things your application needs to run
-
First create a docker file
touch Dockerfile
inside the project you want to dockerize then. Inside this file specifyFROM
=> TheFROM
instruction initializes a new build stage and sets the Base Image for subsequent instructions. As such, a valid Dockerfile must start with aFROM
instruction. The image can be any valid image – it is especially easy to start by pulling an image from the Public Repositories. for instance use the nodejs official docker imagenode:latest
ADD
=> The ADD instruction copies new files, directories or remote file URLs from<src>
and adds them to the filesystem of the image at the path. For instance to add all files || folders in the root directory use double dots. .
RUN
=> TheRUN
instruction will execute any commands in a new layer on top of the current image and commit the results. The resulting committed image will be used for the next step in the Dockerfile. For instance you can instal npm all packages inside the container using theRUN
instruction i.enpm install
CMD
=> There can only be oneCMD
instruction in aDockerfile
. If you list more than oneCMD
then only the lastCMD
will take effect. The main purpose of aCMD
is to provide defaults for an executing container. These defaults can include an executable, or they can omit the executable, in which case you must specify anENTRYPOINT
instruction as well. For instance you can run a nodejs application using theRUN
instruction i.enode app.js
.
-
To create a container in docker use the
docker build
command. For instancedocker build -t divine-website:latest .
this will build a docker image from our dockerfile thats inside the our project directory. To see all available docker build command usedocker build --help
. NB: always use-t
or--tag
flag to add a tag to a container
- To create a tag use the
tag
flag like sodocker tag {from latest version of the docker image} { to version 1.0 of the docker image}
egsudo docker tag divine-website:latest divine-website:1.1
- To create a version for a container you need the specify the version when running the container like
docker run --name {name you want to call the container} -d -p 5000:80 {the docker image}:{the version}
. For instance I want to create a new container from an image I have already created, I will rundocker run --name divine-website-v1 -d -p 5000:80 divine-website:1.1
. NB:1.1
in this instance is the a tag for this docker image which specifies the version
There are two types of registries which are, Public and Private Registry. You can push this registries to docker registry providers like Docker HUB, Quay.io, Amazon ECR.
- To push a new docker image to a docker registry use the
docker push
instruction. egdocker push codewithdiv/nodejs-api:tagname
. NB: Before you push to a docker registry first tag the image you want to pushdocker tag {repository name} {with the name of registry you want to push to, eg - codewithdiv/nodejs-api:tagname}
-
What is a
docker network
? A docker network is a virtual network that you can use to connect containers to each other. -
To get the list of all networks use the
docker network ls
command. -
To create a docker network use the
docker network create
instruction. egdocker network create {name of the network}
-
To connect a container to a network use the
docker network connect
instruction. egdocker network connect {name of the network} {name of the container}
- Note: You can connect multiple containers to a network by using the
docker network connect
instruction multiple times.
- Note: You can connect multiple containers to a network by using the
-
To disconnect a container from a network use the
docker network disconnect
instruction. egdocker network disconnect {name of the network} {name of the container}