How to employ Docker containers?
Last updated
Last updated
Docker is a common containerization tool that is used to give software programs access to a filesystem that has all the components they require to function. Given the merciless consistency of its run-time environment, using Docker containers guarantees that the program will act consistently everywhere it is deployed.
This article will give you a quick overview of the connection between Docker images and Docker containers.
A Docker image may be compared to a blank template that is used to build Docker containers. In contrast to a traditional Linux distribution, a Docker image often only includes what is absolutely necessary to execute the application. Images serve as the foundation for Docker containers.
The docker run
command brings images to life by building a container on top of the image and adding a read-write layer. A union file system is a collection of read-only layers coupled with read-write layers.
The docker run
command generates a fresh container from the specified image each time you use it.
There are certain prerequisites that need to be met before you begin:
Ubuntu 20.04 installed, or an Ubuntu 20.04 server set up.
Docker installed and set up. (Note: click here to learn how to install and use Docker on ubuntu.)
User account with admin privileges.
Working knowledge of the Linux command line.
Step 1: The base Ubuntu image will be used to construct a new container when the following 'docker run' command is used. The flag '-t' provides us with a terminal and '-i' helps us to interact with the terminal.
The CLI changes and displays the 12-character container ID and the fact that the root user is currently logged in.
Step 2: We will 'echo' some data into the container's /tmp directory, this will make a modification to the container.
Now, use the cat
command to ascertain that the changes were saved.
Step 3: Use the following command to exit the container.
Step 4: Our container is terminated as soon as we leave the bash terminal, because Docker containers stop operating immediately as the command they issued is finished. We won't see ours if we use the following command, which only displays running containers.
Step 5: Adding the -a
flag to the docker ps
command displays all containers regardless of the fact if they are running or stopped, our container will be visible now.
Whenever a container is created it is assigned a container ID and container name, both of which are randomly generated. The docker ps -a
command also displays additional information like the image used to create the container, the command that was executed on it, etc.
Step 6: Re-running the docker run -ti
command creates a completely new container.
We can confirm that it is a new container as a new container ID is displayed and when we try to locate our example file, we won't find it. This might cause some confusion but there is no reason to worry, we will now exit the new container and see that both the containers exist.
Step 7: Run the docker start
command along with -a
and -i
flags to restart a pre-existing container and make it interactive, then add the container ID of the container you wish to open after the flags.
Now use the following command and find that the file we created earlier still exists.
Step 8: Exit the container.
Step 9: Delete both of the containers that were created using the docker rm
command followed by the container ID or the container name.
Note: docker rm
command works only on stopped containers.
We learned how to create, manage, and remove Docker containers using the docker run
, docker attach
, and docker rm
commands.