Jump to content

Taoron

Members
  • Content Count

    30
  • Joined

  • Last visited

  • Points

    2,903 [ Donate ]

Community Reputation

0 Neutral

About Taoron

  • Other groups Community Planning Committee,
    Moderators
  • Rank
    Rank 2
  • Birthday 02/10/1981
  1. A Docker image is the blueprint of Docker containers that contains the application and everything you need to run the application. A container is a runtime instance of an image. In this tutorial, we will explain what Dockerfile is, how to create one, and how to build a Docker image with Dockerfile. What is Dockerfile A Dockerfile is a text file that contains all the commands a user could run on the command line to create an image. It includes all the instructions needed by Docker to build the image. Docker images are made up of a series of filesystem layers representing instructions in the image’s Dockerfile that makes up an executable software application. The Docker file takes the following form: # Comment INSTRUCTION arguments Copy INSTRUCTION is not case-sensitive, but the convention is to use UPPERCASE for its names. Below is the list with a short description of some of the most used Dockerfile instructions: ARG - This instruction allows you to define variables that can be passed at build-time. You can also set a default value. FROM - The base image for building a new image. This instruction must be the first non-comment instruction in the Dockerfile. The only exception from this rule is when you want to use a variable in the FROM argument. In this case, FROM can be preceded by one or more ARG instructions. LABEL - Used to add metadata to an image, such as description, version, author ..etc. You can specify more than one LABEL, and each LABEL instruction is a key-value pair. RUN - The commands specified in this instruction will be executed during the build process. Each RUN instruction creates a new layer on top of the current image. ADD - Used to copy files and directories from the specified source to the specified destination on the docker image. The source can be local files or directories or an URL. If the source is a local tar archive, then it is automatically unpacked into the Docker image. COPY - Similar to ADD but the source can be only a local file or directory. ENV - This instruction allows you to define an environment variable. CMD - Used to specify a command that will be executed when you run a container. You can use only one CMD instruction in your Dockerfile. ENTRYPOINT - Similar to CMD, this instruction defines what command will be executed when running a container. WORKDIR - This directive sets the current working directory for the RUN, CMD, ENTRYPOINT, COPY, and ADD instructions. USER - Set the username or UID to use when running any following RUN, CMD, ENTRYPOINT, COPY, and ADD instructions. VOLUME - Enables you to mount a host machine directory to the container. EXPOSE - Used to specify the port on which the container listens at runtime. To, exclude files and directories from being added to the image, create a .dockerignore file in the context directory. The syntax of the .dockerignore is similar to the one of the Git’s .gitignore file. For a complete reference and detailed explanation of Dockerfile instructions see the official Dockerfile reference page. Create a Dockerfile The most common scenario when creating Docker images is to pull an existing image from a registry (usually from Docker Hub) and specify the changes you want to make on the base image. The most commonly used base image when creating Docker images is Alpine because it is small and optimized to be run in RAM. The Docker Hub is cloud-based registry service which among other functionalities is used for keeping the Docker images either in a public or private repository. In this example, we will create a Docker image for the Redis server. We’ll use the latest ubuntu 18.04 as a base image. First, create a directory that will contain the Dockerfile and all the necessary files: mkdir ~/redis_docker Navigate to the directory and create the following Dockerfile: cd ~/redis_dockernano Dockerfile Dockerfile FROM ubuntu:18.04 RUN apt-get update && \ apt-get install -y redis-server && \ apt-get clean EXPOSE 6379 CMD ["redis-server", "--protected-mode no"] Copy Let’s explain the meaning of each of the lines in the Dockerfile: On line 1 we are defining the base image. The RUN instruction that starts on line 3 will update the apt index, install the “redis-server” package and clean the apt cache. The commands used in instructions are the same as the commands you would use to install redis on Ubuntu server. The EXPOSE instruction defines the port on which the redis server listens. In the last line, we are using the CMD instruction to set the default command that will be executed when the container runs. Save the file and close the editor. Building the Image The next step is to build the image. To do so run the following command from the directory where the Dockerfile is located: docker build -t linuxize/redis . Copy The option -t specifies the image name and optionally a username and tag in the ‘username/imagename:tag’ format. The output of the build process will look something like this: Sending build context to Docker daemon 3.584kB Step 1/4 : FROM ubuntu:18.04 ---> 7698f282e524 Step 2/4 : RUN apt-get update && apt-get install -y gosu redis-server && apt-get clean ---> Running in e80d4dd69263 ... Removing intermediate container e80d4dd69263 ---> e19fb7653fca Step 3/4 : EXPOSE 6379 ---> Running in 8b2a45f457cc Removing intermediate container 8b2a45f457cc ---> 13b92565c201 Step 4/4 : CMD ["redis-server", "--protected-mode no"] ---> Running in a67ec50c7048 Removing intermediate container a67ec50c7048 ---> d8acc14d9b6b Successfully built d8acc14d9b6b Successfully tagged linuxize/redis:latest WHen the build process is completed the new image will be listed in the image list: docker image ls Copy REPOSITORY TAG IMAGE ID CREATED SIZE linuxize/redis latest d8acc14d9b6b 4 minutes ago 100MB ubuntu 18.04 7698f282e524 5 days ago 69.9MB If you want to push the image to Docker Hub see Pushing a Docker container image to Docker Hub. Running a Container Now that the image is created you run a container from it by running: docker run -d -p 6379:6379 --name redis linuxize/redis Copy The -d options tell Docker to run the container in detached mode, the -p 6379:6379 option will publish the port 6379 to the host machine and the --name redis option specifies the container name. The last argument linuxize/redis is the name of the image, which is used to run the container. When the container starts, use the following command to list all running containers: docker container ls Copy CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 6b7d424cd915 linuxize/redis:v0.0.1 "redis-server '--pro…" 5 minutes ago Up 5 minutes 0.0.0.0:6379->6379/tcp redis To verify that everything works as it should use the redis-cli to connect to the docker container: redis-cli ping Copy The redis server should respond with PONG. Conclusion This tutorial covered only the basics of using Dockerfiles to build images. To learn more about how to write Dockerfiles and the recommended best practices see Best practices for writing Dockerfiles. If you have any questions, please leave a comment below.
  2. Removing All Unused Objects The docker system prune command will remove all stopped containers, all dangling images, and all unused networks: docker system prune You’ll be prompted to continue, use the -f or --force flag to bypass the prompt. WARNING! This will remove: - all stopped containers - all networks not used by at least one container - all dangling images - all build cache Are you sure you want to continue? [y/N] If you also want to remove all unused volumes, pass the --volumes flag: docker system prune --volumes WARNING! This will remove: - all stopped containers - all networks not used by at least one container - all volumes not used by at least one container - all dangling images - all build cache Are you sure you want to continue? [y/N] y Removing Docker Containers Docker containers are not automatically removed when you stop them unless you start the container using the --rm flag. Remove one or more containers To remove one or more Docker images use the docker container rm command followed by the ID of the containers you want to remove. You can get a list of all containers by passing the -a flag to the docker container ls command: docker container ls -a The output should look something like this: CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES cc3f2ff51cab centos "/bin/bash" 2 months ago Created competent_nightingale cd20b396a061 solita/ubuntu-systemd "/bin/bash -c 'exec …" 2 months ago Exited (137) 2 months ago systemd fb62432cf3c1 ubuntu "/bin/bash" 3 months ago Exited (130) 3 months ago jolly_mirzakhani Once you know the CONTAINER ID of the containers you want to delete, pass it to the docker container rm command. For example, to remove the first two containers listed in the output above run: docker container rm cc3f2ff51cab cd20b396a061 If you get an error similar to the following, it means that the container is running. You’ll need to stop the container before removing it. Error response from daemon: You cannot remove a running container fc983ebf4771d42a8bd0029df061cb74dc12cb174530b2036987575b83442b47. Stop the container before attempting removal or force remove. Remove all stopped containers Before performing the removal command, you can get a list of all non-running (stopped) containers that will be removed using the following command: docker container ls -a --filter status=exited --filter status=created To remove all stopped containers use the docker container prune command: docker container prune You’ll be prompted to continue, use the -f or --force flag to bypass the prompt. WARNING! This will remove all stopped containers. Are you sure you want to continue? [y/N] y Remove containers using filters The docker container prune command allows you to remove containers based on condition using the filtering flag --filter. At the time of the writing of this article, the currently supported filters are until and label. You can use more than one filter by using multiple --filter flags. For example, to remove all images that are created more than 12 hours ago, run: docker container prune --filter "until=12h" Stop and remove all containers You can get a list of all Docker containers on your system using the docker container ls -aq command. To stop all running containers use the docker container stop command followed by a list of all containers IDs. docker container stop $(docker container ls -aq) Once all containers are stopped, you can remove them using the docker container rm command followed by the containers ID list. docker container rm $(docker container ls -aq) Removing Docker Images Remove one or more images To remove one or more Docker images use the docker images ls command to find the ID of the images you want to remove. docker image ls The output should look something like this: REPOSITORY TAG IMAGE ID CREATED SIZE centos latest 75835a67d134 7 days ago 200MB ubuntu latest 2a4cca5ac898 2 months ago 111MB linuxize/fedora latest a45d6dca3361 3 months ago 311MB java 8-jre e44d62cf8862 3 months ago 311MB Once you’ve located the images you want to remove, pass their IMAGE ID to the docker image rm command. For example, to remove the first two images listed in the output above run: docker image rm 75835a67d134 2a4cca5ac898 If you get an error similar to the one shown below, it means that an existing container uses the image. To remove the image, you will have to remove the container first. Error response from daemon: conflict: unable to remove repository reference "centos" (must force) - container cd20b396a061 is using its referenced image 75835a67d134 Copy Remove dangling images Docker provides a docker image prune command that can be used to remove dangled and unused images. A dangling image is an image that is not tagged and is not used by any container. To remove dangling images type: docker image prune You’ll be prompted to continue, use the -f or --force flag to bypass the prompt. WARNING! This will remove all dangling images. Are you sure you want to continue? [y/N] y When removing dangling images, if the images build by you are not tagged, they will be removed too. Remove all unused images To remove all images which are not referenced by any existing container, not just the dangling ones, use the prune command with the -a flag: docker image prune -a WARNING! This will remove all images without at least one container associated to them. Are you sure you want to continue? [y/N] y Remove images using filters With the docker image prune command, you can also remove images based on a certain condition using the filtering flag --filter. At the time of the writing of this article, the currently supported filters are until and label. You can use more than one filter by using multiple --filter flags. For example, to remove all images that are created more than 12 hours ago, you would run: docker image prune -a --filter "until=12h" Removing Docker Volumes Remove one or more volumes To remove one or more Docker volumes use the docker volume ls command to find the ID of the volumes you want to remove. docker volume ls The output should look something like this: DRIVER VOLUME NAME local 4e12af8913af888ba67243dec78419bf18adddc3c7a4b2345754b6db64293163 local terano Once you’ve found the VOLUME NAME of the volumes you want to remove, pass them to the docker volume rm command. For example, to remove the first volume listed in the output above, run: docker volume rm 4e12af8913af888ba67243dec78419bf18adddc3c7a4b2345754b6db64293163 If you get an error similar to the one shown below, it means that an existing container uses the volume. To remove the volume, you will have to remove the container first. Error response from daemon: remove 4e12af8913af888ba67243dec78419bf18adddc3c7a4b2345754b6db64293163: volume is in use - [c7188935a38a6c3f9f11297f8c98ce9996ef5ddad6e6187be62bad3001a66c8e] Copy Remove all unused volumes To remove all unused volumes use the docker image prune command: docker volume prune You’ll be prompted to continue, use the -f or --force flag to bypass the prompt. WARNING! This will remove all local volumes not used by at least one container. Are you sure you want to continue? [y/N] Removing Docker Networks Remove one or more networks To remove one or more Docker networks use the docker network ls command to find the ID of the networks you want to remove. docker network ls The output should look something like this: NETWORK ID NAME DRIVER SCOPE 107b8ac977e3 bridge bridge local ab998267377d host host local c520032c3d31 my-bridge-network bridge local 9bc81b63f740 none null local Once you’ve located the networks you want to remove, pass their NETWORK ID to the docker network rm command. For example to remove the network with the name my-bridge-network run: docker network rm c520032c3d31 If you get an error similar to the one shown below, it means that an existing container uses the network. To remove the network you will have to remove the container first. Error response from daemon: network my-bridge-network id 6f5293268bb91ad2498b38b0bca970083af87237784017be24ea208d2233c5aa has active endpoints Copy Remove all unused network Use the docker network prune command to remove all unused networks. docker network prune You’ll be prompted to continue, use the -f or --force flag to bypass the prompt. WARNING! This will remove all networks not used by at least one container. Are you sure you want to continue? [y/N] Remove networks using filters With the docker network prune command you can remove networks based on condition using the filtering flag --filter. At the time of the writing of this article, the currently supported filters are until and label. You can use more than one filter by using multiple --filter flags. For example, to remove all networks that are created more than 12 hours ago, run: docker network prune -a --filter "until=12h" Conclusion In this guide, we have shown you some of the common commands for removing Docker containers, images, volumes, and networks. You should also check out the official Docker documentation. If you have any questions, please leave a comment below.
  3. The Great Escape is busy at work developing an online community and storefront for the technology industry.
  4. Draft Placeholder
  5. Hello! I'm a new member. I just wanted to introduce myself to the community. I am the developer of the website and add all the cool things you see here! I am always open for suggestions on new categories, addons, apps that you'd like to see. Please let me know if I can make your stay here any more enjoyable! Thanks! -- Taoron
  6. Taoron

    Taoron

  7. Greetings, I'm just testing the forum. Thank you!
×
×
  • Create New...

Important Information

By using The Great Escaped Online Community, you agree to our Privacy Policy and Terms of Use