docker run hello-world -> creates a container of the image hello-world
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
Container Lifecycle
docker ps -> lists all docker images running currently
The above command will show the following data:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
-----------------------------------------------------------------------------------
docker ps --all
Lists all previously run docker images
-----------------------------------------------------------------------------------
docker run = docker create + docker start
example:
docker create busybox echo hi there
aksjdbkdjfbkede83hqd83d
The above will create a container and output its id
docker start aksjdbkdjfbkede83hqd83d
aksjdbkdjfbkede83hqd83d
This command will run the container but will not show the output
docker start -a aksjdbkdjfbkede83hqd83d
hi there
The -a is responsible for displaying the output
-----------------------------------------------------------------------------------
docker system prune
The above command is used for deleting containers and build cache
-----------------------------------------------------------------------------------
docker logs <container_id>
The above command is used to check for the logs of a conatiner without having to
rerun the whole container again
-----------------------------------------------------------------------------------
docker stop <container_id>
docker kill <container_id>
Both the commands are used to stop a running container, the difference is that
stop command allows the process to take its own time to stop, whereas the kill
command instantly kills the container. By default docker allows only 10 seconds
ffor stop command and initiates kill after that.
STOP -> SIGTERM (Term Signal to the process in a container)
KILL -> SIGKILL (Kill Signal to the process in a container)
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
Multi Command Containers
docker run redis (Run the redis container)
docker ps (Get the container_id)
docker exec -it <container_id> redis cli (Multiple commands)
-it flag allows the second command to get the text input if any
To create your own image, ypu have to create a docker file
Dockerfile → Docker Client → Docker Server → Usable Image
Creating a Docker file:
Specify a base image → Run some commands to install additional programs → Specify a comand to run on container setup
# Use an existing docker image as a base
FROM alpine
# Download and install a dependency
RUN apk add --update redis
# Tell the image what to do when it starts as a container
CMD ["redis-server"]
After creating the Dockerfile, you have to build the image using the following command:
# Make sure to be in the Dockerfile's directory
docker build .
After this the build process takes place and the final usable image is created. Now this can used as a container through the image id. Now the image id will be random string and it is painful to copy and paste the id every time to run it. To avoid this we can do image tagging
Image tagging is giving the custom names to the image:
docker build -t vinitjahagirdar1909157/redis:latest .
Breakdown:
-t vinitjahagirdar/redis:latest → tags the image