Containers lab

Learn Docker

A comprehensive guide to Docker, covering its history, architecture, and practical usage in container orchestration.

Docker Prerequisites Here are the steps to create a Docker Hub account: 1. Go to https://hub.docker.com/signup and click on the "Sign Up" button. 2. Fill out the registration form with your name, email address, and password. 3. Agree to the terms of service and privacy policy by checking the box. 4. Click on the "Sign Up" button to complete the registration process. 5. You will receive a verification email from Docker Hub. Follow the link in the email to verify your email address. 6. Once your email address is verified, you can log in to Docker Hub using your email address and password. You can now create and manage your repositories, and upload your Docker images to share with the community. That's it! You now have a Docker Hub account and you can start using it to store, share, and distribute your Docker images. Hello World in Docker run your first hello world example is a command that runs a simple Docker container to verify that Docker is correctly installed on your system and working as expected. When you run this command, Docker will first check if the "hello-world" image is available locally. If the image is not found, Docker will download it from the Docker Hub registry. Once the "hello-world" image is available, Docker will create a container from the image and run it. The container will print a message to the console to indicate that everything is working correctly. Here's an example of what you might see when you run docker run hello-world: check it out list of docker images docker inspect is a command used to retrieve detailed information about one or more Docker objects, such as containers, images, networks, volumes, and more. The command allows you to inspect the configuration and state of a Docker object, including its metadata, networking information, storage configuration, and more. Here's the basic syntax of the docker inspect command: OPTIONS: Optional flags that modify the output of the command. OBJECT: The name or ID of the Docker object to inspect. For example, to inspect a running Docker container named hello-world , you could use the following command: Docker Image Filtering running docker run alpine command would download the Alpine Linux image from Docker Hub and start a new container based on that image. You can then use the container to run commands or applications. When you exit the container, it will stop running. Alpine Linux is a lightweight Linux distribution that is commonly used in Docker containers due to its small size and security features. docker images filtering The docker images command allows you to filter Docker images based on various criteria using the --filter option. Here are some common filters that you can use with the docker images command: Filters images that are or are not "dangling," meaning they are not tagged and not referenced by any container. Or to list images created before a specific image, you can run: Images as Tar Files Images and Container as Tarfile Docker provides the ability to save images and containers as tar files, which can be useful for sharing with others or transferring between systems. Here's how to do it: Saving an Image as a Tar File To save a Docker image as a tar file, use the docker save command with the image name and output file name: docker load is a command used to load images or container archives that were previously saved using the docker save command. When you use the docker save command, it creates a tar archive of one or more Docker images and/or containers. You can then use the docker load command to load this tar archive back into Docker. The syntax for using the docker load command is as follows: Pushing to DockerHub Pull nginx image from dockerhub using Run Docker with ngnix –name docker-nginx : Name given to the container that is run is docker-nginx-p 80:80 : the port we are exposing and mapping from local machine port number to that of container, in the format localmachineport:containerport-d : Detached mode – Runs the container in background check all running docker containers open localhost with specific port Include a static Web Application in the Docker with NGINX To include our static Web Application into the Docker Image with NGINX, we shall create a Dockerfile (including commands to build image) and an html file with name index.html (acting as our web application) in a directory named nginx-app. create dockerfile with following content : create index.html file with following content Build Dockerfile run updated ngnix webapp List docker images Tag Docker Images Login into Your DockerHub Account Building a Base Image write simple c program Compile C program create dockerfile with following content : Build Dockerfile without any base image run docker image Dockerfile ADD Here's an example of a Dockerfile that uses the ADD instruction to copy a local file into a Docker image: Build Dockerfile ### run docker images Dockerfile COPY COPY is a dockerfile command that copies files from a local source location to a destination in the Docker container. A Dockerfile is a text file with instructions to set up a Docker container. create myfile1.txt and myfile2.txt with following content : The general syntax of the COPY command is: Here, and are file paths. is the path to the source folder containing files to be copied. This option can be left empty to copy the contents of the current directory. The source of the files has to be a directory on the local computer. is the destination of the COPY command inside the docker container. This is the path where files are to be copied. Build Dockerfile using following Command #### check inside container and search for text file both file successfully copied inside container Dockerfile CMD The CMD command we saw earlier followed the Shell syntax: However, it is better practice to use the JSON array format: A CMD command can be overridden by providing the executable and its parameters in the docker ​run command. For example: build dockerfile run docker container Dockerfile Entrypoint Running a Docker Container with ENTRYPOINT Let's learn the details in this case by actually executing ENTRYPOINT in exec form. The following is an example of a Dockerfile that uses the exec form of ENTRYPOINT, which outputs a character string on the command line. Build dockerfile run docker container Overwrite with --entrypoint option On the other hand, in ENTRYPOINT, you can change the instruction by using the option of —entrypoint as follows. CMD and ENTRYPOINT have similar roles and are confusing, but they have different functions. CMD, ENTRYPOINT, and ENTRYPOINT also behave differently between shell form and exec form, so it's a good idea to use each function properly. The instructions in the Dockerfile are a bit complicated, but you can use them effectively if you understand them. Dockerfile WORKDIR The WORKDIR command is used to define the working directory of a Docker container at any given time. The command is specified in the Dockerfile. Any RUN, CMD, ADD, COPY, or ENTRYPOINT command will be executed in the specified working directory. WORKDIR instruction Dockerfile for Docker Quick Start build dockerfile run docker container output Dockerfile RUN The RUN command is the central executing directive for Dockerfiles. It takes a command as its argument and runs it to form the image. Unlike CMD, it actually is used to build the image (forming another layer on top of the previous one which is committed). create dockerfile with following content build docker container run docer container added user as my-app with whale emoji Dockerfile ARG Build Dockerfile Inspect Env variable output docker container run sangam14/arg-dockerfile env Pass env values while building dockerfile Inspect Env of new docker image cat dockerfile.arg1 build docker image and pass build args Dockerfile Volumes cat dockerfile.vol Build above dockerfile run dockerg output check it out all mounted volumes output run container stop running container lets run container exec into running container check it out mount point using docker inspect stop running container remove container remove volume verify once Dockerfile USER cat dockerfile.user build dockerfile run docker container in detach mode If we check the owner of the sleep process on the host, we can see it belongs to the user with uid 1000, the one that is created in the image Dockerfile HEALTHCHECK cat Dockerfile build docker container expose docker port on 80 port build dockerfile inspect healthcheck ★ --interval: This specifies the period between each health check (the default is 30s). ★ --timeout: If no success response is received within this period, the health check is considered failed (the default is 30s). ★ --start-period: The duration to wait before running the first health check. This is used to give a startup time for the container (the default is 0s). ★ --retries : The container will be considered unhealthy if the health check failed consecutively for the given number of retries (the default is 3). Dockerfile ENV cat dockerfile build dockerfile inspect Env Config change envirmonment variable Dockerfile ONBUILD The ONBUILD instruction The ONBUILD instruction is a trigger. It sets instructions that will be executed when another image is built from the image being build. This is useful for building images which will be used as a base to build other images. You can't chain ONBUILD instructions with ONBUILD. ONBUILD can't be used to trigger FROM instructions. build dockerfile create another dockerfile here you will see default ngnix index page build docker file above container copied index.html form local Multi-Container CLI We will create two containers (linux1, linux2) based on the same image (ubuntu) additional flags: -dstarts the container as “detached”. Use “docker attach” to attach to it later on. --rm cleans up the container after stopping. The container will be removed, basically the same as “docker rm containeridentifier” after stopping the container. So everything is kept tidy. --name will give the container a dedicated name, which makes it easier to address the container later on. Creates container “linux2” Attaches to container linux1 Creates a new directory on container linux1 Shows that “mylinux1” was created Attaches to container linux2 Shows that the directory of linux2 is different than linux1, although they are both from the same image “ubuntu” They are separated, they don’t share their file-system The bash process is isolated in the container Shows only one container which is running, the other one got removed Docker Compose Basics cat Dockerfile add index.php file creat docker-compose.yaml to run docker compose output open browser Compose Volume Mount Lets build docker compose file above when we do docker compose up it will start apache with php 7.2 it will mount corrent directory to /var/www/html also it mount port 80 of the container to port 8080 on the host create index.php with following content run with docker compose open localhost 8080 http://localhost:8080 Custom Dockerfile with Compose Build Own Dockerfile and Docker Compose with Custom configuration crate docker compose file with following content here using dockerfile to generate an image mount folder inside your directory automatically create index.php with following content build docker compose file check it out localhost 80 http://localhost:8080 update your existing Dockerfile with following content update existing docker-compose.yml rebuild docker compose PHP, Apache, and DB with Compose we will see detach form logs upon start and user multiservices in one docker container here you see two services phpapp and myphpapp-app and image called phpapp with 123 tag another service called db form mysql this container restarts always which means it crashes ? the it restarts automatically ! upon start we set a password for the root user "my!!root!!pw" just fo demostrate create dockerfile with following content crete index.php with following content build docke