DockerFile commands are :
- FROM is the first command in the DockerFile. Its syntax is FROM <image name>.
- RUN <some Unix command> – DockerFile command that executes in the container created from the image. Examples : (RUN apt-get update) (RUN apt-get install -y vim) (RUN apt-get install -y nginx)
- WORKDIR <directory path> – sets current directory in the current container (like pwd in Unix)
- EXPOSE <port number> – defines internal port number of the application
- CMD <Unix command> – command to be executed inside the container created by given DockerFile. There can be only one CMD inside the DockerFile
Each DockerFile command (but CMD) entails creating additional layer
There is only one mandatory filed in the DockerFile – FROM
docker build -t <image name> <path to the DockerFile> – command used for converting DockerFile into an image (for example, docker build -t nginx:v21 .)
docker build -t <image name> -f <name of the file (if another name but DockerFile is used)> <path to the file with another name> – command used for converting file with another name (not DockerFile) into an image
Practically, it is recommended to use DockerFile name, not to change it.
There are two more commands in DockerFile : ADD and COPY. ADD is a very advanced command compared to COPY. COPY is just copying the file from one directory to another. Whereas ADD untars and removes untared archive file from the filesystem. See example :
Example shows that ADD is more advanced alternative to COPY.
Another advantage of ADD command compared to COPY is that ADD can download the contents of web path (i.e. data from web url), COPY can not.
Docker Hub is a storage of Docker Images. There are two types of repositories in Docker Hub – private and public. Public repositories are accessible for everyone. Private repositories are accessible only for users which are granted access. Also, Docker Hub consists of two tier : free (or Community Edition) Tier and enterprise (or Enterprise Edition) Tier. Free Tier allows creating any number of public and only one private repository. Enterprise Tier allows creating any number of public and any number of private repositories. Another Enterprise Tier feature is ability for so called image scanning, which is not presented in Free Tier. Image scanning in this case is scanning images for viruses and other malicious content.
docker tag <old repository>:<old tag> <new repository>:<new tag> – command used to change repository : tag of docker image
How to push an image to Docker Hub ?
- docker login – command used to login to Docker Hub
- repository name of the image must match the targeted repository name in the Docker Hub (via docker tag command)
- execute command docker push <new repository>:<tag name>
There is no defined limit of how many images can be pushed to a repository.
docker system df – command that displays information regarding the amount of disk space used by the docker daemon
docker image prune – command that allows to clean up unused images. By default, this command cleans up only dangling images. A dangling image is one that is not tagged and is not referenced by any container (docker image prune -a – command that removes all images without at least one container associated with them (all layers, see above about layers) )
docker image rm <image name> – command used to delete image with the given name (if there is at least one running container using that image, it won’t be deleted; to force deletion, docker image rm <image name> -f must be used)
How to delete an image from Docker Repository in the Docker Hub ? Select repository, then Tags, mark an image, dropdown Action, select Delete.
docker history <image name> – command that shows the entire history of an image with the given name
docker save <IMAGE_ID> -o <file name>.tar – command for converting image to archive (tar file)
docker load < <name of file with archived image> – command for restoring previously archived image
docker export <CONTAINER_ID> > <file name>.tar – command for converting container to archive
docker import <name of file with archived container> <image name> – command that creates an image from previously archived container (it does not restore the container)