Docker in High-Level
Docker Images
A Docker image is an executable package that includes everything needed to run an application such as the code, a runtime, libraries, environment variables and configuration files. A container is a runtime instance of an image.
Layers of Docker Images
The Docker images are made up of layers make them to be extremely efficient and flexible. Note that the layers cannot be modified but we can build news images from existing images.
Dockerfile
The configuration file that builds that Docker image is called the Dockerfile, which is a text file that contains all the commands needed to build a docker image.
![](https://predictivehacks.com/wp-content/uploads/2021/03/image-35.png)
By convention, you should call name the Dockerfile as Dockerfile without any extension, which is a flat file. We will discuss more about the Dockerfile instructions but let’s have a look at some of them:
- FROM : This is the first instruction and is used to define the base image
- ADD : Copies a file into the image but supports also tar and remote URL
- COPY : Copy files into the image, it is most common than the ADD
- VOLUME : Creates a mount point as defined when the container is run
- ENTRYPOINT : The executable runs when a container is running
- EXPOSE : Documents the ports that should be published
- CMD : Provide arguments for the entrypoint
- ENV : Defines environmental variables in the container (usually for credentials)
- RUN: Runs a new command in a new layer
- WORKDIR : Sets the working directory of the container
Docker Commands Cheat Sheet
Commands | Comments |
docker system prune | prune the images |
docker ps -a | get the images |
docker create busybox <container_id> | create an new container, here based on busybox |
docker start <container_id> | start a container |
docker logs <container_id> | get the logs of the container |
docker stop <container_id> | stop a container |
docker kill <container_id> | kill a container |
docker exec -it <container_id> <command> | execute a command in an interactive terminal |
docker run -it busybox sh | run the shell (sh) command within busybox in an interactive terminal |
docker build . | build a docker image from a Dockerfile |
docker run <image_id> | run the docker image |
docker build -t gpipis/myproject:latest . | build a docker imager form a Dockerfile with a tag |
docker run gpipis/myproject:latest | run the docker image |
COPY <path to folder to copy from on your machine relative to build context> <place to copy stuff to inside the conainer> | the COPY instruction inside Dockerfile |
COPY ./ ./ | the COPY instruction inside Dockerfile referring to home directories |
docker run -p <route incoming requests to this port on local host to>:<this port inside the container> <image id> | docker run with port mapping |
docker run -p 8080:8080 <docker image id or name> | could be 5000:8080 and then at your browser would be localhost:5000 The second port should be consistent with your Application |
WORKDIR /usr/app | this is part of the Dockerfile. Any following command will be executed relative to this path in the container. It is good to put it in the /usr but you could put it in the var or in the home |
docker-compose.yml | like the Dockerfile this is the file name for the docker compose |
docker-compose up –build | build and run the docker-compose.yml |
docker-compose up | run |
docker-compose up -d | launch in backgroun |
docker-compose down | stop containers |
docker save <image name> > myname.tar | creation of image tar file from Docker image |
docker load -i myname.tar | load an image from a tar file |
docker export <imagename> > myname.tar | creation of image tar file from a container |
docker import – <gpipis/foo> < myname.tar | import the contents from a tarball to create a filesystem image |
References:
LinkedIn Learning: Docker Essential Training